2021-01-16 22:00:17 +00:00
|
|
|
package db
|
2021-01-10 00:44:02 +00:00
|
|
|
|
|
|
|
import (
|
2021-02-04 10:16:13 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
2021-02-09 00:42:20 +00:00
|
|
|
"gorm.io/gorm"
|
2021-01-12 22:06:27 +00:00
|
|
|
|
2021-02-04 10:16:13 +00:00
|
|
|
"reichard.io/imagini/graph/model"
|
2021-01-10 00:44:02 +00:00
|
|
|
)
|
|
|
|
|
2021-02-04 10:16:13 +00:00
|
|
|
func (dbm *DBManager) CreateUser(user *model.User) error {
|
|
|
|
log.Info("[db] Creating user: ", user.Username)
|
|
|
|
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(*user.Password), bcrypt.DefaultCost)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
stringHashedPassword := string(hashedPassword)
|
|
|
|
user.Password = &stringHashedPassword
|
|
|
|
return dbm.db.Create(user).Error
|
2021-01-10 00:44:02 +00:00
|
|
|
}
|
|
|
|
|
2021-02-04 10:16:13 +00:00
|
|
|
func (dbm *DBManager) User(user *model.User) (int64, error) {
|
|
|
|
var count int64
|
|
|
|
err := dbm.db.Where(user).First(user).Count(&count).Error
|
|
|
|
return count, err
|
2021-01-10 00:44:02 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 00:42:20 +00:00
|
|
|
func (dbm *DBManager) Users(filters *model.UserFilter, page *model.Page, order *model.Order) ([]*model.User, model.PageResponse, error) {
|
|
|
|
// Initial User Filter
|
|
|
|
tx := dbm.db.Session(&gorm.Session{}).Model(&model.Tag{})
|
|
|
|
|
|
|
|
// Dynamically Generate Base Query
|
|
|
|
tx, pageResponse := dbm.generateBaseQuery(tx, filters, page, order)
|
|
|
|
|
|
|
|
// Acquire Results
|
2021-02-04 10:16:13 +00:00
|
|
|
var foundUsers []*model.User
|
2021-02-09 00:42:20 +00:00
|
|
|
err := tx.Find(&foundUsers).Error
|
|
|
|
return foundUsers, pageResponse, err
|
2021-02-03 03:55:35 +00:00
|
|
|
}
|
|
|
|
|
2021-02-04 10:16:13 +00:00
|
|
|
func (dbm *DBManager) DeleteUser(user model.User) error {
|
|
|
|
return nil
|
2021-01-10 00:44:02 +00:00
|
|
|
}
|
|
|
|
|
2021-02-04 10:16:13 +00:00
|
|
|
func (dbm *DBManager) UpdatePassword(user model.User, pw string) {
|
2021-01-10 00:44:02 +00:00
|
|
|
|
|
|
|
}
|