FIX[BE]: adjust response on proudctwarehouses

This commit is contained in:
aguhh18
2025-10-20 08:45:31 +07:00
parent 4218298234
commit 5c3787886b
8 changed files with 291 additions and 122 deletions
@@ -1,6 +1,6 @@
CREATE TABLE project_chickins ( CREATE TABLE IF NOT EXISTS project_chickins (
id BIGSERIAL PRIMARY KEY, id BIGSERIAL PRIMARY KEY,
project_floc_id BIGINT NOT NULL, project_floc_kandang_id BIGINT NOT NULL,
chick_in_date DATE NOT NULL, chick_in_date DATE NOT NULL,
quantity NUMERIC(15, 3) NOT NULL, quantity NUMERIC(15, 3) NOT NULL,
note TEXT, note TEXT,
@@ -10,12 +10,27 @@ CREATE TABLE project_chickins (
deleted_at TIMESTAMPTZ deleted_at TIMESTAMPTZ
); );
CREATE INDEX idx_project_chickins_project_floc_id ON project_chickins (project_floc_id); -- FOREIGN KEYS (dijalankan setelah semua tabel parent ada)
DO $$
BEGIN
IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'project_flock_kandangs') THEN
ALTER TABLE project_chickins
ADD CONSTRAINT fk_project_floc_kandang_id
FOREIGN KEY (project_floc_kandang_id)
REFERENCES project_flock_kandangs(id)
ON DELETE RESTRICT ON UPDATE CASCADE;
END IF;
CREATE INDEX idx_project_chickins_created_by ON project_chickins (created_by); IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'users') THEN
ALTER TABLE project_chickins
ADD CONSTRAINT fk_created_by
FOREIGN KEY (created_by)
REFERENCES users(id)
ON DELETE RESTRICT ON UPDATE CASCADE;
END IF;
END $$;
ALTER TABLE project_chickins -- INDEXES
ADD CONSTRAINT fk_project_floc_id FOREIGN KEY (project_floc_id) REFERENCES project_flocks (id); CREATE INDEX IF NOT EXISTS idx_project_chickins_project_floc_kandang_id ON project_chickins (project_floc_kandang_id);
ALTER TABLE project_chickins CREATE INDEX IF NOT EXISTS idx_project_chickins_created_by ON project_chickins (created_by);
ADD CONSTRAINT fk_created_by FOREIGN KEY (created_by) REFERENCES users (id);
+11 -11
View File
@@ -9,16 +9,16 @@ import (
const () const ()
type ProjectChickin struct { type ProjectChickin struct {
Id uint `gorm:"primaryKey"` Id uint `gorm:"primaryKey"`
ProjectFlocId uint `gorm:"not null"` ProjectFlocKandangId uint `gorm:"not null"`
ChickInDate time.Time `gorm:"not null"` ChickInDate time.Time `gorm:"not null"`
Quantity float64 `gorm:"not null"` Quantity float64 `gorm:"not null"`
Note string `gorm:"type:text"` Note string `gorm:"type:text"`
CreatedBy uint `gorm:"not null"` CreatedBy uint `gorm:"not null"`
CreatedAt time.Time `gorm:"autoCreateTime"` CreatedAt time.Time `gorm:"autoCreateTime"`
UpdatedAt time.Time `gorm:"autoUpdateTime"` UpdatedAt time.Time `gorm:"autoUpdateTime"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"` DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
ProjectFlock ProjectFlock `gorm:"foreignKey:ProjectFlocId;references:Id"` ProjectFlockKandang ProjectFlockKandang `gorm:"foreignKey:ProjectFlocKandangId;references:Id"`
CreatedUser User `gorm:"foreignKey:CreatedBy;references:Id"` CreatedUser User `gorm:"foreignKey:CreatedBy;references:Id"`
} }
@@ -34,7 +34,14 @@ func NewProductWarehouseService(repo repository.ProductWarehouseRepository, vali
} }
func (s productWarehouseService) withRelations(db *gorm.DB) *gorm.DB { func (s productWarehouseService) withRelations(db *gorm.DB) *gorm.DB {
return db.Preload("Product.Flags").Preload("Product").Preload("Warehouse").Preload("CreatedUser") return db.
Preload("Product.Flags").
Preload("Product").
Preload("Warehouse").
Preload("Warehouse.Location").
Preload("Warehouse.Area").
Preload("Warehouse.Kandang").
Preload("CreatedUser")
} }
func (s productWarehouseService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.ProductWarehouse, int64, error) { func (s productWarehouseService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.ProductWarehouse, int64, error) {
@@ -6,23 +6,52 @@ import (
entity "gitlab.com/mbugroup/lti-api.git/internal/entities" entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
) )
// === DTO Structs === // === DTO Structs (ordered) ===
type ChickinBaseDTO struct { type FlockDTO struct {
Id uint `json:"id"` Id uint `json:"id"`
ProjectFlocId uint `json:"project_floc_id"` Name string `json:"name"`
ChickInDate time.Time `json:"chick_in_date"`
Quantity float64 `json:"quantity"`
Note string `json:"note"`
} }
type ChickinSimpleDTO struct { type KandangDTO struct {
Id uint `json:"id"` Id uint `json:"id"`
ProjectFlocId uint `json:"project_floc_id"` Name string `json:"name"`
ChickInDate time.Time `json:"chick_in_date"` }
Quantity float64 `json:"quantity"`
Note string `json:"note"` type ProductCategoryDTO struct {
CreatedBy uint `json:"created_by"` Id uint `json:"id"`
Name string `json:"name"`
}
type AreaDTO struct {
Id uint `json:"id"`
Name string `json:"name"`
}
type FcrDTO struct {
Id uint `json:"id"`
Name string `json:"name"`
}
type LocationDTO struct {
Id uint `json:"id"`
Name string `json:"name"`
}
type ProjectFlockDTO struct {
Id uint `json:"id"`
Period int `json:"period"`
Flock *FlockDTO `json:"flock"`
ProductCategory *ProductCategoryDTO `json:"product_category"`
Area *AreaDTO `json:"area"`
Fcr *FcrDTO `json:"fcr"`
Location *LocationDTO `json:"location"`
}
type ProjectFlockKandangDTO struct {
Id uint `json:"id"`
ProjectFlock *ProjectFlockDTO `json:"project_flock"`
Kandang *KandangDTO `json:"kandang"`
} }
type UserBaseDTO struct { type UserBaseDTO struct {
@@ -30,56 +59,159 @@ type UserBaseDTO struct {
Name string `json:"name"` Name string `json:"name"`
} }
type ChickinBaseDTO struct {
Id uint `json:"id"`
ProjectFlocKandangId uint `json:"project_floc_kandang_id"`
ChickInDate time.Time `json:"chick_in_date"`
Quantity float64 `json:"quantity"`
Note string `json:"note"`
}
type ChickinSimpleDTO struct {
Id uint `json:"id"`
ProjectFlocKandangId uint `json:"project_floc_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 { type ChickinListDTO struct {
ChickinBaseDTO ChickinBaseDTO
ProjectFlock *ProjectFlockDTO `json:"project_flock"` ProjectFlockKandang *ProjectFlockKandangDTO `json:"project_flock_kandang"`
CreatedUser *UserBaseDTO `json:"created_user"` CreatedUser *UserBaseDTO `json:"created_user"`
CreatedAt time.Time `json:"created_at"` CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"` UpdatedAt time.Time `json:"updated_at"`
}
type ProjectFlockDTO struct {
Id uint `json:"id"`
Period int `json:"period"`
FlockId uint `json:"flock_id"`
FlockName string `json:"flock_name"`
}
// === Mapper Functions ===
func ToProjectFlockDTO(e entity.ProjectFlock) ProjectFlockDTO {
return ProjectFlockDTO{
Id: e.Id,
Period: e.Period,
FlockId: e.FlockId,
FlockName: e.Flock.Name,
}
} }
type ChickinDetailDTO struct { type ChickinDetailDTO struct {
ChickinListDTO ChickinListDTO
} }
// === Mapper Functions === // === Mapper Functions (ordered) ===
func ToFlockDTO(e entity.Flock) FlockDTO {
return FlockDTO{
Id: e.Id,
Name: e.Name,
}
}
func ToKandangDTO(e entity.Kandang) KandangDTO {
return KandangDTO{
Id: e.Id,
Name: e.Name,
}
}
func ToProductCategoryDTO(e entity.ProductCategory) ProductCategoryDTO {
return ProductCategoryDTO{
Id: e.Id,
Name: e.Name,
}
}
func ToAreaDTO(e entity.Area) AreaDTO {
return AreaDTO{
Id: e.Id,
Name: e.Name,
}
}
func ToFcrDTO(e entity.Fcr) FcrDTO {
return FcrDTO{
Id: e.Id,
Name: e.Name,
}
}
func ToLocationDTO(e entity.Location) LocationDTO {
return LocationDTO{
Id: e.Id,
Name: e.Name,
}
}
func ToProjectFlockDTO(e entity.ProjectFlock) ProjectFlockDTO {
var flock *FlockDTO
if e.Flock.Id != 0 {
mapped := ToFlockDTO(e.Flock)
flock = &mapped
}
var productCategory *ProductCategoryDTO
if e.ProductCategory.Id != 0 {
mapped := ToProductCategoryDTO(e.ProductCategory)
productCategory = &mapped
}
var area *AreaDTO
if e.Area.Id != 0 {
mapped := ToAreaDTO(e.Area)
area = &mapped
}
var fcr *FcrDTO
if e.Fcr.Id != 0 {
mapped := ToFcrDTO(e.Fcr)
fcr = &mapped
}
var location *LocationDTO
if e.Location.Id != 0 {
mapped := ToLocationDTO(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 *KandangDTO
if e.Kandang.Id != 0 {
mapped := ToKandangDTO(e.Kandang)
kandang = &mapped
}
return ProjectFlockKandangDTO{
Id: e.Id,
ProjectFlock: pf,
Kandang: kandang,
}
}
func ToUserBaseDTO(e entity.User) UserBaseDTO {
return UserBaseDTO{
Id: e.Id,
Name: e.Name,
}
}
func ToChickinBaseDTO(e entity.ProjectChickin) ChickinBaseDTO { func ToChickinBaseDTO(e entity.ProjectChickin) ChickinBaseDTO {
return ChickinBaseDTO{ return ChickinBaseDTO{
Id: e.Id, Id: e.Id,
ProjectFlocId: e.ProjectFlocId, ProjectFlocKandangId: e.ProjectFlocKandangId,
ChickInDate: e.ChickInDate, ChickInDate: e.ChickInDate,
Quantity: e.Quantity, Quantity: e.Quantity,
Note: e.Note, Note: e.Note,
} }
} }
func ToChickinSimpleDTO(e entity.ProjectChickin) ChickinSimpleDTO { func ToChickinSimpleDTO(e entity.ProjectChickin) ChickinSimpleDTO {
return ChickinSimpleDTO{ return ChickinSimpleDTO{
Id: e.Id, Id: e.Id,
ProjectFlocId: e.ProjectFlocId, ProjectFlocKandangId: e.ProjectFlocKandangId,
ChickInDate: e.ChickInDate, ChickInDate: e.ChickInDate,
Quantity: e.Quantity, Quantity: e.Quantity,
Note: e.Note, Note: e.Note,
CreatedBy: e.CreatedBy, CreatedBy: e.CreatedBy,
} }
} }
@@ -89,19 +221,17 @@ func ToChickinListDTO(e entity.ProjectChickin) ChickinListDTO {
mapped := ToUserBaseDTO(e.CreatedUser) mapped := ToUserBaseDTO(e.CreatedUser)
createdUser = &mapped createdUser = &mapped
} }
var pfk *ProjectFlockKandangDTO
var projectFlock *ProjectFlockDTO if e.ProjectFlockKandang.Id != 0 {
if e.ProjectFlock.Id != 0 { mapped := ToProjectFlockKandangDTO(e.ProjectFlockKandang)
mapped := ToProjectFlockDTO(e.ProjectFlock) pfk = &mapped
projectFlock = &mapped
} }
return ChickinListDTO{ return ChickinListDTO{
ChickinBaseDTO: ToChickinBaseDTO(e), ChickinBaseDTO: ToChickinBaseDTO(e),
ProjectFlock: projectFlock, ProjectFlockKandang: pfk,
CreatedAt: e.CreatedAt, CreatedUser: createdUser,
UpdatedAt: e.UpdatedAt, CreatedAt: e.CreatedAt,
CreatedUser: createdUser, UpdatedAt: e.UpdatedAt,
} }
} }
@@ -126,10 +256,3 @@ func ToChickinDetailDTO(e entity.ProjectChickin) ChickinDetailDTO {
ChickinListDTO: ToChickinListDTO(e), ChickinListDTO: ToChickinListDTO(e),
} }
} }
func ToUserBaseDTO(e entity.User) UserBaseDTO {
return UserBaseDTO{
Id: e.Id,
Name: e.Name,
}
}
@@ -25,12 +25,13 @@ func (ChickinModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *
kandangRepo := rKandang.NewKandangRepository(db) kandangRepo := rKandang.NewKandangRepository(db)
auditlogrepo := rAuditLog.NewAuditLogRepository(db) auditlogrepo := rAuditLog.NewAuditLogRepository(db)
warehouseRepo := rWarehouse.NewWarehouseRepository(db) warehouseRepo := rWarehouse.NewWarehouseRepository(db)
projectflockkandangrepo := rProjectFlock.NewProjectFlockKandangRepository(db)
projectFlockRepo := rProjectFlock.NewProjectflockRepository(db) projectFlockRepo := rProjectFlock.NewProjectflockRepository(db)
productWarehouseRepo := rProductWarehouse.NewProductWarehouseRepository(db) productWarehouseRepo := rProductWarehouse.NewProductWarehouseRepository(db)
userRepo := rUser.NewUserRepository(db) userRepo := rUser.NewUserRepository(db)
chickinService := sChickin.NewChickinService(chickinRepo, kandangRepo, warehouseRepo, productWarehouseRepo, projectFlockRepo, auditlogrepo, validate) chickinService := sChickin.NewChickinService(chickinRepo, kandangRepo, warehouseRepo, productWarehouseRepo, projectFlockRepo, auditlogrepo, projectflockkandangrepo, validate)
userService := sUser.NewUserService(userRepo, validate) userService := sUser.NewUserService(userRepo, validate)
ChickinRoutes(router, userService, chickinService) ChickinRoutes(router, userService, chickinService)
@@ -8,6 +8,7 @@ import (
KandangRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/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" rWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/repositories"
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/production/chickins/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" 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" rProjectFlock "gitlab.com/mbugroup/lti-api.git/internal/modules/production/project_flocks/repositories"
@@ -30,34 +31,41 @@ type ChickinService interface {
} }
type chickinService struct { type chickinService struct {
Log *logrus.Logger Log *logrus.Logger
Validate *validator.Validate Validate *validator.Validate
Repository repository.ProjectChickinRepository Repository repository.ProjectChickinRepository
KandangRepo KandangRepo.KandangRepository KandangRepo KandangRepo.KandangRepository
WarehouseRepo rWarehouse.WarehouseRepository WarehouseRepo rWarehouse.WarehouseRepository
ProductWarehouseRepo rProductWarehouse.ProductWarehouseRepository ProductWarehouseRepo rProductWarehouse.ProductWarehouseRepository
ProjectFlockRepo rProjectFlock.ProjectflockRepository ProjectFlockRepo rProjectFlock.ProjectflockRepository
AuditLogRepo AuditLogRepo.AuditLogRepository AuditLogRepo AuditLogRepo.AuditLogRepository
ProjectflockKandangRepo rProjectFlockKandang.ProjectFlockKandangRepository
} }
func NewChickinService(repo repository.ProjectChickinRepository, kandangRepo KandangRepo.KandangRepository, warehouseRepo rWarehouse.WarehouseRepository, productWarehouseRepo rProductWarehouse.ProductWarehouseRepository, projectFlockRepo rProjectFlock.ProjectflockRepository, auditLogRepo AuditLogRepo.AuditLogRepository, validate *validator.Validate) ChickinService { func NewChickinService(repo repository.ProjectChickinRepository, kandangRepo KandangRepo.KandangRepository, warehouseRepo rWarehouse.WarehouseRepository, productWarehouseRepo rProductWarehouse.ProductWarehouseRepository, projectFlockRepo rProjectFlock.ProjectflockRepository, auditLogRepo AuditLogRepo.AuditLogRepository, projectflockkandangRepo rProjectFlockKandang.ProjectFlockKandangRepository, validate *validator.Validate) ChickinService {
return &chickinService{ return &chickinService{
Log: utils.Log, Log: utils.Log,
Validate: validate, Validate: validate,
Repository: repo, Repository: repo,
KandangRepo: kandangRepo, KandangRepo: kandangRepo,
WarehouseRepo: warehouseRepo, WarehouseRepo: warehouseRepo,
ProductWarehouseRepo: productWarehouseRepo, ProductWarehouseRepo: productWarehouseRepo,
ProjectFlockRepo: projectFlockRepo, ProjectFlockRepo: projectFlockRepo,
AuditLogRepo: auditLogRepo, AuditLogRepo: auditLogRepo,
ProjectflockKandangRepo: projectflockkandangRepo,
} }
} }
func (s chickinService) withRelations(db *gorm.DB) *gorm.DB { func (s chickinService) withRelations(db *gorm.DB) *gorm.DB {
return db. return db.
Preload("CreatedUser"). Preload("CreatedUser").
Preload("ProjectFlock"). Preload("ProjectFlockKandang.Kandang").
Preload("ProjectFlock.ProductCategory") Preload("ProjectFlockKandang.ProjectFlock").
Preload("ProjectFlockKandang.ProjectFlock.Flock").
Preload("ProjectFlockKandang.ProjectFlock.ProductCategory").
Preload("ProjectFlockKandang.ProjectFlock.Area").
Preload("ProjectFlockKandang.ProjectFlock.Fcr").
Preload("ProjectFlockKandang.ProjectFlock.Location")
} }
@@ -101,25 +109,27 @@ func (s *chickinService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entit
} }
// ambil salah satu kandang dari project_floc_id dari kandang repository // ambil salah satu kandang dari project_floc_id dari kandang repository
kandang, err := s.KandangRepo.GetFirstByProjectFlockID(c.Context(), 1) projectflockkandang, err := s.ProjectflockKandangRepo.GetByID(c.Context(), 1)
if err != nil { if err != nil {
s.Log.Errorf("Failed to get kandang: %+v", err) s.Log.Errorf("Failed to get projectflock kandang: %+v", err)
return nil, err return nil, err
} }
// ambil warehouse dari kandangid // ambil warehouse dari kandangid
warehouse, err := s.WarehouseRepo.GetByKandangID(c.Context(), kandang.Id) warehouse, err := s.WarehouseRepo.GetByKandangID(c.Context(), projectflockkandang.KandangId)
if err != nil { if err != nil {
s.Log.Errorf("Failed to get warehouse: %+v", err) s.Log.Errorf("Failed to get warehouse: %+v", err)
return nil, err return nil, err
} }
// getprojectflock id with relation // getprojectflock id with relation
projectFlock, err := s.ProjectFlockRepo.GetByID( projectFlock, err := s.ProjectFlockRepo.GetByID(
c.Context(), c.Context(),
req.ProjectFlockId, projectflockkandang.ProjectFlockId,
func(db *gorm.DB) *gorm.DB { func(db *gorm.DB) *gorm.DB {
return db.Preload("ProductCategory") return db.Preload("ProductCategory")
}, },
) )
if err != nil { if err != nil {
s.Log.Errorf("Failed to get project flock: %+v", err) s.Log.Errorf("Failed to get project flock: %+v", err)
return nil, fiber.NewError(fiber.StatusNotFound, "Project Flock not found") return nil, fiber.NewError(fiber.StatusNotFound, "Project Flock not found")
@@ -153,11 +163,11 @@ func (s *chickinService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entit
} }
newChickin := &entity.ProjectChickin{ newChickin := &entity.ProjectChickin{
ProjectFlocId: req.ProjectFlockId, ProjectFlocKandangId: projectflockkandang.ProjectFlockId,
ChickInDate: chickinDate, ChickInDate: chickinDate,
Quantity: productWarehouse.Quantity, Quantity: productWarehouse.Quantity,
Note: "", Note: "",
CreatedBy: 1, //todo: ganti dengan CreatedBy: 1, //todo: ganti dengan
} }
err = s.Repository.CreateOne(c.Context(), newChickin, nil) err = s.Repository.CreateOne(c.Context(), newChickin, nil)
@@ -183,7 +193,7 @@ func (s *chickinService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entit
stockAvailability := &entity.StockAvailability{ stockAvailability := &entity.StockAvailability{
EntityType: entity.EntityTypeProjectFlockKandang, EntityType: entity.EntityTypeProjectFlockKandang,
ReservedQuantity: productWarehouse.Quantity, ReservedQuantity: productWarehouse.Quantity,
EntityId: req.ProjectFlockId, //todo: nanti pakek projct flock kandang id EntityId: projectflockkandang.Id,
ProductId: productWarehouse.ProductId, ProductId: productWarehouse.ProductId,
} }
@@ -218,7 +228,8 @@ func (s *chickinService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entit
} }
} }
return nil, nil
return s.GetOne(c, newChickin.Id)
} }
func (s chickinService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.ProjectChickin, error) { func (s chickinService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.ProjectChickin, error) {
@@ -276,7 +287,7 @@ func (s *chickinService) Approve(c *fiber.Ctx, id uint) error {
// get stock avaibility untuk di update // get stock avaibility untuk di update
var stockAvailability entity.StockAvailability var stockAvailability entity.StockAvailability
err = s.ProductWarehouseRepo.DB().WithContext(c.Context()). err = s.ProductWarehouseRepo.DB().WithContext(c.Context()).
Where("entity_type = ? AND entity_id = ? ", entity.EntityTypeProjectFlockKandang, chickin.ProjectFlocId). Where("entity_type = ? AND entity_id = ? ", entity.EntityTypeProjectFlockKandang, chickin.ProjectFlocKandangId).
First(&stockAvailability).Error First(&stockAvailability).Error
if err != nil { if err != nil {
s.Log.Errorf("Failed to get stock availability: %+v", err) s.Log.Errorf("Failed to get stock availability: %+v", err)
@@ -288,7 +299,5 @@ func (s *chickinService) Approve(c *fiber.Ctx, id uint) error {
newReservedQuantity = 0 newReservedQuantity = 0
} }
return nil return nil
} }
@@ -1,8 +1,8 @@
package validation package validation
type Create struct { type Create struct {
ProjectFlockId uint `json:"project_flock_id" validate:"required,number,min=1"` ProjectFlockKandangId uint `json:"project_flock_kandang_id" validate:"required,number,min=1"`
ChickInDate string `json:"chick_in_date" validate:"required,datetime=2006-01-02"` ChickInDate string `json:"chick_in_date" validate:"required,datetime=2006-01-02"`
} }
type Update struct { type Update struct {
@@ -9,6 +9,7 @@ import (
) )
type ProjectFlockKandangRepository interface { type ProjectFlockKandangRepository interface {
GetByID(ctx context.Context, id uint) (*entity.ProjectFlockKandang, error)
CreateMany(ctx context.Context, records []*entity.ProjectFlockKandang) error CreateMany(ctx context.Context, records []*entity.ProjectFlockKandang) error
MarkDetached(ctx context.Context, projectFlockID uint, kandangIDs []uint, detachedAt time.Time) error MarkDetached(ctx context.Context, projectFlockID uint, kandangIDs []uint, detachedAt time.Time) error
GetAll(ctx context.Context) ([]entity.ProjectFlockKandang, error) GetAll(ctx context.Context) ([]entity.ProjectFlockKandang, error)
@@ -62,3 +63,16 @@ func (r *projectFlockKandangRepositoryImpl) WithTx(tx *gorm.DB) ProjectFlockKand
func (r *projectFlockKandangRepositoryImpl) DB() *gorm.DB { func (r *projectFlockKandangRepositoryImpl) DB() *gorm.DB {
return r.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
}