refactor: Update service and repository methods to use context

- Refactored multiple service and repository methods to accept context as a parameter, enhancing consistency and enabling better control over request lifecycles.
- Updated handlers to utilize context in method calls, improving error handling and performance.
- Cleaned up Dockerfile by removing unnecessary whitespace.
This commit is contained in:
lan
2025-12-03 15:27:12 +08:00
parent 4824a997dd
commit 0bcd9336c4
32 changed files with 833 additions and 497 deletions

View File

@@ -47,7 +47,7 @@ func NewProfileService(
func (s *profileService) Create(ctx context.Context, userID int64, name string) (*model.Profile, error) {
// 验证用户存在
user, err := s.userRepo.FindByID(userID)
user, err := s.userRepo.FindByID(ctx, userID)
if err != nil || user == nil {
return nil, errors.New("用户不存在")
}
@@ -56,7 +56,7 @@ func (s *profileService) Create(ctx context.Context, userID int64, name string)
}
// 检查角色名是否已存在
existingName, err := s.profileRepo.FindByName(name)
existingName, err := s.profileRepo.FindByName(ctx, name)
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fmt.Errorf("查询角色名失败: %w", err)
}
@@ -80,12 +80,12 @@ func (s *profileService) Create(ctx context.Context, userID int64, name string)
IsActive: true,
}
if err := s.profileRepo.Create(profile); err != nil {
if err := s.profileRepo.Create(ctx, profile); err != nil {
return nil, fmt.Errorf("创建档案失败: %w", err)
}
// 设置活跃状态
if err := s.profileRepo.SetActive(profileUUID, userID); err != nil {
if err := s.profileRepo.SetActive(ctx, profileUUID, userID); err != nil {
return nil, fmt.Errorf("设置活跃状态失败: %w", err)
}
@@ -104,7 +104,7 @@ func (s *profileService) GetByUUID(ctx context.Context, uuid string) (*model.Pro
}
// 缓存未命中,从数据库查询
profile2, err := s.profileRepo.FindByUUID(uuid)
profile2, err := s.profileRepo.FindByUUID(ctx, uuid)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, ErrProfileNotFound
@@ -131,7 +131,7 @@ func (s *profileService) GetByUserID(ctx context.Context, userID int64) ([]*mode
}
// 缓存未命中,从数据库查询
profiles, err := s.profileRepo.FindByUserID(userID)
profiles, err := s.profileRepo.FindByUserID(ctx, userID)
if err != nil {
return nil, fmt.Errorf("查询档案列表失败: %w", err)
}
@@ -148,7 +148,7 @@ func (s *profileService) GetByUserID(ctx context.Context, userID int64) ([]*mode
func (s *profileService) Update(ctx context.Context, uuid string, userID int64, name *string, skinID, capeID *int64) (*model.Profile, error) {
// 获取档案并验证权限
profile, err := s.profileRepo.FindByUUID(uuid)
profile, err := s.profileRepo.FindByUUID(ctx, uuid)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, ErrProfileNotFound
@@ -162,7 +162,7 @@ func (s *profileService) Update(ctx context.Context, uuid string, userID int64,
// 检查角色名是否重复
if name != nil && *name != profile.Name {
existingName, err := s.profileRepo.FindByName(*name)
existingName, err := s.profileRepo.FindByName(ctx, *name)
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fmt.Errorf("查询角色名失败: %w", err)
}
@@ -180,7 +180,7 @@ func (s *profileService) Update(ctx context.Context, uuid string, userID int64,
profile.CapeID = capeID
}
if err := s.profileRepo.Update(profile); err != nil {
if err := s.profileRepo.Update(ctx, profile); err != nil {
return nil, fmt.Errorf("更新档案失败: %w", err)
}
@@ -190,12 +190,12 @@ func (s *profileService) Update(ctx context.Context, uuid string, userID int64,
s.cacheKeys.ProfileList(userID),
)
return s.profileRepo.FindByUUID(uuid)
return s.profileRepo.FindByUUID(ctx, uuid)
}
func (s *profileService) Delete(ctx context.Context, uuid string, userID int64) error {
// 获取档案并验证权限
profile, err := s.profileRepo.FindByUUID(uuid)
profile, err := s.profileRepo.FindByUUID(ctx, uuid)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return ErrProfileNotFound
@@ -207,7 +207,7 @@ func (s *profileService) Delete(ctx context.Context, uuid string, userID int64)
return ErrProfileNoPermission
}
if err := s.profileRepo.Delete(uuid); err != nil {
if err := s.profileRepo.Delete(ctx, uuid); err != nil {
return fmt.Errorf("删除档案失败: %w", err)
}
@@ -222,7 +222,7 @@ func (s *profileService) Delete(ctx context.Context, uuid string, userID int64)
func (s *profileService) SetActive(ctx context.Context, uuid string, userID int64) error {
// 获取档案并验证权限
profile, err := s.profileRepo.FindByUUID(uuid)
profile, err := s.profileRepo.FindByUUID(ctx, uuid)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return ErrProfileNotFound
@@ -234,11 +234,11 @@ func (s *profileService) SetActive(ctx context.Context, uuid string, userID int6
return ErrProfileNoPermission
}
if err := s.profileRepo.SetActive(uuid, userID); err != nil {
if err := s.profileRepo.SetActive(ctx, uuid, userID); err != nil {
return fmt.Errorf("设置活跃状态失败: %w", err)
}
if err := s.profileRepo.UpdateLastUsedAt(uuid); err != nil {
if err := s.profileRepo.UpdateLastUsedAt(ctx, uuid); err != nil {
return fmt.Errorf("更新使用时间失败: %w", err)
}
@@ -249,7 +249,7 @@ func (s *profileService) SetActive(ctx context.Context, uuid string, userID int6
}
func (s *profileService) CheckLimit(ctx context.Context, userID int64, maxProfiles int) error {
count, err := s.profileRepo.CountByUserID(userID)
count, err := s.profileRepo.CountByUserID(ctx, userID)
if err != nil {
return fmt.Errorf("查询档案数量失败: %w", err)
}
@@ -261,7 +261,7 @@ func (s *profileService) CheckLimit(ctx context.Context, userID int64, maxProfil
}
func (s *profileService) GetByNames(ctx context.Context, names []string) ([]*model.Profile, error) {
profiles, err := s.profileRepo.GetByNames(names)
profiles, err := s.profileRepo.GetByNames(ctx, names)
if err != nil {
return nil, fmt.Errorf("查找失败: %w", err)
}
@@ -270,7 +270,7 @@ func (s *profileService) GetByNames(ctx context.Context, names []string) ([]*mod
func (s *profileService) GetByProfileName(ctx context.Context, name string) (*model.Profile, error) {
// Profile name 查询通常不会频繁缓存,但为了一致性也添加
profile, err := s.profileRepo.FindByName(name)
profile, err := s.profileRepo.FindByName(ctx, name)
if err != nil {
return nil, errors.New("用户角色未创建")
}