feat: add GetLinesByRequestId method to InventoryRequestController and service for retrieving request lines
Deploy Application / deploy (push) Successful in 31s Details

This commit is contained in:
Habib Fatkhul Rohman 2025-11-28 15:07:52 +07:00
parent cf380fc711
commit 3dd7e60827
5 changed files with 146 additions and 86 deletions

View File

@ -19,6 +19,7 @@ type InventoryRequestController interface {
Delete(ctx *gin.Context)
GetById(ctx *gin.Context)
GetAll(ctx *gin.Context)
GetLinesByRequestId(ctx *gin.Context)
CreateLine(ctx *gin.Context)
UpdateLine(ctx *gin.Context)
DeleteLine(ctx *gin.Context)
@ -29,6 +30,19 @@ type inventoryRequestController struct {
db *gorm.DB
}
// GetLinesByRequestId implements InventoryRequestController.
func (c *inventoryRequestController) GetLinesByRequestId(ctx *gin.Context) {
id := ctx.Param("id")
lines, err := c.requestService.GetLinesByRequestId(ctx, id)
if err != nil {
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_INVENTORY_REQUEST_LINE, err.Error(), nil)
ctx.JSON(http.StatusInternalServerError, res)
return
}
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_GET_INVENTORY_REQUEST_LINE, lines)
ctx.JSON(http.StatusOK, res)
}
func (c *inventoryRequestController) DeleteLine(ctx *gin.Context) {
id := ctx.Param("id")
if err := c.requestService.DeleteLine(ctx, id); err != nil {

View File

@ -1,6 +1,11 @@
package dto
import pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto"
import (
"github.com/Caknoooo/go-gin-clean-starter/database/entities"
pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto"
"github.com/Caknoooo/go-gin-clean-starter/pkg/utils"
"github.com/google/uuid"
)
const (
MESSAGE_FAILED_CREATE_INVENTORY_REQUEST = "failed create inventory request"
@ -18,6 +23,8 @@ const (
MESSAGE_SUCCESS_DELETE_INVENTORY_REQUEST = "success delete inventory request"
MESSAGE_SUCCESS_DELETE_INVENTORY_REQUEST_LINE = "success delete inventory request line"
MESSAGE_FAILED_GET_DATA_FROM_BODY = "failed get data from body"
MESSAGE_FAILED_GET_INVENTORY_REQUEST_LINE = "failed get inventory request line"
MESSAGE_SUCCESS_GET_INVENTORY_REQUEST_LINE = "success get inventory request line"
)
type InventoryRequestCreateRequest struct {
@ -47,17 +54,17 @@ type InventoryRequestUpdateRequest struct {
}
type InventoryRequestResponse struct {
ID string `json:"id"`
ReferenceNumber string `json:"reference_number"`
DocumentNumber string `json:"document_number"`
DueDate string `json:"due_date"`
RequestType string `json:"request_type"`
Note string `json:"note"`
Status string `json:"status"`
Client pkgdto.IdNameResponse `json:"client"`
RequestLines []InventoryRequestLineResponse `json:"request_lines"`
LineCount int `json:"line_count"`
Assignment AssignmentResponse `json:"assignment,omitempty"`
ID string `json:"id"`
ReferenceNumber string `json:"reference_number"`
DocumentNumber string `json:"document_number"`
DueDate string `json:"due_date"`
RequestType string `json:"request_type"`
Note string `json:"note"`
Status string `json:"status"`
Client pkgdto.IdNameResponse `json:"client"`
// RequestLines []InventoryRequestLineResponse `json:"request_lines"`
// LineCount int `json:"line_count"`
Assignment *AssignmentResponse `json:"assignment"`
}
type AssignmentResponse struct {
@ -93,3 +100,99 @@ type InventoryRequestLineUpdateRequest struct {
Quantity *float64 `json:"quantity"`
ProductID *string `json:"product_id"`
}
func ToAssignmentResponse(e entities.TAssignmentEntity) AssignmentResponse {
users := make([]AssignmentUserResponse, 0)
for _, user := range e.AssignmentUsers {
userResp := AssignmentUserResponse{
ID: user.ID.String(),
TaskType: user.TaskType,
User: pkgdto.IdNameResponse{ID: user.User.ID.String(), Name: user.User.Name},
Role: pkgdto.IdNameResponse{ID: user.Role.ID.String(), Name: user.Role.Name},
}
users = append(users, userResp)
}
return AssignmentResponse{
ID: e.ID.String(),
DocumentType: e.DocumentType,
DocumentID: e.DocumentID.String(),
AssignmentUsers: users,
}
}
func ToInventoryRequestResponse(e entities.TInventoryRequestEntity) InventoryRequestResponse {
client := pkgdto.IdNameResponse{}
if e.Client.ID != uuid.Nil {
client = pkgdto.IdNameResponse{
ID: e.Client.ID.String(),
Name: e.Client.Name,
}
}
// lines := make([]InventoryRequestLineResponse, 0)
// for _, line := range e.RequestLines {
// product := InventoryRequestLineProductResponse{}
// if line.Product.ID != uuid.Nil {
// product = InventoryRequestLineProductResponse{
// ID: line.Product.ID.String(),
// Name: line.Product.Name,
// RefNumber: line.Product.RefNumber,
// Uom: pkgdto.IdNameResponse{
// ID: line.Product.Uom.ID.String(),
// Name: line.Product.Uom.Name,
// },
// }
// }
// lines = append(lines, InventoryRequestLineResponse{
// ID: line.ID.String(),
// Quantity: line.Quantity,
// // CurrentStock: line.Product.CurrentStock,
// Product: product,
// ClientID: line.ClientID.String(),
// })
// }
var assignment *AssignmentResponse
if e.Assignment.ID != uuid.Nil {
ass := ToAssignmentResponse(e.Assignment)
assignment = &ass
} else {
assignment = nil
}
return InventoryRequestResponse{
ID: e.ID.String(),
ReferenceNumber: e.ReferenceNumber,
DocumentNumber: e.DocumentNumber,
DueDate: utils.DateTimeToString(e.DueDate),
RequestType: e.RequestType,
Note: e.Note,
Status: e.Status,
Client: client,
// LineCount: len(lines),
// RequestLines: lines,
Assignment: assignment,
}
}
func ToInventoryRequestLineResponses(linesEntity []entities.TInventoryRequestLineEntity) []InventoryRequestLineResponse {
lines := make([]InventoryRequestLineResponse, 0)
for _, line := range linesEntity {
product := InventoryRequestLineProductResponse{}
if line.Product.ID != uuid.Nil {
product = InventoryRequestLineProductResponse{
ID: line.Product.ID.String(),
Name: line.Product.Name,
RefNumber: line.Product.RefNumber,
Uom: pkgdto.IdNameResponse{
ID: line.Product.Uom.ID.String(),
Name: line.Product.Uom.Name,
},
}
}
lines = append(lines, InventoryRequestLineResponse{
ID: line.ID.String(),
Quantity: line.Quantity,
Product: product,
ClientID: line.ClientID.String(),
})
}
return lines
}

View File

@ -41,8 +41,8 @@ func (r *inventoryRequestRepository) GetById(ctx context.Context, tx *gorm.DB, i
var request entities.TInventoryRequestEntity
if err := tx.WithContext(ctx).
Preload("Client").
Preload("RequestLines").
Preload("RequestLines.Product").
// Preload("RequestLines").
// Preload("RequestLines.Product").
First(&request, "id = ?", id).Error; err != nil {
return request, err
}

View File

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

View File

@ -19,6 +19,7 @@ type InventoryRequestService interface {
GetAll(ctx context.Context, filter query.InventoryRequestFilter) ([]dtodomain.InventoryRequestResponse, int64, error)
Update(ctx context.Context, req dtodomain.InventoryRequestUpdateRequest, id string) (dtodomain.InventoryRequestResponse, error)
Delete(ctx context.Context, id string) error
GetLinesByRequestId(ctx context.Context, requestId string) ([]dtodomain.InventoryRequestLineResponse, error)
CreateLine(ctx context.Context, requestId string, req dtodomain.InventoryRequestLineCreateRequest) (dtodomain.InventoryRequestLineResponse, error)
UpdateLine(ctx context.Context, lineId string, req dtodomain.InventoryRequestLineUpdateRequest) (dtodomain.InventoryRequestLineResponse, error)
DeleteLine(ctx context.Context, lineId string) error
@ -30,6 +31,15 @@ type inventoryRequestService struct {
requestLineRepo repository.InventoryRequestLineRepository
}
// GetLinesByRequestId implements InventoryRequestService.
func (s *inventoryRequestService) GetLinesByRequestId(ctx context.Context, requestId string) ([]dtodomain.InventoryRequestLineResponse, error) {
lines, err := s.requestLineRepo.GetAllByRequestId(ctx, requestId)
if err != nil {
return nil, err
}
return dtodomain.ToInventoryRequestLineResponses(lines), nil
}
func (s *inventoryRequestService) DeleteLine(ctx context.Context, lineId string) error {
return s.requestLineRepo.Delete(ctx, nil, lineId)
}
@ -77,74 +87,6 @@ func (s *inventoryRequestService) UpdateLine(ctx context.Context, lineId string,
}, nil
}
func toAssignmentResponse(e entities.TAssignmentEntity) dtodomain.AssignmentResponse {
users := make([]dtodomain.AssignmentUserResponse, 0)
for _, user := range e.AssignmentUsers {
userResp := dtodomain.AssignmentUserResponse{
ID: user.ID.String(),
TaskType: user.TaskType,
User: pkgdto.IdNameResponse{ID: user.User.ID.String(), Name: user.User.Name},
Role: pkgdto.IdNameResponse{ID: user.Role.ID.String(), Name: user.Role.Name},
}
users = append(users, userResp)
}
return dtodomain.AssignmentResponse{
ID: e.ID.String(),
DocumentType: e.DocumentType,
DocumentID: e.DocumentID.String(),
AssignmentUsers: users,
}
}
func toInventoryRequestResponse(e entities.TInventoryRequestEntity) dtodomain.InventoryRequestResponse {
client := pkgdto.IdNameResponse{}
if e.Client.ID != uuid.Nil {
client = pkgdto.IdNameResponse{
ID: e.Client.ID.String(),
Name: e.Client.Name,
}
}
lines := make([]dtodomain.InventoryRequestLineResponse, 0)
for _, line := range e.RequestLines {
product := dtodomain.InventoryRequestLineProductResponse{}
if line.Product.ID != uuid.Nil {
product = dtodomain.InventoryRequestLineProductResponse{
ID: line.Product.ID.String(),
Name: line.Product.Name,
RefNumber: line.Product.RefNumber,
Uom: pkgdto.IdNameResponse{
ID: line.Product.Uom.ID.String(),
Name: line.Product.Uom.Name,
},
}
}
lines = append(lines, dtodomain.InventoryRequestLineResponse{
ID: line.ID.String(),
Quantity: line.Quantity,
// CurrentStock: line.Product.CurrentStock,
Product: product,
ClientID: line.ClientID.String(),
})
}
assignment := dtodomain.AssignmentResponse{}
if e.Assignment.ID != uuid.Nil {
assignment = toAssignmentResponse(e.Assignment)
}
return dtodomain.InventoryRequestResponse{
ID: e.ID.String(),
ReferenceNumber: e.ReferenceNumber,
DocumentNumber: e.DocumentNumber,
DueDate: utils.DateTimeToString(e.DueDate),
RequestType: e.RequestType,
Note: e.Note,
Status: e.Status,
Client: client,
LineCount: len(lines),
RequestLines: lines,
Assignment: assignment,
}
}
func (s *inventoryRequestService) Create(ctx context.Context, req dtodomain.InventoryRequestCreateRequest) (dtodomain.InventoryRequestResponse, error) {
tx := s.db.Begin()
defer func() {
@ -212,7 +154,7 @@ func (s *inventoryRequestService) Create(ctx context.Context, req dtodomain.Inve
if err != nil {
return dtodomain.InventoryRequestResponse{}, err
}
return toInventoryRequestResponse(result), nil
return dtodomain.ToInventoryRequestResponse(result), nil
}
func (s *inventoryRequestService) GetById(ctx context.Context, id string) (dtodomain.InventoryRequestResponse, error) {
@ -220,7 +162,7 @@ func (s *inventoryRequestService) GetById(ctx context.Context, id string) (dtodo
if err != nil {
return dtodomain.InventoryRequestResponse{}, err
}
return toInventoryRequestResponse(request), nil
return dtodomain.ToInventoryRequestResponse(request), nil
}
func (s *inventoryRequestService) GetAll(ctx context.Context, filter query.InventoryRequestFilter) ([]dtodomain.InventoryRequestResponse, int64, error) {
@ -230,7 +172,7 @@ func (s *inventoryRequestService) GetAll(ctx context.Context, filter query.Inven
}
var responses []dtodomain.InventoryRequestResponse
for _, e := range requests {
responses = append(responses, toInventoryRequestResponse(e))
responses = append(responses, dtodomain.ToInventoryRequestResponse(e))
}
if responses == nil {
responses = make([]dtodomain.InventoryRequestResponse, 0)
@ -268,7 +210,7 @@ func (s *inventoryRequestService) Update(ctx context.Context, req dtodomain.Inve
if err != nil {
return dtodomain.InventoryRequestResponse{}, err
}
return toInventoryRequestResponse(result), nil
return dtodomain.ToInventoryRequestResponse(result), nil
}
func (s *inventoryRequestService) Delete(ctx context.Context, id string) error {