From 5c3787886b38b066cb992fda08cc61b6c6c913fb Mon Sep 17 00:00:00 2001 From: aguhh18 Date: Mon, 20 Oct 2025 08:45:31 +0700 Subject: [PATCH] FIX[BE]: adjust response on proudctwarehouses --- ...0649_create_project_chick_ins_table.up.sql | 31 ++- internal/entities/project_chickin.go | 22 +- .../services/product_warehouse.service.go | 9 +- .../production/chickins/dto/chickin.dto.go | 255 +++++++++++++----- .../modules/production/chickins/module.go | 3 +- .../chickins/services/chickin.service.go | 75 +++--- .../validations/chickin.validation.go | 4 +- .../projectflock_kandang.repository.go | 14 + 8 files changed, 291 insertions(+), 122 deletions(-) diff --git a/internal/database/migrations/20251018120649_create_project_chick_ins_table.up.sql b/internal/database/migrations/20251018120649_create_project_chick_ins_table.up.sql index a3b7dfb3..04475e21 100644 --- a/internal/database/migrations/20251018120649_create_project_chick_ins_table.up.sql +++ b/internal/database/migrations/20251018120649_create_project_chick_ins_table.up.sql @@ -1,6 +1,6 @@ -CREATE TABLE project_chickins ( +CREATE TABLE IF NOT EXISTS project_chickins ( id BIGSERIAL PRIMARY KEY, - project_floc_id BIGINT NOT NULL, + project_floc_kandang_id BIGINT NOT NULL, chick_in_date DATE NOT NULL, quantity NUMERIC(15, 3) NOT NULL, note TEXT, @@ -10,12 +10,27 @@ CREATE TABLE project_chickins ( 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 -ADD CONSTRAINT fk_project_floc_id FOREIGN KEY (project_floc_id) REFERENCES project_flocks (id); +-- INDEXES +CREATE INDEX IF NOT EXISTS idx_project_chickins_project_floc_kandang_id ON project_chickins (project_floc_kandang_id); -ALTER TABLE project_chickins -ADD CONSTRAINT fk_created_by FOREIGN KEY (created_by) REFERENCES users (id); \ No newline at end of file +CREATE INDEX IF NOT EXISTS idx_project_chickins_created_by ON project_chickins (created_by); \ No newline at end of file diff --git a/internal/entities/project_chickin.go b/internal/entities/project_chickin.go index 631c8ff3..07536187 100644 --- a/internal/entities/project_chickin.go +++ b/internal/entities/project_chickin.go @@ -9,16 +9,16 @@ import ( const () type ProjectChickin struct { - Id uint `gorm:"primaryKey"` - ProjectFlocId uint `gorm:"not null"` - ChickInDate time.Time `gorm:"not null"` - Quantity float64 `gorm:"not null"` - Note string `gorm:"type:text"` - CreatedBy uint `gorm:"not null"` - CreatedAt time.Time `gorm:"autoCreateTime"` - UpdatedAt time.Time `gorm:"autoUpdateTime"` - DeletedAt gorm.DeletedAt `gorm:"index" json:"-"` + Id uint `gorm:"primaryKey"` + ProjectFlocKandangId uint `gorm:"not null"` + ChickInDate time.Time `gorm:"not null"` + Quantity float64 `gorm:"not null"` + Note string `gorm:"type:text"` + CreatedBy uint `gorm:"not null"` + CreatedAt time.Time `gorm:"autoCreateTime"` + UpdatedAt time.Time `gorm:"autoUpdateTime"` + DeletedAt gorm.DeletedAt `gorm:"index" json:"-"` - ProjectFlock ProjectFlock `gorm:"foreignKey:ProjectFlocId;references:Id"` - CreatedUser User `gorm:"foreignKey:CreatedBy;references:Id"` + ProjectFlockKandang ProjectFlockKandang `gorm:"foreignKey:ProjectFlocKandangId;references:Id"` + CreatedUser User `gorm:"foreignKey:CreatedBy;references:Id"` } diff --git a/internal/modules/inventory/product-warehouses/services/product_warehouse.service.go b/internal/modules/inventory/product-warehouses/services/product_warehouse.service.go index 9afe5707..4fad5dc5 100644 --- a/internal/modules/inventory/product-warehouses/services/product_warehouse.service.go +++ b/internal/modules/inventory/product-warehouses/services/product_warehouse.service.go @@ -34,7 +34,14 @@ func NewProductWarehouseService(repo repository.ProductWarehouseRepository, vali } 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) { diff --git a/internal/modules/production/chickins/dto/chickin.dto.go b/internal/modules/production/chickins/dto/chickin.dto.go index 7a8b6773..c89caa0e 100644 --- a/internal/modules/production/chickins/dto/chickin.dto.go +++ b/internal/modules/production/chickins/dto/chickin.dto.go @@ -6,23 +6,52 @@ import ( entity "gitlab.com/mbugroup/lti-api.git/internal/entities" ) -// === DTO Structs === +// === DTO Structs (ordered) === -type ChickinBaseDTO struct { - Id uint `json:"id"` - ProjectFlocId uint `json:"project_floc_id"` - ChickInDate time.Time `json:"chick_in_date"` - Quantity float64 `json:"quantity"` - Note string `json:"note"` +type FlockDTO struct { + Id uint `json:"id"` + Name string `json:"name"` } -type ChickinSimpleDTO struct { - Id uint `json:"id"` - ProjectFlocId uint `json:"project_floc_id"` - ChickInDate time.Time `json:"chick_in_date"` - Quantity float64 `json:"quantity"` - Note string `json:"note"` - CreatedBy uint `json:"created_by"` +type KandangDTO struct { + Id uint `json:"id"` + Name string `json:"name"` +} + +type ProductCategoryDTO struct { + 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 { @@ -30,56 +59,159 @@ type UserBaseDTO struct { 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 { ChickinBaseDTO - ProjectFlock *ProjectFlockDTO `json:"project_flock"` - CreatedUser *UserBaseDTO `json:"created_user"` - CreatedAt time.Time `json:"created_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, - } + ProjectFlockKandang *ProjectFlockKandangDTO `json:"project_flock_kandang"` + CreatedUser *UserBaseDTO `json:"created_user"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` } type ChickinDetailDTO struct { 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 { return ChickinBaseDTO{ - Id: e.Id, - ProjectFlocId: e.ProjectFlocId, - ChickInDate: e.ChickInDate, - Quantity: e.Quantity, - Note: e.Note, + Id: e.Id, + ProjectFlocKandangId: e.ProjectFlocKandangId, + ChickInDate: e.ChickInDate, + Quantity: e.Quantity, + Note: e.Note, } } func ToChickinSimpleDTO(e entity.ProjectChickin) ChickinSimpleDTO { return ChickinSimpleDTO{ - Id: e.Id, - ProjectFlocId: e.ProjectFlocId, - ChickInDate: e.ChickInDate, - Quantity: e.Quantity, - Note: e.Note, - CreatedBy: e.CreatedBy, + Id: e.Id, + ProjectFlocKandangId: e.ProjectFlocKandangId, + ChickInDate: e.ChickInDate, + Quantity: e.Quantity, + Note: e.Note, + CreatedBy: e.CreatedBy, } } @@ -89,19 +221,17 @@ func ToChickinListDTO(e entity.ProjectChickin) ChickinListDTO { mapped := ToUserBaseDTO(e.CreatedUser) createdUser = &mapped } - - var projectFlock *ProjectFlockDTO - if e.ProjectFlock.Id != 0 { - mapped := ToProjectFlockDTO(e.ProjectFlock) - projectFlock = &mapped + var pfk *ProjectFlockKandangDTO + if e.ProjectFlockKandang.Id != 0 { + mapped := ToProjectFlockKandangDTO(e.ProjectFlockKandang) + pfk = &mapped } - return ChickinListDTO{ - ChickinBaseDTO: ToChickinBaseDTO(e), - ProjectFlock: projectFlock, - CreatedAt: e.CreatedAt, - UpdatedAt: e.UpdatedAt, - CreatedUser: createdUser, + ChickinBaseDTO: ToChickinBaseDTO(e), + ProjectFlockKandang: pfk, + CreatedUser: createdUser, + CreatedAt: e.CreatedAt, + UpdatedAt: e.UpdatedAt, } } @@ -126,10 +256,3 @@ func ToChickinDetailDTO(e entity.ProjectChickin) ChickinDetailDTO { ChickinListDTO: ToChickinListDTO(e), } } - -func ToUserBaseDTO(e entity.User) UserBaseDTO { - return UserBaseDTO{ - Id: e.Id, - Name: e.Name, - } -} diff --git a/internal/modules/production/chickins/module.go b/internal/modules/production/chickins/module.go index 30146724..abfc56ca 100644 --- a/internal/modules/production/chickins/module.go +++ b/internal/modules/production/chickins/module.go @@ -25,12 +25,13 @@ func (ChickinModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate * kandangRepo := rKandang.NewKandangRepository(db) auditlogrepo := rAuditLog.NewAuditLogRepository(db) warehouseRepo := rWarehouse.NewWarehouseRepository(db) + projectflockkandangrepo := rProjectFlock.NewProjectFlockKandangRepository(db) projectFlockRepo := rProjectFlock.NewProjectflockRepository(db) productWarehouseRepo := rProductWarehouse.NewProductWarehouseRepository(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) ChickinRoutes(router, userService, chickinService) diff --git a/internal/modules/production/chickins/services/chickin.service.go b/internal/modules/production/chickins/services/chickin.service.go index 75dc0242..fbb692fa 100644 --- a/internal/modules/production/chickins/services/chickin.service.go +++ b/internal/modules/production/chickins/services/chickin.service.go @@ -8,6 +8,7 @@ import ( 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" @@ -30,34 +31,41 @@ type ChickinService interface { } 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 + 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 } -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{ - Log: utils.Log, - Validate: validate, - Repository: repo, - KandangRepo: kandangRepo, - WarehouseRepo: warehouseRepo, - ProductWarehouseRepo: productWarehouseRepo, - ProjectFlockRepo: projectFlockRepo, - AuditLogRepo: auditLogRepo, + Log: utils.Log, + Validate: validate, + Repository: repo, + KandangRepo: kandangRepo, + WarehouseRepo: warehouseRepo, + ProductWarehouseRepo: productWarehouseRepo, + ProjectFlockRepo: projectFlockRepo, + AuditLogRepo: auditLogRepo, + ProjectflockKandangRepo: projectflockkandangRepo, } } func (s chickinService) withRelations(db *gorm.DB) *gorm.DB { return db. Preload("CreatedUser"). - Preload("ProjectFlock"). - Preload("ProjectFlock.ProductCategory") + Preload("ProjectFlockKandang.Kandang"). + 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 - kandang, err := s.KandangRepo.GetFirstByProjectFlockID(c.Context(), 1) + projectflockkandang, err := s.ProjectflockKandangRepo.GetByID(c.Context(), 1) 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 } // 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 { s.Log.Errorf("Failed to get warehouse: %+v", err) return nil, err } + // getprojectflock id with relation projectFlock, err := s.ProjectFlockRepo.GetByID( c.Context(), - req.ProjectFlockId, + 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") @@ -153,11 +163,11 @@ func (s *chickinService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entit } newChickin := &entity.ProjectChickin{ - ProjectFlocId: req.ProjectFlockId, - ChickInDate: chickinDate, - Quantity: productWarehouse.Quantity, - Note: "", - CreatedBy: 1, //todo: ganti dengan + ProjectFlocKandangId: projectflockkandang.ProjectFlockId, + ChickInDate: chickinDate, + Quantity: productWarehouse.Quantity, + Note: "", + CreatedBy: 1, //todo: ganti dengan } 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{ EntityType: entity.EntityTypeProjectFlockKandang, ReservedQuantity: productWarehouse.Quantity, - EntityId: req.ProjectFlockId, //todo: nanti pakek projct flock kandang id + EntityId: projectflockkandang.Id, 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) { @@ -276,7 +287,7 @@ func (s *chickinService) Approve(c *fiber.Ctx, id uint) error { // get stock avaibility untuk di update var stockAvailability entity.StockAvailability 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 if err != nil { 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 } - - return nil } diff --git a/internal/modules/production/chickins/validations/chickin.validation.go b/internal/modules/production/chickins/validations/chickin.validation.go index 152b3f22..b57950b0 100644 --- a/internal/modules/production/chickins/validations/chickin.validation.go +++ b/internal/modules/production/chickins/validations/chickin.validation.go @@ -1,8 +1,8 @@ package validation type Create struct { - ProjectFlockId uint `json:"project_flock_id" validate:"required,number,min=1"` - ChickInDate string `json:"chick_in_date" validate:"required,datetime=2006-01-02"` + 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 { diff --git a/internal/modules/production/project_flocks/repositories/projectflock_kandang.repository.go b/internal/modules/production/project_flocks/repositories/projectflock_kandang.repository.go index 9b89a399..9999e1a8 100644 --- a/internal/modules/production/project_flocks/repositories/projectflock_kandang.repository.go +++ b/internal/modules/production/project_flocks/repositories/projectflock_kandang.repository.go @@ -9,6 +9,7 @@ import ( ) type ProjectFlockKandangRepository interface { + GetByID(ctx context.Context, id uint) (*entity.ProjectFlockKandang, error) CreateMany(ctx context.Context, records []*entity.ProjectFlockKandang) error MarkDetached(ctx context.Context, projectFlockID uint, kandangIDs []uint, detachedAt time.Time) 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 { 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 +}