28 lines
783 B
Go
28 lines
783 B
Go
package models
|
|
|
|
import (
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
)
|
|
|
|
type User struct {
|
|
ID uint `json:"id" gorm:"primaryKey"`
|
|
Email string `json:"email" gorm:"size:255;uniqueIndex;not null"`
|
|
PasswordHash string `json:"-" gorm:"size:255;not null"`
|
|
IsAdmin bool `json:"isAdmin" gorm:"not null;default:false;index"`
|
|
SocialRating *UserSocialRating `json:"socialRating,omitempty" gorm:"foreignKey:UserID"`
|
|
CreatedAt time.Time `json:"createdAt"`
|
|
UpdatedAt time.Time `json:"updatedAt"`
|
|
}
|
|
|
|
func (u *User) AfterCreate(tx *gorm.DB) error {
|
|
rating := UserSocialRating{UserID: u.ID}
|
|
|
|
return tx.
|
|
Omit(clause.Associations).
|
|
Where("user_id = ?", u.ID).
|
|
FirstOrCreate(&rating).Error
|
|
}
|