add GET delivery count route

This commit is contained in:
Egor Pozharov
2026-04-14 15:38:45 +06:00
parent fc46fb372f
commit 10233808f4
5 changed files with 46 additions and 1 deletions

View File

@@ -15,6 +15,7 @@ type Querier interface {
DeleteDelivery(ctx context.Context, id pgtype.UUID) error
GetDeliveriesByDate(ctx context.Context, date pgtype.Date) ([]Delivery, error)
GetDeliveryByID(ctx context.Context, id pgtype.UUID) (Delivery, error)
GetDeliveryCount(ctx context.Context) ([]GetDeliveryCountRow, error)
UpdateDelivery(ctx context.Context, arg UpdateDeliveryParams) error
}

View File

@@ -127,6 +127,35 @@ func (q *Queries) GetDeliveryByID(ctx context.Context, id pgtype.UUID) (Delivery
return i, err
}
const getDeliveryCount = `-- name: GetDeliveryCount :many
SELECT COUNT(*) as count, date FROM deliveries WHERE date >= DATE_TRUNC('month', CURRENT_DATE) GROUP BY date
`
type GetDeliveryCountRow struct {
Count int64 `db:"count" json:"count"`
Date pgtype.Date `db:"date" json:"date"`
}
func (q *Queries) GetDeliveryCount(ctx context.Context) ([]GetDeliveryCountRow, error) {
rows, err := q.db.Query(ctx, getDeliveryCount)
if err != nil {
return nil, err
}
defer rows.Close()
items := []GetDeliveryCountRow{}
for rows.Next() {
var i GetDeliveryCountRow
if err := rows.Scan(&i.Count, &i.Date); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const updateDelivery = `-- name: UpdateDelivery :exec
UPDATE deliveries SET date = $1, pickup_location = $2, product_name = $3, address = $4, phone = $5, additional_phone = $6, has_elevator = $7, comment = $8, updated_at = NOW() WHERE id = $9
`