Skip to content
This repository has been archived by the owner on Jan 25, 2025. It is now read-only.

Commit

Permalink
Fixed bug
Browse files Browse the repository at this point in the history
  • Loading branch information
tahirmurata committed Sep 26, 2024
1 parent 577e986 commit 2a0593a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
36 changes: 21 additions & 15 deletions internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type Service interface {

CountEntryPerHour(node sqlc.Node) (map[string][24]EntryPerHour, error)

CountFoodStallPerHour(node sqlc.Node) (map[string][24]FoodStallPerHour, error)
CountFoodStallPerHour(node sqlc.Node) ([]FoodStallPerHour, error)

CountExhibitionPerHour(node sqlc.Node) ([24]ExhibitionPerHour, error)
}
Expand Down Expand Up @@ -813,7 +813,7 @@ func (s *DbService) CountEntryPerHour(node sqlc.Node) (map[string][24]EntryPerHo
}
for _, row := range rows {
temp := countPerHour[string(entryType)]
temp[int(row.Hour.Int.Int64())] = EntryPerHour{int(row.Hour.Int.Int64()), int(row.Count)}
temp[int(row.Hour)] = EntryPerHour{int(row.Hour), int(row.Count)}
countPerHour[string(entryType)] = temp
}
}
Expand All @@ -822,42 +822,48 @@ func (s *DbService) CountEntryPerHour(node sqlc.Node) (map[string][24]EntryPerHo
}

type FoodStallPerHour struct {
Name string
Foods [24]FoodPerHour
}

type FoodPerHour struct {
Hour int
Count int
}

func (s *DbService) CountFoodStallPerHour(node sqlc.Node) (map[string][24]FoodStallPerHour, error) {
func (s *DbService) CountFoodStallPerHour(node sqlc.Node) ([]FoodStallPerHour, error) {
ctx := context.Background()

q, err := s.DB.Begin(ctx)
defer func(q pgx.Tx, ctx context.Context) {
_ = q.Rollback(ctx)
}(q, ctx)
if err != nil {
return map[string][24]FoodStallPerHour{}, err
return []FoodStallPerHour{}, err
}
queries := sqlc.New(q)

foods, err := queries.GetFoodsByNodeId(ctx, node.ID)
if err != nil {
return map[string][24]FoodStallPerHour{}, err
return []FoodStallPerHour{}, err
}

var countPerHour = make(map[string][24]FoodStallPerHour)
var countsPerHour = make([]FoodStallPerHour, len(foods))

for _, food := range foods {
countPerHourByNode, err := queries.CountFoodStallPerHourByFoodId(ctx, food.ID)
for i, food := range foods {
countPerHourByFood, err := queries.CountFoodStallPerHourByFoodId(ctx, food.ID)
if err != nil {
return map[string][24]FoodStallPerHour{}, err
return []FoodStallPerHour{}, err
}
for _, row := range countPerHourByNode {
temp := countPerHour[food.Name]
temp[int(row.Hour.Int.Int64())] = FoodStallPerHour{int(row.Hour.Int.Int64()), int(row.Count)}
countPerHour[food.Name] = temp
var countPerHour = [24]FoodPerHour{}

for _, row := range countPerHourByFood {
countPerHour[int(row.Hour-1)] = FoodPerHour{int(row.Hour), int(row.Count)}
}
countsPerHour[i] = FoodStallPerHour{food.Name, countPerHour}
}

return countPerHour, nil
return countsPerHour, nil
}

type ExhibitionPerHour struct {
Expand All @@ -883,7 +889,7 @@ func (s *DbService) CountExhibitionPerHour(node sqlc.Node) ([24]ExhibitionPerHou
return [24]ExhibitionPerHour{}, err
}
for _, row := range rows {
countPerHour[int(row.Hour.Int.Int64())] = ExhibitionPerHour{int(row.Hour.Int.Int64()), int(row.Count)}
countPerHour[int(row.Hour)] = ExhibitionPerHour{int(row.Hour), int(row.Count)}
}

return countPerHour, nil
Expand Down
12 changes: 6 additions & 6 deletions internal/sqlc/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions sql/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ WHERE node_id = $1
AND type = $2;
-- name: CountEntryPerHourByNodeId :many
SELECT COUNT(*) AS count,
EXTRACT(HOUR FROM el.created_at) AS hour
DATE_PART('hour', el.created_at) AS hour
FROM entry_logs el
WHERE el.node_id = $1
AND type = $2
Expand All @@ -181,7 +181,7 @@ ORDER BY hour DESC
LIMIT 24;
-- name: CountFoodStallPerHourByFoodId :many
SELECT SUM(fsl.quantity) AS count,
EXTRACT(HOUR FROM fsl.created_at) AS hour
DATE_PART('hour', fsl.created_at) AS hour
FROM food_stall_logs fsl
JOIN node_foods nf ON fsl.node_food_id = nf.id
WHERE nf.food_id = $1
Expand All @@ -191,7 +191,7 @@ ORDER BY hour
LIMIT 24;
-- name: CountExhibitionPerHourByNodeId :many
SELECT COUNT(*) AS count,
EXTRACT(HOUR FROM el.created_at) AS hour
DATE_PART('hour', el.created_at) AS hour
FROM exhibition_logs el
WHERE el.node_id = $1
AND DATE(el.created_at) = CURRENT_DATE
Expand Down

0 comments on commit 2a0593a

Please sign in to comment.