mirror of
https://github.com/ForFarmTeam/ForFarm.git
synced 2025-12-19 14:04:08 +01:00
feat: implement inventory repository with status and category relations
This commit is contained in:
parent
6dac03a489
commit
b79f3f69f3
@ -31,12 +31,11 @@ func (p *postgresInventoryRepository) fetch(ctx context.Context, query string, a
|
|||||||
&i.ID,
|
&i.ID,
|
||||||
&i.UserID,
|
&i.UserID,
|
||||||
&i.Name,
|
&i.Name,
|
||||||
&i.Category,
|
&i.CategoryID,
|
||||||
&i.Type,
|
|
||||||
&i.Quantity,
|
&i.Quantity,
|
||||||
&i.Unit,
|
&i.UnitID,
|
||||||
&i.DateAdded,
|
&i.DateAdded,
|
||||||
&i.Status,
|
&i.StatusID,
|
||||||
&i.CreatedAt,
|
&i.CreatedAt,
|
||||||
&i.UpdatedAt,
|
&i.UpdatedAt,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
@ -49,18 +48,49 @@ func (p *postgresInventoryRepository) fetch(ctx context.Context, query string, a
|
|||||||
|
|
||||||
func (p *postgresInventoryRepository) GetByID(ctx context.Context, id, userID string) (domain.InventoryItem, error) {
|
func (p *postgresInventoryRepository) GetByID(ctx context.Context, id, userID string) (domain.InventoryItem, error) {
|
||||||
query := `
|
query := `
|
||||||
SELECT id, user_id, name, category, type, quantity, unit, date_added, status, created_at, updated_at
|
SELECT
|
||||||
FROM inventory_items
|
i.id, i.user_id, i.name, i.category_id, i.quantity, i.unit_id,
|
||||||
WHERE id = $1 AND user_id = $2`
|
i.date_added, i.status_id, i.created_at, i.updated_at,
|
||||||
|
c.name as category_name,
|
||||||
|
s.name as status_name,
|
||||||
|
u.name as unit_name
|
||||||
|
FROM inventory_items i
|
||||||
|
LEFT JOIN inventory_category c ON i.category_id = c.id
|
||||||
|
LEFT JOIN inventory_status s ON i.status_id = s.id
|
||||||
|
LEFT JOIN harvest_units u ON i.unit_id = u.id
|
||||||
|
WHERE i.id = $1 AND i.user_id = $2`
|
||||||
|
|
||||||
items, err := p.fetch(ctx, query, id, userID)
|
rows, err := p.conn.Query(ctx, query, id, userID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return domain.InventoryItem{}, err
|
return domain.InventoryItem{}, err
|
||||||
}
|
}
|
||||||
if len(items) == 0 {
|
defer rows.Close()
|
||||||
|
|
||||||
|
if !rows.Next() {
|
||||||
return domain.InventoryItem{}, domain.ErrNotFound
|
return domain.InventoryItem{}, domain.ErrNotFound
|
||||||
}
|
}
|
||||||
return items[0], nil
|
|
||||||
|
var item domain.InventoryItem
|
||||||
|
err = rows.Scan(
|
||||||
|
&item.ID,
|
||||||
|
&item.UserID,
|
||||||
|
&item.Name,
|
||||||
|
&item.CategoryID,
|
||||||
|
&item.Quantity,
|
||||||
|
&item.UnitID,
|
||||||
|
&item.DateAdded,
|
||||||
|
&item.StatusID,
|
||||||
|
&item.CreatedAt,
|
||||||
|
&item.UpdatedAt,
|
||||||
|
&item.Category.Name,
|
||||||
|
&item.Status.Name,
|
||||||
|
&item.Unit.Name,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return domain.InventoryItem{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return item, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *postgresInventoryRepository) GetByUserID(
|
func (p *postgresInventoryRepository) GetByUserID(
|
||||||
@ -74,58 +104,57 @@ func (p *postgresInventoryRepository) GetByUserID(
|
|||||||
argPos := 2
|
argPos := 2
|
||||||
|
|
||||||
query.WriteString(`
|
query.WriteString(`
|
||||||
SELECT id, user_id, name, category, type, quantity, unit, date_added, status, created_at, updated_at
|
SELECT
|
||||||
FROM inventory_items
|
i.id, i.user_id, i.name, i.category_id, i.quantity, i.unit_id,
|
||||||
WHERE user_id = $1`)
|
i.date_added, i.status_id, i.created_at, i.updated_at,
|
||||||
|
c.name as category_name,
|
||||||
|
s.name as status_name,
|
||||||
|
u.name as unit_name
|
||||||
|
FROM inventory_items i
|
||||||
|
LEFT JOIN inventory_category c ON i.category_id = c.id
|
||||||
|
LEFT JOIN inventory_status s ON i.status_id = s.id
|
||||||
|
LEFT JOIN harvest_units u ON i.unit_id = u.id
|
||||||
|
WHERE i.user_id = $1`)
|
||||||
|
|
||||||
if filter.Category != "" {
|
if filter.CategoryID != 0 {
|
||||||
query.WriteString(fmt.Sprintf(" AND category = $%d", argPos))
|
query.WriteString(fmt.Sprintf(" AND i.category_id = $%d", argPos))
|
||||||
args = append(args, filter.Category)
|
args = append(args, filter.CategoryID)
|
||||||
argPos++
|
argPos++
|
||||||
}
|
}
|
||||||
|
|
||||||
if filter.Type != "" {
|
if filter.StatusID != 0 {
|
||||||
query.WriteString(fmt.Sprintf(" AND type = $%d", argPos))
|
query.WriteString(fmt.Sprintf(" AND i.status_id = $%d", argPos))
|
||||||
args = append(args, filter.Type)
|
args = append(args, filter.StatusID)
|
||||||
argPos++
|
|
||||||
}
|
|
||||||
|
|
||||||
if filter.Status != "" {
|
|
||||||
query.WriteString(fmt.Sprintf(" AND status = $%d", argPos))
|
|
||||||
args = append(args, filter.Status)
|
|
||||||
argPos++
|
argPos++
|
||||||
}
|
}
|
||||||
|
|
||||||
if !filter.StartDate.IsZero() {
|
if !filter.StartDate.IsZero() {
|
||||||
query.WriteString(fmt.Sprintf(" AND date_added >= $%d", argPos))
|
query.WriteString(fmt.Sprintf(" AND i.date_added >= $%d", argPos))
|
||||||
args = append(args, filter.StartDate)
|
args = append(args, filter.StartDate)
|
||||||
argPos++
|
argPos++
|
||||||
}
|
}
|
||||||
|
|
||||||
if !filter.EndDate.IsZero() {
|
if !filter.EndDate.IsZero() {
|
||||||
query.WriteString(fmt.Sprintf(" AND date_added <= $%d", argPos))
|
query.WriteString(fmt.Sprintf(" AND i.date_added <= $%d", argPos))
|
||||||
args = append(args, filter.EndDate)
|
args = append(args, filter.EndDate)
|
||||||
argPos++
|
argPos++
|
||||||
}
|
}
|
||||||
|
|
||||||
if filter.SearchQuery != "" {
|
if filter.SearchQuery != "" {
|
||||||
query.WriteString(fmt.Sprintf(" AND name ILIKE $%d", argPos))
|
query.WriteString(fmt.Sprintf(" AND i.name ILIKE $%d", argPos))
|
||||||
args = append(args, "%"+filter.SearchQuery+"%")
|
args = append(args, "%"+filter.SearchQuery+"%")
|
||||||
argPos++
|
argPos++
|
||||||
}
|
}
|
||||||
|
|
||||||
if sort.Field == "" {
|
if sort.Field == "" {
|
||||||
sort.Field = "date_added"
|
sort.Field = "i.date_added"
|
||||||
sort.Direction = "desc"
|
sort.Direction = "desc"
|
||||||
}
|
}
|
||||||
|
|
||||||
validSortFields := map[string]bool{
|
validSortFields := map[string]bool{
|
||||||
"name": true,
|
"name": true,
|
||||||
"category": true,
|
|
||||||
"type": true,
|
|
||||||
"quantity": true,
|
"quantity": true,
|
||||||
"date_added": true,
|
"date_added": true,
|
||||||
"status": true,
|
|
||||||
"created_at": true,
|
"created_at": true,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,14 +167,52 @@ func (p *postgresInventoryRepository) GetByUserID(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return p.fetch(ctx, query.String(), args...)
|
rows, err := p.conn.Query(ctx, query.String(), args...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
var items []domain.InventoryItem
|
||||||
|
for rows.Next() {
|
||||||
|
var item domain.InventoryItem
|
||||||
|
err := rows.Scan(
|
||||||
|
&item.ID,
|
||||||
|
&item.UserID,
|
||||||
|
&item.Name,
|
||||||
|
&item.CategoryID,
|
||||||
|
&item.Quantity,
|
||||||
|
&item.UnitID,
|
||||||
|
&item.DateAdded,
|
||||||
|
&item.StatusID,
|
||||||
|
&item.CreatedAt,
|
||||||
|
&item.UpdatedAt,
|
||||||
|
&item.Category.Name,
|
||||||
|
&item.Status.Name,
|
||||||
|
&item.Unit.Name,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
items = append(items, item)
|
||||||
|
}
|
||||||
|
|
||||||
|
return items, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *postgresInventoryRepository) GetAll(ctx context.Context) ([]domain.InventoryItem, error) {
|
func (p *postgresInventoryRepository) GetAll(ctx context.Context) ([]domain.InventoryItem, error) {
|
||||||
query := `
|
query := `
|
||||||
SELECT id, user_id, name, category, type, quantity, unit, date_added, status, created_at, updated_at
|
SELECT
|
||||||
FROM inventory_items
|
i.id, i.user_id, i.name, i.category_id, i.quantity, i.unit_id,
|
||||||
ORDER BY created_at DESC`
|
i.date_added, i.status_id, i.created_at, i.updated_at,
|
||||||
|
c.name as category_name,
|
||||||
|
s.name as status_name,
|
||||||
|
u.name as unit_name
|
||||||
|
FROM inventory_items i
|
||||||
|
LEFT JOIN inventory_category c ON i.category_id = c.id
|
||||||
|
LEFT JOIN inventory_status s ON i.status_id = s.id
|
||||||
|
LEFT JOIN harvest_units u ON i.unit_id = u.id
|
||||||
|
ORDER BY i.created_at DESC`
|
||||||
return p.fetch(ctx, query)
|
return p.fetch(ctx, query)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -157,20 +224,19 @@ 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, user_id, name, category, type, quantity, unit, date_added, status, created_at, updated_at)
|
(id, user_id, name, category_id, quantity, unit_id, date_added, status_id, created_at, updated_at)
|
||||||
VALUES (gen_random_uuid(), $1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
VALUES (gen_random_uuid(), $1, $2, $3, $4, $5, $6, $7, $8, $9)
|
||||||
RETURNING id`
|
RETURNING id`
|
||||||
return p.conn.QueryRow(
|
return p.conn.QueryRow(
|
||||||
ctx,
|
ctx,
|
||||||
query,
|
query,
|
||||||
item.UserID,
|
item.UserID,
|
||||||
item.Name,
|
item.Name,
|
||||||
item.Category,
|
item.CategoryID,
|
||||||
item.Type,
|
|
||||||
item.Quantity,
|
item.Quantity,
|
||||||
item.Unit,
|
item.UnitID,
|
||||||
item.DateAdded,
|
item.DateAdded,
|
||||||
item.Status,
|
item.StatusID,
|
||||||
item.CreatedAt,
|
item.CreatedAt,
|
||||||
item.UpdatedAt,
|
item.UpdatedAt,
|
||||||
).Scan(&item.ID)
|
).Scan(&item.ID)
|
||||||
@ -179,26 +245,24 @@ func (p *postgresInventoryRepository) CreateOrUpdate(ctx context.Context, item *
|
|||||||
query := `
|
query := `
|
||||||
UPDATE inventory_items
|
UPDATE inventory_items
|
||||||
SET name = $1,
|
SET name = $1,
|
||||||
category = $2,
|
category_id = $2,
|
||||||
type = $3,
|
quantity = $3,
|
||||||
quantity = $4,
|
unit_id = $4,
|
||||||
unit = $5,
|
date_added = $5,
|
||||||
date_added = $6,
|
status_id = $6,
|
||||||
status = $7,
|
updated_at = $7
|
||||||
updated_at = $8
|
WHERE id = $8 AND user_id = $9
|
||||||
WHERE id = $9 AND user_id = $10
|
|
||||||
RETURNING id`
|
RETURNING id`
|
||||||
|
|
||||||
return p.conn.QueryRow(
|
return p.conn.QueryRow(
|
||||||
ctx,
|
ctx,
|
||||||
query,
|
query,
|
||||||
item.Name,
|
item.Name,
|
||||||
item.Category,
|
item.CategoryID,
|
||||||
item.Type,
|
|
||||||
item.Quantity,
|
item.Quantity,
|
||||||
item.Unit,
|
item.UnitID,
|
||||||
item.DateAdded,
|
item.DateAdded,
|
||||||
item.Status,
|
item.StatusID,
|
||||||
item.UpdatedAt,
|
item.UpdatedAt,
|
||||||
item.ID,
|
item.ID,
|
||||||
item.UserID,
|
item.UserID,
|
||||||
@ -210,3 +274,41 @@ func (p *postgresInventoryRepository) Delete(ctx context.Context, id, userID str
|
|||||||
_, err := p.conn.Exec(ctx, query, id, userID)
|
_, err := p.conn.Exec(ctx, query, id, userID)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *postgresInventoryRepository) GetStatuses(ctx context.Context) ([]domain.InventoryStatus, error) {
|
||||||
|
query := `SELECT id, name FROM inventory_status ORDER BY id`
|
||||||
|
rows, err := p.conn.Query(ctx, query)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
var statuses []domain.InventoryStatus
|
||||||
|
for rows.Next() {
|
||||||
|
var s domain.InventoryStatus
|
||||||
|
if err := rows.Scan(&s.ID, &s.Name); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
statuses = append(statuses, s)
|
||||||
|
}
|
||||||
|
return statuses, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *postgresInventoryRepository) GetCategories(ctx context.Context) ([]domain.InventoryCategory, error) {
|
||||||
|
query := `SELECT id, name FROM inventory_category ORDER BY id`
|
||||||
|
rows, err := p.conn.Query(ctx, query)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
var categories []domain.InventoryCategory
|
||||||
|
for rows.Next() {
|
||||||
|
var c domain.InventoryCategory
|
||||||
|
if err := rows.Scan(&c.ID, &c.Name); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
categories = append(categories, c)
|
||||||
|
}
|
||||||
|
return categories, nil
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user