feat: add GetLinesByMovementId method to InventoryMovementController and service for retrieving movement lines

This commit is contained in:
Habib Fatkhul Rohman 2025-11-28 14:39:02 +07:00
parent 0eed8f6a7b
commit cf380fc711
5 changed files with 36 additions and 1 deletions

View File

@ -17,6 +17,7 @@ type InventoryMovementController interface {
Delete(ctx *gin.Context) Delete(ctx *gin.Context)
GetById(ctx *gin.Context) GetById(ctx *gin.Context)
GetAll(ctx *gin.Context) GetAll(ctx *gin.Context)
GetLinesByMovementId(ctx *gin.Context)
CreateLine(ctx *gin.Context) CreateLine(ctx *gin.Context)
UpdateLine(ctx *gin.Context) UpdateLine(ctx *gin.Context)
DeleteLine(ctx *gin.Context) DeleteLine(ctx *gin.Context)
@ -26,6 +27,18 @@ type inventoryMovementController struct {
movementService service.InventoryMovementService movementService service.InventoryMovementService
} }
func (c *inventoryMovementController) GetLinesByMovementId(ctx *gin.Context) {
id := ctx.Param("id")
result, err := c.movementService.GetLinesByMovementId(ctx, id)
if err != nil {
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_INVENTORY_MOVEMENT_LINE, err.Error(), nil)
ctx.JSON(http.StatusInternalServerError, res)
return
}
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_GET_INVENTORY_MOVEMENT_LINE, result)
ctx.JSON(http.StatusOK, res)
}
func (c *inventoryMovementController) Create(ctx *gin.Context) { func (c *inventoryMovementController) Create(ctx *gin.Context) {
var req dto.InventoryMovementCreateRequest var req dto.InventoryMovementCreateRequest
if err := ctx.ShouldBindJSON(&req); err != nil { if err := ctx.ShouldBindJSON(&req); err != nil {

View File

@ -22,6 +22,8 @@ const (
MESSAGE_SUCCESS_DELETE_INVENTORY_MOVEMENT = "success delete inventory movement" MESSAGE_SUCCESS_DELETE_INVENTORY_MOVEMENT = "success delete inventory movement"
MESSAGE_SUCCESS_DELETE_INVENTORY_MOVEMENT_LINE = "success delete inventory movement line" MESSAGE_SUCCESS_DELETE_INVENTORY_MOVEMENT_LINE = "success delete inventory movement line"
MESSAGE_FAILED_GET_DATA_FROM_BODY = "failed get data from body" MESSAGE_FAILED_GET_DATA_FROM_BODY = "failed get data from body"
MESSAGE_FAILED_GET_INVENTORY_MOVEMENT_LINE = "failed get inventory movement line"
MESSAGE_SUCCESS_GET_INVENTORY_MOVEMENT_LINE = "success get inventory movement line"
) )
type InventoryMovementCreateRequest struct { type InventoryMovementCreateRequest struct {

View File

@ -52,7 +52,9 @@ func (r *inventoryMovementRepository) GetAll(ctx context.Context, filter query.I
db = query.ApplyInventoryMovementFilters(db, filter) db = query.ApplyInventoryMovementFilters(db, filter)
var movements []entities.TInventoryMovementEntity var movements []entities.TInventoryMovementEntity
var total int64 var total int64
if err := db.Find(&movements).Count(&total).Error; err != nil { if err := db.
Preload("Client").
Find(&movements).Count(&total).Error; err != nil {
return nil, 0, err return nil, 0, err
} }
return movements, total, nil return movements, total, nil

View File

@ -20,6 +20,7 @@ func RegisterRoutes(server *gin.Engine, injector *do.Injector) {
movementRoutes.PUT(":id", middlewares.Authenticate(jwtService), movementController.Update) movementRoutes.PUT(":id", middlewares.Authenticate(jwtService), movementController.Update)
movementRoutes.DELETE(":id", middlewares.Authenticate(jwtService), movementController.Delete) movementRoutes.DELETE(":id", middlewares.Authenticate(jwtService), movementController.Delete)
movementRoutes.GET("", middlewares.Authenticate(jwtService), movementController.GetAll) movementRoutes.GET("", middlewares.Authenticate(jwtService), movementController.GetAll)
movementRoutes.GET(":id/lines", middlewares.Authenticate(jwtService), movementController.GetLinesByMovementId)
movementRoutes.POST(":id/lines", middlewares.Authenticate(jwtService), movementController.CreateLine) movementRoutes.POST(":id/lines", middlewares.Authenticate(jwtService), movementController.CreateLine)
movementRoutes.PUT("lines/:id", middlewares.Authenticate(jwtService), movementController.UpdateLine) movementRoutes.PUT("lines/:id", middlewares.Authenticate(jwtService), movementController.UpdateLine)
movementRoutes.DELETE("lines/:id", middlewares.Authenticate(jwtService), movementController.DeleteLine) movementRoutes.DELETE("lines/:id", middlewares.Authenticate(jwtService), movementController.DeleteLine)

View File

@ -18,6 +18,7 @@ type InventoryMovementService interface {
GetAll(ctx context.Context, filter query.InventoryMovementFilter) ([]dtodomain.InventoryMovementResponse, int64, error) GetAll(ctx context.Context, filter query.InventoryMovementFilter) ([]dtodomain.InventoryMovementResponse, int64, error)
Update(ctx context.Context, req dtodomain.InventoryMovementUpdateRequest, id string) (dtodomain.InventoryMovementResponse, error) Update(ctx context.Context, req dtodomain.InventoryMovementUpdateRequest, id string) (dtodomain.InventoryMovementResponse, error)
Delete(ctx context.Context, id string) error Delete(ctx context.Context, id string) error
GetLinesByMovementId(ctx context.Context, movementId string) ([]dtodomain.InventoryMovementLineResponse, error)
CreateLine(ctx context.Context, movementId string, req dtodomain.InventoryMovementLineCreateRequest) (dtodomain.InventoryMovementLineResponse, error) CreateLine(ctx context.Context, movementId string, req dtodomain.InventoryMovementLineCreateRequest) (dtodomain.InventoryMovementLineResponse, error)
UpdateLine(ctx context.Context, lineId string, req dtodomain.InventoryMovementLineUpdateRequest) (dtodomain.InventoryMovementLineResponse, error) UpdateLine(ctx context.Context, lineId string, req dtodomain.InventoryMovementLineUpdateRequest) (dtodomain.InventoryMovementLineResponse, error)
DeleteLine(ctx context.Context, lineId string) error DeleteLine(ctx context.Context, lineId string) error
@ -29,6 +30,22 @@ type inventoryMovementService struct {
movementLineRepo repository.InventoryMovementLineRepository movementLineRepo repository.InventoryMovementLineRepository
} }
// GetLinesByMovementId implements InventoryMovementService.
func (s *inventoryMovementService) GetLinesByMovementId(ctx context.Context, movementId string) ([]dtodomain.InventoryMovementLineResponse, error) {
lines, err := s.movementLineRepo.GetAllByMovementId(ctx, movementId)
if err != nil {
return nil, err
}
var responses []dtodomain.InventoryMovementLineResponse
for _, e := range lines {
responses = append(responses, dtodomain.ToInventoryMovementLineResponse(e))
}
if responses == nil {
responses = make([]dtodomain.InventoryMovementLineResponse, 0)
}
return responses, nil
}
func (s *inventoryMovementService) Create(ctx context.Context, req dtodomain.InventoryMovementCreateRequest) (dtodomain.InventoryMovementResponse, error) { func (s *inventoryMovementService) Create(ctx context.Context, req dtodomain.InventoryMovementCreateRequest) (dtodomain.InventoryMovementResponse, error) {
tx := s.db.Begin() tx := s.db.Begin()
defer func() { defer func() {