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

186 lines
5.3 KiB
Go

package platform
import (
"errors"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"base/internal/domain/profile"
"base/internal/dto"
"base/internal/server/middleware"
)
func (ctl *Controller) registerSpecialistRoutes(router *gin.RouterGroup) {
protected := router.Use(ctl.middleware.AuthShield())
protected.PUT("/page-sections/hero", ctl.SpecialistUpdateHero)
protected.PUT("/page-sections/contact", ctl.SpecialistUpdateContact)
protected.PUT("/page-sections/skills", ctl.SpecialistUpdateSkills)
protected.GET("/page-sections", ctl.SpecialistGetPageSections)
protected.GET("/profile", ctl.SpecialistGetProfile)
}
// SpecialistUpdateHero updates the hero section of the specialist's profile.
// @Summary update hero section
// @Tags Specialist
// @Accept json
// @Produce json
// @Security Bearer
// @Param request body dto.HeroDTO true "hero section"
// @Success 200 {object} dto.SuccessResponse
// @Failure 401 {object} dto.ErrorResponse
// @Failure 404 {object} dto.ErrorResponse
// @Router /api/specialists/v1/page-sections/hero [put]
func (ctl *Controller) SpecialistUpdateHero(c *gin.Context) {
userID, err := getUserIDFromContext(c)
if err != nil {
return
}
var req dto.HeroDTO
if !ctl.validateRequest(c, &req) {
return
}
if err := ctl.specialistService.UpdateHero(c.Request.Context(), userID, req); err != nil {
ctl.handleSpecialistError(c, err)
return
}
r := dto.OK().WithMessage("hero updated")
c.JSON(r.Status, r)
}
// SpecialistUpdateContact updates the contact section.
// @Summary update contact section
// @Tags Specialist
// @Accept json
// @Produce json
// @Security Bearer
// @Param request body dto.ContactDTO true "contact section"
// @Success 200 {object} dto.SuccessResponse
// @Failure 401 {object} dto.ErrorResponse
// @Failure 404 {object} dto.ErrorResponse
// @Router /api/specialists/v1/page-sections/contact [put]
func (ctl *Controller) SpecialistUpdateContact(c *gin.Context) {
userID, err := getUserIDFromContext(c)
if err != nil {
return
}
var req dto.ContactDTO
if !ctl.validateRequest(c, &req) {
return
}
if err := ctl.specialistService.UpdateContact(c.Request.Context(), userID, req); err != nil {
ctl.handleSpecialistError(c, err)
return
}
r := dto.OK().WithMessage("contact updated")
c.JSON(r.Status, r)
}
// SpecialistUpdateSkills updates the skills section.
// @Summary update skills section
// @Tags Specialist
// @Accept json
// @Produce json
// @Security Bearer
// @Param request body dto.SkillsUpdateRequest true "skills section"
// @Success 200 {object} dto.SuccessResponse
// @Failure 401 {object} dto.ErrorResponse
// @Failure 404 {object} dto.ErrorResponse
// @Router /api/specialists/v1/page-sections/skills [put]
func (ctl *Controller) SpecialistUpdateSkills(c *gin.Context) {
userID, err := getUserIDFromContext(c)
if err != nil {
return
}
var req dto.SkillsUpdateRequest
if !ctl.validateRequest(c, &req) {
return
}
if err := ctl.specialistService.UpdateSkills(c.Request.Context(), userID, req); err != nil {
ctl.handleSpecialistError(c, err)
return
}
r := dto.OK().WithMessage("skills updated")
c.JSON(r.Status, r)
}
// SpecialistGetPageSections returns hero, contact, skills for the specialist.
// @Summary get page sections
// @Tags Specialist
// @Produce json
// @Security Bearer
// @Success 200 {object} dto.PageSectionsResponse
// @Failure 401 {object} dto.ErrorResponse
// @Failure 404 {object} dto.ErrorResponse
// @Router /api/specialists/v1/page-sections [get]
func (ctl *Controller) SpecialistGetPageSections(c *gin.Context) {
userID, err := getUserIDFromContext(c)
if err != nil {
return
}
resp, err := ctl.specialistService.GetPageSections(c.Request.Context(), userID)
if err != nil {
ctl.handleSpecialistError(c, err)
return
}
r := dto.OK().WithData(resp)
c.JSON(r.Status, r)
}
// SpecialistGetProfile returns the specialist's full profile.
// @Summary get specialist profile
// @Tags Specialist
// @Produce json
// @Security Bearer
// @Success 200 {object} dto.ProfileResponse
// @Failure 401 {object} dto.ErrorResponse
// @Failure 404 {object} dto.ErrorResponse
// @Router /api/specialists/v1/profile [get]
func (ctl *Controller) SpecialistGetProfile(c *gin.Context) {
userID, err := getUserIDFromContext(c)
if err != nil {
return
}
resp, err := ctl.specialistService.GetProfile(c.Request.Context(), userID)
if err != nil {
ctl.handleSpecialistError(c, err)
return
}
r := dto.OK().WithData(resp)
c.JSON(r.Status, r)
}
func getUserIDFromContext(c *gin.Context) (uuid.UUID, error) {
val, exists := c.Get(middleware.UserIDKey)
if !exists {
c.JSON(dto.Unauthorized().Status, dto.Unauthorized())
return uuid.Nil, errors.New("unauthorized")
}
str, ok := val.(string)
if !ok {
c.JSON(dto.Unauthorized().Status, dto.Unauthorized())
return uuid.Nil, errors.New("invalid user id type")
}
id, err := uuid.Parse(str)
if err != nil {
c.JSON(dto.BadRequest().Status, dto.BadRequest().WithMessage("invalid user ID"))
return uuid.Nil, err
}
return id, nil
}
func (ctl *Controller) handleSpecialistError(c *gin.Context, err error) {
switch {
case errors.Is(err, profile.ErrProfileNotFound):
r := dto.NotFound().WithMessage("profile not found")
c.JSON(r.Status, r)
default:
ctl.logger.Error().Err(err).Msg("specialist error")
r := dto.InternalServerError()
c.JSON(r.Status, r)
}
}