refactor: change all user to account
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
package account
|
||||
|
||||
type User struct {
|
||||
type Account struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
Username string `gorm:"unique" json:"username"`
|
||||
Password string `json:"-"`
|
||||
|
||||
@@ -4,60 +4,60 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type UserRepository struct {
|
||||
type AccountRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewUserRepository(db *gorm.DB) *UserRepository {
|
||||
return &UserRepository{db: db}
|
||||
func NewAccountRepository(db *gorm.DB) *AccountRepository {
|
||||
return &AccountRepository{db: db}
|
||||
}
|
||||
|
||||
func (ur *UserRepository) CreateUser(user *User) error {
|
||||
if err := ur.db.Create(user).Error; err != nil {
|
||||
func (ar *AccountRepository) CreateAccount(account *Account) error {
|
||||
if err := ar.db.Create(account).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ur *UserRepository) RenameByID(id uint, newUsername string) error {
|
||||
if err := ur.db.Model(&User{}).Where("id = ?", id).Update("username", newUsername).Error; err != nil {
|
||||
func (ar *AccountRepository) RenameByID(id uint, newUsername string) error {
|
||||
if err := ar.db.Model(&Account{}).Where("id = ?", id).Update("username", newUsername).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ur *UserRepository) ChangePassword(id uint, newPassword string) error {
|
||||
if err := ur.db.Model(&User{}).Where("id = ?", id).Update("password", newPassword).Error; err != nil {
|
||||
func (ar *AccountRepository) ChangePassword(id uint, newPassword string) error {
|
||||
if err := ar.db.Model(&Account{}).Where("id = ?", id).Update("password", newPassword).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ur *UserRepository) FindByID(id uint) (*User, error) {
|
||||
var user User
|
||||
if err := ur.db.First(&user, id).Error; err != nil {
|
||||
func (ar *AccountRepository) FindByID(id uint) (*Account, error) {
|
||||
var account Account
|
||||
if err := ar.db.First(&account, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &user, nil
|
||||
return &account, nil
|
||||
}
|
||||
|
||||
func (ur *UserRepository) FindByUsername(username string) (*User, error) {
|
||||
var user User
|
||||
if err := ur.db.Where("username = ?", username).First(&user).Error; err != nil {
|
||||
func (ar *AccountRepository) FindByUsername(username string) (*Account, error) {
|
||||
var account Account
|
||||
if err := ar.db.Where("username = ?", username).First(&account).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &user, nil
|
||||
return &account, nil
|
||||
}
|
||||
|
||||
func (ur *UserRepository) Login(id uint, token string) error {
|
||||
if err := ur.db.Model(&User{}).Where("id = ?", id).Update("token", token).Error; err != nil {
|
||||
func (ar *AccountRepository) Login(id uint, token string) error {
|
||||
if err := ar.db.Model(&Account{}).Where("id = ?", id).Update("token", token).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ur *UserRepository) Logout(id uint, token string) error {
|
||||
if err := ur.db.Model(&User{}).Where("id = ?", id).Update("token", "").Error; err != nil {
|
||||
func (ar *AccountRepository) Logout(id uint, token string) error {
|
||||
if err := ar.db.Model(&Account{}).Where("id = ?", id).Update("token", "").Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -7,94 +7,94 @@ import (
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
type UserService struct {
|
||||
userRepository *UserRepository
|
||||
type AccountService struct {
|
||||
accountRepository *AccountRepository
|
||||
}
|
||||
|
||||
func NewUserService(userRepository *UserRepository) *UserService {
|
||||
return &UserService{userRepository: userRepository}
|
||||
func NewAccountService(accountRepository *AccountRepository) *AccountService {
|
||||
return &AccountService{accountRepository: accountRepository}
|
||||
}
|
||||
|
||||
func (us *UserService) CreateUser(user *User) error {
|
||||
passwordHash, err := bcrypt.GenerateFromPassword([]byte(user.Password), bcrypt.DefaultCost)
|
||||
func (as *AccountService) CreateAccount(account *Account) error {
|
||||
passwordHash, err := bcrypt.GenerateFromPassword([]byte(account.Password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
user.Password = string(passwordHash)
|
||||
if err := us.userRepository.CreateUser(user); err != nil {
|
||||
account.Password = string(passwordHash)
|
||||
if err := as.accountRepository.CreateAccount(account); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (us *UserService) RenameByID(id uint, newUsername string) error {
|
||||
if err := us.userRepository.RenameByID(id, newUsername); err != nil {
|
||||
func (as *AccountService) RenameByID(id uint, newUsername string) error {
|
||||
if err := as.accountRepository.RenameByID(id, newUsername); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (us *UserService) ChangePassword(username, oldPassword, newPassword string) error {
|
||||
user, err := us.FindByUsername(username)
|
||||
func (as *AccountService) ChangePassword(username, oldPassword, newPassword string) error {
|
||||
account, err := as.FindByUsername(username)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(oldPassword)); err != nil {
|
||||
if err := bcrypt.CompareHashAndPassword([]byte(account.Password), []byte(oldPassword)); err != nil {
|
||||
return err
|
||||
}
|
||||
passwordHash, err := bcrypt.GenerateFromPassword([]byte(newPassword), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := us.userRepository.ChangePassword(user.ID, string(passwordHash)); err != nil {
|
||||
if err := as.accountRepository.ChangePassword(account.ID, string(passwordHash)); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (us *UserService) FindByID(id uint) (*User, error) {
|
||||
if user, err := us.userRepository.FindByID(id); err != nil {
|
||||
func (as *AccountService) FindByID(id uint) (*Account, error) {
|
||||
if account, err := as.accountRepository.FindByID(id); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return user, nil
|
||||
return account, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (us *UserService) FindByUsername(username string) (*User, error) {
|
||||
if user, err := us.userRepository.FindByUsername(username); err != nil {
|
||||
func (as *AccountService) FindByUsername(username string) (*Account, error) {
|
||||
if account, err := as.accountRepository.FindByUsername(username); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return user, nil
|
||||
return account, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (us *UserService) Login(username, password string) (string, error) {
|
||||
user, err := us.FindByUsername(username)
|
||||
func (as *AccountService) Login(username, password string) (string, error) {
|
||||
account, err := as.FindByUsername(username)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password)); err != nil {
|
||||
if err := bcrypt.CompareHashAndPassword([]byte(account.Password), []byte(password)); err != nil {
|
||||
return "", err
|
||||
}
|
||||
// generate token
|
||||
token, err := auth.GenerateToken(user.ID, user.Username)
|
||||
token, err := auth.GenerateToken(account.ID, account.Username)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := us.userRepository.Login(user.ID, token); err != nil {
|
||||
if err := as.accountRepository.Login(account.ID, token); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return token, nil
|
||||
}
|
||||
|
||||
func (us *UserService) Logout(userID uint) error {
|
||||
user, err := us.FindByID(userID)
|
||||
func (as *AccountService) Logout(accountID uint) error {
|
||||
account, err := as.FindByID(accountID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if user.Token == "" {
|
||||
return errors.New("user already logged out")
|
||||
if account.Token == "" {
|
||||
return errors.New("account already logged out")
|
||||
}
|
||||
return us.userRepository.Logout(user.ID, user.Token)
|
||||
return as.accountRepository.Logout(account.ID, account.Token)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user