package Database import ( "errors" "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Models" "gorm.io/gorm" "gorm.io/gorm/clause" ) func GetUserById(id string) (Models.User, error) { var ( userData Models.User err error ) err = DB.Preload(clause.Associations). First(&userData, "id = ?", id). Error userData.Password = "" return userData, err } func GetUserByEmail(email string) (Models.User, error) { var ( userData Models.User err error ) err = DB.Preload(clause.Associations). First(&userData, "email = ?", email). Error return userData, err } func GetUsers(page, pageSize int) ([]Models.User, error) { var ( users []Models.User i int err error ) switch { case pageSize > 100: pageSize = 100 case pageSize <= 0: pageSize = 10 } err = DB.Model(Models.User{}). Offset(0). Limit(10). Find(&users). Error for i, _ = range users { users[i].Password = "" } return users, err } func CheckUniqueEmail(email string) error { var ( exists bool err error ) err = DB.Model(Models.User{}). Select("count(*) > 0"). Where("email = ?", email). Find(&exists). Error if err != nil { return err } if exists { return errors.New("Invalid email") } return nil } func CreateUser(userData *Models.User) error { var ( err error ) err = DB.Session(&gorm.Session{FullSaveAssociations: true}). Create(userData). Error userData.Password = "" return err } func UpdateUser(id string, userData *Models.User) error { var ( err error ) err = DB.Model(&Models.User{}). Select("*"). Omit("id", "created_at", "updated_at", "deleted_at"). Where("id = ?", id). Updates(userData). Error userData.Password = "" return err } func DeleteUser(userData *Models.User) error { return DB.Session(&gorm.Session{FullSaveAssociations: true}). Delete(userData). Error }