services.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. )
  11. type ServiceHandler struct {
  12. serviceRepo *repository.ServiceRepo
  13. tagRepo *repository.TagRepo
  14. }
  15. func NewServiceHandler(serviceRepo *repository.ServiceRepo, tagRepo *repository.TagRepo) *ServiceHandler {
  16. return &ServiceHandler{serviceRepo: serviceRepo, tagRepo: tagRepo}
  17. }
  18. func (h *ServiceHandler) List(w http.ResponseWriter, r *http.Request) {
  19. q := r.URL.Query()
  20. filter := models.ServiceFilter{Status: "published"}
  21. if tags := q["tags"]; len(tags) > 0 {
  22. filter.Tags = tags
  23. }
  24. if mr := q.Get("min_rating"); mr != "" {
  25. if v, err := strconv.ParseFloat(mr, 64); err == nil {
  26. filter.MinRating = v
  27. }
  28. }
  29. if pmin := q.Get("price_min"); pmin != "" {
  30. if v, err := strconv.Atoi(pmin); err == nil {
  31. filter.PriceMin = &v
  32. }
  33. }
  34. if pmax := q.Get("price_max"); pmax != "" {
  35. if v, err := strconv.Atoi(pmax); err == nil {
  36. filter.PriceMax = &v
  37. }
  38. }
  39. if limit := q.Get("limit"); limit != "" {
  40. if v, err := strconv.Atoi(limit); err == nil {
  41. filter.Limit_ = v
  42. }
  43. }
  44. services, err := h.serviceRepo.List(r.Context(), filter)
  45. if err != nil {
  46. writeError(w, http.StatusInternalServerError, err.Error())
  47. return
  48. }
  49. if services == nil {
  50. services = []*models.Service{}
  51. }
  52. writeJSON(w, http.StatusOK, map[string]interface{}{"data": services})
  53. }
  54. func (h *ServiceHandler) GetByID(w http.ResponseWriter, r *http.Request) {
  55. id := chi.URLParam(r, "id")
  56. svc, err := h.serviceRepo.GetByID(r.Context(), id)
  57. if err != nil {
  58. writeError(w, http.StatusInternalServerError, err.Error())
  59. return
  60. }
  61. if svc == nil {
  62. writeError(w, http.StatusNotFound, "service not found")
  63. return
  64. }
  65. tags, err := h.serviceRepo.GetTags(r.Context(), id)
  66. if err == nil {
  67. svc.Tags = tags
  68. }
  69. writeJSON(w, http.StatusOK, svc)
  70. }
  71. type createServiceRequest struct {
  72. Title string `json:"title"`
  73. Description *string `json:"description"`
  74. Price int `json:"price"`
  75. Currency string `json:"currency"`
  76. DurationMinutes *int `json:"duration_minutes"`
  77. Tags []string `json:"tags"`
  78. }
  79. func (h *ServiceHandler) Create(w http.ResponseWriter, r *http.Request) {
  80. userID := middleware.GetUserID(r.Context())
  81. var req createServiceRequest
  82. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  83. writeError(w, http.StatusBadRequest, "invalid request body")
  84. return
  85. }
  86. if req.Title == "" || req.Price <= 0 {
  87. writeError(w, http.StatusBadRequest, "title and price are required")
  88. return
  89. }
  90. tags := make([]models.Tag, len(req.Tags))
  91. for i, t := range req.Tags {
  92. tags[i] = models.Tag{ID: t}
  93. }
  94. svc := &models.Service{
  95. ExecutorID: userID,
  96. Title: req.Title,
  97. Description: req.Description,
  98. Price: req.Price,
  99. Currency: req.Currency,
  100. DurationMinutes: req.DurationMinutes,
  101. Status: "published",
  102. Tags: tags,
  103. }
  104. if err := h.serviceRepo.Create(r.Context(), svc); err != nil {
  105. writeError(w, http.StatusInternalServerError, err.Error())
  106. return
  107. }
  108. writeJSON(w, http.StatusCreated, svc)
  109. }
  110. func (h *ServiceHandler) Update(w http.ResponseWriter, r *http.Request) {
  111. id := chi.URLParam(r, "id")
  112. var req createServiceRequest
  113. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  114. writeError(w, http.StatusBadRequest, "invalid request body")
  115. return
  116. }
  117. svc, err := h.serviceRepo.GetByID(r.Context(), id)
  118. if err != nil {
  119. writeError(w, http.StatusInternalServerError, err.Error())
  120. return
  121. }
  122. if svc == nil {
  123. writeError(w, http.StatusNotFound, "service not found")
  124. return
  125. }
  126. svc.Title = req.Title
  127. svc.Description = req.Description
  128. svc.Price = req.Price
  129. svc.Currency = req.Currency
  130. svc.DurationMinutes = req.DurationMinutes
  131. if err := h.serviceRepo.Update(r.Context(), svc); err != nil {
  132. writeError(w, http.StatusInternalServerError, err.Error())
  133. return
  134. }
  135. writeJSON(w, http.StatusOK, svc)
  136. }
  137. func (h *ServiceHandler) Delete(w http.ResponseWriter, r *http.Request) {
  138. id := chi.URLParam(r, "id")
  139. if err := h.serviceRepo.SoftDelete(r.Context(), id); err != nil {
  140. writeError(w, http.StatusInternalServerError, err.Error())
  141. return
  142. }
  143. w.WriteHeader(http.StatusNoContent)
  144. }