feat: implemented own profile getter;

experiment: using custom automapper function to map profile to profileDto;
refactor: adjusted ProfileService to use pointer return types with models
This commit is contained in:
2025-07-20 21:39:21 +03:00
parent df54829a67
commit 705b420b9e
3 changed files with 28 additions and 13 deletions

View File

@@ -234,5 +234,5 @@ func (ctrl *ProfileController) uploadAvatar(c *gin.Context) {
return
}
c.JSON(http.StatusOK, dto.UrlDto{Url: link})
c.JSON(http.StatusOK, dto.UrlDto{Url: *link})
}

View File

@@ -20,18 +20,20 @@ package services
import (
"easywish/internal/database"
"easywish/internal/dto"
errs "easywish/internal/errors"
mapspecial "easywish/internal/utils/mapSpecial"
"github.com/go-redis/redis/v8"
"go.uber.org/zap"
)
type ProfileService interface {
GetProfileByUsername(cinfo dto.ClientInfo, username string) (dto.ProfileDto, error)
GetMyProfile(cinfo dto.ClientInfo) (dto.ProfileDto, error)
GetProfileByUsername(cinfo dto.ClientInfo, username string) (*dto.ProfileDto, error)
GetMyProfile(cinfo dto.ClientInfo) (*dto.ProfileDto, error)
UpdateProfile(cinfo dto.ClientInfo, newProfile dto.ProfileDto) (bool, error)
GetProfileSettings(cinfo dto.ClientInfo) (dto.ProfileSettingsDto, error)
GetProfileSettings(cinfo dto.ClientInfo) (*dto.ProfileSettingsDto, error)
UpdateProfileSettings(cinfo dto.ClientInfo, newProfileSettings dto.ProfileSettingsDto) (bool, error)
UploadAvatar(cinfo dto.ClientInfo, filePath string) (string, error)
UploadAvatar(cinfo dto.ClientInfo, filePath string) (*string, error)
}
type profileServiceImpl struct {
@@ -44,15 +46,28 @@ func NewProfileService(_log *zap.Logger, _dbctx database.DbContext, _redis *redi
return &profileServiceImpl{log: _log, dbctx: _dbctx, redis: _redis}
}
func (p *profileServiceImpl) GetMyProfile(cinfo dto.ClientInfo) (dto.ProfileDto, error) {
// XXX: untested
func (p *profileServiceImpl) GetMyProfile(cinfo dto.ClientInfo) (*dto.ProfileDto, error) {
db := database.NewDbHelper(p.dbctx);
profile, err := db.Queries.GetProfileByUsername(db.CTX, cinfo.Username); if err != nil {
p.log.Error(
"Failed to find user profile by username",
zap.Error(err))
return nil, errs.ErrServerError
}
var profileDto dto.ProfileDto
mapspecial.MapProfileDto(profile, profileDto)
return &profileDto, nil
}
func (p *profileServiceImpl) GetProfileByUsername(cinfo dto.ClientInfo, username string) (*dto.ProfileDto, error) {
panic("unimplemented")
}
func (p *profileServiceImpl) GetProfileByUsername(cinfo dto.ClientInfo, username string) (dto.ProfileDto, error) {
panic("unimplemented")
}
func (p *profileServiceImpl) GetProfileSettings(cinfo dto.ClientInfo) (dto.ProfileSettingsDto, error) {
func (p *profileServiceImpl) GetProfileSettings(cinfo dto.ClientInfo) (*dto.ProfileSettingsDto, error) {
panic("unimplemented")
}
@@ -64,6 +79,6 @@ func (p *profileServiceImpl) UpdateProfileSettings(cinfo dto.ClientInfo, newProf
panic("unimplemented")
}
func (p *profileServiceImpl) UploadAvatar(cinfo dto.ClientInfo, filePath string) (string, error) {
func (p *profileServiceImpl) UploadAvatar(cinfo dto.ClientInfo, filePath string) (*string, error) {
panic("unimplemented")
}

View File

@@ -24,7 +24,7 @@ import (
"github.com/rafiulgits/go-automapper"
)
func Map(dbModel database.Profile, dtoModel dto.ProfileDto) {
func MapProfileDto(dbModel database.Profile, dtoModel dto.ProfileDto) {
automapper.Map(dbModel, &dbModel, func(src *database.Profile, dst *dto.ProfileDto) {
dst.Birthday = int64(dbModel.Birthday.Time.UnixMilli())
})