review/model/user.go

78 lines
1.5 KiB
Go

package model
import (
"gorm.io/gorm"
)
type User struct {
gorm.Model
Uid string
Password string
Avatar string
}
func CheckUser_(uid string) int {
var user User
db.Table("user").Where("uid", uid).First(&user)
if user.ID != 0 {
return 1001
}
return 200
}
func GetUser_(uid string) (User, int, string) {
var user User
if err := db.Find(&user, "Uid", uid).Error; err != nil {
return user, 500, err.Error()
}
return user, 200, "success"
}
func GetUsers_() []User {
var users []User
db.Find(&users)
return users
}
func CreateUser_(user *User) (int, string) {
code := CheckUser_(user.Uid)
if code == 1001 {
return 500, "already created"
}
if err := db.Create(&user).Error; err != nil {
return 500, err.Error()
}
return 200, "success"
}
func UpdateUser_(user *User) (int, string) {
if err := db.Model(&User{}).Where("uid", user.Uid).Updates(&user).Error; err != nil {
return 500, err.Error()
}
return 200, "success"
}
func DeleteUser_(uid string) (User, int, string) {
var user User
if err := db.Delete(&user, "Uid", uid).Error; err != nil {
return user, 500, err.Error()
}
return user, 200, "sucess"
}
func Login_(user *User) (int, string) {
code := CheckUser_(user.Uid)
if code != 1001 {
return 500, "no such user"
}
var user_ User
if err := db.Find(&user_, "Uid", user.Uid).Error; err != nil {
return 500, err.Error()
}
if user_.Password == user.Password {
return 200, "success"
} else {
return 500, "wrong password"
}
}