feat: add inventory storage management with CRUD operations and routing
This commit is contained in:
parent
5773c34131
commit
bac881a54d
|
|
@ -19,6 +19,7 @@ import (
|
||||||
inventoryreceipt "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_receipt"
|
inventoryreceipt "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_receipt"
|
||||||
inventoryrequest "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_request"
|
inventoryrequest "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_request"
|
||||||
inventoryreturn "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_return"
|
inventoryreturn "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_return"
|
||||||
|
inventorystorage "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_storage"
|
||||||
maintenancegroup "github.com/Caknoooo/go-gin-clean-starter/modules/maintenance_group"
|
maintenancegroup "github.com/Caknoooo/go-gin-clean-starter/modules/maintenance_group"
|
||||||
"github.com/Caknoooo/go-gin-clean-starter/modules/menu"
|
"github.com/Caknoooo/go-gin-clean-starter/modules/menu"
|
||||||
"github.com/Caknoooo/go-gin-clean-starter/modules/monitoring"
|
"github.com/Caknoooo/go-gin-clean-starter/modules/monitoring"
|
||||||
|
|
@ -173,6 +174,7 @@ func main() {
|
||||||
inventoryissue.RegisterRoutes(server, injector)
|
inventoryissue.RegisterRoutes(server, injector)
|
||||||
inventoryreturn.RegisterRoutes(server, injector)
|
inventoryreturn.RegisterRoutes(server, injector)
|
||||||
inventorymovement.RegisterRoutes(server, injector)
|
inventorymovement.RegisterRoutes(server, injector)
|
||||||
|
inventorystorage.RegisterRoutes(server, injector)
|
||||||
|
|
||||||
// register swagger route
|
// register swagger route
|
||||||
server.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
server.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ type TInventoryMovementEntity struct {
|
||||||
SourceLocationID uuid.UUID `gorm:"type:uuid;index;" json:"source_location_id"`
|
SourceLocationID uuid.UUID `gorm:"type:uuid;index;" json:"source_location_id"`
|
||||||
DestinationLocationID uuid.UUID `gorm:"type:uuid;index;" json:"destination_location_id"`
|
DestinationLocationID uuid.UUID `gorm:"type:uuid;index;" json:"destination_location_id"`
|
||||||
|
|
||||||
MovementLines []TInventoryMovementLineEntity `gorm:"foreignKey:InventoryMovementID;references:ID"`
|
MovementLines []TInventoryMovementLineEntity `gorm:"foreignKey:InvMovementID;references:ID"`
|
||||||
SourceLocation MWarehouseEntity `gorm:"foreignKey:SourceLocationID;references:ID"`
|
SourceLocation MWarehouseEntity `gorm:"foreignKey:SourceLocationID;references:ID"`
|
||||||
DestinationLocation MWarehouseEntity `gorm:"foreignKey:DestinationLocationID;references:ID"`
|
DestinationLocation MWarehouseEntity `gorm:"foreignKey:DestinationLocationID;references:ID"`
|
||||||
Client M_Client `gorm:"foreignKey:ClientID;references:ID"`
|
Client M_Client `gorm:"foreignKey:ClientID;references:ID"`
|
||||||
|
|
|
||||||
|
|
@ -1 +1,126 @@
|
||||||
package controller
|
package controller
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/Caknoooo/go-gin-clean-starter/modules/inventory_storage/dto"
|
||||||
|
"github.com/Caknoooo/go-gin-clean-starter/modules/inventory_storage/query"
|
||||||
|
"github.com/Caknoooo/go-gin-clean-starter/modules/inventory_storage/service"
|
||||||
|
"github.com/Caknoooo/go-gin-clean-starter/pkg/utils"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/samber/do"
|
||||||
|
)
|
||||||
|
|
||||||
|
type InventoryStorageController interface {
|
||||||
|
Create(ctx *gin.Context)
|
||||||
|
Update(ctx *gin.Context)
|
||||||
|
Delete(ctx *gin.Context)
|
||||||
|
GetById(ctx *gin.Context)
|
||||||
|
GetAll(ctx *gin.Context)
|
||||||
|
}
|
||||||
|
|
||||||
|
type inventoryStorageController struct {
|
||||||
|
inventoryStorageService service.InventoryStorageService
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *inventoryStorageController) Create(ctx *gin.Context) {
|
||||||
|
var req dto.InventoryStorageCreateRequest
|
||||||
|
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||||
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_DATA_FROM_BODY, err.Error(), nil)
|
||||||
|
ctx.JSON(http.StatusBadRequest, res)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
created, err := c.inventoryStorageService.Create(ctx, req)
|
||||||
|
if err != nil {
|
||||||
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_CREATE_INVENTORY_STORAGE, err.Error(), nil)
|
||||||
|
ctx.JSON(http.StatusInternalServerError, res)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_CREATE_INVENTORY_STORAGE, created)
|
||||||
|
ctx.JSON(http.StatusOK, res)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *inventoryStorageController) Update(ctx *gin.Context) {
|
||||||
|
id := ctx.Param("id")
|
||||||
|
var req dto.InventoryStorageUpdateRequest
|
||||||
|
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||||
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_DATA_FROM_BODY, err.Error(), nil)
|
||||||
|
ctx.JSON(http.StatusBadRequest, res)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
updated, err := c.inventoryStorageService.Update(ctx, req, id)
|
||||||
|
if err != nil {
|
||||||
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_UPDATE_INVENTORY_STORAGE, err.Error(), nil)
|
||||||
|
ctx.JSON(http.StatusInternalServerError, res)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_UPDATE_INVENTORY_STORAGE, updated)
|
||||||
|
ctx.JSON(http.StatusOK, res)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *inventoryStorageController) Delete(ctx *gin.Context) {
|
||||||
|
id := ctx.Param("id")
|
||||||
|
if err := c.inventoryStorageService.Delete(ctx, id); err != nil {
|
||||||
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_DELETE_INVENTORY_STORAGE, err.Error(), nil)
|
||||||
|
ctx.JSON(http.StatusInternalServerError, res)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_DELETE_INVENTORY_STORAGE, nil)
|
||||||
|
ctx.JSON(http.StatusOK, res)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *inventoryStorageController) GetById(ctx *gin.Context) {
|
||||||
|
id := ctx.Param("id")
|
||||||
|
inventoryStorage, err := c.inventoryStorageService.GetById(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_INVENTORY_STORAGE, err.Error(), nil)
|
||||||
|
ctx.JSON(http.StatusNotFound, res)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_GET_INVENTORY_STORAGE, inventoryStorage)
|
||||||
|
ctx.JSON(http.StatusOK, res)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *inventoryStorageController) GetAll(ctx *gin.Context) {
|
||||||
|
clientId := ctx.MustGet("client_id").(string)
|
||||||
|
var filter query.InventoryStorageFilter
|
||||||
|
filter.ClientID = clientId
|
||||||
|
if err := ctx.ShouldBindQuery(&filter); err != nil {
|
||||||
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_INVENTORY_STORAGE, err.Error(), nil)
|
||||||
|
ctx.JSON(http.StatusBadRequest, res)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
getAll := ctx.Query("get_all")
|
||||||
|
if getAll != "" {
|
||||||
|
inventoryStorages, _, err := c.inventoryStorageService.GetAll(ctx, filter)
|
||||||
|
if err != nil {
|
||||||
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_INVENTORY_STORAGE, err.Error(), nil)
|
||||||
|
ctx.JSON(http.StatusBadRequest, res)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
response := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_GET_INVENTORY_STORAGE, inventoryStorages)
|
||||||
|
ctx.JSON(http.StatusOK, response)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
perPage := utils.ParseInt(ctx.DefaultQuery("per_page", "10"))
|
||||||
|
page := utils.ParseInt(ctx.DefaultQuery("page", "1"))
|
||||||
|
filter.PerPage = perPage
|
||||||
|
filter.Page = (page - 1) * perPage
|
||||||
|
inventoryStorages, total, err := c.inventoryStorageService.GetAll(ctx, filter)
|
||||||
|
if err != nil {
|
||||||
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_INVENTORY_STORAGE, err.Error(), nil)
|
||||||
|
ctx.JSON(http.StatusInternalServerError, res)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
paginationResponse := utils.BuildPaginationResponse(perPage, page, total)
|
||||||
|
res := utils.BuildResponseSuccessWithPagination(http.StatusOK, dto.MESSAGE_SUCCESS_GET_INVENTORY_STORAGE, inventoryStorages, paginationResponse)
|
||||||
|
ctx.JSON(http.StatusOK, res)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewInventoryStorageController(i *do.Injector, inventoryStorageService service.InventoryStorageService) InventoryStorageController {
|
||||||
|
return &inventoryStorageController{
|
||||||
|
inventoryStorageService: inventoryStorageService,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1 +1,80 @@
|
||||||
package dto
|
package dto
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"github.com/Caknoooo/go-gin-clean-starter/database/entities"
|
||||||
|
pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
MESSAGE_FAILED_CREATE_INVENTORY_STORAGE = "failed create inventory storage"
|
||||||
|
MESSAGE_SUCCESS_CREATE_INVENTORY_STORAGE = "success create inventory storage"
|
||||||
|
MESSAGE_FAILED_GET_INVENTORY_STORAGE = "failed get inventory storage"
|
||||||
|
MESSAGE_SUCCESS_GET_INVENTORY_STORAGE = "success get inventory storage"
|
||||||
|
MESSAGE_FAILED_UPDATE_INVENTORY_STORAGE = "failed update inventory storage"
|
||||||
|
MESSAGE_SUCCESS_UPDATE_INVENTORY_STORAGE = "success update inventory storage"
|
||||||
|
MESSAGE_FAILED_DELETE_INVENTORY_STORAGE = "failed delete inventory storage"
|
||||||
|
MESSAGE_SUCCESS_DELETE_INVENTORY_STORAGE = "success delete inventory storage"
|
||||||
|
MESSAGE_FAILED_GET_DATA_FROM_BODY = "failed get data from body"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrCreateInventoryStorage = errors.New("failed to create inventory storage")
|
||||||
|
ErrGetInventoryStorageById = errors.New("failed to get inventory storage by id")
|
||||||
|
ErrUpdateInventoryStorage = errors.New("failed to update inventory storage")
|
||||||
|
ErrDeleteInventoryStorage = errors.New("failed to delete inventory storage")
|
||||||
|
)
|
||||||
|
|
||||||
|
type InventoryStorageCreateRequest struct {
|
||||||
|
ProductID string `json:"product_id" binding:"required"`
|
||||||
|
AisleID string `json:"aisle_id" binding:"required"`
|
||||||
|
UomID string `json:"uom_id" binding:"required"`
|
||||||
|
ClientID string `json:"client_id" binding:"required"`
|
||||||
|
OnHandQuantity float64 `json:"on_hand_quantity"`
|
||||||
|
AvailableQuantity float64 `json:"available_quantity"`
|
||||||
|
InvRequestID string `json:"inv_request_id"`
|
||||||
|
InvReceiptID string `json:"inv_receipt_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type InventoryStorageUpdateRequest struct {
|
||||||
|
OnHandQuantity float64 `json:"on_hand_quantity"`
|
||||||
|
AvailableQuantity float64 `json:"available_quantity"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type InventoryStorageResponse struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
OnHandQuantity float64 `json:"on_hand_quantity"`
|
||||||
|
AvailableQuantity float64 `json:"available_quantity"`
|
||||||
|
Product InvStorageProductResponse `json:"product"`
|
||||||
|
AisleID pkgdto.IdNameResponse `json:"aisle"`
|
||||||
|
Uom pkgdto.IdNameResponse `json:"uom"`
|
||||||
|
InvReceipt InvStorageInvResponse `json:"inv_receipt"`
|
||||||
|
InvRequest InvStorageInvResponse `json:"inv_request"`
|
||||||
|
Client pkgdto.IdNameResponse `json:"client"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type InvStorageProductResponse struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
RefNumber string `json:"ref_number"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type InvStorageInvResponse struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
DocumentNumber string `json:"document_number"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToInventoryStorageResponse(e entities.InventoryStorageEntity) InventoryStorageResponse {
|
||||||
|
return InventoryStorageResponse{
|
||||||
|
ID: e.ID.String(),
|
||||||
|
OnHandQuantity: e.OnHandQuantity,
|
||||||
|
AvailableQuantity: e.AvailableQuantity,
|
||||||
|
Product: InvStorageProductResponse{ID: e.ProductID.String(), Name: e.Product.Name, RefNumber: e.Product.RefNumber},
|
||||||
|
AisleID: pkgdto.IdNameResponse{ID: e.AisleID.String(), Name: e.Aisle.Name},
|
||||||
|
Uom: pkgdto.IdNameResponse{ID: e.Uom.ID.String(), Name: e.Uom.Name},
|
||||||
|
InvReceipt: InvStorageInvResponse{ID: e.InvReceiptID.String(), DocumentNumber: e.InvReceipt.DocumentNumber},
|
||||||
|
InvRequest: InvStorageInvResponse{ID: e.InvRequestID.String(), DocumentNumber: e.InvRequest.DocumentNumber},
|
||||||
|
Client: pkgdto.IdNameResponse{ID: e.Client.ID.String(), Name: e.Client.Name},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1 +1,26 @@
|
||||||
package query
|
package query
|
||||||
|
|
||||||
|
import (
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type InventoryStorageFilter struct {
|
||||||
|
ProductID string `form:"product_id"`
|
||||||
|
AisleID string `form:"aisle_id"`
|
||||||
|
ClientID string `form:"client_id"`
|
||||||
|
PerPage int `form:"per_page"`
|
||||||
|
Page int `form:"page"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func ApplyInventoryStorageFilters(db *gorm.DB, filter InventoryStorageFilter) *gorm.DB {
|
||||||
|
if filter.ProductID != "" {
|
||||||
|
db = db.Where("product_id = ?", filter.ProductID)
|
||||||
|
}
|
||||||
|
if filter.AisleID != "" {
|
||||||
|
db = db.Where("aisle_id = ?", filter.AisleID)
|
||||||
|
}
|
||||||
|
if filter.ClientID != "" {
|
||||||
|
db = db.Where("client_id = ?", filter.ClientID)
|
||||||
|
}
|
||||||
|
return db
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1 +1,90 @@
|
||||||
package repository
|
package repository
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/Caknoooo/go-gin-clean-starter/database/entities"
|
||||||
|
"github.com/Caknoooo/go-gin-clean-starter/modules/inventory_storage/query"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type InventoryStorageRepository interface {
|
||||||
|
Create(ctx context.Context, tx *gorm.DB, inventoryStorage entities.InventoryStorageEntity) (entities.InventoryStorageEntity, error)
|
||||||
|
GetById(ctx context.Context, tx *gorm.DB, inventoryStorageId string) (entities.InventoryStorageEntity, error)
|
||||||
|
GetAll(ctx context.Context, filter query.InventoryStorageFilter) ([]entities.InventoryStorageEntity, int64, error)
|
||||||
|
Update(ctx context.Context, tx *gorm.DB, inventoryStorage entities.InventoryStorageEntity) (entities.InventoryStorageEntity, error)
|
||||||
|
Delete(ctx context.Context, tx *gorm.DB, inventoryStorageId string) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type inventoryStorageRepository struct {
|
||||||
|
db *gorm.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewInventoryStorageRepository(db *gorm.DB) InventoryStorageRepository {
|
||||||
|
return &inventoryStorageRepository{db: db}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *inventoryStorageRepository) Create(ctx context.Context, tx *gorm.DB, inventoryStorage entities.InventoryStorageEntity) (entities.InventoryStorageEntity, error) {
|
||||||
|
if tx == nil {
|
||||||
|
tx = r.db
|
||||||
|
}
|
||||||
|
if err := tx.WithContext(ctx).Create(&inventoryStorage).Error; err != nil {
|
||||||
|
return inventoryStorage, err
|
||||||
|
}
|
||||||
|
return inventoryStorage, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *inventoryStorageRepository) GetById(ctx context.Context, tx *gorm.DB, inventoryStorageId string) (entities.InventoryStorageEntity, error) {
|
||||||
|
if tx == nil {
|
||||||
|
tx = r.db
|
||||||
|
}
|
||||||
|
var inventoryStorage entities.InventoryStorageEntity
|
||||||
|
if err := tx.WithContext(ctx).
|
||||||
|
Preload("Product").
|
||||||
|
Preload("Aisle").
|
||||||
|
Preload("Uom").
|
||||||
|
Preload("Client").
|
||||||
|
Preload("InvRequest").
|
||||||
|
Preload("InvReceipt").
|
||||||
|
First(&inventoryStorage, "id = ?", inventoryStorageId).Error; err != nil {
|
||||||
|
return inventoryStorage, err
|
||||||
|
}
|
||||||
|
return inventoryStorage, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *inventoryStorageRepository) GetAll(ctx context.Context, filter query.InventoryStorageFilter) ([]entities.InventoryStorageEntity, int64, error) {
|
||||||
|
var inventoryStorages []entities.InventoryStorageEntity
|
||||||
|
var total int64
|
||||||
|
db := query.ApplyInventoryStorageFilters(r.db, filter)
|
||||||
|
db.Model(&entities.InventoryStorageEntity{}).Count(&total)
|
||||||
|
if filter.PerPage > 0 && filter.Page > 0 {
|
||||||
|
db = db.Offset((filter.Page - 1) * filter.PerPage).Limit(filter.PerPage)
|
||||||
|
}
|
||||||
|
if err := db.Preload("Product").Preload("Aisle").Preload("Uom").Preload("Client").Preload("InvRequest").Preload("InvReceipt").Find(&inventoryStorages).Error; err != nil {
|
||||||
|
return inventoryStorages, total, err
|
||||||
|
}
|
||||||
|
return inventoryStorages, total, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *inventoryStorageRepository) Update(ctx context.Context, tx *gorm.DB, inventoryStorage entities.InventoryStorageEntity) (entities.InventoryStorageEntity, error) {
|
||||||
|
if tx == nil {
|
||||||
|
tx = r.db
|
||||||
|
}
|
||||||
|
if err := tx.WithContext(ctx).Model(&entities.InventoryStorageEntity{}).
|
||||||
|
Where("id = ?", inventoryStorage.ID).
|
||||||
|
Select("*").
|
||||||
|
Updates(&inventoryStorage).Error; err != nil {
|
||||||
|
return inventoryStorage, err
|
||||||
|
}
|
||||||
|
return inventoryStorage, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *inventoryStorageRepository) Delete(ctx context.Context, tx *gorm.DB, inventoryStorageId string) error {
|
||||||
|
if tx == nil {
|
||||||
|
tx = r.db
|
||||||
|
}
|
||||||
|
if err := tx.WithContext(ctx).Delete(&entities.InventoryStorageEntity{}, "id = ?", inventoryStorageId).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1 +1,24 @@
|
||||||
package inventory_storage
|
package inventorystorage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/Caknoooo/go-gin-clean-starter/middlewares"
|
||||||
|
"github.com/Caknoooo/go-gin-clean-starter/modules/auth/service"
|
||||||
|
"github.com/Caknoooo/go-gin-clean-starter/modules/inventory_storage/controller"
|
||||||
|
"github.com/Caknoooo/go-gin-clean-starter/pkg/constants"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/samber/do"
|
||||||
|
)
|
||||||
|
|
||||||
|
func RegisterRoutes(server *gin.Engine, injector *do.Injector) {
|
||||||
|
inventoryStorageController := do.MustInvoke[controller.InventoryStorageController](injector)
|
||||||
|
jwtService := do.MustInvokeNamed[service.JWTService](injector, constants.JWTService)
|
||||||
|
|
||||||
|
inventoryStorageRoutes := server.Group("/api/v1/inventory-storages")
|
||||||
|
{
|
||||||
|
inventoryStorageRoutes.POST("", middlewares.Authenticate(jwtService), inventoryStorageController.Create)
|
||||||
|
inventoryStorageRoutes.GET("/:id", middlewares.Authenticate(jwtService), inventoryStorageController.GetById)
|
||||||
|
inventoryStorageRoutes.PUT("/:id", middlewares.Authenticate(jwtService), inventoryStorageController.Update)
|
||||||
|
inventoryStorageRoutes.DELETE("/:id", middlewares.Authenticate(jwtService), inventoryStorageController.Delete)
|
||||||
|
inventoryStorageRoutes.GET("", middlewares.Authenticate(jwtService), inventoryStorageController.GetAll)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1 +1,157 @@
|
||||||
package service
|
package service
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/Caknoooo/go-gin-clean-starter/database/entities"
|
||||||
|
dtodomain "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_storage/dto"
|
||||||
|
"github.com/Caknoooo/go-gin-clean-starter/modules/inventory_storage/query"
|
||||||
|
"github.com/Caknoooo/go-gin-clean-starter/modules/inventory_storage/repository"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type InventoryStorageService interface {
|
||||||
|
Create(ctx context.Context, req dtodomain.InventoryStorageCreateRequest) (dtodomain.InventoryStorageResponse, error)
|
||||||
|
GetById(ctx context.Context, inventoryStorageId string) (dtodomain.InventoryStorageResponse, error)
|
||||||
|
GetAll(ctx context.Context, filter query.InventoryStorageFilter) ([]dtodomain.InventoryStorageResponse, int64, error)
|
||||||
|
Update(ctx context.Context, req dtodomain.InventoryStorageUpdateRequest, inventoryStorageId string) (dtodomain.InventoryStorageResponse, error)
|
||||||
|
Delete(ctx context.Context, inventoryStorageId string) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type inventoryStorageService struct {
|
||||||
|
db *gorm.DB
|
||||||
|
inventoryStorageRepo repository.InventoryStorageRepository
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *inventoryStorageService) Create(ctx context.Context, req dtodomain.InventoryStorageCreateRequest) (dtodomain.InventoryStorageResponse, error) {
|
||||||
|
tx := s.db.Begin()
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
productID, err := uuid.Parse(req.ProductID)
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return dtodomain.InventoryStorageResponse{}, err
|
||||||
|
}
|
||||||
|
aisleID, err := uuid.Parse(req.AisleID)
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return dtodomain.InventoryStorageResponse{}, err
|
||||||
|
}
|
||||||
|
uomID, err := uuid.Parse(req.UomID)
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return dtodomain.InventoryStorageResponse{}, err
|
||||||
|
}
|
||||||
|
clientID, err := uuid.Parse(req.ClientID)
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return dtodomain.InventoryStorageResponse{}, err
|
||||||
|
}
|
||||||
|
InvRequestID, err := uuid.Parse(req.InvRequestID)
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return dtodomain.InventoryStorageResponse{}, err
|
||||||
|
}
|
||||||
|
InvReceiptID, err := uuid.Parse(req.InvReceiptID)
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return dtodomain.InventoryStorageResponse{}, err
|
||||||
|
}
|
||||||
|
inventoryStorage := entities.InventoryStorageEntity{
|
||||||
|
ProductID: productID,
|
||||||
|
AisleID: aisleID,
|
||||||
|
UomID: uomID,
|
||||||
|
ClientID: clientID,
|
||||||
|
InvRequestID: InvRequestID,
|
||||||
|
InvReceiptID: InvReceiptID,
|
||||||
|
OnHandQuantity: req.OnHandQuantity,
|
||||||
|
AvailableQuantity: req.AvailableQuantity,
|
||||||
|
}
|
||||||
|
created, err := s.inventoryStorageRepo.Create(ctx, tx, inventoryStorage)
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return dtodomain.InventoryStorageResponse{}, err
|
||||||
|
}
|
||||||
|
tx.Commit()
|
||||||
|
result, err := s.inventoryStorageRepo.GetById(ctx, nil, created.ID.String())
|
||||||
|
if err != nil {
|
||||||
|
return dtodomain.InventoryStorageResponse{}, err
|
||||||
|
}
|
||||||
|
return dtodomain.ToInventoryStorageResponse(result), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *inventoryStorageService) GetById(ctx context.Context, inventoryStorageId string) (dtodomain.InventoryStorageResponse, error) {
|
||||||
|
inventoryStorage, err := s.inventoryStorageRepo.GetById(ctx, nil, inventoryStorageId)
|
||||||
|
if err != nil {
|
||||||
|
return dtodomain.InventoryStorageResponse{}, err
|
||||||
|
}
|
||||||
|
return dtodomain.ToInventoryStorageResponse(inventoryStorage), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *inventoryStorageService) GetAll(ctx context.Context, filter query.InventoryStorageFilter) ([]dtodomain.InventoryStorageResponse, int64, error) {
|
||||||
|
inventoryStorages, total, err := s.inventoryStorageRepo.GetAll(ctx, filter)
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
var responses []dtodomain.InventoryStorageResponse
|
||||||
|
for _, e := range inventoryStorages {
|
||||||
|
responses = append(responses, dtodomain.ToInventoryStorageResponse(e))
|
||||||
|
}
|
||||||
|
if responses == nil {
|
||||||
|
responses = make([]dtodomain.InventoryStorageResponse, 0)
|
||||||
|
}
|
||||||
|
return responses, total, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *inventoryStorageService) Update(ctx context.Context, req dtodomain.InventoryStorageUpdateRequest, inventoryStorageId string) (dtodomain.InventoryStorageResponse, error) {
|
||||||
|
tx := s.db.Begin()
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
inventoryStorage, err := s.inventoryStorageRepo.GetById(ctx, tx, inventoryStorageId)
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return dtodomain.InventoryStorageResponse{}, err
|
||||||
|
}
|
||||||
|
inventoryStorage.OnHandQuantity = req.OnHandQuantity
|
||||||
|
inventoryStorage.AvailableQuantity = req.AvailableQuantity
|
||||||
|
updated, err := s.inventoryStorageRepo.Update(ctx, tx, inventoryStorage)
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return dtodomain.InventoryStorageResponse{}, err
|
||||||
|
}
|
||||||
|
tx.Commit()
|
||||||
|
result, err := s.inventoryStorageRepo.GetById(ctx, nil, updated.ID.String())
|
||||||
|
if err != nil {
|
||||||
|
return dtodomain.InventoryStorageResponse{}, err
|
||||||
|
}
|
||||||
|
return dtodomain.ToInventoryStorageResponse(result), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *inventoryStorageService) Delete(ctx context.Context, inventoryStorageId string) error {
|
||||||
|
tx := s.db.Begin()
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
if err := s.inventoryStorageRepo.Delete(ctx, tx, inventoryStorageId); err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
tx.Commit()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewInventoryStorageService(db *gorm.DB, inventoryStorageRepo repository.InventoryStorageRepository) InventoryStorageService {
|
||||||
|
return &inventoryStorageService{
|
||||||
|
db: db,
|
||||||
|
inventoryStorageRepo: inventoryStorageRepo,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -88,6 +88,10 @@ import (
|
||||||
inventoryMovementRepo "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_movement/repository"
|
inventoryMovementRepo "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_movement/repository"
|
||||||
inventoryMovementService "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_movement/service"
|
inventoryMovementService "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_movement/service"
|
||||||
|
|
||||||
|
inventoryStorageController "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_storage/controller"
|
||||||
|
inventoryStorageRepo "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_storage/repository"
|
||||||
|
inventoryStorageService "github.com/Caknoooo/go-gin-clean-starter/modules/inventory_storage/service"
|
||||||
|
|
||||||
"github.com/Caknoooo/go-gin-clean-starter/modules/user/controller"
|
"github.com/Caknoooo/go-gin-clean-starter/modules/user/controller"
|
||||||
"github.com/Caknoooo/go-gin-clean-starter/modules/user/repository"
|
"github.com/Caknoooo/go-gin-clean-starter/modules/user/repository"
|
||||||
userService "github.com/Caknoooo/go-gin-clean-starter/modules/user/service"
|
userService "github.com/Caknoooo/go-gin-clean-starter/modules/user/service"
|
||||||
|
|
@ -149,6 +153,7 @@ func RegisterDependencies(injector *do.Injector) {
|
||||||
inventoryReturnLineRepository := inventoryReturnLineRepo.NewInventoryReturnLineRepository(db)
|
inventoryReturnLineRepository := inventoryReturnLineRepo.NewInventoryReturnLineRepository(db)
|
||||||
inventoryMovementRepository := inventoryMovementRepo.NewInventoryMovementRepository(db)
|
inventoryMovementRepository := inventoryMovementRepo.NewInventoryMovementRepository(db)
|
||||||
inventoryMovementLineRepository := inventoryMovementLineRepo.NewInventoryMovementLineRepository(db)
|
inventoryMovementLineRepository := inventoryMovementLineRepo.NewInventoryMovementLineRepository(db)
|
||||||
|
inventoryStorageRepository := inventoryStorageRepo.NewInventoryStorageRepository(db)
|
||||||
|
|
||||||
// Service
|
// Service
|
||||||
userServ := userService.NewUserService(userRepository, roleRepository, warehouseRepository, refreshTokenRepository, jwtService, db)
|
userServ := userService.NewUserService(userRepository, roleRepository, warehouseRepository, refreshTokenRepository, jwtService, db)
|
||||||
|
|
@ -170,6 +175,7 @@ func RegisterDependencies(injector *do.Injector) {
|
||||||
inventoryIssueServ := inventoryIssueService.NewInventoryIssueService(db, inventoryIssueRepository, inventoryIssueLineRepository)
|
inventoryIssueServ := inventoryIssueService.NewInventoryIssueService(db, inventoryIssueRepository, inventoryIssueLineRepository)
|
||||||
inventoryReturnServ := inventoryReturnService.NewInventoryReturnService(db, inventoryReturnRepository, inventoryReturnLineRepository, inventoryIssueLineRepository, productRepository)
|
inventoryReturnServ := inventoryReturnService.NewInventoryReturnService(db, inventoryReturnRepository, inventoryReturnLineRepository, inventoryIssueLineRepository, productRepository)
|
||||||
inventoryMovementServ := inventoryMovementService.NewInventoryMovementService(db, inventoryMovementRepository, inventoryMovementLineRepository)
|
inventoryMovementServ := inventoryMovementService.NewInventoryMovementService(db, inventoryMovementRepository, inventoryMovementLineRepository)
|
||||||
|
inventoryStorageService := inventoryStorageService.NewInventoryStorageService(db, inventoryStorageRepository)
|
||||||
|
|
||||||
// Controller
|
// Controller
|
||||||
do.Provide(
|
do.Provide(
|
||||||
|
|
@ -277,4 +283,9 @@ func RegisterDependencies(injector *do.Injector) {
|
||||||
return inventoryMovementController.NewInventoryMovementController(i, inventoryMovementServ), nil
|
return inventoryMovementController.NewInventoryMovementController(i, inventoryMovementServ), nil
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
do.Provide(
|
||||||
|
injector, func(i *do.Injector) (inventoryStorageController.InventoryStorageController, error) {
|
||||||
|
return inventoryStorageController.NewInventoryStorageController(i, inventoryStorageService), nil
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue