Files
base/internal/delivery/http/platform/profile.go
2026-04-10 18:25:21 +03:30

275 lines
7.6 KiB
Go

package platform
import (
profileDomian "base/internal/domain/profile"
"errors"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"base/internal/dto"
)
// CreateProfile godoc
// @Summary create profile
// @Description create a new profile
// @Tags Profile
// @Accept json
// @Produce json
// @Param request body dto.CreateProfileRequest true "create profile request"
// @Success 201 {object} dto.ProfileResponse "profile response"
// @Failure 400 {object} dto.ErrorResponse "invalid request"
// @Failure 500 {object} dto.ErrorResponse "internal server error"
// @Router /api/v1/profiles [post]
func (ctl *Controller) CreateProfile(c *gin.Context) {
lg := ctl.logger.With().
Str("module", "platform").
Str("router", "profile").
Str("handler", "CreateProfile").
Logger()
var req dto.CreateProfileRequest
if !ctl.validateRequest(c, &req) {
return
}
profile, err := ctl.profileService.Create(c.Request.Context(), req)
if err != nil {
lg.Error().Err(err).Msg("failed to create profile")
r := dto.InternalServerError().WithMessage("failed to create profile")
c.JSON(r.Status, r)
return
}
r := dto.Created(profile)
c.JSON(r.Status, r)
}
// GetProfile godoc
// @Summary get profile by ID
// @Description get profile by ID
// @Tags Profile
// @Accept json
// @Produce json
// @Param id path string true "profile ID"
// @Success 200 {object} dto.ProfileResponse "profile response"
// @Failure 400 {object} dto.ErrorResponse "invalid request"
// @Failure 404 {object} dto.ErrorResponse "profile not found"
// @Failure 500 {object} dto.ErrorResponse "internal server error"
// @Router /api/v1/profiles/{id} [get]
func (ctl *Controller) GetProfile(c *gin.Context) {
lg := ctl.logger.With().
Str("module", "platform").
Str("router", "profile").
Str("handler", "GetProfile").
Logger()
var req dto.GetProfileRequest
if !ctl.validateRequest(c, &req) {
return
}
id, err := uuid.Parse(req.ID)
if err != nil {
lg.Error().Err(err).Msg("invalid profile ID")
r := dto.BadRequest().WithMessage("invalid profile ID")
c.JSON(r.Status, r)
return
}
profile, err := ctl.profileService.GetByID(c.Request.Context(), id)
if err != nil {
lg.Error().Err(err).Msg("failed to get profile")
switch {
case errors.Is(err, profileDomian.ErrProfileNotFound):
r := dto.NotFound().WithMessage("profile not found")
c.JSON(r.Status, r)
default:
r := dto.InternalServerError()
c.JSON(r.Status, r)
}
return
}
r := dto.OK().WithData(profile)
c.JSON(r.Status, r)
}
// GetProfileByHandle godoc
// @Summary get profile by handle
// @Description get profile by handle
// @Tags Profile
// @Accept json
// @Produce json
// @Param handle path string true "profile handle"
// @Success 200 {object} dto.ProfileResponse "profile response"
// @Failure 400 {object} dto.ErrorResponse "invalid request"
// @Failure 404 {object} dto.ErrorResponse "profile not found"
// @Failure 500 {object} dto.ErrorResponse "internal server error"
// @Router /api/v1/profiles/handle/{handle} [get]
func (ctl *Controller) GetProfileByHandle(c *gin.Context) {
lg := ctl.logger.With().
Str("module", "platform").
Str("router", "profile").
Str("handler", "GetProfileByHandle").
Logger()
var req dto.GetProfileByHandleRequest
if !ctl.validateRequest(c, &req) {
return
}
profile, err := ctl.profileService.GetByHandle(c.Request.Context(), req.Handle)
if err != nil {
lg.Error().Err(err).Msg("failed to get profile by handle")
switch {
case errors.Is(err, profileDomian.ErrProfileNotFound):
r := dto.NotFound().WithMessage("profile not found")
c.JSON(r.Status, r)
default:
r := dto.InternalServerError()
c.JSON(r.Status, r)
}
return
}
r := dto.OK().WithData(profile)
c.JSON(r.Status, r)
}
// UpdateProfile godoc
// @Summary update profile
// @Description update an existing profile
// @Tags Profile
// @Accept json
// @Produce json
// @Param id path string true "profile ID"
// @Param request body dto.UpdateProfileRequest true "update profile request"
// @Success 200 {object} dto.ProfileResponse "profile response"
// @Failure 400 {object} dto.ErrorResponse "invalid request"
// @Failure 404 {object} dto.ErrorResponse "profile not found"
// @Failure 500 {object} dto.ErrorResponse "internal server error"
// @Router /api/v1/profiles/{id} [put]
func (ctl *Controller) UpdateProfile(c *gin.Context) {
lg := ctl.logger.With().
Str("module", "platform").
Str("router", "profile").
Str("handler", "UpdateProfile").
Logger()
var req dto.UpdateProfileRequest
if !ctl.validateRequest(c, &req) {
return
}
profile, err := ctl.profileService.Update(c.Request.Context(), req)
if err != nil {
lg.Error().Err(err).Msg("failed to update profile")
switch {
case errors.Is(err, profileDomian.ErrProfileNotFound):
r := dto.NotFound().WithMessage("profile not found")
c.JSON(r.Status, r)
default:
r := dto.InternalServerError()
c.JSON(r.Status, r)
}
return
}
r := dto.OK().WithData(profile)
c.JSON(r.Status, r)
}
// ListProfiles godoc
// @Summary list profiles
// @Description list profiles with filtering and pagination
// @Tags Profile
// @Accept json
// @Produce json
// @Param role_id query string false "role ID"
// @Param first_name query string false "first name"
// @Param last_name query string false "last name"
// @Param company query string false "company"
// @Param skill_name query string false "skill name"
// @Param page query int false "page number" default(1)
// @Param page_size query int false "page size" default(10)
// @Param sorted_by query string false "sort field"
// @Param ascending query bool false "ascending order" default(false)
// @Success 200 {object} dto.ListProfilesResponse "list profiles response"
// @Failure 400 {object} dto.ErrorResponse "invalid request"
// @Failure 500 {object} dto.ErrorResponse "internal server error"
// @Router /api/v1/profiles [get]
func (ctl *Controller) ListProfiles(c *gin.Context) {
lg := ctl.logger.With().
Str("module", "platform").
Str("router", "profile").
Str("handler", "ListProfiles").
Logger()
var req dto.ListProfilesRequest
if !ctl.validateRequest(c, &req) {
return
}
profiles, err := ctl.profileService.List(c.Request.Context(), req)
if err != nil {
lg.Error().Err(err).Msg("failed to list profiles")
r := dto.InternalServerError()
c.JSON(r.Status, r)
return
}
r := dto.OK().WithData(profiles)
c.JSON(r.Status, r)
}
// DeleteProfile godoc
// @Summary delete profile
// @Description delete a profile
// @Tags Profile
// @Accept json
// @Produce json
// @Param id path string true "profile ID"
// @Success 200 {object} dto.SuccessResponse "success response"
// @Failure 400 {object} dto.ErrorResponse "invalid request"
// @Failure 404 {object} dto.ErrorResponse "profile not found"
// @Failure 500 {object} dto.ErrorResponse "internal server error"
// @Router /api/v1/profiles/{id} [delete]
func (ctl *Controller) DeleteProfile(c *gin.Context) {
lg := ctl.logger.With().
Str("module", "platform").
Str("router", "profile").
Str("handler", "DeleteProfile").
Logger()
var req dto.DeleteProfileRequest
if !ctl.validateRequest(c, &req) {
return
}
id, err := uuid.Parse(req.ID)
if err != nil {
lg.Error().Err(err).Msg("invalid profile ID")
r := dto.BadRequest().WithMessage("invalid profile ID")
c.JSON(r.Status, r)
return
}
err = ctl.profileService.Delete(c.Request.Context(), id)
if err != nil {
lg.Error().Err(err).Msg("failed to delete profile")
switch {
case errors.Is(err, profileDomian.ErrProfileNotFound):
r := dto.NotFound().WithMessage("profile not found")
c.JSON(r.Status, r)
default:
r := dto.InternalServerError()
c.JSON(r.Status, r)
}
return
}
r := dto.OK().WithMessage("profile deleted successfully")
c.JSON(r.Status, r)
}