Init
This commit is contained in:
commit
f90fdc0598
99 changed files with 15260 additions and 0 deletions
42
user/user.go
Normal file
42
user/user.go
Normal file
|
@ -0,0 +1,42 @@
|
|||
package user
|
||||
|
||||
import (
|
||||
"cafe/config"
|
||||
"cafe/types"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
Username string `gorm:"primaryKey" json:"username" validate:"required"`
|
||||
ShowColdDrinks bool `json:"show_cold_drinks" validate:"required"`
|
||||
ShowHotDrinks bool `json:"show_hot_drinks" validate:"required"`
|
||||
}
|
||||
|
||||
func DoesUserExist(username string) (User, error) {
|
||||
var user User
|
||||
result := config.Cafe.Database.ORM.Limit(1).Find(&user, "username = ?", username)
|
||||
if result.RowsAffected == 0 {
|
||||
return user, fmt.Errorf(types.CannotFind.String())
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func GetUserOrCreate(username string) (User, error) {
|
||||
var user User
|
||||
err := config.Cafe.Database.ORM.Where(User{Username: username}).Attrs(User{ShowHotDrinks: true, ShowColdDrinks: true}).FirstOrCreate(&user).Error
|
||||
if err != nil {
|
||||
return user, fmt.Errorf(types.CannotCreate.String())
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func UpdateUser(old *User, new *User) error {
|
||||
err := config.Cafe.Database.ORM.First(old).Updates(map[string]interface{}{
|
||||
"Username": new.Username,
|
||||
"ShowColdDrinks": new.ShowColdDrinks,
|
||||
"ShowHotDrinks": new.ShowHotDrinks}).Error
|
||||
if err != nil {
|
||||
return fmt.Errorf(types.CannotUpdate.String())
|
||||
}
|
||||
return nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue