From 420a35adbd14fcc90f0f1ce48924f0141433d651 Mon Sep 17 00:00:00 2001 From: shouta0715 Date: Sun, 28 Jan 2024 00:08:29 +0900 Subject: [PATCH] fix: menu order by offered_at --- app/infrastructure/db/query/menu.sql | 12 ++++++------ app/infrastructure/db/sqlc/menu.sql.go | 12 ++++++------ app/infrastructure/db/sqlc/menu_test.go | 10 ++++++---- app/util/random.go | 2 +- 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/app/infrastructure/db/query/menu.sql b/app/infrastructure/db/query/menu.sql index 579e368..b4cd357 100644 --- a/app/infrastructure/db/query/menu.sql +++ b/app/infrastructure/db/query/menu.sql @@ -26,8 +26,8 @@ WHERE id = sqlc.arg(id) SELECT * FROM menus AS m WHERE city_code = sqlc.arg(city_code) - AND offered_at >= sqlc.arg(offered_at) -ORDER BY offered_at + AND offered_at <= sqlc.arg(offered_at) +ORDER BY offered_at DESC LIMIT ? OFFSET ?; -- name: GetMenuWithDishes :one @@ -63,9 +63,9 @@ SELECT m.*, FROM menus AS m LEFT JOIN dishes AS d ON m.id = d.menu_id WHERE m.city_code = sqlc.arg(city_code) - AND m.offered_at >= sqlc.arg(offered_at) + AND m.offered_at <= sqlc.arg(offered_at) GROUP BY m.id -ORDER BY offered_at +ORDER BY offered_at DESC LIMIT ? OFFSET ?; -- name: ListMenuWithDishes :many @@ -82,7 +82,7 @@ SELECT m.*, ) AS dishes FROM menus AS m LEFT JOIN dishes AS d ON m.id = d.menu_id -WHERE m.offered_at >= sqlc.arg(offered_at) +WHERE m.offered_at <= sqlc.arg(offered_at) GROUP BY m.id -ORDER BY offered_at +ORDER BY offered_at DESC LIMIT ? OFFSET ?; \ No newline at end of file diff --git a/app/infrastructure/db/sqlc/menu.sql.go b/app/infrastructure/db/sqlc/menu.sql.go index 4908345..8a24853 100644 --- a/app/infrastructure/db/sqlc/menu.sql.go +++ b/app/infrastructure/db/sqlc/menu.sql.go @@ -134,8 +134,8 @@ const listMenuByCity = `-- name: ListMenuByCity :many SELECT id, offered_at, photo_url, created_at, elementary_school_calories, junior_high_school_calories, city_code FROM menus AS m WHERE city_code = ? - AND offered_at >= ? -ORDER BY offered_at + AND offered_at <= ? +ORDER BY offered_at DESC LIMIT ? OFFSET ? ` @@ -196,9 +196,9 @@ SELECT m.id, m.offered_at, m.photo_url, m.created_at, m.elementary_school_calori ) AS dishes FROM menus AS m LEFT JOIN dishes AS d ON m.id = d.menu_id -WHERE m.offered_at >= ? +WHERE m.offered_at <= ? GROUP BY m.id -ORDER BY offered_at +ORDER BY offered_at DESC LIMIT ? OFFSET ? ` @@ -266,9 +266,9 @@ SELECT m.id, m.offered_at, m.photo_url, m.created_at, m.elementary_school_calori FROM menus AS m LEFT JOIN dishes AS d ON m.id = d.menu_id WHERE m.city_code = ? - AND m.offered_at >= ? + AND m.offered_at <= ? GROUP BY m.id -ORDER BY offered_at +ORDER BY offered_at DESC LIMIT ? OFFSET ? ` diff --git a/app/infrastructure/db/sqlc/menu_test.go b/app/infrastructure/db/sqlc/menu_test.go index 1c76584..170b9cb 100644 --- a/app/infrastructure/db/sqlc/menu_test.go +++ b/app/infrastructure/db/sqlc/menu_test.go @@ -96,8 +96,9 @@ func TestGetWithDishes(t *testing.T) { } func TestFetchMenuWithDishesByCity(t *testing.T) { + start := time.Now() for i := 0; i < 10; i++ { - menu := createRandomMenu(t) + menu := createRandomMenuFromStart(t, start) for j := 0; j < 10; j++ { createRandomDish(t, menu.ID) @@ -105,9 +106,10 @@ func TestFetchMenuWithDishesByCity(t *testing.T) { } arg := ListMenuWithDishesByCityParams{ - Limit: 5, - Offset: 5, - CityCode: cityCode, + Limit: 5, + Offset: 5, + CityCode: cityCode, + OfferedAt: start, } results, err := testQuery.ListMenuWithDishesByCity(context.Background(), arg) diff --git a/app/util/random.go b/app/util/random.go index 3dc6a9e..78eb1db 100644 --- a/app/util/random.go +++ b/app/util/random.go @@ -70,7 +70,7 @@ func RandomYYYYMMDD() string { func RandomDateFromStart(start time.Time) time.Time { // YYYY-MM-DD - year := RandomInt(start.Year(), start.Year()+1000) + year := RandomInt(start.Year()-1000, start.Year()) month := RandomInt(1, 12) day := RandomInt(1, 31)