bookings.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Package handlers
  2. package handlers
  3. import (
  4. "encoding/json"
  5. "errors"
  6. "net/http"
  7. "time"
  8. "github.com/go-chi/chi/v5"
  9. "github.com/jackc/pgx/v5/pgconn"
  10. "gogs.fxtmmsk.ru/foxtime/photoplaces/backend/internal/middleware"
  11. "gogs.fxtmmsk.ru/foxtime/photoplaces/backend/internal/models"
  12. "gogs.fxtmmsk.ru/foxtime/photoplaces/backend/internal/repository"
  13. "gogs.fxtmmsk.ru/foxtime/photoplaces/backend/internal/validator"
  14. )
  15. type BookingHandler struct {
  16. bookingRepo *repository.BookingRepo
  17. }
  18. func NewBookingHandler(bookingRepo *repository.BookingRepo) *BookingHandler {
  19. return &BookingHandler{bookingRepo: bookingRepo}
  20. }
  21. type createBookingRequest struct {
  22. PlaceID string `json:"place_id" validate:"required,uuid"`
  23. StartTime string `json:"start_time" validate:"required,datetime=2006-01-02T15:04:05Z07:00"`
  24. EndTime string `json:"end_time" validate:"required,datetime=2006-01-02T15:04:05Z07:00"`
  25. Comment *string `json:"comment" validate:"omitempty,max=1000"`
  26. }
  27. func (h *BookingHandler) Create(w http.ResponseWriter, r *http.Request) {
  28. userID := middleware.GetUserID(r.Context())
  29. r.Body = http.MaxBytesReader(w, r.Body, maxRequestBodySize)
  30. var req createBookingRequest
  31. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  32. writeError(w, http.StatusBadRequest, "invalid request body", err)
  33. return
  34. }
  35. if err := validator.Validate(req); err != nil {
  36. writeValidationError(w, err)
  37. return
  38. }
  39. start, err := time.Parse(time.RFC3339, req.StartTime)
  40. if err != nil {
  41. writeError(w, http.StatusBadRequest, "invalid start_time format, expected RFC3339", err)
  42. return
  43. }
  44. end, err := time.Parse(time.RFC3339, req.EndTime)
  45. if err != nil {
  46. writeError(w, http.StatusBadRequest, "invalid end_time format, expected RFC3339", err)
  47. return
  48. }
  49. if !end.After(start) {
  50. writeError(w, http.StatusBadRequest, "end_time must be after start_time", nil)
  51. return
  52. }
  53. minHours, err := h.bookingRepo.GetPlaceMinHours(r.Context(), req.PlaceID)
  54. if err != nil {
  55. if errors.Is(err, repository.ErrPlaceNotFound) {
  56. writeError(w, http.StatusNotFound, "place not found", nil)
  57. return
  58. }
  59. writeError(w, http.StatusInternalServerError, "failed to validate booking", err)
  60. return
  61. }
  62. if end.Sub(start) < time.Duration(minHours)*time.Hour {
  63. writeError(w, http.StatusBadRequest, "booking duration is less than minimum hours required", nil)
  64. return
  65. }
  66. booking := &models.Booking{
  67. PlaceID: req.PlaceID,
  68. UserID: userID,
  69. StartTime: start,
  70. EndTime: end,
  71. Comment: req.Comment,
  72. }
  73. if err := h.bookingRepo.Create(r.Context(), booking); err != nil {
  74. if errors.Is(err, repository.ErrPlaceNotFound) {
  75. writeError(w, http.StatusNotFound, "place not found", nil)
  76. return
  77. }
  78. var pgErr *pgconn.PgError
  79. if errors.As(err, &pgErr) && pgErr.Code == "23P01" { // exclusion_constraint_violation
  80. writeError(w, http.StatusConflict, "time slot is not available", err)
  81. return
  82. }
  83. writeError(w, http.StatusInternalServerError, "failed to create booking", err)
  84. return
  85. }
  86. writeJSON(w, http.StatusCreated, booking)
  87. }
  88. func (h *BookingHandler) ListMy(w http.ResponseWriter, r *http.Request) {
  89. userID := middleware.GetUserID(r.Context())
  90. bookings, err := h.bookingRepo.ListByUser(r.Context(), userID)
  91. if err != nil {
  92. writeError(w, http.StatusInternalServerError, "failed to list bookings", err)
  93. return
  94. }
  95. if bookings == nil {
  96. bookings = []*models.Booking{}
  97. }
  98. writeJSON(w, http.StatusOK, map[string]interface{}{"data": bookings})
  99. }
  100. func (h *BookingHandler) Cancel(w http.ResponseWriter, r *http.Request) {
  101. id := chi.URLParam(r, "id")
  102. userID := middleware.GetUserID(r.Context())
  103. booking, err := h.bookingRepo.GetByID(r.Context(), id)
  104. if err != nil {
  105. writeError(w, http.StatusInternalServerError, "failed to get booking", err)
  106. return
  107. }
  108. if booking == nil {
  109. writeError(w, http.StatusNotFound, "booking not found", nil)
  110. return
  111. }
  112. if booking.UserID != userID {
  113. writeError(w, http.StatusForbidden, "not your booking", nil)
  114. return
  115. }
  116. ok, err := h.bookingRepo.UpdateStatusIfPending(r.Context(), id, "cancelled")
  117. if err != nil {
  118. writeError(w, http.StatusInternalServerError, "failed to cancel booking", err)
  119. return
  120. }
  121. if !ok {
  122. writeError(w, http.StatusConflict, "booking cannot be cancelled in its current state", nil)
  123. return
  124. }
  125. writeJSON(w, http.StatusOK, map[string]string{"status": "cancelled"})
  126. }
  127. func (h *BookingHandler) Confirm(w http.ResponseWriter, r *http.Request) {
  128. id := chi.URLParam(r, "id")
  129. if err := h.bookingRepo.UpdateStatus(r.Context(), id, "confirmed"); err != nil {
  130. writeError(w, http.StatusInternalServerError, "failed to confirm booking", err)
  131. return
  132. }
  133. writeJSON(w, http.StatusOK, map[string]string{"status": "confirmed"})
  134. }