Init Eslogad
This commit is contained in:
@@ -0,0 +1,172 @@
|
||||
package transformer
|
||||
|
||||
import (
|
||||
"eslogad-be/internal/contract"
|
||||
"eslogad-be/internal/entities"
|
||||
"math"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func PaginationToRequest(page, limit int) (int, int) {
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
if limit < 1 {
|
||||
limit = 10
|
||||
}
|
||||
if limit > 100 {
|
||||
limit = 100
|
||||
}
|
||||
return page, limit
|
||||
}
|
||||
|
||||
func CreatePaginationResponse(totalCount, page, limit int) contract.PaginationResponse {
|
||||
totalPages := int(math.Ceil(float64(totalCount) / float64(limit)))
|
||||
if totalPages < 1 {
|
||||
totalPages = 1
|
||||
}
|
||||
|
||||
return contract.PaginationResponse{
|
||||
TotalCount: totalCount,
|
||||
Page: page,
|
||||
Limit: limit,
|
||||
TotalPages: totalPages,
|
||||
}
|
||||
}
|
||||
|
||||
func CreateListUsersResponse(users []contract.UserResponse, totalCount, page, limit int) *contract.ListUsersResponse {
|
||||
pagination := CreatePaginationResponse(totalCount, page, limit)
|
||||
return &contract.ListUsersResponse{
|
||||
Users: users,
|
||||
Pagination: pagination,
|
||||
}
|
||||
}
|
||||
|
||||
func CreateErrorResponse(message string, code int) *contract.ErrorResponse {
|
||||
return &contract.ErrorResponse{
|
||||
Error: "error",
|
||||
Message: message,
|
||||
Code: code,
|
||||
}
|
||||
}
|
||||
|
||||
func CreateValidationErrorResponse(message string, details map[string]string) *contract.ValidationErrorResponse {
|
||||
return &contract.ValidationErrorResponse{
|
||||
Error: "validation_error",
|
||||
Message: message,
|
||||
Details: details,
|
||||
Code: 400,
|
||||
}
|
||||
}
|
||||
|
||||
func CreateSuccessResponse(message string, data interface{}) *contract.SuccessResponse {
|
||||
return &contract.SuccessResponse{
|
||||
Message: message,
|
||||
Data: data,
|
||||
}
|
||||
}
|
||||
|
||||
func RolesToContract(roles []entities.Role) []contract.RoleResponse {
|
||||
if roles == nil {
|
||||
return nil
|
||||
}
|
||||
res := make([]contract.RoleResponse, 0, len(roles))
|
||||
for _, r := range roles {
|
||||
res = append(res, contract.RoleResponse{ID: r.ID, Name: r.Name, Code: r.Code})
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func PositionsToContract(positions []entities.Position) []contract.PositionResponse {
|
||||
if positions == nil {
|
||||
return nil
|
||||
}
|
||||
res := make([]contract.PositionResponse, 0, len(positions))
|
||||
for _, p := range positions {
|
||||
res = append(res, contract.PositionResponse{ID: p.ID, Name: p.Name, Code: p.Code, Path: p.Path})
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func ProfileEntityToContract(p *entities.UserProfile) *contract.UserProfileResponse {
|
||||
if p == nil {
|
||||
return nil
|
||||
}
|
||||
return &contract.UserProfileResponse{
|
||||
UserID: p.UserID,
|
||||
FullName: p.FullName,
|
||||
DisplayName: p.DisplayName,
|
||||
Phone: p.Phone,
|
||||
AvatarURL: p.AvatarURL,
|
||||
JobTitle: p.JobTitle,
|
||||
EmployeeNo: p.EmployeeNo,
|
||||
Bio: p.Bio,
|
||||
Timezone: p.Timezone,
|
||||
Locale: p.Locale,
|
||||
Preferences: map[string]interface{}(p.Preferences),
|
||||
NotificationPrefs: map[string]interface{}(p.NotificationPrefs),
|
||||
LastSeenAt: p.LastSeenAt,
|
||||
CreatedAt: p.CreatedAt,
|
||||
UpdatedAt: p.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func ProfileUpdateToEntity(userID uuid.UUID, req *contract.UpdateUserProfileRequest, existing *entities.UserProfile) *entities.UserProfile {
|
||||
prof := &entities.UserProfile{}
|
||||
if existing != nil {
|
||||
*prof = *existing
|
||||
} else {
|
||||
prof.UserID = userID
|
||||
}
|
||||
if req.FullName != nil {
|
||||
prof.FullName = *req.FullName
|
||||
}
|
||||
if req.DisplayName != nil {
|
||||
prof.DisplayName = req.DisplayName
|
||||
}
|
||||
if req.Phone != nil {
|
||||
prof.Phone = req.Phone
|
||||
}
|
||||
if req.AvatarURL != nil {
|
||||
prof.AvatarURL = req.AvatarURL
|
||||
}
|
||||
if req.JobTitle != nil {
|
||||
prof.JobTitle = req.JobTitle
|
||||
}
|
||||
if req.EmployeeNo != nil {
|
||||
prof.EmployeeNo = req.EmployeeNo
|
||||
}
|
||||
if req.Bio != nil {
|
||||
prof.Bio = req.Bio
|
||||
}
|
||||
if req.Timezone != nil {
|
||||
prof.Timezone = *req.Timezone
|
||||
}
|
||||
if req.Locale != nil {
|
||||
prof.Locale = *req.Locale
|
||||
}
|
||||
if req.Preferences != nil {
|
||||
prof.Preferences = entities.JSONB(*req.Preferences)
|
||||
}
|
||||
if req.NotificationPrefs != nil {
|
||||
prof.NotificationPrefs = entities.JSONB(*req.NotificationPrefs)
|
||||
}
|
||||
return prof
|
||||
}
|
||||
|
||||
func TitlesToContract(titles []entities.Title) []contract.TitleResponse {
|
||||
if titles == nil {
|
||||
return nil
|
||||
}
|
||||
out := make([]contract.TitleResponse, 0, len(titles))
|
||||
for _, t := range titles {
|
||||
out = append(out, contract.TitleResponse{
|
||||
ID: t.ID,
|
||||
Name: t.Name,
|
||||
Code: t.Code,
|
||||
Description: t.Description,
|
||||
})
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package transformer
|
||||
|
||||
import (
|
||||
"eslogad-be/internal/contract"
|
||||
"eslogad-be/internal/entities"
|
||||
)
|
||||
|
||||
func CreateUserRequestToEntity(req *contract.CreateUserRequest, passwordHash string) *entities.User {
|
||||
if req == nil {
|
||||
return nil
|
||||
}
|
||||
return &entities.User{
|
||||
Name: req.Name,
|
||||
Email: req.Email,
|
||||
PasswordHash: passwordHash,
|
||||
IsActive: true,
|
||||
}
|
||||
}
|
||||
|
||||
func UpdateUserEntity(existing *entities.User, req *contract.UpdateUserRequest) *entities.User {
|
||||
if existing == nil || req == nil {
|
||||
return existing
|
||||
}
|
||||
if req.Name != nil {
|
||||
existing.Name = *req.Name
|
||||
}
|
||||
if req.Email != nil {
|
||||
existing.Email = *req.Email
|
||||
}
|
||||
if req.IsActive != nil {
|
||||
existing.IsActive = *req.IsActive
|
||||
}
|
||||
return existing
|
||||
}
|
||||
|
||||
func EntityToContract(user *entities.User) *contract.UserResponse {
|
||||
if user == nil {
|
||||
return nil
|
||||
}
|
||||
return &contract.UserResponse{
|
||||
ID: user.ID,
|
||||
Name: user.Name,
|
||||
Email: user.Email,
|
||||
IsActive: user.IsActive,
|
||||
CreatedAt: user.CreatedAt,
|
||||
UpdatedAt: user.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func EntitiesToContracts(users []*entities.User) []contract.UserResponse {
|
||||
if users == nil {
|
||||
return nil
|
||||
}
|
||||
responses := make([]contract.UserResponse, len(users))
|
||||
for i, u := range users {
|
||||
resp := EntityToContract(u)
|
||||
if resp != nil {
|
||||
responses[i] = *resp
|
||||
}
|
||||
}
|
||||
return responses
|
||||
}
|
||||
Reference in New Issue
Block a user