feat(BE-50): add getOne endpoint for adjustment history

This commit is contained in:
aguhh18
2025-10-13 09:36:38 +07:00
parent cd4c908334
commit ce28429efd
4 changed files with 43 additions and 7 deletions
@@ -2,6 +2,7 @@ package controller
import ( import (
"math" "math"
"strconv"
"gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/adjustments/dto" "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/adjustments/dto"
service "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/adjustments/services" service "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/adjustments/services"
@@ -78,3 +79,25 @@ func (u *AdjustmentController) AdjustmentHistory(c *fiber.Ctx) error {
Data: adjustmentDTOs, Data: adjustmentDTOs,
}) })
} }
func (u *AdjustmentController) GetOne(c *fiber.Ctx) error {
param := c.Params("id")
id, err := strconv.Atoi(param)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
}
stockLog, err := u.AdjustmentService.GetOne(c, uint(id))
if err != nil {
return err
}
// Use DTO for response
return c.Status(fiber.StatusOK).
JSON(response.Success{
Code: fiber.StatusOK,
Status: "success",
Message: "Get adjustment successfully",
Data: dto.ToAdjustmentDetailDTO(stockLog),
})
}
@@ -4,15 +4,17 @@ import (
"time" "time"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities" entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
productCategoryDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/product-categories/dto"
userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto" userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
) )
// === DTO Structs === // === DTO Structs ===
type ProductBaseDTO struct { type ProductBaseDTO struct {
Id uint `json:"id"` Id uint `json:"id"`
Name string `json:"name"` Name string `json:"name"`
SKU string `json:"sku"` SKU string `json:"sku"`
ProductCategory *productCategoryDTO.ProductCategoryBaseDTO `json:"product_category,omitempty"`
} }
type WarehouseBaseDTO struct { type WarehouseBaseDTO struct {
@@ -61,10 +63,18 @@ func ToProductBaseDTO(e *entity.Product) *ProductBaseDTO {
if e.Sku != nil { if e.Sku != nil {
sku = *e.Sku sku = *e.Sku
} }
var category *productCategoryDTO.ProductCategoryBaseDTO
if e.ProductCategory.Id != 0 {
mapped := productCategoryDTO.ToProductCategoryBaseDTO(e.ProductCategory)
category = &mapped
}
return &ProductBaseDTO{ return &ProductBaseDTO{
Id: e.Id, Id: e.Id,
Name: e.Name, Name: e.Name,
SKU: sku, SKU: sku,
ProductCategory: category,
} }
} }
@@ -17,5 +17,6 @@ func AdjustmentRoutes(v1 fiber.Router, u user.UserService, s adjustment.Adjustme
// Standard CRUD routes following master data pattern // Standard CRUD routes following master data pattern
route.Get("/", ctrl.AdjustmentHistory) // Get all with pagination and filters route.Get("/", ctrl.AdjustmentHistory) // Get all with pagination and filters
route.Post("/", ctrl.Adjustment) // Create adjustment route.Post("/", ctrl.Adjustment) // Create adjustment
route.Get("/:id", ctrl.GetOne)
} }
@@ -50,7 +50,9 @@ func (s *adjustmentService) withRelations(db *gorm.DB) *gorm.DB {
} }
func (s *adjustmentService) GetOne(c *fiber.Ctx, id uint) (*entity.StockLog, error) { func (s *adjustmentService) GetOne(c *fiber.Ctx, id uint) (*entity.StockLog, error) {
stockLog, err := s.StockLogsRepository.GetByID(c.Context(), id, s.withRelations) stockLog, err := s.StockLogsRepository.GetByID(c.Context(), id, func(db *gorm.DB) *gorm.DB {
return s.withRelations(db).Preload("ProductWarehouse.Product.ProductCategory")
})
if err != nil { if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) { if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fiber.NewError(fiber.StatusNotFound, "Adjustment not found") return nil, fiber.NewError(fiber.StatusNotFound, "Adjustment not found")