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/query"
|
||||||
"github.com/Caknoooo/go-gin-clean-starter/modules/user/service"
|
"github.com/Caknoooo/go-gin-clean-starter/modules/user/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"
|
||||||
|
"github.com/google/uuid"
|
||||||
"github.com/samber/do"
|
"github.com/samber/do"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
@ -354,11 +356,17 @@ func ToUserResponse(user query.M_User) dto.UserResponse {
|
||||||
var roles []dto.UserRolesResponse
|
var roles []dto.UserRolesResponse
|
||||||
for _, role := range user.Roles {
|
for _, role := range user.Roles {
|
||||||
roles = append(roles, dto.UserRolesResponse{
|
roles = append(roles, dto.UserRolesResponse{
|
||||||
ID: role.ID.String(), // pastikan role.ID bertipe uuid/atau string
|
ID: role.ID.String(),
|
||||||
Name: role.Name,
|
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{
|
return dto.UserResponse{
|
||||||
ID: user.ID,
|
ID: user.ID,
|
||||||
Name: user.Name,
|
Name: user.Name,
|
||||||
|
|
@ -369,6 +377,7 @@ func ToUserResponse(user query.M_User) dto.UserResponse {
|
||||||
Email: user.Email,
|
Email: user.Email,
|
||||||
PhotoUrl: user.PhotoUrl,
|
PhotoUrl: user.PhotoUrl,
|
||||||
Roles: roles,
|
Roles: roles,
|
||||||
|
Client: client,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,15 +7,17 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type M_User struct {
|
type M_User struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Username string `json:"username"`
|
Username string `json:"username"`
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
Gender string `json:"gender"`
|
Gender string `json:"gender"`
|
||||||
Address string `json:"address"`
|
Address string `json:"address"`
|
||||||
Phone string `json:"phone"`
|
Phone string `json:"phone"`
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
PhotoUrl string `json:"photo_url"`
|
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 `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"`
|
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)
|
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
|
// Manual preload untuk roles dengan field terbatas
|
||||||
// for _, include := range f.Includes {
|
// for _, include := range f.Includes {
|
||||||
// if include == "Roles" {
|
// if include == "Roles" {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue