2021-05-09 15:07:23 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/Damillora/Shioriko/pkg/database"
|
|
|
|
"github.com/Damillora/Shioriko/pkg/models"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
)
|
|
|
|
|
|
|
|
func CreateUser(model models.UserCreateModel) (*database.User, error) {
|
|
|
|
passwd, err := bcrypt.GenerateFromPassword([]byte(model.Password), bcrypt.DefaultCost)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
user := database.User{
|
|
|
|
ID: uuid.NewString(),
|
|
|
|
Email: model.Email,
|
|
|
|
Username: model.Username,
|
|
|
|
Password: string(passwd),
|
|
|
|
}
|
|
|
|
result := database.DB.Create(&user)
|
|
|
|
if result.Error != nil {
|
|
|
|
return nil, result.Error
|
|
|
|
}
|
|
|
|
return &user, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetUser(id string) *database.User {
|
|
|
|
var user database.User
|
|
|
|
database.DB.Where("id = ?", id).First(&user)
|
|
|
|
|
|
|
|
return &user
|
|
|
|
}
|
|
|
|
func GetUserFromUsername(username string) *database.User {
|
|
|
|
var user database.User
|
|
|
|
database.DB.Where("username = ?", username).First(&user)
|
|
|
|
|
|
|
|
return &user
|
|
|
|
}
|
|
|
|
|
|
|
|
func UpdateUser(id string, model models.UserUpdateModel) (*database.User, error) {
|
|
|
|
var user database.User
|
|
|
|
result := database.DB.Where("id = ?", id).First(&user)
|
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
return nil, result.Error
|
|
|
|
}
|
|
|
|
user.Email = model.Email
|
|
|
|
user.Username = model.Username
|
|
|
|
|
|
|
|
if user.Password != "" {
|
2022-04-15 19:20:14 +00:00
|
|
|
verifyErr := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(model.OldPassword))
|
|
|
|
if verifyErr != nil {
|
|
|
|
return nil, verifyErr
|
|
|
|
}
|
|
|
|
|
|
|
|
passwd, err := bcrypt.GenerateFromPassword([]byte(model.NewPassword), bcrypt.DefaultCost)
|
2021-05-09 15:07:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
user.Password = string(passwd)
|
|
|
|
}
|
|
|
|
|
|
|
|
result = database.DB.Save(&user)
|
|
|
|
if result.Error != nil {
|
|
|
|
return nil, result.Error
|
|
|
|
}
|
|
|
|
return &user, nil
|
|
|
|
}
|