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)
|
||||
}
|
||||
|
||||
@@ -10,17 +10,17 @@ import (
|
||||
var secret = []byte("change-me-in-env")
|
||||
|
||||
type Claims struct {
|
||||
UserID uint `json:"user_id"`
|
||||
Username string `json:"username"`
|
||||
AccountID uint `json:"account_id"`
|
||||
Username string `json:"username"`
|
||||
jwt.RegisteredClaims
|
||||
}
|
||||
|
||||
func GenerateToken(userID uint, username string) (string, error) {
|
||||
func GenerateToken(accountID uint, username string) (string, error) {
|
||||
now := time.Now()
|
||||
|
||||
claims := Claims{
|
||||
UserID: userID,
|
||||
Username: username,
|
||||
AccountID: accountID,
|
||||
Username: username,
|
||||
RegisteredClaims: jwt.RegisteredClaims{
|
||||
ExpiresAt: jwt.NewNumericDate(now.Add(24 * time.Hour)),
|
||||
IssuedAt: jwt.NewNumericDate(now),
|
||||
|
||||
@@ -22,7 +22,7 @@ func NewDB(dbcfg config.DatabaseConfig) (*gorm.DB, error) {
|
||||
}
|
||||
|
||||
func AutoMigrate(db *gorm.DB) error {
|
||||
return db.AutoMigrate(&account.User{})
|
||||
return db.AutoMigrate(&account.Account{})
|
||||
}
|
||||
|
||||
func CloseDB(db *gorm.DB) error {
|
||||
|
||||
@@ -6,16 +6,16 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type UserHandler struct {
|
||||
userService *account.UserService
|
||||
type AccountHandler struct {
|
||||
accountService *account.AccountService
|
||||
}
|
||||
|
||||
type CreateUserRequest struct {
|
||||
type CreateAccountRequest struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
type CreateUserResponse struct {
|
||||
type CreateAccountResponse struct {
|
||||
}
|
||||
|
||||
type RenameByIDRequest struct {
|
||||
@@ -66,86 +66,86 @@ type LogoutRequest struct {
|
||||
type LogoutResponse struct {
|
||||
}
|
||||
|
||||
func NewUserHandler(userService *account.UserService) *UserHandler {
|
||||
return &UserHandler{userService: userService}
|
||||
func NewAccountHandler(accountService *account.AccountService) *AccountHandler {
|
||||
return &AccountHandler{accountService: accountService}
|
||||
}
|
||||
func (h *UserHandler) CreateUser(c *gin.Context) {
|
||||
var req CreateUserRequest
|
||||
func (h *AccountHandler) CreateAccount(c *gin.Context) {
|
||||
var req CreateAccountRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(400, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if err := h.userService.CreateUser(&account.User{
|
||||
if err := h.accountService.CreateAccount(&account.Account{
|
||||
Username: req.Username,
|
||||
Password: req.Password,
|
||||
}); err != nil {
|
||||
c.JSON(500, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(200, gin.H{"message": "user created"})
|
||||
c.JSON(200, gin.H{"message": "account created"})
|
||||
}
|
||||
|
||||
func (h *UserHandler) RenameByID(c *gin.Context) {
|
||||
func (h *AccountHandler) RenameByID(c *gin.Context) {
|
||||
var req RenameByIDRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(400, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if err := h.userService.RenameByID(req.ID, req.NewUsername); err != nil {
|
||||
if err := h.accountService.RenameByID(req.ID, req.NewUsername); err != nil {
|
||||
c.JSON(500, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(200, gin.H{"message": "user renamed"})
|
||||
c.JSON(200, gin.H{"message": "account renamed"})
|
||||
}
|
||||
|
||||
func (h *UserHandler) ChangePassword(c *gin.Context) {
|
||||
func (h *AccountHandler) ChangePassword(c *gin.Context) {
|
||||
var req ChangePasswordRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(400, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if err := h.userService.ChangePassword(req.Username, req.OldPassword, req.NewPassword); err != nil {
|
||||
if err := h.accountService.ChangePassword(req.Username, req.OldPassword, req.NewPassword); err != nil {
|
||||
c.JSON(500, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(200, gin.H{"message": "password changed"})
|
||||
}
|
||||
|
||||
func (h *UserHandler) FindByID(c *gin.Context) {
|
||||
func (h *AccountHandler) FindByID(c *gin.Context) {
|
||||
var req FindByIDRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(400, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if user, err := h.userService.FindByID(req.ID); err != nil {
|
||||
if account, err := h.accountService.FindByID(req.ID); err != nil {
|
||||
c.JSON(500, gin.H{"error": err.Error()})
|
||||
return
|
||||
} else {
|
||||
c.JSON(200, user)
|
||||
c.JSON(200, account)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *UserHandler) FindByUsername(c *gin.Context) {
|
||||
func (h *AccountHandler) FindByUsername(c *gin.Context) {
|
||||
var req FindByUsernameRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(400, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if user, err := h.userService.FindByUsername(req.Username); err != nil {
|
||||
if account, err := h.accountService.FindByUsername(req.Username); err != nil {
|
||||
c.JSON(500, gin.H{"error": err.Error()})
|
||||
return
|
||||
} else {
|
||||
c.JSON(200, user)
|
||||
c.JSON(200, account)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *UserHandler) Login(c *gin.Context) {
|
||||
func (h *AccountHandler) Login(c *gin.Context) {
|
||||
var req LoginRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(400, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if token, err := h.userService.Login(req.Username, req.Password); err != nil {
|
||||
if token, err := h.accountService.Login(req.Username, req.Password); err != nil {
|
||||
c.JSON(500, gin.H{"error": err.Error()})
|
||||
return
|
||||
} else {
|
||||
@@ -153,13 +153,13 @@ func (h *UserHandler) Login(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
func (h *UserHandler) Logout(c *gin.Context) {
|
||||
func (h *AccountHandler) Logout(c *gin.Context) {
|
||||
var req LogoutRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(400, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if err := h.userService.Logout(req.ID); err != nil {
|
||||
if err := h.accountService.Logout(req.ID); err != nil {
|
||||
c.JSON(500, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -11,26 +11,26 @@ import (
|
||||
func SetRouter(db *gorm.DB) *gin.Engine {
|
||||
r := gin.Default()
|
||||
|
||||
userRepository := account.NewUserRepository(db)
|
||||
userService := account.NewUserService(userRepository)
|
||||
userHandler := NewUserHandler(userService)
|
||||
userGroup := r.Group("/user")
|
||||
accountRepository := account.NewAccountRepository(db)
|
||||
accountService := account.NewAccountService(accountRepository)
|
||||
accountHandler := NewAccountHandler(accountService)
|
||||
accountGroup := r.Group("/account")
|
||||
{
|
||||
userGroup.POST("/register", userHandler.CreateUser)
|
||||
userGroup.POST("/rename", userHandler.RenameByID)
|
||||
userGroup.POST("/changePassword", userHandler.ChangePassword)
|
||||
userGroup.POST("/findByID", userHandler.FindByID)
|
||||
userGroup.POST("/findByUsername", userHandler.FindByUsername)
|
||||
accountGroup.POST("/register", accountHandler.CreateAccount)
|
||||
accountGroup.POST("/rename", accountHandler.RenameByID)
|
||||
accountGroup.POST("/changePassword", accountHandler.ChangePassword)
|
||||
accountGroup.POST("/findByID", accountHandler.FindByID)
|
||||
accountGroup.POST("/findByUsername", accountHandler.FindByUsername)
|
||||
}
|
||||
authGroup := r.Group("/auth")
|
||||
{
|
||||
authGroup.POST("/login", userHandler.Login)
|
||||
authGroup.POST("/login", accountHandler.Login)
|
||||
}
|
||||
|
||||
protectedAuthGroup := authGroup.Group("")
|
||||
protectedAuthGroup.Use(middleware.JWTAuth())
|
||||
{
|
||||
protectedAuthGroup.POST("/logout", userHandler.Logout)
|
||||
protectedAuthGroup.POST("/logout", accountHandler.Logout)
|
||||
}
|
||||
|
||||
return r
|
||||
|
||||
@@ -33,7 +33,7 @@ func JWTAuth() gin.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
c.Set("userID", claims.UserID)
|
||||
c.Set("accountID", claims.AccountID)
|
||||
c.Set("username", claims.Username)
|
||||
|
||||
c.Next()
|
||||
|
||||
Reference in New Issue
Block a user