services.go 5.9 KB

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