feat(maintenance_group): Enhance MaintenanceGroup structure and responses with ClientID and total role count

This commit is contained in:
Habib Fatkhul Rohman 2025-11-03 14:37:58 +07:00
parent e3127c075d
commit d110a31c4f
4 changed files with 65 additions and 29 deletions

View File

@ -7,6 +7,7 @@ import (
"github.com/Caknoooo/go-gin-clean-starter/modules/maintenance_group/query"
"github.com/Caknoooo/go-gin-clean-starter/modules/maintenance_group/service"
"github.com/Caknoooo/go-gin-clean-starter/pkg/constants"
pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto"
"github.com/Caknoooo/go-gin-clean-starter/pkg/utils"
"github.com/Caknoooo/go-pagination"
"github.com/gin-gonic/gin"
@ -152,17 +153,18 @@ func (c *maintenanceGroupController) GetById(ctx *gin.Context) {
// @Failure 400 {object} map[string]interface{}
// @Router /maintenance-groups [get]
func (c *maintenanceGroupController) GetAll(ctx *gin.Context) {
clientId := ctx.MustGet("client_id").(string)
// clientId := ctx.MustGet("client_id").(string)
var filter = &query.MaintenanceGroupFilter{
Name: ctx.Query("name"),
Code: ctx.Query("code"),
Description: ctx.Query("description"),
ClientID: clientId,
ClientID: ctx.Query("client_id"),
Includes: ctx.QueryArray("includes"),
}
logrus.Info(filter.ClientID)
filter.BindPagination(ctx)
ctx.ShouldBindQuery(filter)
logrus.Infof("pagination: page=%d, page_size=%d", filter.Pagination.Page, filter.Pagination.PerPage)
ctx.ShouldBindQuery(&filter)
// logrus.Infof("pagination: page=%d, page_size=%d", filter.Pagination.Page, filter.Pagination.PerPage)
logrus.Info("1")
groups, total, err := pagination.PaginatedQueryWithIncludableAndOptions[query.M_MaintenanceGroup](
c.db,
@ -172,8 +174,7 @@ func (c *maintenanceGroupController) GetAll(ctx *gin.Context) {
// Dialect: pagination.MySQL, // atau PostgreSQL jika perlu
},
)
logrus.Info("total:", total, " len groups:", len(groups))
logrus.Info("2")
groupsResponse := ToMaintGroupSimpleResponses(groups)
if err != nil {
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_LIST_MG, err.Error(), nil)
ctx.JSON(http.StatusBadRequest, res)
@ -183,6 +184,35 @@ func (c *maintenanceGroupController) GetAll(ctx *gin.Context) {
// groupResponses := dto.ToMaintGroupResponses(groups)
paginationResponse := pagination.CalculatePagination(filter.Pagination, total)
response := pagination.NewPaginatedResponse(http.StatusOK, dto.MESSAGE_SUCCESS_GET_LIST_MG, groups, paginationResponse)
response := pagination.NewPaginatedResponse(http.StatusOK, dto.MESSAGE_SUCCESS_GET_LIST_MG, groupsResponse, paginationResponse)
ctx.JSON(http.StatusOK, response)
}
func ToMaintGroupSimpleResponses(groups []query.M_MaintenanceGroup) []dto.MaintGroupResponse {
res := make([]dto.MaintGroupResponse, 0, len(groups))
for _, g := range groups {
roles := make([]dto.MaintenanceGroupRoleResponse, 0, len(g.MaintenanceGroupRoles))
for _, r := range g.MaintenanceGroupRoles {
roles = append(roles, dto.MaintenanceGroupRoleResponse{
ID: r.ID.String(),
Level: r.Level,
Role: pkgdto.RoleResponse{
ID: r.Role.ID.String(),
Name: r.Role.Name,
}})
}
res = append(res, dto.MaintGroupResponse{
ID: g.ID,
Name: g.Name,
Code: g.Code,
Description: g.Description,
TotalMaintenanceGroupRole: g.TotalMaintenanceGroupRole,
Client: pkgdto.IdNameResponse{
ID: g.Client.ID.String(),
Name: g.Client.Name,
},
MaintenanceGroupRoles: &roles,
})
}
return res
}

View File

@ -75,7 +75,8 @@ type (
Code string `json:"code"`
Name string `json:"name"`
Description string `json:"description"`
Client dto.ClientResponse `json:"client"`
TotalMaintenanceGroupRole int `json:"total_maintenance_group_role" binding:"omitempty"`
Client dto.IdNameResponse `json:"client"`
MaintenanceGroupRoles *[]MaintenanceGroupRoleResponse `json:"maintenance_group_roles"`
}

View File

@ -1,7 +1,9 @@
package query
import (
"github.com/Caknoooo/go-gin-clean-starter/database/entities"
"github.com/Caknoooo/go-pagination"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
)
@ -11,6 +13,9 @@ type M_MaintenanceGroup struct {
Code string `json:"code"`
Description string `json:"description"`
TotalMaintenanceGroupRole int `json:"total_maintenance_group_role"`
ClientID string `json:"client_id"` // <-- Add this line
Client entities.M_Client `gorm:"foreignKey:ClientID;references:ID" json:"client"`
MaintenanceGroupRoles []entities.M_MaintenanceGroupRole `gorm:"foreignKey:MaintenanceGroupID;references:ID" json:"maintenance_group_roles"`
}
type MaintenanceGroupFilter struct {
@ -23,23 +28,23 @@ type MaintenanceGroupFilter struct {
}
func (f *MaintenanceGroupFilter) ApplyFilters(query *gorm.DB) *gorm.DB {
// Apply your filters here
// ...existing code...
if f.Name != "" {
query = query.Where("name ILIKE ?", "%"+f.Name+"%")
}
if f.ClientID != "" {
query = query.Where("client_id = ?", f.ClientID)
logrus.Infof("Filtering by ClientID: %s", f.ClientID)
}
// Manual preload untuk roles dengan field terbatas
// for _, include := range f.Includes {
// if include == "Roles" {
// query = query.Preload("Roles", func(db *gorm.DB) *gorm.DB {
// return db.Select("id", "name") // Hanya ambil id dan name
// })
// }
// }
// Hitung total maintenance group role per group
// Ganti ini:
// query = query.Model(entities.M_MaintenanceGroup{}).Preload("Client")
// Jadi:
query = query.Model(&M_MaintenanceGroup{}).
Preload("Client").
Preload("MaintenanceGroupRoles").
Preload("MaintenanceGroupRoles.Role")
query = query.Select("m_maintenance_groups.*, " +
"(SELECT COUNT(*) FROM m_maintenance_group_roles mgr WHERE mgr.maintenance_group_id = m_maintenance_groups.id AND mgr.deleted_at IS NULL) as total_maintenance_group_role")

View File

@ -183,7 +183,7 @@ func (m *maintenanceGroupService) GetById(ctx context.Context, maintGroupId stri
}
// Mapping client
client := staticDto.ClientResponse{
client := staticDto.IdNameResponse{
ID: group.Client.ID.String(),
Name: group.Client.Name,
}