2023-04-10 17:30:28 +05:30
|
|
|
package user
|
|
|
|
|
|
|
|
|
|
import "gorm.io/gorm"
|
|
|
|
|
|
|
|
|
|
type UserEntity struct {
|
|
|
|
|
gorm.Model
|
|
|
|
|
Name string
|
|
|
|
|
SlackUserId string
|
|
|
|
|
Active bool
|
2023-10-20 13:05:40 +05:30
|
|
|
IsBot bool
|
|
|
|
|
Email string
|
|
|
|
|
RealName string
|
|
|
|
|
Image string
|
2023-04-10 17:30:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (UserEntity) TableName() string {
|
|
|
|
|
return "houston_user"
|
|
|
|
|
}
|
2024-02-23 16:05:20 +05:30
|
|
|
|
|
|
|
|
func (entity UserEntity) ToDTO() *UserDTO {
|
|
|
|
|
return &UserDTO{
|
|
|
|
|
ID: entity.ID,
|
|
|
|
|
Name: entity.Name,
|
|
|
|
|
SlackUserId: entity.SlackUserId,
|
|
|
|
|
Active: entity.Active,
|
|
|
|
|
IsBot: entity.IsBot,
|
|
|
|
|
Email: entity.Email,
|
|
|
|
|
RealName: entity.RealName,
|
|
|
|
|
Image: entity.Image,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type UserDTO struct {
|
|
|
|
|
ID uint `json:"id"`
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
SlackUserId string `json:"slack_user_id"`
|
|
|
|
|
Active bool `json:"active"`
|
|
|
|
|
IsBot bool `json:"is_bot"`
|
|
|
|
|
Email string `json:"email"`
|
|
|
|
|
RealName string `json:"real_name"`
|
|
|
|
|
Image string `json:"image"`
|
|
|
|
|
}
|
2024-04-02 16:25:34 +05:30
|
|
|
|
|
|
|
|
func (entity *UserEntity) Update(user UserEntity) (UserEntity, bool) {
|
|
|
|
|
setterFunctions := []func(UserEntity) bool{
|
2025-03-13 14:35:29 +05:30
|
|
|
entity.setNameIfNotEqual, entity.setImageIfNotEqual,
|
2024-04-02 16:25:34 +05:30
|
|
|
entity.setRealNameIfNotEqual, entity.setActiveIfNotEqual, entity.setIsBotIfNotEqual,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hasUpdated := false
|
|
|
|
|
for _, setter := range setterFunctions {
|
|
|
|
|
hasUpdated = setter(user) || hasUpdated
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return *entity, hasUpdated
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (entity *UserEntity) setNameIfNotEqual(user UserEntity) bool {
|
|
|
|
|
if entity.Name != user.Name {
|
|
|
|
|
entity.Name = user.Name
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (entity *UserEntity) setImageIfNotEqual(user UserEntity) bool {
|
|
|
|
|
if entity.Image != user.Image {
|
|
|
|
|
entity.Image = user.Image
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (entity *UserEntity) setRealNameIfNotEqual(user UserEntity) bool {
|
|
|
|
|
if entity.RealName != user.RealName {
|
|
|
|
|
entity.RealName = user.RealName
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (entity *UserEntity) setActiveIfNotEqual(user UserEntity) bool {
|
|
|
|
|
if entity.Active != user.Active {
|
|
|
|
|
entity.Active = user.Active
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (entity *UserEntity) setIsBotIfNotEqual(user UserEntity) bool {
|
|
|
|
|
if entity.IsBot != user.IsBot {
|
|
|
|
|
entity.IsBot = user.IsBot
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|