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

[UPDATE] AddIngredientStock endpoint #59

Merged
merged 1 commit into from
Oct 14, 2024
Merged
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
65 changes: 29 additions & 36 deletions internal/core/service/ingredient.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ func (s *IngredientService) GetIngredientDetail(c *fiber.Ctx, ingredientID strin
}

sort.Slice(stockDetails, func(i, j int) bool {
dateI, _ := time.Parse("02/01/2006", stockDetails[i].ExpirationDate)
dateJ, _ := time.Parse("02/01/2006", stockDetails[j].ExpirationDate)
return dateI.After(dateJ)
})
dateI, _ := time.Parse("02/01/2006", stockDetails[i].ExpirationDate)
dateJ, _ := time.Parse("02/01/2006", stockDetails[j].ExpirationDate)
return dateI.After(dateJ)
})

stocks = append(stocks, stockDetails...)

Expand Down Expand Up @@ -269,51 +269,44 @@ func (s *IngredientService) AddIngredientStock(c *fiber.Ctx, ingredientStock *do
userID := ingredientStock.UserID
ingredientStockImg := ingredientStock.Img

var ingredientStockPayload *domain.AddIngredientStockPayload
commonPayload := domain.AddIngredientStockPayload{
IngredientStockID: ingredientStockID,
IngredientID: ingredientID,
IngredientQuantity: quantity,
Price: price,
ExpirationDate: expirationDate,
IngredientSupplier: ingredientStock.Supplier,
IngredientBrand: ingredientStock.IngredientBrand,
Note: ingredientStock.Note,
}

if ingredientStockImg != "" {
ingredientStockURL, err := util.UploadIngredientStockImage(userID, ingredientID, ingredientStockID, ingredientStockImg)
if err != nil {
return err
}
ingredientStockPayload = &domain.AddIngredientStockPayload{
IngredientStockID: ingredientStockID,
IngredientID: ingredientID,
IngredientQuantity: quantity,
Price: price,
ExpirationDate: expirationDate,
IngredientSupplier: ingredientStock.Supplier,
IngredientBrand: ingredientStock.IngredientBrand,
IngredientStockURL: ingredientStockURL,
Note: ingredientStock.Note,
}
} else {
ingredientStockPayload = &domain.AddIngredientStockPayload{
IngredientStockID: ingredientStockID,
IngredientID: ingredientID,
IngredientQuantity: quantity,
Price: price,
ExpirationDate: expirationDate,
IngredientSupplier: ingredientStock.Supplier,
IngredientBrand: ingredientStock.IngredientBrand,
Note: ingredientStock.Note,
}
commonPayload.IngredientStockURL = ingredientStockURL
}

ingredientStockPayload := &commonPayload

err := s.ingredientRepo.AddIngredientStock(c, ingredientStockPayload)
if err != nil {
return err
}

ingredientNote := &domain.AddIngredientNotePayload{
IngredientNoteID: ingredientNoteID,
IngredientStockID: ingredientStockID,
Note: ingredientStock.Note,
NoteCreatedAt: noteCreatedAt,
}
if ingredientStock.Note != "" {
ingredientNote := &domain.AddIngredientNotePayload{
IngredientNoteID: ingredientNoteID,
IngredientStockID: ingredientStockID,
Note: ingredientStock.Note,
NoteCreatedAt: noteCreatedAt,
}

err = s.ingredientRepo.AddIngredientNote(c, ingredientNote)
if err != nil {
return err
err = s.ingredientRepo.AddIngredientNote(c, ingredientNote)
if err != nil {
return err
}
}

return nil
Expand Down
Loading