bookings.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Package repository
  2. package repository
  3. import (
  4. "context"
  5. "errors"
  6. "fmt"
  7. "math"
  8. "github.com/jackc/pgx/v5"
  9. "github.com/jackc/pgx/v5/pgxpool"
  10. "gogs.fxtmmsk.ru/foxtime/photoplaces/backend/internal/models"
  11. )
  12. var ErrPlaceNotFound = errors.New("place not found")
  13. type BookingRepo struct {
  14. pool *pgxpool.Pool
  15. }
  16. func NewBookingRepo(pool *pgxpool.Pool) *BookingRepo {
  17. return &BookingRepo{pool: pool}
  18. }
  19. func (r *BookingRepo) Create(ctx context.Context, b *models.Booking) error {
  20. tx, err := r.pool.Begin(ctx)
  21. if err != nil {
  22. return fmt.Errorf("begin tx: %w", err)
  23. }
  24. defer tx.Rollback(ctx)
  25. var hourlyRate *int
  26. err = tx.QueryRow(ctx,
  27. `SELECT hourly_rate, currency FROM places WHERE id = $1 AND deleted_at IS NULL FOR UPDATE`,
  28. b.PlaceID).Scan(&hourlyRate, &b.Currency)
  29. if err != nil {
  30. if errors.Is(err, pgx.ErrNoRows) {
  31. return ErrPlaceNotFound
  32. }
  33. return fmt.Errorf("lock place: %w", err)
  34. }
  35. if hourlyRate != nil {
  36. hours := math.Ceil(b.EndTime.Sub(b.StartTime).Hours())
  37. if hours < 1 {
  38. hours = 1
  39. }
  40. total := *hourlyRate * int(hours)
  41. b.TotalPrice = &total
  42. }
  43. err = tx.QueryRow(ctx,
  44. `INSERT INTO bookings (place_id, user_id, start_time, end_time, status, total_price, currency, comment)
  45. VALUES ($1, $2, $3, $4, 'pending', $5, $6, $7)
  46. RETURNING id, created_at, updated_at`,
  47. b.PlaceID, b.UserID, b.StartTime, b.EndTime, b.TotalPrice, b.Currency, b.Comment,
  48. ).Scan(&b.ID, &b.CreatedAt, &b.UpdatedAt)
  49. if err != nil {
  50. return fmt.Errorf("insert booking: %w", err)
  51. }
  52. return tx.Commit(ctx)
  53. }
  54. func (r *BookingRepo) GetByID(ctx context.Context, id string) (*models.Booking, error) {
  55. row := r.pool.QueryRow(ctx,
  56. `SELECT id, place_id, user_id, start_time, end_time, status, total_price, currency, comment,
  57. created_at, updated_at
  58. FROM bookings WHERE id = $1`, id)
  59. var b models.Booking
  60. err := row.Scan(&b.ID, &b.PlaceID, &b.UserID, &b.StartTime, &b.EndTime,
  61. &b.Status, &b.TotalPrice, &b.Currency, &b.Comment, &b.CreatedAt, &b.UpdatedAt)
  62. if err != nil {
  63. if err == pgx.ErrNoRows {
  64. return nil, nil
  65. }
  66. return nil, err
  67. }
  68. return &b, nil
  69. }
  70. func (r *BookingRepo) ListByUser(ctx context.Context, userID string) ([]*models.Booking, error) {
  71. rows, err := r.pool.Query(ctx,
  72. `SELECT id, place_id, user_id, start_time, end_time, status, total_price, currency, comment,
  73. created_at, updated_at
  74. FROM bookings WHERE user_id = $1 ORDER BY start_time DESC`, userID)
  75. if err != nil {
  76. return nil, err
  77. }
  78. defer rows.Close()
  79. return scanBookings(rows)
  80. }
  81. func (r *BookingRepo) ListByPlace(ctx context.Context, placeID string) ([]*models.Booking, error) {
  82. rows, err := r.pool.Query(ctx,
  83. `SELECT id, place_id, user_id, start_time, end_time, status, total_price, currency, comment,
  84. created_at, updated_at
  85. FROM bookings WHERE place_id = $1 ORDER BY start_time DESC`, placeID)
  86. if err != nil {
  87. return nil, err
  88. }
  89. defer rows.Close()
  90. return scanBookings(rows)
  91. }
  92. func (r *BookingRepo) UpdateStatusIfPending(ctx context.Context, id, status string) (bool, error) {
  93. tag, err := r.pool.Exec(ctx,
  94. `UPDATE bookings SET status=$1, updated_at=now() WHERE id=$2 AND status='pending'`, status, id)
  95. if err != nil {
  96. return false, fmt.Errorf("update status if pending: %w", err)
  97. }
  98. return tag.RowsAffected() > 0, nil
  99. }
  100. func (r *BookingRepo) GetPlaceMinHours(ctx context.Context, placeID string) (int, error) {
  101. var minHours int
  102. err := r.pool.QueryRow(ctx,
  103. `SELECT min_hours FROM places WHERE id = $1 AND deleted_at IS NULL`, placeID).Scan(&minHours)
  104. if err != nil {
  105. if errors.Is(err, pgx.ErrNoRows) {
  106. return 0, ErrPlaceNotFound
  107. }
  108. return 0, fmt.Errorf("get place min_hours: %w", err)
  109. }
  110. return minHours, nil
  111. }
  112. func scanBookings(rows pgx.Rows) ([]*models.Booking, error) {
  113. var bookings []*models.Booking
  114. for rows.Next() {
  115. var b models.Booking
  116. if err := rows.Scan(&b.ID, &b.PlaceID, &b.UserID, &b.StartTime, &b.EndTime,
  117. &b.Status, &b.TotalPrice, &b.Currency, &b.Comment, &b.CreatedAt, &b.UpdatedAt); err != nil {
  118. return nil, err
  119. }
  120. bookings = append(bookings, &b)
  121. }
  122. return bookings, nil
  123. }