profile_service.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package service
  2. import (
  3. "errors"
  4. "io"
  5. "github.com/2930134478/AI-CS/backend/infra"
  6. "github.com/2930134478/AI-CS/backend/repository"
  7. "gorm.io/gorm"
  8. )
  9. // ProfileService 负责个人资料相关的业务逻辑。
  10. type ProfileService struct {
  11. users *repository.UserRepository
  12. storage infra.StorageService
  13. }
  14. // NewProfileService 创建 ProfileService 实例。
  15. func NewProfileService(users *repository.UserRepository, storage infra.StorageService) *ProfileService {
  16. return &ProfileService{
  17. users: users,
  18. storage: storage,
  19. }
  20. }
  21. // GetProfile 获取用户的个人资料。
  22. func (s *ProfileService) GetProfile(userID uint) (*ProfileResult, error) {
  23. user, err := s.users.GetByID(userID)
  24. if err != nil {
  25. if errors.Is(err, gorm.ErrRecordNotFound) {
  26. return nil, errors.New("用户不存在")
  27. }
  28. return nil, err
  29. }
  30. return &ProfileResult{
  31. ID: user.ID,
  32. Username: user.Username,
  33. Role: user.Role,
  34. AvatarURL: user.AvatarURL,
  35. Nickname: user.Nickname,
  36. Email: user.Email,
  37. }, nil
  38. }
  39. // UpdateProfile 更新用户的个人资料。
  40. func (s *ProfileService) UpdateProfile(input UpdateProfileInput) (*ProfileResult, error) {
  41. // 检查用户是否存在
  42. if _, err := s.users.GetByID(input.UserID); err != nil {
  43. if errors.Is(err, gorm.ErrRecordNotFound) {
  44. return nil, errors.New("用户不存在")
  45. }
  46. return nil, err
  47. }
  48. updates := make(map[string]interface{})
  49. if input.Nickname != nil {
  50. updates["nickname"] = *input.Nickname
  51. }
  52. if input.Email != nil {
  53. updates["email"] = *input.Email
  54. }
  55. if len(updates) > 0 {
  56. if err := s.users.UpdateFields(input.UserID, updates); err != nil {
  57. return nil, err
  58. }
  59. }
  60. return s.GetProfile(input.UserID)
  61. }
  62. // UploadAvatar 上传用户头像。
  63. func (s *ProfileService) UploadAvatar(userID uint, file io.Reader, filename string) (*ProfileResult, error) {
  64. // 检查用户是否存在
  65. user, err := s.users.GetByID(userID)
  66. if err != nil {
  67. if errors.Is(err, gorm.ErrRecordNotFound) {
  68. return nil, errors.New("用户不存在")
  69. }
  70. return nil, err
  71. }
  72. // 如果已有头像,删除旧头像
  73. if user.AvatarURL != "" {
  74. if err := s.storage.DeleteFile(user.AvatarURL); err != nil {
  75. // 删除失败不阻止更新,只记录警告
  76. // log.Printf("删除旧头像失败: %v", err)
  77. }
  78. }
  79. // 保存新头像
  80. avatarURL, err := s.storage.SaveAvatar(userID, file, filename)
  81. if err != nil {
  82. return nil, err
  83. }
  84. // 更新用户头像URL
  85. updates := map[string]interface{}{
  86. "avatar_url": avatarURL,
  87. }
  88. if err := s.users.UpdateFields(userID, updates); err != nil {
  89. return nil, err
  90. }
  91. return s.GetProfile(userID)
  92. }