Skip to content

Commit

Permalink
Merge pull request #55 from BakingUp/order
Browse files Browse the repository at this point in the history
[ADD] getAllStocksForOrder endpoint
  • Loading branch information
KNattawat89 authored Oct 11, 2024
2 parents dcb5c09 + fc768ae commit 0d4d0fa
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 22 deletions.
1 change: 1 addition & 0 deletions internal/adapter/handler/http/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func NewRouter(a *fiber.App, ingredientHandler IngredientHandler, recipeHandler
stock.Get("/getStockDetail", stockHandler.GetStockDetail)
stock.Delete("/deleteStock", stockHandler.DeleteStock)
stock.Get("/getStockBatch", stockHandler.GetStockBatch)
stock.Get("/getAllStocksForOrder", stockHandler.GetAllStocksForOrder)
}

order := api.Group("/order")
Expand Down
13 changes: 13 additions & 0 deletions internal/adapter/handler/http/stock.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ func (sh *StockHandler) GetAllStocks(c *fiber.Ctx) error {
return nil
}

func (sh *StockHandler) GetAllStocksForOrder(c *fiber.Ctx) error {
userID := c.Query("user_id")

stocks, err := sh.svc.GetAllStocksForOrder(c, userID)
if err != nil {
handleError(c, 400, "Cannot get all stocks for order page.", err.Error())
return nil
}

handleSuccess(c, stocks)
return nil
}

// GetStockDetail godoc
// @Summary Get stock details
// @Description Get stock details by recipe ID and user ID
Expand Down
18 changes: 18 additions & 0 deletions internal/adapter/storage/postgres/repository/stock.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ func (sr *StockRepository) GetAllStocks(c *fiber.Ctx, userID string) ([]db.Recip
return recipes, nil
}

//func (sr *StockRepository) GetAllStocksForOrder(c *fiber.Ctx, userID string) ([]db.RecipesModel, error) {
// recipes, err := sr.db.Recipes.FindMany(
// db.Recipes.UserID.Equals(userID),
// ).With(
// db.Recipes.Stocks.Fetch().With(
// db.Stocks.StockDetail.Fetch(),
// ),
// db.Recipes.RecipeImages.Fetch(),
// ).Exec(c.Context())
//
// if err != nil {
// return nil, err
// }
//
// return recipes, nil
//
//}

func (sr *StockRepository) GetStockDetail(c *fiber.Ctx, recipeID string) (*db.StocksModel, error) {
stock, err := sr.db.Stocks.FindFirst(
db.Stocks.RecipeID.Equals(recipeID),
Expand Down
40 changes: 27 additions & 13 deletions internal/core/domain/stock.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ type StockList struct {
}

type StockDetail struct {
StockDetailId string `json:"stock_detail_id"`
StockDetailId string `json:"stock_detail_id"`
CreatedAt time.Time `json:"created_at"`
LSTStatus string `json:"lst_status"`
Quantity int `json:"quantity"`
SellByDate string `json:"sell_by_date"`
LSTStatus string `json:"lst_status"`
Quantity int `json:"quantity"`
SellByDate string `json:"sell_by_date"`
}

type StockItemDetail struct {
StockName string `json:"stock_name"`
StockURL []string `json:"stock_url"`
Quantity int `json:"quantity"`
LST int `json:"lst"`
SellingPrice float64 `json:"selling_price"`
StockLessThan int `json:"stock_less_than"`
StockDetails []StockDetail `json:"stock_details"`
StockName string `json:"stock_name"`
StockURL []string `json:"stock_url"`
Quantity int `json:"quantity"`
LST int `json:"lst"`
SellingPrice float64 `json:"selling_price"`
StockLessThan int `json:"stock_less_than"`
StockDetails []StockDetail `json:"stock_details"`
}

type StockBatch struct {
Expand All @@ -40,6 +40,20 @@ type StockBatch struct {
RecipeURL string `json:"recipe_url"`
Quantity int `json:"quantity"`
SellByDate string `json:"sell_by_date"`
Note string `json:"note"`
Note string `json:"note"`
NoteCreatedAt string `json:"note_created_at"`
}
}

type StockOrderPage struct {
RecipeID string `json:"recipe_id"`
RecipeName string `json:"recipe_name"`
Quantity int `json:"quantity"`
SellByDate string `json:"sell_by_date"`
RecipeURL string `json:"recipe_url"`
SellingPrice float64 `json:"selling_price"`
Profit float64 `json:"profit"`
}

type OrderStockList struct {
OrderStocks []StockOrderPage `json:"order_stocks"`
}
2 changes: 2 additions & 0 deletions internal/core/port/stock.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ type StockRepository interface {
GetStockDetail(c *fiber.Ctx, recipeID string) (*db.StocksModel, error)
DeleteStock(c *fiber.Ctx, recipeID string) error
GetStockBatch(c *fiber.Ctx, stockDetailID string) (*db.StockDetailModel, error)
//GetAllStocksForOrder(c *fiber.Ctx, userID string) (*db.RecipesModel, error)
}

type StockService interface {
GetAllStocks(c *fiber.Ctx, userID string) (*domain.StockList, error)
GetStockDetail(c *fiber.Ctx, recipeID string) (*domain.StockItemDetail, error)
DeleteStock(c *fiber.Ctx, recipeID string) error
GetStockBatch(c *fiber.Ctx, stockDetailID string) (*domain.StockBatch, error)
GetAllStocksForOrder(c *fiber.Ctx, userID string) (*domain.OrderStockList, error)
}
71 changes: 66 additions & 5 deletions internal/core/service/stock.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/BakingUp/BakingUp-Backend/internal/core/util"
"github.com/BakingUp/BakingUp-Backend/prisma/db"
"github.com/gofiber/fiber/v2"
"time"
)

type StockService struct {
Expand Down Expand Up @@ -74,6 +75,66 @@ func (s *StockService) GetAllStocks(c *fiber.Ctx, userID string) (*domain.StockL

}

func (s *StockService) GetAllStocksForOrder(c *fiber.Ctx, userID string) (*domain.OrderStockList, error) {
stocks, err := s.stockRepo.GetAllStocks(c, userID)
if err != nil {
return nil, err
}

language, err := s.userService.GetUserLanguage(c, userID)
if err != nil {
return nil, err
}

var stockOrderPages []domain.StockOrderPage
recipe := make(map[string]db.RecipesModel)

for _, recipeItem := range stocks {
recipe[recipeItem.RecipeID] = recipeItem
}

for _, recipe := range stocks {
var stockOrderPage domain.StockOrderPage

stockOrderPage.RecipeID = recipe.RecipeID
stockOrderPage.RecipeName = util.GetRecipeName(&recipe, language)

if stockItem, ok := recipe.Stocks(); ok {
var dateTime time.Time
for _, stockDetail := range stockItem.StockDetail() {
stockOrderPage.Quantity += stockDetail.Quantity

if dateTime.IsZero() {
dateTime = stockDetail.SellByDate
} else if stockDetail.SellByDate.Before(dateTime) {
dateTime = stockDetail.SellByDate
}
}
profit := stockItem.SellingPrice - stockItem.Cost
stockOrderPage.SellByDate = dateTime.Format("02/01/2006")
stockOrderPage.SellingPrice = stockItem.SellingPrice
stockOrderPage.Profit = profit
} else {
continue
}

for _, recipeImage := range recipe.RecipeImages() {
if recipeImage.RecipeID == recipe.RecipeID {
stockOrderPage.RecipeURL = recipeImage.RecipeURL
break
}
}

stockOrderPages = append(stockOrderPages, stockOrderPage)
}

orderStockList := &domain.OrderStockList{
OrderStocks: stockOrderPages,
}

return orderStockList, nil
}

func (s *StockService) GetStockDetail(c *fiber.Ctx, recipeID string) (*domain.StockItemDetail, error) {
stock, err := s.stockRepo.GetStockDetail(c, recipeID)
if err != nil {
Expand Down Expand Up @@ -140,10 +201,10 @@ func (s *StockService) GetStockBatch(c *fiber.Ctx, stockDetailID string) (*domai
}

images := stockDetail.Stock().Recipe().RecipeImages()
firstRecipeURL := ""
if len(images) != 0 {
firstRecipeURL = images[0].RecipeURL
}
firstRecipeURL := ""
if len(images) != 0 {
firstRecipeURL = images[0].RecipeURL
}
stockNote, _ := stockDetail.Note()
stockBatch := &domain.StockBatch{
StockDetailId: stockDetail.StockDetailID,
Expand All @@ -156,4 +217,4 @@ func (s *StockService) GetStockBatch(c *fiber.Ctx, stockDetailID string) (*domai
}

return stockBatch, nil
}
}
8 changes: 4 additions & 4 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,10 @@ model Recipes {
recipe_thai_name String
total_time DateTime
serving Int
scale_servings Int
hidden_cost Float
labor_cost Float
profit_margin Float
scale_servings Int @default(-1)
hidden_cost Float @default(10)
labor_cost Float @default(150)
profit_margin Float @default(30)
recipe_images Recipe_images[]
recipe_ingredients Recipe_ingredients[]
recipe_eng_instruction_steps Recipe_eng_instruction_steps[]
Expand Down

0 comments on commit 0d4d0fa

Please sign in to comment.