places.go 3.8 KB

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