Files
VLoop/internal/account/service.go

127 lines
3.3 KiB
Go
Raw Normal View History

package account
2025-12-05 14:03:57 +08:00
import (
"context"
2025-12-05 14:03:57 +08:00
"errors"
"feedsystem_video_go/internal/auth"
2025-12-16 18:46:51 +08:00
"github.com/go-sql-driver/mysql"
2025-12-05 14:03:57 +08:00
"golang.org/x/crypto/bcrypt"
2025-12-16 18:46:51 +08:00
"gorm.io/gorm"
2025-12-05 14:03:57 +08:00
)
2025-12-05 12:48:03 +08:00
2025-12-05 14:25:39 +08:00
type AccountService struct {
accountRepository *AccountRepository
}
2025-12-16 18:46:51 +08:00
var (
ErrUsernameTaken = errors.New("username already exists")
ErrNewUsernameRequired = errors.New("new_username is required")
)
2025-12-05 14:25:39 +08:00
func NewAccountService(accountRepository *AccountRepository) *AccountService {
return &AccountService{accountRepository: accountRepository}
}
func (as *AccountService) CreateAccount(ctx context.Context, account *Account) error {
2025-12-05 14:25:39 +08:00
passwordHash, err := bcrypt.GenerateFromPassword([]byte(account.Password), bcrypt.DefaultCost)
2025-12-05 12:48:03 +08:00
if err != nil {
return err
}
2025-12-05 14:25:39 +08:00
account.Password = string(passwordHash)
if err := as.accountRepository.CreateAccount(ctx, account); err != nil {
return err
}
return nil
}
2025-12-16 18:46:51 +08:00
func (as *AccountService) Rename(ctx context.Context, accountID uint, newUsername string) (string, error) {
if newUsername == "" {
return "", ErrNewUsernameRequired
}
2025-12-16 18:46:51 +08:00
token, err := auth.GenerateToken(accountID, newUsername)
if err != nil {
return "", err
}
if err := as.accountRepository.RenameWithToken(ctx, accountID, newUsername, token); err != nil {
var mysqlErr *mysql.MySQLError
if errors.As(err, &mysqlErr) && mysqlErr.Number == 1062 {
return "", ErrUsernameTaken
}
if errors.Is(err, gorm.ErrRecordNotFound) {
return "", err
}
return "", err
}
return token, nil
}
func (as *AccountService) ChangePassword(ctx context.Context, username, oldPassword, newPassword string) error {
account, err := as.FindByUsername(ctx, username)
if err != nil {
return err
}
2025-12-05 14:25:39 +08:00
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 := as.accountRepository.ChangePassword(ctx, account.ID, string(passwordHash)); err != nil {
return err
}
as.Logout(ctx, account.ID)
return nil
}
func (as *AccountService) FindByID(ctx context.Context, id uint) (*Account, error) {
if account, err := as.accountRepository.FindByID(ctx, id); err != nil {
return nil, err
} else {
2025-12-05 14:25:39 +08:00
return account, nil
}
}
func (as *AccountService) FindByUsername(ctx context.Context, username string) (*Account, error) {
if account, err := as.accountRepository.FindByUsername(ctx, username); err != nil {
return nil, err
} else {
2025-12-05 14:25:39 +08:00
return account, nil
}
}
2025-12-05 14:03:57 +08:00
func (as *AccountService) Login(ctx context.Context, username, password string) (string, error) {
account, err := as.FindByUsername(ctx, username)
2025-12-05 14:03:57 +08:00
if err != nil {
return "", err
}
2025-12-05 14:25:39 +08:00
if err := bcrypt.CompareHashAndPassword([]byte(account.Password), []byte(password)); err != nil {
2025-12-05 14:03:57 +08:00
return "", err
}
// generate token
2025-12-05 14:25:39 +08:00
token, err := auth.GenerateToken(account.ID, account.Username)
2025-12-05 14:03:57 +08:00
if err != nil {
return "", err
}
if err := as.accountRepository.Login(ctx, account.ID, token); err != nil {
2025-12-05 14:03:57 +08:00
return "", err
}
return token, nil
}
func (as *AccountService) Logout(ctx context.Context, accountID uint) error {
account, err := as.FindByID(ctx, accountID)
2025-12-05 14:03:57 +08:00
if err != nil {
return err
}
2025-12-05 14:25:39 +08:00
if account.Token == "" {
return errors.New("account already logged out")
2025-12-05 14:03:57 +08:00
}
return as.accountRepository.Logout(ctx, account.ID)
2025-12-05 14:03:57 +08:00
}