Skip to content
Open
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
13 changes: 11 additions & 2 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ package main
import (
"log"

"github.com/fun-dotto/api-template/internal/database"
api "github.com/fun-dotto/funch-api/generated"
"github.com/fun-dotto/funch-api/internal/database"
"github.com/fun-dotto/funch-api/internal/handler"
"github.com/fun-dotto/funch-api/internal/repository"
"github.com/fun-dotto/funch-api/internal/service"
"github.com/getkin/kin-openapi/openapi3"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
Expand All @@ -29,6 +33,10 @@ func main() {
log.Fatalf("Failed to migrate database: %v", err)
}

menuItemRepository := repository.NewMenuItemRepository(db)
menuItemService := service.NewMenuItemService(menuItemRepository)
h := handler.NewHandler(menuItemService)

spec, err := openapi3.NewLoader().LoadFromFile("openapi/openapi.yaml")
if err != nil {
log.Fatalf("Failed to load OpenAPI spec: %v", err)
Expand All @@ -40,7 +48,8 @@ func main() {

router.Use(middleware.OapiRequestValidator(spec))

// TODO: Implement handler
strictHandler := api.NewStrictHandler(h, nil)
api.RegisterHandlers(router, strictHandler)
Comment on lines 49 to +52

addr := ":8080"
log.Printf("Server starting on %s", addr)
Expand Down
84 changes: 84 additions & 0 deletions generated/api.gen.go

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

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/fun-dotto/api-template
module github.com/fun-dotto/funch-api

go 1.25.6

Expand Down
71 changes: 71 additions & 0 deletions internal/database/funch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package database

import (
"time"

"github.com/fun-dotto/funch-api/internal/domain"
)

type MenuItem struct {
ID string `gorm:"primaryKey;type:uuid"`
Date time.Time `gorm:"not null;index;type:date"`
Name string `gorm:"not null"`
ImageURL string `gorm:"not null"`
Category string `gorm:"not null;index"`

CreatedAt time.Time `gorm:"autoCreateTime"`
UpdatedAt time.Time `gorm:"autoUpdateTime"`
}

type MenuItemPrice struct {
ID string `gorm:"primaryKey;type:uuid"`
MenuItemID string `gorm:"not null;index;type:uuid"`
Size string `gorm:"not null"`
Price int32 `gorm:"not null"`

CreatedAt time.Time `gorm:"autoCreateTime"`
UpdatedAt time.Time `gorm:"autoUpdateTime"`

MenuItem MenuItem `gorm:"foreignKey:MenuItemID"`
}

func (m *MenuItem) ToDomain(prices []MenuItemPrice) domain.MenuItem {
domainPrices := make([]domain.Price, len(prices))
for i, p := range prices {
domainPrices[i] = domain.Price{
Size: domain.Size(p.Size),
Price: p.Price,
}
}

return domain.MenuItem{
ID: m.ID,
Date: m.Date,
Name: m.Name,
ImageURL: m.ImageURL,
Category: domain.Category(m.Category),
Prices: domainPrices,
}
}

func FromDomainMenuItem(menuItem domain.MenuItem) MenuItem {
return MenuItem{
ID: menuItem.ID,
Date: menuItem.Date,
Name: menuItem.Name,
ImageURL: menuItem.ImageURL,
Category: string(menuItem.Category),
}
}

func FromDomainMenuItemPrices(menuItemID string, prices []domain.Price) []MenuItemPrice {
result := make([]MenuItemPrice, len(prices))
for i, p := range prices {
result[i] = MenuItemPrice{
MenuItemID: menuItemID,
Size: string(p.Size),
Price: p.Price,
}
}
return result
}
5 changes: 4 additions & 1 deletion internal/database/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ package database
import "gorm.io/gorm"

func AutoMigrate(db *gorm.DB) error {
return db.AutoMigrate()
return db.AutoMigrate(
&MenuItem{},
&MenuItemPrice{},
)
}
35 changes: 35 additions & 0 deletions internal/domain/funch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package domain

import "time"

type Category string

const (
CategorySetAndSingle Category = "SetAndSingle"
CategoryBowlAndCurry Category = "BowlAndCurry"
CategoryNoodle Category = "Noodle"
CategorySide Category = "Side"
CategoryDessert Category = "Dessert"
)

type Size string

const (
SizeSmall Size = "Small"
SizeMedium Size = "Medium"
SizeLarge Size = "Large"
)

type MenuItem struct {
ID string
Date time.Time
Name string
ImageURL string
Category Category
Prices []Price
}

type Price struct {
Size Size
Price int32
}
27 changes: 27 additions & 0 deletions internal/handler/converter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package handler

import (
openapi_types "github.com/oapi-codegen/runtime/types"

api "github.com/fun-dotto/funch-api/generated"
"github.com/fun-dotto/funch-api/internal/domain"
)

func toApiMenuItem(menuItem domain.MenuItem) api.MenuItem {
apiPrices := make([]api.Price, len(menuItem.Prices))
for i, price := range menuItem.Prices {
apiPrices[i] = api.Price{
Size: api.Size(price.Size),
Price: price.Price,
}
}

return api.MenuItem{
Id: menuItem.ID,
Date: openapi_types.Date{Time: menuItem.Date},
Name: menuItem.Name,
ImageUrl: menuItem.ImageURL,
Category: api.Category(menuItem.Category),
Prices: apiPrices,
}
}
12 changes: 12 additions & 0 deletions internal/handler/create_menu_items_v1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package handler

import (
"context"
"fmt"

api "github.com/fun-dotto/funch-api/generated"
)

func (h *Handler) MenuItemsV1Create(ctx context.Context, request api.MenuItemsV1CreateRequestObject) (api.MenuItemsV1CreateResponseObject, error) {
return nil, fmt.Errorf("not implemented")
Comment on lines +5 to +11
}
14 changes: 14 additions & 0 deletions internal/handler/funch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package handler

import (
"github.com/fun-dotto/funch-api/internal/service"
)

type Handler struct {
menuItemService *service.MenuItemService
}

func NewHandler(menuItemService *service.MenuItemService) *Handler {
return &Handler{menuItemService: menuItemService}
}

25 changes: 25 additions & 0 deletions internal/handler/get_menu_items_v1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package handler

import (
"context"

api "github.com/fun-dotto/funch-api/generated"
)

func (h *Handler) MenuItemsV1List(ctx context.Context, request api.MenuItemsV1ListRequestObject) (api.MenuItemsV1ListResponseObject, error) {
date := request.Params.Date.Time

menuItems, err := h.menuItemService.GetMenuItemsByDate(ctx, date)
if err != nil {
return nil, err
}

apiMenuItems := make([]api.MenuItem, len(menuItems))
for i, menuItem := range menuItems {
apiMenuItems[i] = toApiMenuItem(menuItem)
}

return api.MenuItemsV1List200JSONResponse{
MenuItems: apiMenuItems,
}, nil
}
Loading
Loading