mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-25 07:45:44 +00:00
adjust api get all project flock kandang with periode
This commit is contained in:
+37
-10
@@ -24,22 +24,49 @@ func NewProjectFlockKandangController(projectFlockKandangService service.Project
|
||||
|
||||
func (u *ProjectFlockKandangController) GetAll(c *fiber.Ctx) error {
|
||||
query := &validation.Query{
|
||||
Page: c.QueryInt("page", 1),
|
||||
Limit: c.QueryInt("limit", 10),
|
||||
Search: c.Query("search", ""),
|
||||
ProjectFlockId: uint(c.QueryInt("project_flock_id", 0)),
|
||||
KandangId: uint(c.QueryInt("kandang_id", 0)),
|
||||
Category: c.Query("category", ""),
|
||||
AreaId: uint(c.QueryInt("area_id", 0)),
|
||||
SortBy: c.Query("sort_by", ""),
|
||||
SortOrder: c.Query("sort_order", ""),
|
||||
StepName: c.Query("step_name", ""),
|
||||
Page: c.QueryInt("page", 1),
|
||||
Limit: c.QueryInt("limit", 10),
|
||||
Search: c.Query("search", ""),
|
||||
NameWithPeriode: c.QueryBool("name_with_periode", false),
|
||||
ProjectFlockId: uint(c.QueryInt("project_flock_id", 0)),
|
||||
KandangId: uint(c.QueryInt("kandang_id", 0)),
|
||||
Category: c.Query("category", ""),
|
||||
AreaId: uint(c.QueryInt("area_id", 0)),
|
||||
SortBy: c.Query("sort_by", ""),
|
||||
SortOrder: c.Query("sort_order", ""),
|
||||
StepName: c.Query("step_name", ""),
|
||||
}
|
||||
|
||||
if query.Page < 1 || query.Limit < 1 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0")
|
||||
}
|
||||
|
||||
if query.NameWithPeriode {
|
||||
results, totalResults, err := u.ProjectFlockKandangService.GetAllNameWithPeriode(c, query)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
data := make([]dto.ProjectFlockKandangNameWithPeriodDTO, 0, len(results))
|
||||
for _, result := range results {
|
||||
data = append(data, dto.ToProjectFlockKandangNameWithPeriodDTOValues(result.Id, result.KandangName, result.Period))
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.SuccessWithPaginate[dto.ProjectFlockKandangNameWithPeriodDTO]{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Get all projectFlockKandangs successfully",
|
||||
Meta: response.Meta{
|
||||
Page: query.Page,
|
||||
Limit: query.Limit,
|
||||
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
|
||||
TotalResults: totalResults,
|
||||
},
|
||||
Data: data,
|
||||
})
|
||||
}
|
||||
|
||||
results, totalResults, err := u.ProjectFlockKandangService.GetAll(c, query)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
+26
-3
@@ -60,6 +60,11 @@ type ProjectFlockKandangListDTO struct {
|
||||
ChickinApproval *approvalDTO.ApprovalRelationDTO `json:"chickin_approval,omitempty"`
|
||||
}
|
||||
|
||||
type ProjectFlockKandangNameWithPeriodDTO struct {
|
||||
Id uint `json:"id"`
|
||||
NameWithPeriod string `json:"name_with_period"`
|
||||
}
|
||||
|
||||
type ProjectFlockKandangDetailDTO struct {
|
||||
ProjectFlockKandangListDTO
|
||||
Chickins []chickinDTO.ChickinRelationDTO `json:"chickins,omitempty"`
|
||||
@@ -129,13 +134,17 @@ func toKandangRelation(kandang entity.Kandang) *kandangDTO.KandangRelationDTO {
|
||||
}
|
||||
|
||||
func toNameWithPeriod(kandang entity.Kandang, period int) string {
|
||||
if kandang.Name == "" {
|
||||
return toNameWithPeriodValue(kandang.Name, period)
|
||||
}
|
||||
|
||||
func toNameWithPeriodValue(kandangName string, period int) string {
|
||||
if kandangName == "" {
|
||||
return ""
|
||||
}
|
||||
if period == 0 {
|
||||
return kandang.Name
|
||||
return kandangName
|
||||
}
|
||||
return kandang.Name + " Period " + strconv.Itoa(period)
|
||||
return kandangName + " Period " + strconv.Itoa(period)
|
||||
}
|
||||
|
||||
func toApprovalDTOSelector(
|
||||
@@ -167,6 +176,20 @@ func ToProjectFlockKandangListDTO(e entity.ProjectFlockKandang) ProjectFlockKand
|
||||
}
|
||||
}
|
||||
|
||||
func ToProjectFlockKandangNameWithPeriodDTO(e entity.ProjectFlockKandang) ProjectFlockKandangNameWithPeriodDTO {
|
||||
return ProjectFlockKandangNameWithPeriodDTO{
|
||||
Id: e.Id,
|
||||
NameWithPeriod: toNameWithPeriod(e.Kandang, e.Period),
|
||||
}
|
||||
}
|
||||
|
||||
func ToProjectFlockKandangNameWithPeriodDTOValues(id uint, kandangName string, period int) ProjectFlockKandangNameWithPeriodDTO {
|
||||
return ProjectFlockKandangNameWithPeriodDTO{
|
||||
Id: id,
|
||||
NameWithPeriod: toNameWithPeriodValue(kandangName, period),
|
||||
}
|
||||
}
|
||||
|
||||
func toCreatedUserDTO(pf entity.ProjectFlock) *userDTO.UserRelationDTO {
|
||||
if pf.CreatedUser.Id != 0 {
|
||||
mapped := userDTO.ToUserRelationDTO(pf.CreatedUser)
|
||||
|
||||
+37
@@ -26,6 +26,7 @@ import (
|
||||
|
||||
type ProjectFlockKandangService interface {
|
||||
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.ProjectFlockKandang, int64, error)
|
||||
GetAllNameWithPeriode(ctx *fiber.Ctx, params *validation.Query) ([]ProjectFlockKandangNameWithPeriode, int64, error)
|
||||
GetOne(ctx *fiber.Ctx, id uint) (*entity.ProjectFlockKandang, map[uint]float64, []entity.ProductWarehouse, error)
|
||||
CheckClosing(ctx *fiber.Ctx, id uint) (*ClosingCheckResult, error)
|
||||
Closing(ctx *fiber.Ctx, id uint, req *validation.Closing) (*entity.ProjectFlockKandang, error)
|
||||
@@ -51,6 +52,12 @@ type ClosingCheckResult struct {
|
||||
Expenses []ExpenseSummary `json:"expenses"`
|
||||
}
|
||||
|
||||
type ProjectFlockKandangNameWithPeriode struct {
|
||||
Id uint
|
||||
KandangName string
|
||||
Period int
|
||||
}
|
||||
|
||||
type StockRemainingDetail struct {
|
||||
FlagName string `json:"flag_name"`
|
||||
ProductWarehouseId uint `json:"product_warehouse_id"`
|
||||
@@ -133,6 +140,36 @@ func (s projectFlockKandangService) GetAll(c *fiber.Ctx, params *validation.Quer
|
||||
return projectFlockKandangs, total, nil
|
||||
}
|
||||
|
||||
func (s projectFlockKandangService) GetAllNameWithPeriode(c *fiber.Ctx, params *validation.Query) ([]ProjectFlockKandangNameWithPeriode, int64, error) {
|
||||
if err := s.Validate.Struct(params); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
scope, err := m.ResolveLocationScope(c, s.Repository.DB())
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
offset := (params.Page - 1) * params.Limit
|
||||
|
||||
rows, total, err := s.Repository.GetAllNameWithPeriodeScoped(c.Context(), offset, params.Limit, params, scope.IDs, scope.Restrict)
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to get projectFlockKandangs name_with_periode: %+v", err)
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
results := make([]ProjectFlockKandangNameWithPeriode, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
results = append(results, ProjectFlockKandangNameWithPeriode{
|
||||
Id: row.Id,
|
||||
KandangName: row.KandangName,
|
||||
Period: row.Period,
|
||||
})
|
||||
}
|
||||
|
||||
return results, total, nil
|
||||
}
|
||||
|
||||
func (s projectFlockKandangService) GetOne(c *fiber.Ctx, id uint) (*entity.ProjectFlockKandang, map[uint]float64, []entity.ProductWarehouse, error) {
|
||||
scope, err := m.ResolveLocationScope(c, s.Repository.DB())
|
||||
if err != nil {
|
||||
|
||||
+12
-11
@@ -11,19 +11,20 @@ type Update struct {
|
||||
}
|
||||
|
||||
type Query struct {
|
||||
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"`
|
||||
Search string `query:"search" validate:"omitempty,max=50"`
|
||||
ProjectFlockId uint `query:"project_flock_id" validate:"omitempty"`
|
||||
KandangId uint `query:"kandang_id" validate:"omitempty"`
|
||||
Category string `query:"category" validate:"omitempty,oneof=Growing Laying"`
|
||||
AreaId uint `query:"area_id" validate:"omitempty"`
|
||||
SortBy string `query:"sort_by" validate:"omitempty,oneof=created_at period"`
|
||||
SortOrder string `query:"sort_order" validate:"omitempty,oneof=ASC DESC"`
|
||||
StepName string `query:"step_name" validate:"omitempty,max=50"`
|
||||
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
|
||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"`
|
||||
Search string `query:"search" validate:"omitempty,max=50"`
|
||||
NameWithPeriode bool `query:"name_with_periode"`
|
||||
ProjectFlockId uint `query:"project_flock_id" validate:"omitempty"`
|
||||
KandangId uint `query:"kandang_id" validate:"omitempty"`
|
||||
Category string `query:"category" validate:"omitempty,oneof=Growing Laying"`
|
||||
AreaId uint `query:"area_id" validate:"omitempty"`
|
||||
SortBy string `query:"sort_by" validate:"omitempty,oneof=created_at period"`
|
||||
SortOrder string `query:"sort_order" validate:"omitempty,oneof=ASC DESC"`
|
||||
StepName string `query:"step_name" validate:"omitempty,max=50"`
|
||||
}
|
||||
|
||||
type Closing struct {
|
||||
Action string `json:"action" validate:"required,oneof=close unclose"`
|
||||
ClosedDate *string `json:"closed_date,omitempty"`
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user