You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
183 lines
4.3 KiB
183 lines
4.3 KiB
1 year ago
|
package model
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"gorm.io/gorm"
|
||
|
)
|
||
|
|
||
|
type (
|
||
|
userModel interface {
|
||
|
Insert(ctx context.Context, data *User) error
|
||
|
Update(ctx context.Context, id int64, updates map[string]any) error
|
||
|
Delete(ctx context.Context, id int64) error
|
||
|
FindOne(ctx context.Context, id int64) (*User, error)
|
||
|
}
|
||
|
User struct {
|
||
|
updates map[string]any
|
||
|
Id int64 `gorm:"column:id"`
|
||
|
Name string `gorm:"column:name"`
|
||
|
Email string `gorm:"column:email"`
|
||
|
Password string `gorm:"column:password"`
|
||
|
Phone string `gorm:"column:phone"` //+80-18010489927
|
||
|
Wechat string `gorm:"column:wechat"`
|
||
|
FirstName string `gorm:"column:first_name"`
|
||
|
LastName string `gorm:"column:last_name"`
|
||
|
Gender int32 `gorm:"column:gender"`
|
||
|
Skype string `gorm:"column:skype"`
|
||
|
Birthday int32 `gorm:"column:birthday"`
|
||
|
CurrentLocation int32 `gorm:"column:current_location"`
|
||
|
CurrentLocationOther string `gorm:"column:current_location_other"`
|
||
|
WorkExperience int32 `gorm:"column:work_experience"`
|
||
|
Country int32 `gorm:"column:country"`
|
||
|
CountryOther string `gorm:"column:country_other"`
|
||
|
AboutMe string `gorm:"column:about_me"`
|
||
|
}
|
||
|
defaultUserModel struct {
|
||
|
db *gorm.DB
|
||
|
}
|
||
|
)
|
||
|
|
||
|
func (s *User) TableName() string {
|
||
|
return "user"
|
||
|
}
|
||
|
|
||
|
func NewUser() *User {
|
||
|
return &User{
|
||
|
updates: make(map[string]any),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (s *User) SetId(id int64) *User {
|
||
|
s.Id = id
|
||
|
s.set("id", id)
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
func (s *User) SetName(name string) *User {
|
||
|
s.Name = name
|
||
|
s.set("name", name)
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
func (s *User) SetEmail(email string) *User {
|
||
|
s.Email = email
|
||
|
s.set("email", email)
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
func (s *User) SetPassword(password string) *User {
|
||
|
s.Password = password
|
||
|
s.set("password", password)
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
func (s *User) SetPhone(phone string) *User {
|
||
|
s.Phone = phone
|
||
|
s.set("phone", phone)
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
func (s *User) SetWechat(wechat string) *User {
|
||
|
s.Wechat = wechat
|
||
|
s.set("wechat", wechat)
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
func (s *User) SetFirstName(firstName string) *User {
|
||
|
s.FirstName = firstName
|
||
|
s.set("first_name", firstName)
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
func (s *User) SetLastName(lastName string) *User {
|
||
|
s.LastName = lastName
|
||
|
s.set("last_name", lastName)
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
func (s *User) SetGender(gender int32) *User {
|
||
|
s.Gender = gender
|
||
|
s.set("gender", gender)
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
func (s *User) SetSkype(skype string) *User {
|
||
|
s.Skype = skype
|
||
|
s.set("skype", skype)
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
func (s *User) SetBirthday(birthday int32) *User {
|
||
|
s.Birthday = birthday
|
||
|
s.set("birthday", birthday)
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
func (s *User) SetCurrentLocation(currentLocation int32) *User {
|
||
|
s.CurrentLocation = currentLocation
|
||
|
s.set("current_location", currentLocation)
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
func (s *User) SetCurrentLocationOther(currentLocationOther string) *User {
|
||
|
s.CurrentLocationOther = currentLocationOther
|
||
|
s.set("current_location_other", currentLocationOther)
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
func (s *User) SetWorkExperience(workExperience int32) *User {
|
||
|
s.WorkExperience = workExperience
|
||
|
s.set("work_experience", workExperience)
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
func (s *User) SetCountry(country int32) *User {
|
||
|
s.Country = country
|
||
|
s.set("country", country)
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
func (s *User) SetCountryOther(countryOther string) *User {
|
||
|
s.CountryOther = countryOther
|
||
|
s.set("country_other", countryOther)
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
func (s *User) SetAboutMe(aboutMe string) *User {
|
||
|
s.AboutMe = aboutMe
|
||
|
s.set("about_me", aboutMe)
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
func (s *User) set(key string, val any) {
|
||
|
s.updates[key] = val
|
||
|
}
|
||
|
|
||
|
func (s *User) UpdateColumn() map[string]any {
|
||
|
return s.updates
|
||
|
}
|
||
|
|
||
|
func newUserDAO(db *gorm.DB) *defaultUserModel {
|
||
|
return &defaultUserModel{
|
||
|
db: db,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (s *defaultUserModel) Insert(ctx context.Context, data *User) error {
|
||
|
return s.db.Create(data).Error
|
||
|
}
|
||
|
|
||
|
func (s *defaultUserModel) Update(ctx context.Context, id int64, updates map[string]any) error {
|
||
|
return s.db.Model(User{}).Where("id=?", id).Updates(updates).Error
|
||
|
}
|
||
|
|
||
|
func (s *defaultUserModel) Delete(ctx context.Context, id int64) error {
|
||
|
return s.db.Where("id=?", id).Delete(&User{}).Error
|
||
|
}
|
||
|
|
||
|
func (s *defaultUserModel) FindOne(ctx context.Context, id int64) (*User, error) {
|
||
|
row := User{}
|
||
|
err := s.db.Where("id=?", id).Find(&row).Error
|
||
|
return findResultWithError(&row, err)
|
||
|
}
|