43 lines
1.0 KiB
Go
43 lines
1.0 KiB
Go
package query
|
|
|
|
import (
|
|
"errors"
|
|
"gorm.io/gorm"
|
|
"golang.org/x/crypto/bcrypt"
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"reichard.io/imagini/internal/models"
|
|
)
|
|
|
|
func CreateUser(db *gorm.DB, user models.User) error {
|
|
log.Info("[query] Creating user: ", user.Username)
|
|
_, err := User(db, user)
|
|
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
log.Warn("[query] User already exists: ", user.Username)
|
|
return errors.New("User already exists")
|
|
}
|
|
|
|
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(user.Password), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
log.Error(err)
|
|
return err
|
|
}
|
|
user.Password = string(hashedPassword)
|
|
return db.Create(&user).Error
|
|
}
|
|
|
|
func User (db *gorm.DB, user models.User) (models.User, error) {
|
|
var foundUser models.User
|
|
var count int64
|
|
err := db.Where(&user).First(&foundUser).Count(&count).Error
|
|
return foundUser, err
|
|
}
|
|
|
|
func DeleteUser (user models.User) error {
|
|
return nil
|
|
}
|
|
|
|
func UpdatePassword (user models.User, pw string) {
|
|
|
|
}
|