/
product.go
85 lines (69 loc) 路 2.12 KB
/
product.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package repository
import (
"context"
"fmt"
"strings"
"github.com/jmoiron/sqlx"
"github.com/p12s/csv-create-api/internal/domain"
)
// Producter - repository contract
type Producter interface {
Create(ctx context.Context, product domain.Product) error
UpdateById(ctx context.Context, id int, input domain.UpdateProductInput) error
DeleteById(ctx context.Context, id int) error
GetAllProducts(ctx context.Context) ([]domain.Product, error)
}
// Product - product
type Product struct {
db *sqlx.DB
}
// NewProduct - constructor
func NewProduct(db *sqlx.DB) *Product {
return &Product{db: db}
}
// Create - create product
func (p *Product) Create(ctx context.Context, product domain.Product) error {
query := fmt.Sprintf(`INSERT INTO %s (name, price) values ($1, $2)`, productTable)
_, err := p.db.Exec(query, product.Name, product.Price)
if err != nil {
return fmt.Errorf("create product: %w", err)
}
return nil
}
// UpdateById - update product
func (p *Product) UpdateById(ctx context.Context, id int, input domain.UpdateProductInput) error {
setValues := make([]string, 0)
args := make([]interface{}, 0)
argId := 1
if input.Name != nil {
setValues = append(setValues, fmt.Sprintf("name=$%d", argId))
args = append(args, *input.Name)
argId++
}
if input.Price != nil {
setValues = append(setValues, fmt.Sprintf("price=$%d", argId))
args = append(args, *input.Price)
argId++
}
setQuery := strings.Join(setValues, ", ")
query := fmt.Sprintf(`UPDATE %s SET %s WHERE id = $%d`,
productTable, setQuery, argId)
args = append(args, id)
_, err := p.db.Exec(query, args...)
return err
}
// DeleteById - delete product
func (p *Product) DeleteById(ctx context.Context, id int) error {
query := fmt.Sprintf(`DELETE FROM %s WHERE id = $1`, productTable)
_, err := p.db.Exec(query, id)
return err
}
// GetAllProducts - getting all products in csv-file
func (p *Product) GetAllProducts(ctx context.Context) ([]domain.Product, error) {
var products []domain.Product
query := fmt.Sprintf(`SELECT * FROM %s`, productTable)
if err := p.db.Select(&products, query); err != nil {
return nil, err
}
return products, nil
}