mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
Merge branch 'feat/BE/US-75/chick-in-doc' into 'development-before-sso'
fix(BE): add missing product json in transfer get all & support flag param... See merge request mbugroup/lti-api!25
This commit is contained in:
@@ -0,0 +1,161 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"math"
|
||||
"strconv"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/modules/production/chickins/dto"
|
||||
service "gitlab.com/mbugroup/lti-api.git/internal/modules/production/chickins/services"
|
||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/production/chickins/validations"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/response"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type ChickinController struct {
|
||||
ChickinService service.ChickinService
|
||||
}
|
||||
|
||||
func NewChickinController(chickinService service.ChickinService) *ChickinController {
|
||||
return &ChickinController{
|
||||
ChickinService: chickinService,
|
||||
}
|
||||
}
|
||||
|
||||
func (u *ChickinController) GetAll(c *fiber.Ctx) error {
|
||||
query := &validation.Query{
|
||||
Page: c.QueryInt("page", 1),
|
||||
Limit: c.QueryInt("limit", 10),
|
||||
ProjectFlockKandangId: uint(c.QueryInt("project_flock_kandang_id", 0)),
|
||||
}
|
||||
|
||||
result, totalResults, err := u.ChickinService.GetAll(c, query)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.SuccessWithPaginate[dto.ChickinListDTO]{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Get all chickins successfully",
|
||||
Meta: response.Meta{
|
||||
Page: query.Page,
|
||||
Limit: query.Limit,
|
||||
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
|
||||
TotalResults: totalResults,
|
||||
},
|
||||
Data: dto.ToChickinListDTOs(result),
|
||||
})
|
||||
}
|
||||
|
||||
func (u *ChickinController) GetOne(c *fiber.Ctx) error {
|
||||
param := c.Params("id")
|
||||
|
||||
id, err := strconv.Atoi(param)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||
}
|
||||
|
||||
result, err := u.ChickinService.GetOne(c, uint(id))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.Success{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Get chickin successfully",
|
||||
Data: dto.ToChickinListDTO(*result),
|
||||
})
|
||||
}
|
||||
|
||||
func (u *ChickinController) CreateOne(c *fiber.Ctx) error {
|
||||
req := new(validation.Create)
|
||||
|
||||
if err := c.BodyParser(req); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||
}
|
||||
|
||||
result, err := u.ChickinService.CreateOne(c, req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusCreated).
|
||||
JSON(response.Success{
|
||||
Code: fiber.StatusCreated,
|
||||
Status: "success",
|
||||
Message: "Create chickin successfully",
|
||||
Data: dto.ToChickinListDTO(*result),
|
||||
})
|
||||
}
|
||||
|
||||
func (u *ChickinController) UpdateOne(c *fiber.Ctx) error {
|
||||
req := new(validation.Update)
|
||||
param := c.Params("id")
|
||||
|
||||
id, err := strconv.Atoi(param)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||
}
|
||||
|
||||
if err := c.BodyParser(req); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||
}
|
||||
|
||||
result, err := u.ChickinService.UpdateOne(c, req, uint(id))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.Success{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Update chickin successfully",
|
||||
Data: dto.ToChickinListDTO(*result),
|
||||
})
|
||||
}
|
||||
|
||||
func (u *ChickinController) DeleteOne(c *fiber.Ctx) error {
|
||||
param := c.Params("id")
|
||||
|
||||
id, err := strconv.Atoi(param)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||
}
|
||||
|
||||
if err := u.ChickinService.DeleteOne(c, uint(id)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.Common{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Delete chickin successfully",
|
||||
})
|
||||
}
|
||||
|
||||
func (u *ChickinController) Approve(c *fiber.Ctx) error {
|
||||
param := c.Params("id")
|
||||
|
||||
id, err := strconv.Atoi(param)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||
}
|
||||
|
||||
if err := u.ChickinService.Approve(c, uint(id)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.Success{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Approve chickin successfully",
|
||||
Data: nil,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,216 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
areaBaseDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/areas/dto"
|
||||
fcrBaseDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/fcrs/dto"
|
||||
flockBaseDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/flocks/dto"
|
||||
kandangBaseDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/dto"
|
||||
locationBaseDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/locations/dto"
|
||||
productCategoryBaseDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/product-categories/dto"
|
||||
userBaseDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
|
||||
)
|
||||
|
||||
// === DTO Structs (ordered) ===
|
||||
|
||||
type ChickinBaseDTO struct {
|
||||
Id uint `json:"id"`
|
||||
ProjectFlockKandang *ProjectFlockKandangDTO `json:"project_flock_kandang"`
|
||||
ChickInDate time.Time `json:"chick_in_date"`
|
||||
Quantity float64 `json:"quantity"`
|
||||
Note string `json:"note"`
|
||||
}
|
||||
|
||||
type ProjectFlockDTO struct {
|
||||
Id uint `json:"id"`
|
||||
Period int `json:"period"`
|
||||
Flock *flockBaseDTO.FlockBaseDTO `json:"flock"`
|
||||
ProductCategory *productCategoryBaseDTO.ProductCategoryBaseDTO `json:"product_category"`
|
||||
Area *areaBaseDTO.AreaBaseDTO `json:"area"`
|
||||
Fcr *fcrBaseDTO.FcrBaseDTO `json:"fcr"`
|
||||
Location *locationBaseDTO.LocationBaseDTO `json:"location"`
|
||||
}
|
||||
|
||||
type ProjectFlockKandangDTO struct {
|
||||
Id uint `json:"id"`
|
||||
ProjectFlock *ProjectFlockDTO `json:"project_flock"`
|
||||
Kandang *kandangBaseDTO.KandangBaseDTO `json:"kandang"`
|
||||
}
|
||||
|
||||
// gunakan base DTO dari package users
|
||||
|
||||
type ChickinSimpleDTO struct {
|
||||
Id uint `json:"id"`
|
||||
ProjectFlockKandangId uint `json:"project_flock_kandang_id"`
|
||||
ChickInDate time.Time `json:"chick_in_date"`
|
||||
Quantity float64 `json:"quantity"`
|
||||
Note string `json:"note"`
|
||||
CreatedBy uint `json:"created_by"`
|
||||
}
|
||||
|
||||
type ChickinListDTO struct {
|
||||
ChickinBaseDTO
|
||||
ProjectFlockKandang *ProjectFlockKandangDTO `json:"project_flock_kandang"`
|
||||
CreatedUser *userBaseDTO.UserBaseDTO `json:"created_user"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
type ChickinDetailDTO struct {
|
||||
ChickinListDTO
|
||||
}
|
||||
|
||||
// === Mapper Functions (ordered) ===
|
||||
|
||||
func ToFlockDTO(e entity.Flock) flockBaseDTO.FlockBaseDTO {
|
||||
return flockBaseDTO.ToFlockBaseDTO(e)
|
||||
}
|
||||
|
||||
func ToKandangDTO(e entity.Kandang) kandangBaseDTO.KandangBaseDTO {
|
||||
return kandangBaseDTO.ToKandangBaseDTO(e)
|
||||
}
|
||||
|
||||
func ToProductCategoryDTO(e entity.ProductCategory) productCategoryBaseDTO.ProductCategoryBaseDTO {
|
||||
return productCategoryBaseDTO.ToProductCategoryBaseDTO(e)
|
||||
}
|
||||
|
||||
func ToAreaDTO(e entity.Area) areaBaseDTO.AreaBaseDTO {
|
||||
return areaBaseDTO.ToAreaBaseDTO(e)
|
||||
}
|
||||
|
||||
func ToFcrDTO(e entity.Fcr) fcrBaseDTO.FcrBaseDTO {
|
||||
return fcrBaseDTO.ToFcrBaseDTO(e)
|
||||
}
|
||||
|
||||
func ToLocationDTO(e entity.Location) locationBaseDTO.LocationBaseDTO {
|
||||
return locationBaseDTO.ToLocationBaseDTO(e)
|
||||
}
|
||||
|
||||
func ToUserBaseDTO(e entity.User) userBaseDTO.UserBaseDTO {
|
||||
return userBaseDTO.ToUserBaseDTO(e)
|
||||
}
|
||||
|
||||
func ToProjectFlockDTO(e entity.ProjectFlock) ProjectFlockDTO {
|
||||
var flock *flockBaseDTO.FlockBaseDTO
|
||||
if e.Flock.Id != 0 {
|
||||
mapped := flockBaseDTO.ToFlockBaseDTO(e.Flock)
|
||||
flock = &mapped
|
||||
}
|
||||
var productCategory *productCategoryBaseDTO.ProductCategoryBaseDTO
|
||||
if e.ProductCategory.Id != 0 {
|
||||
mapped := productCategoryBaseDTO.ToProductCategoryBaseDTO(e.ProductCategory)
|
||||
productCategory = &mapped
|
||||
}
|
||||
var area *areaBaseDTO.AreaBaseDTO
|
||||
if e.Area.Id != 0 {
|
||||
mapped := areaBaseDTO.ToAreaBaseDTO(e.Area)
|
||||
area = &mapped
|
||||
}
|
||||
var fcr *fcrBaseDTO.FcrBaseDTO
|
||||
if e.Fcr.Id != 0 {
|
||||
mapped := fcrBaseDTO.ToFcrBaseDTO(e.Fcr)
|
||||
fcr = &mapped
|
||||
}
|
||||
var location *locationBaseDTO.LocationBaseDTO
|
||||
if e.Location.Id != 0 {
|
||||
mapped := locationBaseDTO.ToLocationBaseDTO(e.Location)
|
||||
location = &mapped
|
||||
}
|
||||
return ProjectFlockDTO{
|
||||
Id: e.Id,
|
||||
Period: e.Period,
|
||||
Flock: flock,
|
||||
ProductCategory: productCategory,
|
||||
Area: area,
|
||||
Fcr: fcr,
|
||||
Location: location,
|
||||
}
|
||||
}
|
||||
|
||||
func ToProjectFlockKandangDTO(e entity.ProjectFlockKandang) ProjectFlockKandangDTO {
|
||||
var pf *ProjectFlockDTO
|
||||
if e.ProjectFlock.Id != 0 {
|
||||
mapped := ToProjectFlockDTO(e.ProjectFlock)
|
||||
pf = &mapped
|
||||
}
|
||||
var kandang *kandangBaseDTO.KandangBaseDTO
|
||||
if e.Kandang.Id != 0 {
|
||||
mapped := kandangBaseDTO.ToKandangBaseDTO(e.Kandang)
|
||||
kandang = &mapped
|
||||
}
|
||||
return ProjectFlockKandangDTO{
|
||||
Id: e.Id,
|
||||
ProjectFlock: pf,
|
||||
Kandang: kandang,
|
||||
}
|
||||
}
|
||||
|
||||
func ToChickinBaseDTO(e entity.ProjectChickin) ChickinBaseDTO {
|
||||
var pfk *ProjectFlockKandangDTO
|
||||
if e.ProjectFlockKandang.Id != 0 {
|
||||
mapped := ToProjectFlockKandangDTO(e.ProjectFlockKandang)
|
||||
pfk = &mapped
|
||||
}
|
||||
return ChickinBaseDTO{
|
||||
Id: e.Id,
|
||||
ProjectFlockKandang: pfk,
|
||||
ChickInDate: e.ChickInDate,
|
||||
Quantity: e.Quantity,
|
||||
Note: e.Note,
|
||||
}
|
||||
}
|
||||
|
||||
func ToChickinSimpleDTO(e entity.ProjectChickin) ChickinSimpleDTO {
|
||||
return ChickinSimpleDTO{
|
||||
Id: e.Id,
|
||||
ProjectFlockKandangId: e.ProjectFlockKandangId,
|
||||
ChickInDate: e.ChickInDate,
|
||||
Quantity: e.Quantity,
|
||||
Note: e.Note,
|
||||
CreatedBy: e.CreatedBy,
|
||||
}
|
||||
}
|
||||
|
||||
func ToChickinListDTO(e entity.ProjectChickin) ChickinListDTO {
|
||||
var createdUser *userBaseDTO.UserBaseDTO
|
||||
if e.CreatedUser.Id != 0 {
|
||||
mapped := userBaseDTO.ToUserBaseDTO(e.CreatedUser)
|
||||
createdUser = &mapped
|
||||
}
|
||||
var pfk *ProjectFlockKandangDTO
|
||||
if e.ProjectFlockKandang.Id != 0 {
|
||||
mapped := ToProjectFlockKandangDTO(e.ProjectFlockKandang)
|
||||
pfk = &mapped
|
||||
}
|
||||
return ChickinListDTO{
|
||||
ChickinBaseDTO: ToChickinBaseDTO(e),
|
||||
ProjectFlockKandang: pfk,
|
||||
CreatedUser: createdUser,
|
||||
CreatedAt: e.CreatedAt,
|
||||
UpdatedAt: e.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func ToChickinListDTOs(e []entity.ProjectChickin) []ChickinListDTO {
|
||||
result := make([]ChickinListDTO, len(e))
|
||||
for i, r := range e {
|
||||
result[i] = ToChickinListDTO(r)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func ToChickinSimpleDTOs(e []entity.ProjectChickin) []ChickinSimpleDTO {
|
||||
result := make([]ChickinSimpleDTO, len(e))
|
||||
for i, r := range e {
|
||||
result[i] = ToChickinSimpleDTO(r)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func ToChickinDetailDTO(e entity.ProjectChickin) ChickinDetailDTO {
|
||||
return ChickinDetailDTO{
|
||||
ChickinListDTO: ToChickinListDTO(e),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package chickins
|
||||
|
||||
import (
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"gorm.io/gorm"
|
||||
|
||||
rProductWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-warehouses/repositories"
|
||||
rKandang "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/repositories"
|
||||
rWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/repositories"
|
||||
rChickin "gitlab.com/mbugroup/lti-api.git/internal/modules/production/chickins/repositories"
|
||||
sChickin "gitlab.com/mbugroup/lti-api.git/internal/modules/production/chickins/services"
|
||||
rAuditLog "gitlab.com/mbugroup/lti-api.git/internal/modules/shared/repositories"
|
||||
|
||||
rProjectFlock "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/repositories"
|
||||
|
||||
rUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/repositories"
|
||||
sUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
|
||||
)
|
||||
|
||||
type ChickinModule struct{}
|
||||
|
||||
func (ChickinModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
|
||||
chickinRepo := rChickin.NewChickinRepository(db)
|
||||
kandangRepo := rKandang.NewKandangRepository(db)
|
||||
auditlogrepo := rAuditLog.NewAuditLogRepository(db)
|
||||
warehouseRepo := rWarehouse.NewWarehouseRepository(db)
|
||||
projectflockkandangrepo := rProjectFlock.NewProjectFlockKandangRepository(db)
|
||||
projectflockpopulationrepo := rProjectFlock.NewProjectFlockPopulationRepository(db)
|
||||
projectFlockRepo := rProjectFlock.NewProjectflockRepository(db)
|
||||
productWarehouseRepo := rProductWarehouse.NewProductWarehouseRepository(db)
|
||||
|
||||
userRepo := rUser.NewUserRepository(db)
|
||||
|
||||
chickinService := sChickin.NewChickinService(chickinRepo, kandangRepo, warehouseRepo, productWarehouseRepo, projectFlockRepo, auditlogrepo, projectflockkandangrepo, projectflockpopulationrepo, validate)
|
||||
userService := sUser.NewUserService(userRepo, validate)
|
||||
|
||||
ChickinRoutes(router, userService, chickinService)
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ProjectChickinRepository interface {
|
||||
repository.BaseRepository[entity.ProjectChickin]
|
||||
GetFirstByProjectFlockID(ctx context.Context, projectFlockID uint) (*entity.ProjectChickin, error)
|
||||
}
|
||||
|
||||
type ChickinRepositoryImpl struct {
|
||||
*repository.BaseRepositoryImpl[entity.ProjectChickin]
|
||||
}
|
||||
|
||||
func NewChickinRepository(db *gorm.DB) ProjectChickinRepository {
|
||||
return &ChickinRepositoryImpl{
|
||||
BaseRepositoryImpl: repository.NewBaseRepository[entity.ProjectChickin](db),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *ChickinRepositoryImpl) GetFirstByProjectFlockID(ctx context.Context, projectFlockID uint) (*entity.ProjectChickin, error) {
|
||||
var chickin entity.ProjectChickin
|
||||
err := r.DB().WithContext(ctx).
|
||||
Where("project_floc_id = ?", projectFlockID).
|
||||
Where("deleted_at IS NULL").
|
||||
First(&chickin).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &chickin, nil
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package chickins
|
||||
|
||||
import (
|
||||
// m "gitlab.com/mbugroup/lti-api.git/internal/middleware"
|
||||
controller "gitlab.com/mbugroup/lti-api.git/internal/modules/production/chickins/controllers"
|
||||
chickin "gitlab.com/mbugroup/lti-api.git/internal/modules/production/chickins/services"
|
||||
user "gitlab.com/mbugroup/lti-api.git/internal/modules/users/services"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func ChickinRoutes(v1 fiber.Router, u user.UserService, s chickin.ChickinService) {
|
||||
ctrl := controller.NewChickinController(s)
|
||||
|
||||
route := v1.Group("/chickins")
|
||||
|
||||
// route.Get("/", m.Auth(u), ctrl.GetAll)
|
||||
// route.Post("/", m.Auth(u), ctrl.CreateOne)
|
||||
// route.Get("/:id", m.Auth(u), ctrl.GetOne)
|
||||
// route.Patch("/:id", m.Auth(u), ctrl.UpdateOne)
|
||||
// route.Delete("/:id", m.Auth(u), ctrl.DeleteOne)
|
||||
|
||||
route.Get("/", ctrl.GetAll)
|
||||
route.Post("/", ctrl.CreateOne)
|
||||
route.Get("/:id", ctrl.GetOne)
|
||||
route.Patch("/:id", ctrl.UpdateOne)
|
||||
route.Delete("/:id", ctrl.DeleteOne)
|
||||
route.Post("/:id/approve", ctrl.Approve)
|
||||
}
|
||||
@@ -0,0 +1,371 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
rProductWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-warehouses/repositories"
|
||||
KandangRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/repositories"
|
||||
rWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/repositories"
|
||||
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/production/chickins/repositories"
|
||||
rProjectFlockKandang "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/repositories"
|
||||
|
||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/production/chickins/validations"
|
||||
rProjectFlock "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/repositories"
|
||||
AuditLogRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/shared/repositories"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/sirupsen/logrus"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ChickinService interface {
|
||||
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.ProjectChickin, int64, error)
|
||||
GetOne(ctx *fiber.Ctx, id uint) (*entity.ProjectChickin, error)
|
||||
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*entity.ProjectChickin, error)
|
||||
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*entity.ProjectChickin, error)
|
||||
DeleteOne(ctx *fiber.Ctx, id uint) error
|
||||
Approve(ctx *fiber.Ctx, id uint) error
|
||||
}
|
||||
|
||||
type chickinService struct {
|
||||
Log *logrus.Logger
|
||||
Validate *validator.Validate
|
||||
Repository repository.ProjectChickinRepository
|
||||
KandangRepo KandangRepo.KandangRepository
|
||||
WarehouseRepo rWarehouse.WarehouseRepository
|
||||
ProductWarehouseRepo rProductWarehouse.ProductWarehouseRepository
|
||||
ProjectFlockRepo rProjectFlock.ProjectflockRepository
|
||||
AuditLogRepo AuditLogRepo.AuditLogRepository
|
||||
ProjectflockKandangRepo rProjectFlockKandang.ProjectFlockKandangRepository
|
||||
ProjectflockPopulationRepo rProjectFlock.ProjectFlockPopulationRepository
|
||||
}
|
||||
|
||||
func NewChickinService(repo repository.ProjectChickinRepository, kandangRepo KandangRepo.KandangRepository, warehouseRepo rWarehouse.WarehouseRepository, productWarehouseRepo rProductWarehouse.ProductWarehouseRepository, projectFlockRepo rProjectFlock.ProjectflockRepository, auditLogRepo AuditLogRepo.AuditLogRepository, projectflockkandangRepo rProjectFlockKandang.ProjectFlockKandangRepository, projectflockpopulationRepo rProjectFlock.ProjectFlockPopulationRepository, validate *validator.Validate) ChickinService {
|
||||
return &chickinService{
|
||||
Log: utils.Log,
|
||||
Validate: validate,
|
||||
Repository: repo,
|
||||
KandangRepo: kandangRepo,
|
||||
WarehouseRepo: warehouseRepo,
|
||||
ProductWarehouseRepo: productWarehouseRepo,
|
||||
ProjectFlockRepo: projectFlockRepo,
|
||||
AuditLogRepo: auditLogRepo,
|
||||
ProjectflockKandangRepo: projectflockkandangRepo,
|
||||
ProjectflockPopulationRepo: projectflockpopulationRepo,
|
||||
}
|
||||
}
|
||||
|
||||
func (s chickinService) withRelations(db *gorm.DB) *gorm.DB {
|
||||
return db.
|
||||
Preload("CreatedUser").
|
||||
Preload("ProjectFlockKandang.Kandang").
|
||||
Preload("ProjectFlockKandang.Kandang.Location").
|
||||
Preload("ProjectFlockKandang.Kandang.Location.Area").
|
||||
Preload("ProjectFlockKandang.Kandang.Pic").
|
||||
Preload("ProjectFlockKandang.ProjectFlock").
|
||||
Preload("ProjectFlockKandang.ProjectFlock.Flock").
|
||||
Preload("ProjectFlockKandang.ProjectFlock.ProductCategory").
|
||||
Preload("ProjectFlockKandang.ProjectFlock.Area").
|
||||
Preload("ProjectFlockKandang.ProjectFlock.Fcr").
|
||||
Preload("ProjectFlockKandang.ProjectFlock.Location").
|
||||
Preload("ProjectFlockKandang.ProjectFlock.Location.Area")
|
||||
|
||||
}
|
||||
|
||||
func (s chickinService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.ProjectChickin, int64, error) {
|
||||
if err := s.Validate.Struct(params); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
offset := (params.Page - 1) * params.Limit
|
||||
chickins, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
|
||||
db = s.withRelations(db)
|
||||
if params.ProjectFlockKandangId != 0 {
|
||||
return db.Where("project_flock_kandang_id = ?", params.ProjectFlockKandangId)
|
||||
}
|
||||
return db.Order("created_at DESC").Order("updated_at DESC")
|
||||
})
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to get chickins: %+v", err)
|
||||
return nil, 0, err
|
||||
}
|
||||
return chickins, total, nil
|
||||
}
|
||||
|
||||
func (s chickinService) GetOne(c *fiber.Ctx, id uint) (*entity.ProjectChickin, error) {
|
||||
|
||||
chickin, err := s.Repository.GetByID(c.Context(), id, s.withRelations)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "Chickin not found")
|
||||
}
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed get chickin by id: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
return chickin, nil
|
||||
}
|
||||
|
||||
func (s *chickinService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entity.ProjectChickin, error) {
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
projectflockkandang, err := s.ProjectflockKandangRepo.GetByID(c.Context(), 1)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to get projectflock kandang: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
warehouse, err := s.WarehouseRepo.GetByKandangID(c.Context(), projectflockkandang.KandangId)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to get warehouse: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
projectFlock, err := s.ProjectFlockRepo.GetByID(
|
||||
c.Context(),
|
||||
projectflockkandang.ProjectFlockId,
|
||||
func(db *gorm.DB) *gorm.DB {
|
||||
return db.Preload("ProductCategory")
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to get project flock: %+v", err)
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "Project Flock not found")
|
||||
}
|
||||
var productWarehouses []entity.ProductWarehouse
|
||||
err = s.ProductWarehouseRepo.DB().
|
||||
WithContext(c.Context()).
|
||||
Joins("JOIN products ON products.id = product_warehouses.product_id").
|
||||
Joins("JOIN product_categories ON product_categories.id = products.product_category_id").
|
||||
Where("product_categories.code = ? AND product_warehouses.warehouse_id = ?", projectFlock.ProductCategory.Code, warehouse.Id).
|
||||
Order("created_at DESC").
|
||||
Find(&productWarehouses).Error
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to get product warehouses: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
if len(productWarehouses) == 0 {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "Product Warehouse not found for the given Project Flock and Warehouse")
|
||||
}
|
||||
|
||||
// Jumlahkan semua quantity DOC
|
||||
totalQuantity := 0.0
|
||||
for _, pw := range productWarehouses {
|
||||
totalQuantity += pw.Quantity
|
||||
}
|
||||
|
||||
if totalQuantity < 1 {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Insufficient quantity in Product Warehouses")
|
||||
}
|
||||
|
||||
// Buat satu chickin dengan total quantity
|
||||
chickinDate, err := utils.ParseDateString(req.ChickInDate)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to parse chickin date: %+v", err)
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid ChickInDate format")
|
||||
}
|
||||
newChickin := &entity.ProjectChickin{
|
||||
ProjectFlockKandangId: projectflockkandang.ProjectFlockId,
|
||||
ChickInDate: chickinDate,
|
||||
Quantity: totalQuantity,
|
||||
Note: "",
|
||||
CreatedBy: 1, //todo: ganti dengan user login
|
||||
}
|
||||
err = s.Repository.CreateOne(c.Context(), newChickin, nil)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to create chickin: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Update semua product warehouse: set quantity jadi 0
|
||||
for _, pw := range productWarehouses {
|
||||
err = s.ProductWarehouseRepo.PatchOne(c.Context(), pw.Id, map[string]any{
|
||||
"quantity": 0,
|
||||
}, nil)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to update product warehouse quantity: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
existingPopulation, err := s.ProjectflockPopulationRepo.GetByProjectFlockKandangID(c.Context(), req.ProjectFlockKandangId)
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
s.Log.Errorf("Failed to get project flock population: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
if existingPopulation != nil {
|
||||
|
||||
err = s.ProjectflockPopulationRepo.PatchOne(c.Context(), existingPopulation.Id, map[string]any{
|
||||
"reserved_quantity": newChickin.Quantity + existingPopulation.ReservedQuantity,
|
||||
}, nil)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to update project flock population: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
newPopulation := &entity.ProjectFlockPopulation{
|
||||
ProjectFlockKandangId: req.ProjectFlockKandangId,
|
||||
InitialQuantity: 0,
|
||||
CurrentQuantity: 0,
|
||||
ReservedQuantity: newChickin.Quantity,
|
||||
CreatedBy: 1, // todo: ganti dengan user login
|
||||
}
|
||||
err = s.ProjectflockPopulationRepo.CreateOne(c.Context(), newPopulation, nil)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to create project flock population: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return s.GetOne(c, newChickin.Id)
|
||||
}
|
||||
|
||||
func (s chickinService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.ProjectChickin, error) {
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
updateBody := make(map[string]any)
|
||||
|
||||
if req.ChickInDate != "" {
|
||||
updateBody["chick_in_date"] = req.ChickInDate
|
||||
}
|
||||
if len(updateBody) == 0 {
|
||||
return s.GetOne(c, id)
|
||||
}
|
||||
|
||||
if err := s.Repository.PatchOne(c.Context(), id, updateBody, nil); err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "Chickin not found")
|
||||
}
|
||||
s.Log.Errorf("Failed to update chickin: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.GetOne(c, id)
|
||||
}
|
||||
|
||||
func (s chickinService) DeleteOne(c *fiber.Ctx, id uint) error {
|
||||
// todo: cek apakah chickin sudah di approve atau belum
|
||||
|
||||
chickin, err := s.Repository.GetByID(c.Context(), id, nil)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusNotFound, "Chickin not found")
|
||||
}
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed get chickin by id: %+v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
population, err := s.ProjectflockPopulationRepo.GetByProjectFlockKandangID(c.Context(), chickin.ProjectFlockKandangId)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to get project flock population: %+v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
err = s.ProjectflockPopulationRepo.PatchOne(c.Context(), population.Id, map[string]any{
|
||||
"reserved_quantity": population.ReservedQuantity - chickin.Quantity,
|
||||
}, nil)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to update project flock population: %+v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if err := s.Repository.DeleteOne(c.Context(), id); err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusNotFound, "Chickin not found")
|
||||
}
|
||||
s.Log.Errorf("Failed to delete chickin: %+v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
projectflockkandang, err := s.ProjectflockKandangRepo.GetByID(c.Context(), population.ProjectFlockKandangId)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to get projectflock kandang: %+v", err)
|
||||
return err
|
||||
}
|
||||
warehouse, err := s.WarehouseRepo.GetByKandangID(c.Context(), projectflockkandang.KandangId)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to get warehouse: %+v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
projectFlock, err := s.ProjectFlockRepo.GetByID(
|
||||
c.Context(),
|
||||
projectflockkandang.ProjectFlockId,
|
||||
func(db *gorm.DB) *gorm.DB {
|
||||
return db.Preload("ProductCategory")
|
||||
},
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to get project flock: %+v", err)
|
||||
return fiber.NewError(fiber.StatusNotFound, "Project Flock not found")
|
||||
}
|
||||
var productWarehouse entity.ProductWarehouse
|
||||
err = s.ProductWarehouseRepo.DB().WithContext(c.Context()).
|
||||
Joins("JOIN products ON products.id = product_warehouses.product_id").
|
||||
Joins("JOIN product_categories ON product_categories.id = products.product_category_id").
|
||||
Where("product_categories.code = ? AND product_warehouses.warehouse_id = ?", projectFlock.ProductCategory.Code, warehouse.Id).
|
||||
Order("created_at DESC").
|
||||
First(&productWarehouse).Error
|
||||
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusNotFound, "Product Warehouse not found for the given Project Flock and Warehouse")
|
||||
}
|
||||
s.Log.Errorf("Failed to get product warehouse: %+v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
updatedQuantity := productWarehouse.Quantity + chickin.Quantity
|
||||
err = s.ProductWarehouseRepo.PatchOne(c.Context(), productWarehouse.Id, map[string]any{
|
||||
"quantity": updatedQuantity,
|
||||
}, nil)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to update product warehouse quantity: %+v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *chickinService) Approve(c *fiber.Ctx, id uint) error {
|
||||
|
||||
// todo: ini contoh akhir jika sudah approved
|
||||
|
||||
chickin, err := s.Repository.GetByID(
|
||||
c.Context(),
|
||||
id,
|
||||
nil,
|
||||
)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusNotFound, "Chickin not found")
|
||||
}
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed get chickin by id: %+v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
population, err := s.ProjectflockPopulationRepo.GetByProjectFlockKandangID(c.Context(), chickin.ProjectFlockKandangId)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to get project flock population: %+v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
err = s.ProjectflockPopulationRepo.PatchOne(c.Context(), population.Id, map[string]any{
|
||||
"reserved_quantity": population.ReservedQuantity - chickin.Quantity,
|
||||
"initial_quantity": population.InitialQuantity + chickin.Quantity,
|
||||
"current_quantity": population.CurrentQuantity + chickin.Quantity,
|
||||
}, nil)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to update project flock population: %+v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package validation
|
||||
|
||||
type Create struct {
|
||||
ProjectFlockKandangId uint `json:"project_flock_kandang_id" validate:"required,number,min=1"`
|
||||
ChickInDate string `json:"chick_in_date" validate:"required,datetime=2006-01-02"`
|
||||
}
|
||||
|
||||
type Update struct {
|
||||
ChickInDate string `json:"chick_in_date" validate:"required,datetime=2006-01-02"`
|
||||
}
|
||||
|
||||
type Query struct {
|
||||
Page int `query:"page" validate:"omitempty,number,min=1"`
|
||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100"`
|
||||
ProjectFlockKandangId uint `query:"project_flock_kandang_id" validate:"omitempty,number,min=1"`
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
rFlock "gitlab.com/mbugroup/lti-api.git/internal/modules/master/flocks/repositories"
|
||||
rKandang "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/repositories"
|
||||
rProjectflock "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/repositories"
|
||||
|
||||
sProjectflock "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/services"
|
||||
|
||||
rUser "gitlab.com/mbugroup/lti-api.git/internal/modules/users/repositories"
|
||||
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ProjectFlockPopulationRepository interface {
|
||||
repository.BaseRepository[entity.ProjectFlockPopulation]
|
||||
GetByProjectFlockKandangID(ctx context.Context, projectFlockKandangID uint) (*entity.ProjectFlockPopulation, error)
|
||||
}
|
||||
|
||||
type projectFlockPopulationRepositoryImpl struct {
|
||||
*repository.BaseRepositoryImpl[entity.ProjectFlockPopulation]
|
||||
}
|
||||
|
||||
func NewProjectFlockPopulationRepository(db *gorm.DB) ProjectFlockPopulationRepository {
|
||||
return &projectFlockPopulationRepositoryImpl{
|
||||
BaseRepositoryImpl: repository.NewBaseRepository[entity.ProjectFlockPopulation](db),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *projectFlockPopulationRepositoryImpl) GetByProjectFlockKandangID(ctx context.Context, projectFlockKandangID uint) (*entity.ProjectFlockPopulation, error) {
|
||||
var record entity.ProjectFlockPopulation
|
||||
err := r.DB().WithContext(ctx).
|
||||
Where("project_flock_kandang_id = ?", projectFlockKandangID).
|
||||
First(&record).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &record, nil
|
||||
}
|
||||
+14
@@ -8,6 +8,7 @@ import (
|
||||
)
|
||||
|
||||
type ProjectFlockKandangRepository interface {
|
||||
GetByID(ctx context.Context, id uint) (*entity.ProjectFlockKandang, error)
|
||||
CreateMany(ctx context.Context, records []*entity.ProjectFlockKandang) error
|
||||
DeleteMany(ctx context.Context, projectFlockID uint, kandangIDs []uint) error
|
||||
GetAll(ctx context.Context) ([]entity.ProjectFlockKandang, error)
|
||||
@@ -59,3 +60,16 @@ func (r *projectFlockKandangRepositoryImpl) WithTx(tx *gorm.DB) ProjectFlockKand
|
||||
func (r *projectFlockKandangRepositoryImpl) DB() *gorm.DB {
|
||||
return r.db
|
||||
}
|
||||
|
||||
func (r *projectFlockKandangRepositoryImpl) GetByID(ctx context.Context, id uint) (*entity.ProjectFlockKandang, error) {
|
||||
record := new(entity.ProjectFlockKandang)
|
||||
if err := r.db.WithContext(ctx).
|
||||
Preload("ProjectFlock").
|
||||
Preload("ProjectFlock.Flock").
|
||||
Preload("Kandang").
|
||||
Preload("CreatedUser").
|
||||
First(record, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return record, nil
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"gorm.io/gorm"
|
||||
|
||||
chickins "gitlab.com/mbugroup/lti-api.git/internal/modules/production/chickins"
|
||||
projectflocks "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks"
|
||||
recordings "gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings"
|
||||
// MODULE IMPORTS
|
||||
@@ -18,6 +19,7 @@ func RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Valida
|
||||
allModules := []modules.Module{
|
||||
projectflocks.ProjectflockModule{},
|
||||
recordings.RecordingModule{},
|
||||
chickins.ChickinModule{},
|
||||
// MODULE REGISTRY
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user