/
repository.go
147 lines (114 loc) · 3.97 KB
/
repository.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package warehouse_ingredient
import (
"database/sql"
"github.com/Digital-Voting-Team/warehouse-service/internal/pkg/common"
sq "github.com/Masterminds/squirrel"
"github.com/fatih/structs"
"github.com/jmoiron/sqlx"
"gitlab.com/distributed_lab/kit/pgdb"
"time"
)
const warehouseIngredientTableName = "warehouse.warehouse_ingredient"
var (
queryInsertWarehouseIngredient = `INSERT INTO WAREHOUSE.WAREHOUSE_INGREDIENT (INGREDIENT_ID, WAREHOUSE_ID, QUANTITY, ORIGIN, PRICE, EXPIRATION_DATE, DELIVERY_ID)
VALUES (:1, :2, :3, :4, :5, :6, :7) RETURNING ID INTO :8`
)
type query struct {
db *sqlx.DB
sql sq.SelectBuilder
sqlUpdate sq.UpdateBuilder
}
func NewQuery(db *sqlx.DB) Query {
return &query{
db: db,
sql: sq.Select("*").From(warehouseIngredientTableName).PlaceholderFormat(sq.Colon),
sqlUpdate: sq.Update(warehouseIngredientTableName).PlaceholderFormat(sq.Colon),
}
}
func (q *query) New() Query {
return NewQuery(q.db)
}
func (q *query) Get() (*WarehouseIngredient, error) {
var result WarehouseIngredient
sqlString, args, _ := q.sql.ToSql()
err := q.db.Get(&result, sqlString, args...)
if err == sql.ErrNoRows {
return nil, nil
}
return &result, err
}
func (q *query) Select() ([]WarehouseIngredient, error) {
var result []WarehouseIngredient
sqlString, args, _ := q.sql.ToSql()
err := q.db.Select(&result, sqlString, args...)
return result, err
}
func (q *query) Insert(warehouseIngredient WarehouseIngredient) (WarehouseIngredient, error) {
result, err := q.db.Queryx(queryInsertWarehouseIngredient, warehouseIngredient.IngredientId,
warehouseIngredient.WarehouseId, warehouseIngredient.Quantity, warehouseIngredient.Origin,
warehouseIngredient.Price, warehouseIngredient.ExpirationDate, warehouseIngredient.DeliveryId,
&warehouseIngredient.Id)
if err != nil {
return WarehouseIngredient{}, err
}
defer result.Close()
return warehouseIngredient, nil
}
func (q *query) Update(warehouseIngredient WarehouseIngredient) (WarehouseIngredient, error) {
var result *WarehouseIngredient
clauses := structs.Map(warehouseIngredient)
clauses["ingredient_id"] = warehouseIngredient.IngredientId
clauses["warehouse_id"] = warehouseIngredient.WarehouseId
clauses["quantity"] = warehouseIngredient.Quantity
clauses["origin"] = warehouseIngredient.Origin
clauses["price"] = warehouseIngredient.Price
clauses["expiration_date"] = warehouseIngredient.ExpirationDate
clauses["delivery_id"] = warehouseIngredient.DeliveryId
sqlString, args, _ := q.sqlUpdate.SetMap(clauses).ToSql()
_, err := q.db.Exec(sqlString, args...)
result, err = q.Get()
return *result, err
}
func (q *query) Delete(id int64) error {
stmt := sq.Delete(warehouseIngredientTableName).Where(sq.Eq{"id": id}).PlaceholderFormat(sq.Colon)
sqlString, args, _ := stmt.ToSql()
_, err := q.db.Exec(sqlString, args...)
return err
}
func (q *query) Page(pageParams pgdb.OffsetPageParams) Query {
q.sql = common.ApplyPageParams(&pageParams, q.sql, "id")
return q
}
func (q *query) FilterById(ids ...int64) Query {
q.sql = q.sql.Where(sq.Eq{"id": ids})
q.sqlUpdate = q.sqlUpdate.Where(sq.Eq{"id": ids})
return q
}
func (q *query) FilterByIngredientId(ids ...int64) Query {
q.sql = q.sql.Where(sq.Eq{"ingredient_id": ids})
return q
}
func (q *query) FilterByWarehouseId(ids ...int64) Query {
q.sql = q.sql.Where(sq.Eq{"warehouse_id": ids})
return q
}
func (q *query) FilterByQuantity(quantities ...int64) Query {
q.sql = q.sql.Where(sq.Eq{"quantity": quantities})
return q
}
func (q *query) FilterByOrigin(origins ...string) Query {
q.sql = q.sql.Where(sq.Eq{"origin": origins})
return q
}
func (q *query) FilterByPrice(prices ...float64) Query {
q.sql = q.sql.Where(sq.Eq{"price": prices})
return q
}
func (q *query) FilterByExpirationDate(expirationDates ...time.Time) Query {
q.sql = q.sql.Where(sq.Eq{"expiration_date": expirationDates})
return q
}
func (q *query) FilterByDeliveryId(ids ...int64) Query {
q.sql = q.sql.Where(sq.Eq{"delivery_id": ids})
return q
}