Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get Menu By multiple Id #43

Merged
merged 7 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/doc/api
Submodule api updated 1 files
+60 −0 spec/open-api.yaml
2 changes: 1 addition & 1 deletion app/doc/statik/statik.go

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions app/domain/menu_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,22 @@ type MenuRepository interface {
Create(ctx context.Context, menu *Menu) error
GetByID(ctx context.Context, id string, city int32) (*Menu, error)
FetchByCity(ctx context.Context, limit int32, offset int32, offered time.Time, city int32) ([]*Menu, error)
Fetch(ctx context.Context, limit int32, offset int32, offered time.Time) ([]*Menu, error)
FetchByIDs(ctx context.Context, Limit int32, Offset int32, offered time.Time, ids []string) ([]*Menu, error)
}

type MenuUsecase interface {
Create(ctx context.Context, menu *Menu) error
GetByID(ctx context.Context, id string, city int32) (*Menu, error)
FetchByCity(ctx context.Context, limit int32, offset int32, offered time.Time, city int32) ([]*Menu, error)
Fetch(ctx context.Context, limit int32, offset int32, offered time.Time, ids []string) ([]*Menu, error)
}

type MenuController interface {
Create(c echo.Context) error
GetByID(c echo.Context) error
FetchByCity(c echo.Context) error
Fetch(c echo.Context) error
}

func (m *Menu) MarshalJSON() ([]byte, error) {
Expand Down
59 changes: 59 additions & 0 deletions app/domain/mocks/menu_domain.go

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

15 changes: 15 additions & 0 deletions app/infrastructure/db/query/menu.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,19 @@ FROM menus AS m
WHERE city_code = sqlc.arg(city_code)
AND offered_at <= sqlc.arg(offered_at)
ORDER BY offered_at DESC
LIMIT ? OFFSET ?;

-- name: ListMenuInIds :many
SELECT *
FROM menus
WHERE id IN (sqlc.slice(ids))
AND offered_at <= sqlc.arg(offered_at)
ORDER BY offered_at DESC
LIMIT ? OFFSET ?;

-- name: ListMenu :many
SELECT *
FROM menus
WHERE offered_at <= sqlc.arg(offered_at)
ORDER BY offered_at DESC
LIMIT ? OFFSET ?;
106 changes: 106 additions & 0 deletions app/infrastructure/db/sqlc/menu.sql.go

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

57 changes: 57 additions & 0 deletions app/infrastructure/db/sqlc/menu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,63 @@ func TestFetchMenusByCity(t *testing.T) {
require.Len(t, menus, 5)
}

func TestFetchMenus(t *testing.T) {
cityCode := util.RandomCityCode()
start := time.Now()
for i := 0; i < 10; i++ {
createRandomMenuFromStart(t, start, cityCode)
}

arg := ListMenuParams{
Limit: 5,
Offset: 5,
OfferedAt: start,
}

menus, err := testQuery.ListMenu(context.Background(), arg)

require.NoError(t, err)
require.Len(t, menus, 5)
}

func TestFetchMenusInId(t *testing.T) {
cityCode := util.RandomCityCode()
start := time.Now()
menus := make([]*domain.Menu, 0)

for i := 0; i < 10; i++ {
menu := createRandomMenuFromStart(t, start, cityCode)
menus = append(menus, menu)
}

ids := make([]string, 0, 5)

for i := 0; i < 5; i++ {
ids = append(ids, menus[i].ID)
}

arg := ListMenuInIdsParams{
Ids: ids,
OfferedAt: start,
Limit: domain.DEFAULT_LIMIT,
Offset: domain.DEFAULT_OFFSET,
}

resMenus, err := testQuery.ListMenuInIds(context.Background(), arg)

require.NoError(t, err)
require.Len(t, resMenus, 5)

resMenuIds := make([]string, 0, 5)

for _, menu := range resMenus {
resMenuIds = append(resMenuIds, menu.ID)
}

require.ElementsMatch(t, ids, resMenuIds)

}

func createRandomMenu(t *testing.T, cityCode int32) *domain.Menu {
id := util.RandomUlid()

Expand Down
30 changes: 30 additions & 0 deletions app/infrastructure/db/sqlc/mocks/query.go

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

2 changes: 2 additions & 0 deletions app/infrastructure/db/sqlc/querier.go

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

Loading
Loading