feat(user): Enhance user model to include client information and preload client data in queries
Deploy Application / deploy (push) Successful in 22s
Details
Deploy Application / deploy (push) Successful in 22s
Details
This commit is contained in:
parent
f2031c8496
commit
5d27d4f3bd
|
|
@ -9,9 +9,11 @@ import (
|
|||
"github.com/Caknoooo/go-gin-clean-starter/modules/user/query"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/modules/user/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"
|
||||
"github.com/google/uuid"
|
||||
"github.com/samber/do"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
|
@ -354,11 +356,17 @@ func ToUserResponse(user query.M_User) dto.UserResponse {
|
|||
var roles []dto.UserRolesResponse
|
||||
for _, role := range user.Roles {
|
||||
roles = append(roles, dto.UserRolesResponse{
|
||||
ID: role.ID.String(), // pastikan role.ID bertipe uuid/atau string
|
||||
ID: role.ID.String(),
|
||||
Name: role.Name,
|
||||
})
|
||||
}
|
||||
|
||||
client := pkgdto.IdNameResponse{}
|
||||
if user.Client.ID != uuid.Nil {
|
||||
client.ID = user.Client.ID.String()
|
||||
client.Name = user.Client.Name
|
||||
}
|
||||
|
||||
return dto.UserResponse{
|
||||
ID: user.ID,
|
||||
Name: user.Name,
|
||||
|
|
@ -369,6 +377,7 @@ func ToUserResponse(user query.M_User) dto.UserResponse {
|
|||
Email: user.Email,
|
||||
PhotoUrl: user.PhotoUrl,
|
||||
Roles: roles,
|
||||
Client: client,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -7,15 +7,17 @@ import (
|
|||
)
|
||||
|
||||
type M_User struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
Gender string `json:"gender"`
|
||||
Address string `json:"address"`
|
||||
Phone string `json:"phone"`
|
||||
Email string `json:"email"`
|
||||
PhotoUrl string `json:"photo_url"`
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
Gender string `json:"gender"`
|
||||
Address string `json:"address"`
|
||||
Phone string `json:"phone"`
|
||||
Email string `json:"email"`
|
||||
PhotoUrl string `json:"photo_url"`
|
||||
ClientID string `json:"client_id"`
|
||||
Client entities.M_Client `json:"client" gorm:"foreignKey:ClientID;references:ID"`
|
||||
// Roles []entities.M_Role `json:"roles" gorm:"many2many:m_user_roles;"`
|
||||
Roles []entities.M_Role `gorm:"many2many:m_user_roles;foreignKey:ID;joinForeignKey:UserID;References:ID;JoinReferences:RoleID" json:"roles"`
|
||||
}
|
||||
|
|
@ -36,6 +38,11 @@ func (f *UserFilter) ApplyFilters(query *gorm.DB) *gorm.DB {
|
|||
query = query.Where("client_id = ?", f.ClientID)
|
||||
}
|
||||
|
||||
// add preload clients
|
||||
query = query.Model(&M_User{}).Preload("Client", func(db *gorm.DB) *gorm.DB {
|
||||
return db.Select("id", "name")
|
||||
})
|
||||
|
||||
// Manual preload untuk roles dengan field terbatas
|
||||
// for _, include := range f.Includes {
|
||||
// if include == "Roles" {
|
||||
|
|
|
|||
Loading…
Reference in New Issue