package Database import ( "errors" "git.tovijaeschke.xyz/tovi/Envelope/Backend/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 return userData, err } func GetUserByUsername(username string) (Models.User, error) { var ( userData Models.User err error ) err = DB.Preload(clause.Associations). First(&userData, "username = ?", username). Error return userData, err } func CheckUniqueUsername(username string) error { var ( exists bool err error ) err = DB.Model(Models.User{}). Select("count(*) > 0"). Where("username = ?", username). Find(&exists). Error if err != nil { return err } if exists { return errors.New("Invalid username") } return nil } func CreateUser(userData *Models.User) error { var ( err error ) err = DB.Session(&gorm.Session{FullSaveAssociations: true}). Create(userData). Error return err } func UpdateUser(id string, userData *Models.User) error { var ( err error ) err = DB.Model(&userData). Omit("id"). Where("id = ?", id). Updates(userData). Error if err != nil { return err } err = DB.Model(Models.User{}). Where("id = ?", id). First(userData). Error return err } func DeleteUser(userData *Models.User) error { return DB.Session(&gorm.Session{FullSaveAssociations: true}). Delete(userData). Error }