places.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package services
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/photoplaces/backend/internal/models"
  6. "github.com/photoplaces/backend/internal/repository"
  7. )
  8. type PlaceService struct {
  9. placeRepo *repository.PlaceRepo
  10. }
  11. func NewPlaceService(placeRepo *repository.PlaceRepo) *PlaceService {
  12. return &PlaceService{placeRepo: placeRepo}
  13. }
  14. type CreatePlaceInput struct {
  15. OwnerID string
  16. Type string
  17. Title string
  18. Description *string
  19. Address *string
  20. Lat float64
  21. Lng float64
  22. CoverImage *string
  23. AccessInfo *string
  24. HourlyRate *int
  25. Currency string
  26. MinHours int
  27. Tags []models.Tag
  28. Features []models.Feature
  29. }
  30. func (s *PlaceService) Create(ctx context.Context, input CreatePlaceInput) (*models.Place, error) {
  31. place := &models.Place{
  32. Type: input.Type,
  33. OwnerID: input.OwnerID,
  34. Title: input.Title,
  35. Description: input.Description,
  36. Address: input.Address,
  37. Lat: input.Lat,
  38. Lng: input.Lng,
  39. CoverImage: input.CoverImage,
  40. AccessInfo: input.AccessInfo,
  41. Status: "pending_moderation",
  42. HourlyRate: input.HourlyRate,
  43. Currency: input.Currency,
  44. MinHours: input.MinHours,
  45. Tags: input.Tags,
  46. Features: input.Features,
  47. }
  48. if err := s.placeRepo.Create(ctx, place); err != nil {
  49. return nil, fmt.Errorf("create place: %w", err)
  50. }
  51. return place, nil
  52. }
  53. func (s *PlaceService) GetByID(ctx context.Context, id string, fetchTags bool) (*models.Place, error) {
  54. place, err := s.placeRepo.GetByID(ctx, id)
  55. if err != nil {
  56. return nil, err
  57. }
  58. if place == nil {
  59. return nil, nil
  60. }
  61. if fetchTags {
  62. tags, err := s.placeRepo.GetTags(ctx, id)
  63. if err != nil {
  64. return nil, err
  65. }
  66. place.Tags = tags
  67. features, err := s.placeRepo.GetFeatures(ctx, id)
  68. if err != nil {
  69. return nil, err
  70. }
  71. place.Features = features
  72. }
  73. return place, nil
  74. }
  75. func (s *PlaceService) List(ctx context.Context, filter models.PlaceFilter) ([]*models.Place, error) {
  76. return s.placeRepo.List(ctx, filter)
  77. }
  78. type UpdatePlaceInput struct {
  79. ID string
  80. OwnerID string
  81. Title *string
  82. Description *string
  83. Address *string
  84. Lat *float64
  85. Lng *float64
  86. CoverImage *string
  87. AccessInfo *string
  88. HourlyRate *int
  89. Currency *string
  90. MinHours *int
  91. }
  92. func (s *PlaceService) Update(ctx context.Context, input UpdatePlaceInput, isModerator bool) (*models.Place, error) {
  93. place, err := s.placeRepo.GetByID(ctx, input.ID)
  94. if err != nil {
  95. return nil, err
  96. }
  97. if place == nil {
  98. return nil, nil
  99. }
  100. if !isModerator && place.OwnerID != input.OwnerID {
  101. return nil, fmt.Errorf("not your place")
  102. }
  103. if input.Title != nil { place.Title = *input.Title }
  104. if input.Description != nil { place.Description = input.Description }
  105. if input.Address != nil { place.Address = input.Address }
  106. if input.Lat != nil { place.Lat = *input.Lat }
  107. if input.Lng != nil { place.Lng = *input.Lng }
  108. if input.CoverImage != nil { place.CoverImage = input.CoverImage }
  109. if input.AccessInfo != nil { place.AccessInfo = input.AccessInfo }
  110. if input.HourlyRate != nil { place.HourlyRate = input.HourlyRate }
  111. if input.Currency != nil { place.Currency = *input.Currency }
  112. if input.MinHours != nil { place.MinHours = *input.MinHours }
  113. if !isModerator {
  114. place.Status = "pending_moderation"
  115. }
  116. if err := s.placeRepo.Update(ctx, place); err != nil {
  117. return nil, err
  118. }
  119. return place, nil
  120. }
  121. func (s *PlaceService) Moderate(ctx context.Context, id, action, comment, moderatorID string) error {
  122. var status string
  123. switch action {
  124. case "approve":
  125. status = "published"
  126. case "reject":
  127. status = "rejected"
  128. default:
  129. return fmt.Errorf("unknown action: %s", action)
  130. }
  131. return s.placeRepo.UpdateStatus(ctx, id, status, comment)
  132. }
  133. func (s *PlaceService) Delete(ctx context.Context, id string) error {
  134. return s.placeRepo.SoftDelete(ctx, id)
  135. }