2021-01-16 22:00:17 +00:00
|
|
|
package db
|
2021-01-10 00:44:02 +00:00
|
|
|
|
|
|
|
import (
|
2021-01-12 22:06:27 +00:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2021-02-02 20:34:10 +00:00
|
|
|
"reichard.io/imagini/graph/model"
|
2021-01-10 00:44:02 +00:00
|
|
|
)
|
|
|
|
|
2021-02-03 03:55:35 +00:00
|
|
|
func (dbm *DBManager) CreateUser (user *model.User) error {
|
2021-01-19 20:50:48 +00:00
|
|
|
log.Info("[db] Creating user: ", user.Username)
|
2021-02-02 20:34:10 +00:00
|
|
|
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(*user.Password), bcrypt.DefaultCost)
|
2021-01-12 22:06:27 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
2021-02-01 23:24:09 +00:00
|
|
|
return err
|
2021-01-12 22:06:27 +00:00
|
|
|
}
|
2021-02-02 20:34:10 +00:00
|
|
|
stringHashedPassword := string(hashedPassword)
|
|
|
|
user.Password = &stringHashedPassword
|
2021-01-18 21:16:52 +00:00
|
|
|
err = dbm.db.Create(&user).Error
|
2021-02-01 23:24:09 +00:00
|
|
|
return err
|
2021-01-10 00:44:02 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 20:34:10 +00:00
|
|
|
func (dbm *DBManager) User (user *model.User) (model.User, error) {
|
|
|
|
var foundUser model.User
|
2021-01-10 00:44:02 +00:00
|
|
|
var count int64
|
2021-01-16 22:00:17 +00:00
|
|
|
err := dbm.db.Where(&user).First(&foundUser).Count(&count).Error
|
2021-01-10 00:44:02 +00:00
|
|
|
return foundUser, err
|
|
|
|
}
|
|
|
|
|
2021-02-03 03:55:35 +00:00
|
|
|
func (dbm *DBManager) Users () ([]*model.User, int64, error) {
|
|
|
|
var foundUsers []*model.User
|
|
|
|
var count int64
|
|
|
|
err := dbm.db.Find(&foundUsers).Count(&count).Error
|
|
|
|
return foundUsers, count, err
|
|
|
|
}
|
|
|
|
|
2021-02-02 20:34:10 +00:00
|
|
|
func (dbm *DBManager) DeleteUser (user model.User) error {
|
2021-01-10 00:44:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-02 20:34:10 +00:00
|
|
|
func (dbm *DBManager) UpdatePassword (user model.User, pw string) {
|
2021-01-10 00:44:02 +00:00
|
|
|
|
|
|
|
}
|