Skip to content

Commit

Permalink
fix: sql query
Browse files Browse the repository at this point in the history
  • Loading branch information
tankerkiller125 committed Oct 27, 2024
1 parent 012f0cf commit 5c71c98
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 32 deletions.
28 changes: 8 additions & 20 deletions backend/internal/data/repo/repo_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,10 @@ func (r *GroupRepository) StatsPurchasePrice(ctx context.Context, gid uuid.UUID,
// Get the Totals for the Start and End of the Given Time Period
q := `
SELECT
(SELECT Sum(purchase_price)
FROM items
WHERE group_items = '{{ GROUP_ID }}'
AND items.archived = false
AND items.created_at < '{{ START_PRICE }}') AS price_at_start,
(SELECT Sum(purchase_price)
FROM items
WHERE group_items = '{{ GROUP_ID }}'
AND items.archived = false
AND items.created_at < '{{ END_PRICE }}') AS price_at_end
SUM(CASE WHEN created_at < $1 THEN purchase_price ELSE 0 END) AS price_at_start,
SUM(CASE WHEN created_at < $2 THEN purchase_price ELSE 0 END) AS price_at_end
FROM items
WHERE group_items = $3 AND archived = false
`
stats := ValueOverTime{
Start: start,
Expand All @@ -180,11 +174,7 @@ func (r *GroupRepository) StatsPurchasePrice(ctx context.Context, gid uuid.UUID,
var maybeStart *float64
var maybeEnd *float64

q = strings.ReplaceAll(q, "{{ GROUP_ID }}", gid.String())
q = strings.Replace(q, "{{ START_PRICE }}", sqliteDateFormat(start), 1)
q = strings.Replace(q, "{{ END_PRICE }}", sqliteDateFormat(end), 1)

row := r.db.Sql().QueryRowContext(ctx, q)
row := r.db.Sql().QueryRowContext(ctx, q, sqliteDateFormat(start), sqliteDateFormat(end), gid)
err := row.Scan(&maybeStart, &maybeEnd)
if err != nil {
return nil, err
Expand Down Expand Up @@ -240,20 +230,18 @@ func (r *GroupRepository) StatsGroup(ctx context.Context, gid uuid.UUID) (GroupS
SUM(CASE WHEN i.archived = false THEN i.purchase_price * i.quantity ELSE 0 END) as total_item_price,
COUNT(DISTINCT CASE
WHEN i.archived = false
AND (i.lifetime_warranty = true OR i.warranty_expires > '{{ CURRENT_DATE }}')
AND (i.lifetime_warranty = true OR i.warranty_expires > $1)
THEN i.id
END) as total_with_warranty
FROM groups g
LEFT JOIN users u ON u.group_users = g.id
LEFT JOIN items i ON i.group_items = g.id
LEFT JOIN locations l ON l.group_locations = g.id
LEFT JOIN labels lb ON lb.group_labels = g.id
WHERE g.id = '{{ GROUP_ID }}';
WHERE g.id = $2;
`
var stats GroupStatistics
q = strings.ReplaceAll(q, "{{ GROUP_ID }}", gid.String())
q = strings.Replace(q, "{{ CURRENT_DATE }}", time.Now().Format("2006-01-02"), 1)
row := r.db.Sql().QueryRowContext(ctx, q)
row := r.db.Sql().QueryRowContext(ctx, q, sqliteDateFormat(time.Now()), gid)

var maybeTotalItemPrice *float64
var maybeTotalWithWarranty *int
Expand Down
19 changes: 7 additions & 12 deletions backend/internal/data/repo/repo_locations.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (r *LocationRepository) GetAll(ctx context.Context, gid uuid.UUID, filter L
FROM
locations
WHERE
locations.group_locations = '{{ GROUP_ID }}' {{ FILTER_CHILDREN }}
locations.group_locations = $1 {{ FILTER_CHILDREN }}
ORDER BY
locations.name ASC
`
Expand All @@ -131,9 +131,8 @@ func (r *LocationRepository) GetAll(ctx context.Context, gid uuid.UUID, filter L
} else {
query = strings.Replace(query, "{{ FILTER_CHILDREN }}", "", 1)
}
query = strings.Replace(query, "{{ GROUP_ID }}", gid.String(), 1)

rows, err := r.db.Sql().QueryContext(ctx, query)
rows, err := r.db.Sql().QueryContext(ctx, query, gid)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -279,8 +278,8 @@ func (r *LocationRepository) PathForLoc(ctx context.Context, gid, locID uuid.UUI
query := `WITH RECURSIVE location_path AS (
SELECT id, name, location_children
FROM locations
WHERE id = '{{ ID }}' -- Replace ? with the ID of the item's location
AND group_locations = '{{ GROUP_ID }}' -- Replace ? with the ID of the group
WHERE id = $1 -- Replace ? with the ID of the item's location
AND group_locations = $2 -- Replace ? with the ID of the group
UNION ALL
Expand All @@ -292,9 +291,7 @@ func (r *LocationRepository) PathForLoc(ctx context.Context, gid, locID uuid.UUI
SELECT id, name
FROM location_path`

query = strings.Replace(query, "{{ ID }}", locID.String(), 1)
query = strings.Replace(query, "{{ GROUP_ID }}", gid.String(), 1)
rows, err := r.db.Sql().QueryContext(ctx, query)
rows, err := r.db.Sql().QueryContext(ctx, query, locID, gid)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -335,7 +332,7 @@ func (r *LocationRepository) Tree(ctx context.Context, gid uuid.UUID, tq TreeQue
'location' AS node_type
FROM locations
WHERE location_children IS NULL
AND group_locations = '{{ GroupID }}'
AND group_locations = $1
UNION ALL
SELECT c.id,
Expand Down Expand Up @@ -404,9 +401,7 @@ func (r *LocationRepository) Tree(ctx context.Context, gid uuid.UUID, tq TreeQue
query = strings.ReplaceAll(query, "{{ WITH_ITEMS_FROM }}", "")
}

query = strings.ReplaceAll(query, "{{ GroupID }}", gid.String())

rows, err := r.db.Sql().QueryContext(ctx, query)
rows, err := r.db.Sql().QueryContext(ctx, query, gid)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 5c71c98

Please sign in to comment.