mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
adjust api get all employees
This commit is contained in:
@@ -29,10 +29,27 @@ func (u *EmployeesController) GetAll(c *fiber.Ctx) error {
|
||||
Search: c.Query("search", ""),
|
||||
}
|
||||
|
||||
if query.Page < 1 || query.Limit < 1 {
|
||||
if query.Page < 1 || query.Limit < 1 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0")
|
||||
}
|
||||
|
||||
if kandangParam := c.Query("kandang_id", ""); kandangParam != "" {
|
||||
id, err := strconv.Atoi(kandangParam)
|
||||
if err != nil || id <= 0 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "invalid kandang_id")
|
||||
}
|
||||
temp := uint(id)
|
||||
query.KandangId = &temp
|
||||
}
|
||||
|
||||
if activeParam := c.Query("is_active", ""); activeParam != "" {
|
||||
value, err := strconv.ParseBool(activeParam)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "invalid is_active value")
|
||||
}
|
||||
query.IsActive = &value
|
||||
}
|
||||
|
||||
result, totalResults, err := u.EmployeesService.GetAll(c, query)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -41,10 +41,10 @@ func NewEmployeesService(repo repository.EmployeesRepository, validate *validato
|
||||
|
||||
func (s employeesService) withRelations(db *gorm.DB) *gorm.DB {
|
||||
return db.
|
||||
Preload("EmployeeKandangs.Kandang").
|
||||
Preload("EmployeeKandangs.Kandang.Location").
|
||||
Preload("EmployeeKandangs.Kandang.Pic").
|
||||
Preload("EmployeeKandangs.Kandang.CreatedUser")
|
||||
Preload("EmployeeKandangs.Kandang")
|
||||
// Preload("EmployeeKandangs.Kandang.Location").
|
||||
// Preload("EmployeeKandangs.Kandang.Pic").
|
||||
// Preload("EmployeeKandangs.Kandang.CreatedUser")
|
||||
}
|
||||
|
||||
func (s employeesService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.Employees, int64, error) {
|
||||
@@ -57,9 +57,16 @@ func (s employeesService) GetAll(c *fiber.Ctx, params *validation.Query) ([]enti
|
||||
employeess, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
|
||||
db = s.withRelations(db)
|
||||
if params.Search != "" {
|
||||
return db.Where("name LIKE ?", "%"+params.Search+"%")
|
||||
db = db.Where("employees.name LIKE ?", "%"+params.Search+"%")
|
||||
}
|
||||
return db.Order("created_at DESC").Order("updated_at DESC")
|
||||
if params.KandangId != nil {
|
||||
db = db.Joins("JOIN employee_kandangs ek ON ek.employee_id = employees.id").
|
||||
Where("ek.kandang_id = ?", *params.KandangId)
|
||||
}
|
||||
if params.IsActive != nil {
|
||||
db = db.Where("employees.is_active = ?", *params.IsActive)
|
||||
}
|
||||
return db.Order("employees.created_at DESC").Order("employees.updated_at DESC")
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
|
||||
@@ -13,7 +13,9 @@ 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"`
|
||||
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"`
|
||||
KandangId *uint `query:"kandang_id" validate:"omitempty"`
|
||||
IsActive *bool `query:"is_active" validate:"omitempty"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user