services.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package handlers
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "strconv"
  6. "github.com/go-chi/chi/v5"
  7. "github.com/photoplaces/backend/internal/middleware"
  8. "github.com/photoplaces/backend/internal/models"
  9. "github.com/photoplaces/backend/internal/repository"
  10. "github.com/photoplaces/backend/internal/validator"
  11. )
  12. type ServiceHandler struct {
  13. serviceRepo *repository.ServiceRepo
  14. tagRepo *repository.TagRepo
  15. }
  16. func NewServiceHandler(serviceRepo *repository.ServiceRepo, tagRepo *repository.TagRepo) *ServiceHandler {
  17. return &ServiceHandler{serviceRepo: serviceRepo, tagRepo: tagRepo}
  18. }
  19. func (h *ServiceHandler) List(w http.ResponseWriter, r *http.Request) {
  20. q := r.URL.Query()
  21. filter := models.ServiceFilter{Status: "published"}
  22. if tags := q["tags"]; len(tags) > 0 {
  23. filter.Tags = tags
  24. }
  25. if mr := q.Get("min_rating"); mr != "" {
  26. if v, err := strconv.ParseFloat(mr, 64); err == nil {
  27. filter.MinRating = v
  28. }
  29. }
  30. if pmin := q.Get("price_min"); pmin != "" {
  31. if v, err := strconv.Atoi(pmin); err == nil {
  32. filter.PriceMin = &v
  33. }
  34. }
  35. if pmax := q.Get("price_max"); pmax != "" {
  36. if v, err := strconv.Atoi(pmax); err == nil {
  37. filter.PriceMax = &v
  38. }
  39. }
  40. if limit := q.Get("limit"); limit != "" {
  41. if v, err := strconv.Atoi(limit); err == nil {
  42. filter.Limit_ = v
  43. }
  44. }
  45. services, err := h.serviceRepo.List(r.Context(), filter)
  46. if err != nil {
  47. writeError(w, http.StatusInternalServerError, err.Error())
  48. return
  49. }
  50. if services == nil {
  51. services = []*models.Service{}
  52. }
  53. writeJSON(w, http.StatusOK, map[string]interface{}{"data": services})
  54. }
  55. func (h *ServiceHandler) GetByID(w http.ResponseWriter, r *http.Request) {
  56. id := chi.URLParam(r, "id")
  57. svc, err := h.serviceRepo.GetByID(r.Context(), id)
  58. if err != nil {
  59. writeError(w, http.StatusInternalServerError, err.Error())
  60. return
  61. }
  62. if svc == nil {
  63. writeError(w, http.StatusNotFound, "service not found")
  64. return
  65. }
  66. tags, err := h.serviceRepo.GetTags(r.Context(), id)
  67. if err == nil {
  68. svc.Tags = tags
  69. }
  70. writeJSON(w, http.StatusOK, svc)
  71. }
  72. type createServiceRequest struct {
  73. Title string `json:"title" validate:"required,min=1,max=255"`
  74. Description *string `json:"description" validate:"omitempty,max=5000"`
  75. Price int `json:"price" validate:"required,min=1"`
  76. Currency string `json:"currency" validate:"required,currency,len=3"`
  77. DurationMinutes *int `json:"duration_minutes" validate:"omitempty,min=1,max=10080"`
  78. Tags []string `json:"tags" validate:"dive,slug,max=50"`
  79. }
  80. type updateServiceRequest struct {
  81. Title *string `json:"title" validate:"omitempty,min=1,max=255"`
  82. Description *string `json:"description" validate:"omitempty,max=5000"`
  83. Price *int `json:"price" validate:"omitempty,min=1"`
  84. Currency *string `json:"currency" validate:"omitempty,currency,len=3"`
  85. DurationMinutes *int `json:"duration_minutes" validate:"omitempty,min=1,max=10080"`
  86. Tags []string `json:"tags" validate:"dive,slug,max=50"`
  87. }
  88. func (h *ServiceHandler) Create(w http.ResponseWriter, r *http.Request) {
  89. userID := middleware.GetUserID(r.Context())
  90. var req createServiceRequest
  91. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  92. writeError(w, http.StatusBadRequest, "invalid request body")
  93. return
  94. }
  95. if err := validator.Validate(req); err != nil {
  96. writeValidationError(w, err)
  97. return
  98. }
  99. tags := make([]models.Tag, len(req.Tags))
  100. for i, t := range req.Tags {
  101. tags[i] = models.Tag{ID: t}
  102. }
  103. svc := &models.Service{
  104. ExecutorID: userID,
  105. Title: req.Title,
  106. Description: req.Description,
  107. Price: req.Price,
  108. Currency: req.Currency,
  109. DurationMinutes: req.DurationMinutes,
  110. Status: "published",
  111. Tags: tags,
  112. }
  113. if err := h.serviceRepo.Create(r.Context(), svc); err != nil {
  114. writeError(w, http.StatusInternalServerError, err.Error())
  115. return
  116. }
  117. writeJSON(w, http.StatusCreated, svc)
  118. }
  119. func (h *ServiceHandler) Update(w http.ResponseWriter, r *http.Request) {
  120. id := chi.URLParam(r, "id")
  121. userID := middleware.GetUserID(r.Context())
  122. role := middleware.GetUserRole(r.Context())
  123. var req updateServiceRequest
  124. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  125. writeError(w, http.StatusBadRequest, "invalid request body")
  126. return
  127. }
  128. if err := validator.Validate(req); err != nil {
  129. writeValidationError(w, err)
  130. return
  131. }
  132. svc, err := h.serviceRepo.GetByID(r.Context(), id)
  133. if err != nil {
  134. writeError(w, http.StatusInternalServerError, err.Error())
  135. return
  136. }
  137. if svc == nil {
  138. writeError(w, http.StatusNotFound, "service not found")
  139. return
  140. }
  141. if svc.ExecutorID != userID && role != "moderator" && role != "superadmin" {
  142. writeError(w, http.StatusForbidden, "not your service")
  143. return
  144. }
  145. if req.Title != nil { svc.Title = *req.Title }
  146. if req.Description != nil { svc.Description = req.Description }
  147. if req.Price != nil { svc.Price = *req.Price }
  148. if req.Currency != nil { svc.Currency = *req.Currency }
  149. if req.DurationMinutes != nil { svc.DurationMinutes = req.DurationMinutes }
  150. if err := h.serviceRepo.Update(r.Context(), svc); err != nil {
  151. writeError(w, http.StatusInternalServerError, err.Error())
  152. return
  153. }
  154. writeJSON(w, http.StatusOK, svc)
  155. }
  156. func (h *ServiceHandler) Delete(w http.ResponseWriter, r *http.Request) {
  157. id := chi.URLParam(r, "id")
  158. userID := middleware.GetUserID(r.Context())
  159. role := middleware.GetUserRole(r.Context())
  160. svc, err := h.serviceRepo.GetByID(r.Context(), id)
  161. if err != nil {
  162. writeError(w, http.StatusInternalServerError, err.Error())
  163. return
  164. }
  165. if svc == nil {
  166. writeError(w, http.StatusNotFound, "service not found")
  167. return
  168. }
  169. if svc.ExecutorID != userID && role != "moderator" && role != "superadmin" {
  170. writeError(w, http.StatusForbidden, "not your service")
  171. return
  172. }
  173. if err := h.serviceRepo.SoftDelete(r.Context(), id); err != nil {
  174. writeError(w, http.StatusInternalServerError, err.Error())
  175. return
  176. }
  177. w.WriteHeader(http.StatusNoContent)
  178. }