This repository has been archived on 2023-11-13. You can view files and clone it, but cannot push or open issues or pull requests.
imagini/internal/db/users.go
2021-01-16 17:00:17 -05:00

43 lines
1.1 KiB
Go

package db
import (
"errors"
"gorm.io/gorm"
"golang.org/x/crypto/bcrypt"
log "github.com/sirupsen/logrus"
"reichard.io/imagini/internal/models"
)
func (dbm *DBManager) CreateUser(user models.User) error {
log.Info("[query] Creating user: ", user.Username)
_, err := dbm.User(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 dbm.db.Create(&user).Error
}
func (dbm *DBManager) User (user models.User) (models.User, error) {
var foundUser models.User
var count int64
err := dbm.db.Where(&user).First(&foundUser).Count(&count).Error
return foundUser, err
}
func (dbm *DBManager) DeleteUser (user models.User) error {
return nil
}
func (dbm *DBManager) UpdatePassword (user models.User, pw string) {
}