services.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // Package handlers
  2. package handlers
  3. import (
  4. "encoding/json"
  5. "net/http"
  6. "strconv"
  7. "github.com/go-chi/chi/v5"
  8. "gogs.fxtmmsk.ru/foxtime/photoplaces/backend/internal/middleware"
  9. "gogs.fxtmmsk.ru/foxtime/photoplaces/backend/internal/models"
  10. "gogs.fxtmmsk.ru/foxtime/photoplaces/backend/internal/repository"
  11. "gogs.fxtmmsk.ru/foxtime/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, "failed to list services", err)
  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, "failed to get service", err)
  61. return
  62. }
  63. if svc == nil {
  64. writeError(w, http.StatusNotFound, "service not found", nil)
  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. r.Body = http.MaxBytesReader(w, r.Body, maxRequestBodySize)
  92. var req createServiceRequest
  93. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  94. writeError(w, http.StatusBadRequest, "invalid request body", err)
  95. return
  96. }
  97. if err := validator.Validate(req); err != nil {
  98. writeValidationError(w, err)
  99. return
  100. }
  101. tags := make([]models.Tag, len(req.Tags))
  102. for i, t := range req.Tags {
  103. tags[i] = models.Tag{ID: t}
  104. }
  105. svc := &models.Service{
  106. ExecutorID: userID,
  107. Title: req.Title,
  108. Description: req.Description,
  109. Price: req.Price,
  110. Currency: req.Currency,
  111. DurationMinutes: req.DurationMinutes,
  112. Status: "published",
  113. Tags: tags,
  114. }
  115. if err := h.serviceRepo.Create(r.Context(), svc); err != nil {
  116. writeError(w, http.StatusInternalServerError, "failed to create service", err)
  117. return
  118. }
  119. writeJSON(w, http.StatusCreated, svc)
  120. }
  121. func (h *ServiceHandler) Update(w http.ResponseWriter, r *http.Request) {
  122. id := chi.URLParam(r, "id")
  123. userID := middleware.GetUserID(r.Context())
  124. role := middleware.GetUserRole(r.Context())
  125. r.Body = http.MaxBytesReader(w, r.Body, maxRequestBodySize)
  126. var req updateServiceRequest
  127. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  128. writeError(w, http.StatusBadRequest, "invalid request body", err)
  129. return
  130. }
  131. if err := validator.Validate(req); err != nil {
  132. writeValidationError(w, err)
  133. return
  134. }
  135. svc, err := h.serviceRepo.GetByID(r.Context(), id)
  136. if err != nil {
  137. writeError(w, http.StatusInternalServerError, "failed to get service", err)
  138. return
  139. }
  140. if svc == nil {
  141. writeError(w, http.StatusNotFound, "service not found", nil)
  142. return
  143. }
  144. if svc.ExecutorID != userID && role != "moderator" && role != "superadmin" {
  145. writeError(w, http.StatusForbidden, "not your service", nil)
  146. return
  147. }
  148. if req.Title != nil { svc.Title = *req.Title }
  149. if req.Description != nil { svc.Description = req.Description }
  150. if req.Price != nil { svc.Price = *req.Price }
  151. if req.Currency != nil { svc.Currency = *req.Currency }
  152. if req.DurationMinutes != nil { svc.DurationMinutes = req.DurationMinutes }
  153. if err := h.serviceRepo.Update(r.Context(), svc); err != nil {
  154. writeError(w, http.StatusInternalServerError, "failed to update service", err)
  155. return
  156. }
  157. writeJSON(w, http.StatusOK, svc)
  158. }
  159. func (h *ServiceHandler) Delete(w http.ResponseWriter, r *http.Request) {
  160. id := chi.URLParam(r, "id")
  161. userID := middleware.GetUserID(r.Context())
  162. role := middleware.GetUserRole(r.Context())
  163. svc, err := h.serviceRepo.GetByID(r.Context(), id)
  164. if err != nil {
  165. writeError(w, http.StatusInternalServerError, "failed to get service", err)
  166. return
  167. }
  168. if svc == nil {
  169. writeError(w, http.StatusNotFound, "service not found", nil)
  170. return
  171. }
  172. if svc.ExecutorID != userID && role != "moderator" && role != "superadmin" {
  173. writeError(w, http.StatusForbidden, "not your service", nil)
  174. return
  175. }
  176. if err := h.serviceRepo.SoftDelete(r.Context(), id); err != nil {
  177. writeError(w, http.StatusInternalServerError, "failed to delete service", err)
  178. return
  179. }
  180. w.WriteHeader(http.StatusNoContent)
  181. }