diff --git a/internal/account/handler.go b/internal/account/handler.go index e29173b..57dd9ce 100644 --- a/internal/account/handler.go +++ b/internal/account/handler.go @@ -57,7 +57,7 @@ func (h *AccountHandler) CreateAccount(c *gin.Context) { c.JSON(400, gin.H{"error": err.Error()}) return } - if err := h.accountService.CreateAccount(&Account{ + if err := h.accountService.CreateAccount(c.Request.Context(), &Account{ Username: req.Username, Password: req.Password, }); err != nil { @@ -78,7 +78,7 @@ func (h *AccountHandler) Rename(c *gin.Context) { c.JSON(400, gin.H{"error": err.Error()}) return } - if err := h.accountService.Rename(accountID, req.NewUsername); err != nil { + if err := h.accountService.Rename(c.Request.Context(), accountID, req.NewUsername); err != nil { c.JSON(500, gin.H{"error": err.Error()}) return } @@ -91,7 +91,7 @@ func (h *AccountHandler) ChangePassword(c *gin.Context) { c.JSON(400, gin.H{"error": err.Error()}) return } - if err := h.accountService.ChangePassword(req.Username, req.OldPassword, req.NewPassword); err != nil { + if err := h.accountService.ChangePassword(c.Request.Context(), req.Username, req.OldPassword, req.NewPassword); err != nil { c.JSON(500, gin.H{"error": err.Error()}) return } @@ -104,7 +104,7 @@ func (h *AccountHandler) FindByID(c *gin.Context) { c.JSON(400, gin.H{"error": err.Error()}) return } - if account, err := h.accountService.FindByID(req.ID); err != nil { + if account, err := h.accountService.FindByID(c.Request.Context(), req.ID); err != nil { c.JSON(500, gin.H{"error": err.Error()}) return } else { @@ -118,7 +118,7 @@ func (h *AccountHandler) FindByUsername(c *gin.Context) { c.JSON(400, gin.H{"error": err.Error()}) return } - if account, err := h.accountService.FindByUsername(req.Username); err != nil { + if account, err := h.accountService.FindByUsername(c.Request.Context(), req.Username); err != nil { c.JSON(500, gin.H{"error": err.Error()}) return } else { @@ -132,7 +132,7 @@ func (h *AccountHandler) Login(c *gin.Context) { c.JSON(400, gin.H{"error": err.Error()}) return } - if token, err := h.accountService.Login(req.Username, req.Password); err != nil { + if token, err := h.accountService.Login(c.Request.Context(), req.Username, req.Password); err != nil { c.JSON(500, gin.H{"error": err.Error()}) return } else { @@ -146,7 +146,7 @@ func (h *AccountHandler) Logout(c *gin.Context) { c.JSON(400, gin.H{"error": err.Error()}) return } - if err := h.accountService.Logout(accountID); err != nil { + if err := h.accountService.Logout(c.Request.Context(), accountID); err != nil { c.JSON(500, gin.H{"error": err.Error()}) return } diff --git a/internal/account/repo.go b/internal/account/repo.go index b0c340a..fcc6796 100644 --- a/internal/account/repo.go +++ b/internal/account/repo.go @@ -1,6 +1,8 @@ package account import ( + "context" + "gorm.io/gorm" ) @@ -12,52 +14,52 @@ func NewAccountRepository(db *gorm.DB) *AccountRepository { return &AccountRepository{db: db} } -func (ar *AccountRepository) CreateAccount(account *Account) error { - if err := ar.db.Create(account).Error; err != nil { +func (ar *AccountRepository) CreateAccount(ctx context.Context, account *Account) error { + if err := ar.db.WithContext(ctx).Create(account).Error; err != nil { return err } return nil } -func (ar *AccountRepository) Rename(id uint, newUsername string) error { - if err := ar.db.Model(&Account{}).Where("id = ?", id).Update("username", newUsername).Error; err != nil { +func (ar *AccountRepository) Rename(ctx context.Context, id uint, newUsername string) error { + if err := ar.db.WithContext(ctx).Model(&Account{}).Where("id = ?", id).Update("username", newUsername).Error; err != nil { return err } return 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 { +func (ar *AccountRepository) ChangePassword(ctx context.Context, id uint, newPassword string) error { + if err := ar.db.WithContext(ctx).Model(&Account{}).Where("id = ?", id).Update("password", newPassword).Error; err != nil { return err } return nil } -func (ar *AccountRepository) FindByID(id uint) (*Account, error) { +func (ar *AccountRepository) FindByID(ctx context.Context, id uint) (*Account, error) { var account Account - if err := ar.db.First(&account, id).Error; err != nil { + if err := ar.db.WithContext(ctx).First(&account, id).Error; err != nil { return nil, err } return &account, nil } -func (ar *AccountRepository) FindByUsername(username string) (*Account, error) { +func (ar *AccountRepository) FindByUsername(ctx context.Context, username string) (*Account, error) { var account Account - if err := ar.db.Where("username = ?", username).First(&account).Error; err != nil { + if err := ar.db.WithContext(ctx).Where("username = ?", username).First(&account).Error; err != nil { return nil, err } return &account, 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 { +func (ar *AccountRepository) Login(ctx context.Context, id uint, token string) error { + if err := ar.db.WithContext(ctx).Model(&Account{}).Where("id = ?", id).Update("token", token).Error; err != nil { return err } return nil } -func (ar *AccountRepository) Logout(id uint, token string) error { - if err := ar.db.Model(&Account{}).Where("id = ?", id).Update("token", "").Error; err != nil { +func (ar *AccountRepository) Logout(ctx context.Context, id uint) error { + if err := ar.db.WithContext(ctx).Model(&Account{}).Where("id = ?", id).Update("token", "").Error; err != nil { return err } return nil diff --git a/internal/account/service.go b/internal/account/service.go index f13d033..f8c5638 100644 --- a/internal/account/service.go +++ b/internal/account/service.go @@ -1,6 +1,7 @@ package account import ( + "context" "errors" "feedsystem_video_go/internal/auth" @@ -15,27 +16,27 @@ func NewAccountService(accountRepository *AccountRepository) *AccountService { return &AccountService{accountRepository: accountRepository} } -func (as *AccountService) CreateAccount(account *Account) error { +func (as *AccountService) CreateAccount(ctx context.Context, account *Account) error { passwordHash, err := bcrypt.GenerateFromPassword([]byte(account.Password), bcrypt.DefaultCost) if err != nil { return err } account.Password = string(passwordHash) - if err := as.accountRepository.CreateAccount(account); err != nil { + if err := as.accountRepository.CreateAccount(ctx, account); err != nil { return err } return nil } -func (as *AccountService) Rename(accountID uint, newUsername string) error { - if err := as.accountRepository.Rename(accountID, newUsername); err != nil { +func (as *AccountService) Rename(ctx context.Context, accountID uint, newUsername string) error { + if err := as.accountRepository.Rename(ctx, accountID, newUsername); err != nil { return err } return nil } -func (as *AccountService) ChangePassword(username, oldPassword, newPassword string) error { - account, err := as.FindByUsername(username) +func (as *AccountService) ChangePassword(ctx context.Context, username, oldPassword, newPassword string) error { + account, err := as.FindByUsername(ctx, username) if err != nil { return err } @@ -46,30 +47,30 @@ func (as *AccountService) ChangePassword(username, oldPassword, newPassword stri if err != nil { return err } - if err := as.accountRepository.ChangePassword(account.ID, string(passwordHash)); err != nil { + if err := as.accountRepository.ChangePassword(ctx, account.ID, string(passwordHash)); err != nil { return err } return nil } -func (as *AccountService) FindByID(id uint) (*Account, error) { - if account, err := as.accountRepository.FindByID(id); err != 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 { return account, nil } } -func (as *AccountService) FindByUsername(username string) (*Account, error) { - if account, err := as.accountRepository.FindByUsername(username); err != 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 { return account, nil } } -func (as *AccountService) Login(username, password string) (string, error) { - account, err := as.FindByUsername(username) +func (as *AccountService) Login(ctx context.Context, username, password string) (string, error) { + account, err := as.FindByUsername(ctx, username) if err != nil { return "", err } @@ -81,20 +82,20 @@ func (as *AccountService) Login(username, password string) (string, error) { if err != nil { return "", err } - if err := as.accountRepository.Login(account.ID, token); err != nil { + if err := as.accountRepository.Login(ctx, account.ID, token); err != nil { return "", err } return token, nil } -func (as *AccountService) Logout(accountID uint) error { - account, err := as.FindByID(accountID) +func (as *AccountService) Logout(ctx context.Context, accountID uint) error { + account, err := as.FindByID(ctx, accountID) if err != nil { return err } if account.Token == "" { return errors.New("account already logged out") } - return as.accountRepository.Logout(account.ID, account.Token) + return as.accountRepository.Logout(ctx, account.ID) } diff --git a/internal/middleware/jwt.go b/internal/middleware/jwt.go index d8c58db..a8eebd1 100644 --- a/internal/middleware/jwt.go +++ b/internal/middleware/jwt.go @@ -35,7 +35,7 @@ func JWTAuth(accountRepo *account.AccountRepository) gin.HandlerFunc { return } - accountInfo, err := accountRepo.FindByID(claims.AccountID) + accountInfo, err := accountRepo.FindByID(c.Request.Context(), claims.AccountID) if err != nil || accountInfo.Token == "" || accountInfo.Token != tokenString { c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "token has been revoked"}) return