clndr/api/models/user.go

38 lines
596 B
Go
Raw Normal View History

2022-12-18 00:42:21 +00:00
package models
import (
"gorm.io/gorm"
)
type User struct {
gorm.Model
Username string `json:"username" gorm:"uniqueIndex, column: username"`
Password string
}
func (u User) Create() error {
return DB().Create(&u).Error
}
func (u *User) Read() error {
return Read(u)
}
func (u User) ReadAll(offset, limit int) ([]User, error) {
var users []User
err := ReadAll(&users, offset, limit)
return users, err
}
func (u *User) Update() error {
return Update(u, "username", u.Username)
}
func (u User) Delete() error {
err := u.Read()
if err == nil {
err = Delete(&u)
}
return err
}