Skip to content

Commit

Permalink
Users: handle update avatar
Browse files Browse the repository at this point in the history
  • Loading branch information
System-Glitch committed May 10, 2024
1 parent 457d10f commit 43f571a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
2 changes: 1 addition & 1 deletion database/model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
type User struct {
Email string
Username string
Avatar null.String
Avatar null.String `copier:"-"`
Password string
CreatedAt time.Time `json:"createdAt"`
UpdatedAt null.Time `json:"updatedAt"`
Expand Down
16 changes: 8 additions & 8 deletions dto/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ type InternalUser struct {
}

type RegisterUser struct {
Email string `json:"email"`
Username string `json:"username"`
Password string `json:"password" copier:"-"`
Avatar typeutil.Undefined[[]fsutil.File] `json:"avatar" copier:"-"`
Email string `json:"email"`
Username string `json:"username"`
Password string `json:"password" copier:"-"`
Avatar typeutil.Undefined[null.Value[[]fsutil.File]] `json:"avatar" copier:"-"`
}

type UpdateUser struct {
Email typeutil.Undefined[string] `json:"email"`
Username typeutil.Undefined[string] `json:"username"`
Password typeutil.Undefined[string] `json:"password" copier:"-"`
Avatar typeutil.Undefined[[]fsutil.File] `json:"avatar" copier:"-"`
Email typeutil.Undefined[string] `json:"email"`
Username typeutil.Undefined[string] `json:"username"`
Password typeutil.Undefined[string] `json:"password" copier:"-"`
Avatar typeutil.Undefined[null.Value[[]fsutil.File]] `json:"avatar" copier:"-"`
}
24 changes: 22 additions & 2 deletions service/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/go-goyave/goyave-blog-example/database/model"
"github.com/go-goyave/goyave-blog-example/dto"
"github.com/go-goyave/goyave-blog-example/service"
"github.com/guregu/null/v5"
"golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
"goyave.dev/goyave/v5/slog"
Expand Down Expand Up @@ -77,8 +78,8 @@ func (s *Service) Register(ctx context.Context, registerDTO *dto.RegisterUser) e
}
user.Password = string(b)

if registerDTO.Avatar.IsPresent() {
filename, err := s.StorageService.SaveAvatar(registerDTO.Avatar.Val[0])
if registerDTO.Avatar.IsPresent() && registerDTO.Avatar.Val.Valid {
filename, err := s.StorageService.SaveAvatar(registerDTO.Avatar.Val.V[0])
if err != nil {
return errors.New(err)
}
Expand All @@ -105,6 +106,25 @@ func (s *Service) Update(ctx context.Context, id uint, updateDTO *dto.UpdateUser
}

user = typeutil.Copy(user, updateDTO)
if updateDTO.Avatar.Present {
// Delete previous avatar
if user.Avatar.Valid {
err := s.StorageService.Delete(user.Avatar.String)
if err != nil {
return errors.New(err)
}
}
if updateDTO.Avatar.Val.Valid {
// Save new avatar
filename, err := s.StorageService.SaveAvatar(updateDTO.Avatar.Val.V[0])
if err != nil {
return errors.New(err)
}
user.Avatar.SetValid(filename)
} else {
user.Avatar = null.String{}
}
}

_, err = s.Repository.Update(ctx, user)
return errors.New(err)
Expand Down

0 comments on commit 43f571a

Please sign in to comment.