mirror of
https://github.com/ForFarmTeam/ForFarm.git
synced 2025-12-19 14:04:08 +01:00
feat: add user to inventory repository
This commit is contained in:
parent
b39ddfba6f
commit
5a373b5f2b
@ -29,6 +29,7 @@ func (p *postgresInventoryRepository) fetch(ctx context.Context, query string, a
|
|||||||
var i domain.InventoryItem
|
var i domain.InventoryItem
|
||||||
if err := rows.Scan(
|
if err := rows.Scan(
|
||||||
&i.ID,
|
&i.ID,
|
||||||
|
&i.UserID,
|
||||||
&i.Name,
|
&i.Name,
|
||||||
&i.Category,
|
&i.Category,
|
||||||
&i.Type,
|
&i.Type,
|
||||||
@ -46,13 +47,13 @@ func (p *postgresInventoryRepository) fetch(ctx context.Context, query string, a
|
|||||||
return items, nil
|
return items, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *postgresInventoryRepository) GetByID(ctx context.Context, id string) (domain.InventoryItem, error) {
|
func (p *postgresInventoryRepository) GetByID(ctx context.Context, id, userID string) (domain.InventoryItem, error) {
|
||||||
query := `
|
query := `
|
||||||
SELECT id, name, category, type, quantity, unit, date_added, status, created_at, updated_at
|
SELECT id, user_id, name, category, type, quantity, unit, date_added, status, created_at, updated_at
|
||||||
FROM inventory_items
|
FROM inventory_items
|
||||||
WHERE id = $1`
|
WHERE id = $1 AND user_id = $2`
|
||||||
|
|
||||||
items, err := p.fetch(ctx, query, id)
|
items, err := p.fetch(ctx, query, id, userID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return domain.InventoryItem{}, err
|
return domain.InventoryItem{}, err
|
||||||
}
|
}
|
||||||
@ -62,15 +63,20 @@ func (p *postgresInventoryRepository) GetByID(ctx context.Context, id string) (d
|
|||||||
return items[0], nil
|
return items[0], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *postgresInventoryRepository) GetWithFilter(ctx context.Context, filter domain.InventoryFilter, sort domain.InventorySort) ([]domain.InventoryItem, error) {
|
func (p *postgresInventoryRepository) GetByUserID(
|
||||||
|
ctx context.Context,
|
||||||
|
userID string,
|
||||||
|
filter domain.InventoryFilter,
|
||||||
|
sort domain.InventorySort,
|
||||||
|
) ([]domain.InventoryItem, error) {
|
||||||
var query strings.Builder
|
var query strings.Builder
|
||||||
args := []interface{}{}
|
args := []interface{}{userID}
|
||||||
argPos := 1
|
argPos := 2
|
||||||
|
|
||||||
query.WriteString(`
|
query.WriteString(`
|
||||||
SELECT id, name, category, type, quantity, unit, date_added, status, created_at, updated_at
|
SELECT id, user_id, name, category, type, quantity, unit, date_added, status, created_at, updated_at
|
||||||
FROM inventory_items
|
FROM inventory_items
|
||||||
WHERE 1=1`)
|
WHERE user_id = $1`)
|
||||||
|
|
||||||
if filter.Category != "" {
|
if filter.Category != "" {
|
||||||
query.WriteString(fmt.Sprintf(" AND category = $%d", argPos))
|
query.WriteString(fmt.Sprintf(" AND category = $%d", argPos))
|
||||||
@ -135,6 +141,14 @@ func (p *postgresInventoryRepository) GetWithFilter(ctx context.Context, filter
|
|||||||
return p.fetch(ctx, query.String(), args...)
|
return p.fetch(ctx, query.String(), args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *postgresInventoryRepository) GetAll(ctx context.Context) ([]domain.InventoryItem, error) {
|
||||||
|
query := `
|
||||||
|
SELECT id, user_id, name, category, type, quantity, unit, date_added, status, created_at, updated_at
|
||||||
|
FROM inventory_items
|
||||||
|
ORDER BY created_at DESC`
|
||||||
|
return p.fetch(ctx, query)
|
||||||
|
}
|
||||||
|
|
||||||
func (p *postgresInventoryRepository) CreateOrUpdate(ctx context.Context, item *domain.InventoryItem) error {
|
func (p *postgresInventoryRepository) CreateOrUpdate(ctx context.Context, item *domain.InventoryItem) error {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
item.UpdatedAt = now
|
item.UpdatedAt = now
|
||||||
@ -143,12 +157,13 @@ func (p *postgresInventoryRepository) CreateOrUpdate(ctx context.Context, item *
|
|||||||
item.CreatedAt = now
|
item.CreatedAt = now
|
||||||
query := `
|
query := `
|
||||||
INSERT INTO inventory_items
|
INSERT INTO inventory_items
|
||||||
(id, name, category, type, quantity, unit, date_added, status, created_at, updated_at)
|
(id, user_id, name, category, type, quantity, unit, date_added, status, created_at, updated_at)
|
||||||
VALUES (gen_random_uuid(), $1, $2, $3, $4, $5, $6, $7, $8, $9)
|
VALUES (gen_random_uuid(), $1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
||||||
RETURNING id`
|
RETURNING id`
|
||||||
return p.conn.QueryRow(
|
return p.conn.QueryRow(
|
||||||
ctx,
|
ctx,
|
||||||
query,
|
query,
|
||||||
|
item.UserID,
|
||||||
item.Name,
|
item.Name,
|
||||||
item.Category,
|
item.Category,
|
||||||
item.Type,
|
item.Type,
|
||||||
@ -171,7 +186,7 @@ func (p *postgresInventoryRepository) CreateOrUpdate(ctx context.Context, item *
|
|||||||
date_added = $6,
|
date_added = $6,
|
||||||
status = $7,
|
status = $7,
|
||||||
updated_at = $8
|
updated_at = $8
|
||||||
WHERE id = $9
|
WHERE id = $9 AND user_id = $10
|
||||||
RETURNING id`
|
RETURNING id`
|
||||||
|
|
||||||
return p.conn.QueryRow(
|
return p.conn.QueryRow(
|
||||||
@ -186,11 +201,12 @@ func (p *postgresInventoryRepository) CreateOrUpdate(ctx context.Context, item *
|
|||||||
item.Status,
|
item.Status,
|
||||||
item.UpdatedAt,
|
item.UpdatedAt,
|
||||||
item.ID,
|
item.ID,
|
||||||
|
item.UserID,
|
||||||
).Scan(&item.ID)
|
).Scan(&item.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *postgresInventoryRepository) Delete(ctx context.Context, id string) error {
|
func (p *postgresInventoryRepository) Delete(ctx context.Context, id, userID string) error {
|
||||||
query := `DELETE FROM inventory_items WHERE id = $1`
|
query := `DELETE FROM inventory_items WHERE id = $1 AND user_id = $2`
|
||||||
_, err := p.conn.Exec(ctx, query, id)
|
_, err := p.conn.Exec(ctx, query, id, userID)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user