feat(maintenance_group): Enhance MaintenanceGroup structure and responses with ClientID and total role count
This commit is contained in:
parent
e3127c075d
commit
d110a31c4f
|
|
@ -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/query"
|
||||||
"github.com/Caknoooo/go-gin-clean-starter/modules/maintenance_group/service"
|
"github.com/Caknoooo/go-gin-clean-starter/modules/maintenance_group/service"
|
||||||
"github.com/Caknoooo/go-gin-clean-starter/pkg/constants"
|
"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-gin-clean-starter/pkg/utils"
|
||||||
"github.com/Caknoooo/go-pagination"
|
"github.com/Caknoooo/go-pagination"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
|
@ -152,17 +153,18 @@ func (c *maintenanceGroupController) GetById(ctx *gin.Context) {
|
||||||
// @Failure 400 {object} map[string]interface{}
|
// @Failure 400 {object} map[string]interface{}
|
||||||
// @Router /maintenance-groups [get]
|
// @Router /maintenance-groups [get]
|
||||||
func (c *maintenanceGroupController) GetAll(ctx *gin.Context) {
|
func (c *maintenanceGroupController) GetAll(ctx *gin.Context) {
|
||||||
clientId := ctx.MustGet("client_id").(string)
|
// clientId := ctx.MustGet("client_id").(string)
|
||||||
var filter = &query.MaintenanceGroupFilter{
|
var filter = &query.MaintenanceGroupFilter{
|
||||||
Name: ctx.Query("name"),
|
Name: ctx.Query("name"),
|
||||||
Code: ctx.Query("code"),
|
Code: ctx.Query("code"),
|
||||||
Description: ctx.Query("description"),
|
Description: ctx.Query("description"),
|
||||||
ClientID: clientId,
|
ClientID: ctx.Query("client_id"),
|
||||||
Includes: ctx.QueryArray("includes"),
|
Includes: ctx.QueryArray("includes"),
|
||||||
}
|
}
|
||||||
|
logrus.Info(filter.ClientID)
|
||||||
filter.BindPagination(ctx)
|
filter.BindPagination(ctx)
|
||||||
ctx.ShouldBindQuery(filter)
|
ctx.ShouldBindQuery(&filter)
|
||||||
logrus.Infof("pagination: page=%d, page_size=%d", filter.Pagination.Page, filter.Pagination.PerPage)
|
// logrus.Infof("pagination: page=%d, page_size=%d", filter.Pagination.Page, filter.Pagination.PerPage)
|
||||||
logrus.Info("1")
|
logrus.Info("1")
|
||||||
groups, total, err := pagination.PaginatedQueryWithIncludableAndOptions[query.M_MaintenanceGroup](
|
groups, total, err := pagination.PaginatedQueryWithIncludableAndOptions[query.M_MaintenanceGroup](
|
||||||
c.db,
|
c.db,
|
||||||
|
|
@ -172,8 +174,7 @@ func (c *maintenanceGroupController) GetAll(ctx *gin.Context) {
|
||||||
// Dialect: pagination.MySQL, // atau PostgreSQL jika perlu
|
// Dialect: pagination.MySQL, // atau PostgreSQL jika perlu
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
logrus.Info("total:", total, " len groups:", len(groups))
|
groupsResponse := ToMaintGroupSimpleResponses(groups)
|
||||||
logrus.Info("2")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_LIST_MG, err.Error(), nil)
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_LIST_MG, err.Error(), nil)
|
||||||
ctx.JSON(http.StatusBadRequest, res)
|
ctx.JSON(http.StatusBadRequest, res)
|
||||||
|
|
@ -183,6 +184,35 @@ func (c *maintenanceGroupController) GetAll(ctx *gin.Context) {
|
||||||
// groupResponses := dto.ToMaintGroupResponses(groups)
|
// groupResponses := dto.ToMaintGroupResponses(groups)
|
||||||
paginationResponse := pagination.CalculatePagination(filter.Pagination, total)
|
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)
|
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
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -71,12 +71,13 @@ type (
|
||||||
}
|
}
|
||||||
|
|
||||||
MaintGroupResponse struct {
|
MaintGroupResponse struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Code string `json:"code"`
|
Code string `json:"code"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
Client dto.ClientResponse `json:"client"`
|
TotalMaintenanceGroupRole int `json:"total_maintenance_group_role" binding:"omitempty"`
|
||||||
MaintenanceGroupRoles *[]MaintenanceGroupRoleResponse `json:"maintenance_group_roles"`
|
Client dto.IdNameResponse `json:"client"`
|
||||||
|
MaintenanceGroupRoles *[]MaintenanceGroupRoleResponse `json:"maintenance_group_roles"`
|
||||||
}
|
}
|
||||||
|
|
||||||
MaintenanceGroupRoleResponse struct {
|
MaintenanceGroupRoleResponse struct {
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,21 @@
|
||||||
package query
|
package query
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/Caknoooo/go-gin-clean-starter/database/entities"
|
||||||
"github.com/Caknoooo/go-pagination"
|
"github.com/Caknoooo/go-pagination"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type M_MaintenanceGroup struct {
|
type M_MaintenanceGroup struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Code string `json:"code"`
|
Code string `json:"code"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
TotalMaintenanceGroupRole int `json:"total_maintenance_group_role"`
|
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 {
|
type MaintenanceGroupFilter struct {
|
||||||
|
|
@ -23,23 +28,23 @@ type MaintenanceGroupFilter struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *MaintenanceGroupFilter) ApplyFilters(query *gorm.DB) *gorm.DB {
|
func (f *MaintenanceGroupFilter) ApplyFilters(query *gorm.DB) *gorm.DB {
|
||||||
// Apply your filters here
|
// ...existing code...
|
||||||
if f.Name != "" {
|
if f.Name != "" {
|
||||||
query = query.Where("name ILIKE ?", "%"+f.Name+"%")
|
query = query.Where("name ILIKE ?", "%"+f.Name+"%")
|
||||||
}
|
}
|
||||||
if f.ClientID != "" {
|
if f.ClientID != "" {
|
||||||
query = query.Where("client_id = ?", f.ClientID)
|
query = query.Where("client_id = ?", f.ClientID)
|
||||||
|
logrus.Infof("Filtering by ClientID: %s", f.ClientID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Manual preload untuk roles dengan field terbatas
|
// Ganti ini:
|
||||||
// for _, include := range f.Includes {
|
// query = query.Model(entities.M_MaintenanceGroup{}).Preload("Client")
|
||||||
// if include == "Roles" {
|
// Jadi:
|
||||||
// query = query.Preload("Roles", func(db *gorm.DB) *gorm.DB {
|
query = query.Model(&M_MaintenanceGroup{}).
|
||||||
// return db.Select("id", "name") // Hanya ambil id dan name
|
Preload("Client").
|
||||||
// })
|
Preload("MaintenanceGroupRoles").
|
||||||
// }
|
Preload("MaintenanceGroupRoles.Role")
|
||||||
// }
|
|
||||||
// Hitung total maintenance group role per group
|
|
||||||
query = query.Select("m_maintenance_groups.*, " +
|
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")
|
"(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")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -183,7 +183,7 @@ func (m *maintenanceGroupService) GetById(ctx context.Context, maintGroupId stri
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mapping client
|
// Mapping client
|
||||||
client := staticDto.ClientResponse{
|
client := staticDto.IdNameResponse{
|
||||||
ID: group.Client.ID.String(),
|
ID: group.Client.ID.String(),
|
||||||
Name: group.Client.Name,
|
Name: group.Client.Name,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue