This commit is contained in:
27
internal/models/profile.go
Normal file
27
internal/models/profile.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package models
|
||||
|
||||
import "history-api/internal/dtos/response"
|
||||
|
||||
type UserProfileSimple struct {
|
||||
DisplayName string `json:"display_name"`
|
||||
FullName string `json:"full_name"`
|
||||
AvatarUrl string `json:"avatar_url"`
|
||||
Bio string `json:"bio"`
|
||||
Location string `json:"location"`
|
||||
Website string `json:"website"`
|
||||
CountryCode string `json:"country_code"`
|
||||
Phone string `json:"phone"`
|
||||
}
|
||||
|
||||
func (p *UserProfileSimple) ToResponse() *response.UserProfileSimpleResponse {
|
||||
return &response.UserProfileSimpleResponse{
|
||||
DisplayName: p.DisplayName,
|
||||
FullName: p.FullName,
|
||||
AvatarUrl: p.AvatarUrl,
|
||||
Bio: p.Bio,
|
||||
Location: p.Location,
|
||||
Website: p.Website,
|
||||
CountryCode: p.CountryCode,
|
||||
Phone: p.Phone,
|
||||
}
|
||||
}
|
||||
65
internal/models/role.go
Normal file
65
internal/models/role.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"history-api/internal/dtos/response"
|
||||
"history-api/pkg/constant"
|
||||
"time"
|
||||
)
|
||||
|
||||
type RoleSimple struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
func (r *RoleSimple) ToResponse() *response.RoleSimpleResponse {
|
||||
return &response.RoleSimpleResponse{
|
||||
ID: r.ID,
|
||||
Name: r.Name,
|
||||
}
|
||||
}
|
||||
|
||||
func RolesToResponse(rs []*RoleSimple) []*response.RoleSimpleResponse {
|
||||
out := make([]*response.RoleSimpleResponse, len(rs))
|
||||
for i := range rs {
|
||||
out[i] = rs[i].ToResponse()
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
type RoleEntity struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
IsDeleted bool `json:"is_deleted"`
|
||||
CreatedAt *time.Time `json:"created_at"`
|
||||
UpdatedAt *time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
func (r *RoleEntity) ToResponse() *response.RoleResponse {
|
||||
return &response.RoleResponse{
|
||||
ID: r.ID,
|
||||
Name: r.Name,
|
||||
IsDeleted: r.IsDeleted,
|
||||
CreatedAt: r.CreatedAt,
|
||||
UpdatedAt: r.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func RolesEntityToResponse(rs []*RoleEntity) []*response.RoleResponse {
|
||||
out := make([]*response.RoleResponse, len(rs))
|
||||
for i := range rs {
|
||||
out[i] = rs[i].ToResponse()
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func RolesEntityToRoleConstant(rs []*RoleSimple) []constant.Role {
|
||||
out := make([]constant.Role, len(rs))
|
||||
for i := range rs {
|
||||
data, ok := constant.ParseRole(rs[i].Name)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
out[i] = data
|
||||
}
|
||||
return out
|
||||
}
|
||||
35
internal/models/token.go
Normal file
35
internal/models/token.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"history-api/internal/dtos/response"
|
||||
"history-api/pkg/convert"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
type TokenEntity struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
UserID pgtype.UUID `json:"user_id"`
|
||||
Token string `json:"token"`
|
||||
TokenType int16 `json:"token_type"`
|
||||
ExpiresAt pgtype.Timestamptz `json:"expires_at"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
}
|
||||
|
||||
func (t *TokenEntity) ToResponse() *response.TokenResponse {
|
||||
return &response.TokenResponse{
|
||||
ID: convert.UUIDToString(t.ID),
|
||||
UserID: convert.UUIDToString(t.UserID),
|
||||
TokenType: t.TokenType,
|
||||
ExpiresAt: convert.TimeToPtr(t.ExpiresAt),
|
||||
CreatedAt: convert.TimeToPtr(t.CreatedAt),
|
||||
}
|
||||
}
|
||||
|
||||
func TokensEntityToResponse(ts []*TokenEntity) []*response.TokenResponse {
|
||||
out := make([]*response.TokenResponse, len(ts))
|
||||
for i := range ts {
|
||||
out[i] = ts[i].ToResponse()
|
||||
}
|
||||
return out
|
||||
}
|
||||
61
internal/models/user.go
Normal file
61
internal/models/user.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"history-api/internal/dtos/response"
|
||||
"time"
|
||||
)
|
||||
|
||||
type UserEntity struct {
|
||||
ID string `json:"id"`
|
||||
Email string `json:"email"`
|
||||
PasswordHash string `json:"password_hash"`
|
||||
Profile *UserProfileSimple `json:"profile"`
|
||||
IsVerified bool `json:"is_verified"`
|
||||
TokenVersion int32 `json:"token_version"`
|
||||
GoogleID string `json:"google_id"`
|
||||
AuthProvider string `json:"auth_provider"`
|
||||
RefreshToken string `json:"refresh_token"`
|
||||
IsDeleted bool `json:"is_deleted"`
|
||||
CreatedAt *time.Time `json:"created_at"`
|
||||
UpdatedAt *time.Time `json:"updated_at"`
|
||||
Roles []*RoleSimple `json:"roles"`
|
||||
}
|
||||
|
||||
func (u *UserEntity) ParseRoles(data []byte) error {
|
||||
if len(data) == 0 {
|
||||
u.Roles = []*RoleSimple{}
|
||||
return nil
|
||||
}
|
||||
return json.Unmarshal(data, &u.Roles)
|
||||
}
|
||||
|
||||
func (u *UserEntity) ParseProfile(data []byte) error {
|
||||
if len(data) == 0 {
|
||||
u.Profile = &UserProfileSimple{}
|
||||
return nil
|
||||
}
|
||||
return json.Unmarshal(data, &u.Profile)
|
||||
}
|
||||
|
||||
func (u *UserEntity) ToResponse() *response.UserResponse {
|
||||
return &response.UserResponse{
|
||||
ID: u.ID,
|
||||
Email: u.Email,
|
||||
IsVerified: u.IsVerified,
|
||||
TokenVersion: u.TokenVersion,
|
||||
IsDeleted: u.IsDeleted,
|
||||
CreatedAt: u.CreatedAt,
|
||||
UpdatedAt: u.UpdatedAt,
|
||||
Roles: RolesToResponse(u.Roles),
|
||||
Profile: u.Profile.ToResponse(),
|
||||
}
|
||||
}
|
||||
|
||||
func UsersEntityToResponse(rs []*UserEntity) []*response.UserResponse {
|
||||
out := make([]*response.UserResponse, len(rs))
|
||||
for i := range rs {
|
||||
out[i] = rs[i].ToResponse()
|
||||
}
|
||||
return out
|
||||
}
|
||||
Reference in New Issue
Block a user