places.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. package services
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "errors"
  6. "fmt"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "gogs.fxtmmsk.ru/foxtime/photoplaces/backend/internal/models"
  11. )
  12. func encodeCursor(p *models.Place) string {
  13. s := p.ID + "|" + strconv.FormatInt(p.CreatedAt.UnixNano(), 10) + "|" + fmt.Sprintf("%.6f", p.Rating)
  14. return base64.RawURLEncoding.EncodeToString([]byte(s))
  15. }
  16. func DecodeCursor(cursor string) (id string, createdAt time.Time, rating float64, err error) {
  17. b, err := base64.RawURLEncoding.DecodeString(cursor)
  18. if err != nil {
  19. return "", time.Time{}, 0, fmt.Errorf("decode cursor: %w", err)
  20. }
  21. parts := strings.SplitN(string(b), "|", 3)
  22. if len(parts) < 2 {
  23. return "", time.Time{}, 0, fmt.Errorf("invalid cursor format")
  24. }
  25. id = parts[0]
  26. createdAtUnix, err := strconv.ParseInt(parts[1], 10, 64)
  27. if err != nil {
  28. return "", time.Time{}, 0, fmt.Errorf("parse cursor created_at: %w", err)
  29. }
  30. createdAt = time.Unix(0, createdAtUnix)
  31. if len(parts) > 2 {
  32. rating, _ = strconv.ParseFloat(parts[2], 64)
  33. }
  34. return
  35. }
  36. var (
  37. ErrNotYourPlace = errors.New("not your place")
  38. ErrPlaceNotFound = errors.New("place not found")
  39. )
  40. type PlaceRepo interface {
  41. Create(ctx context.Context, place *models.Place) error
  42. GetByID(ctx context.Context, id string) (*models.Place, error)
  43. List(ctx context.Context, filter models.PlaceFilter) ([]*models.Place, error)
  44. Update(ctx context.Context, place *models.Place) error
  45. UpdateStatus(ctx context.Context, id, status, comment string) error
  46. SoftDelete(ctx context.Context, id string) error
  47. SetTags(ctx context.Context, placeID string, tags []models.Tag) error
  48. SetFeatures(ctx context.Context, placeID string, features []models.Feature) error
  49. GetTags(ctx context.Context, placeID string) ([]models.Tag, error)
  50. GetFeatures(ctx context.Context, placeID string) ([]models.Feature, error)
  51. GetTagsBatch(ctx context.Context, placeIDs []string) (map[string][]models.Tag, error)
  52. GetFeaturesBatch(ctx context.Context, placeIDs []string) (map[string][]models.Feature, error)
  53. }
  54. type PlaceService struct {
  55. placeRepo PlaceRepo
  56. }
  57. func NewPlaceService(placeRepo PlaceRepo) *PlaceService {
  58. return &PlaceService{placeRepo: placeRepo}
  59. }
  60. type CreatePlaceInput struct {
  61. OwnerID string
  62. Type string
  63. Title string
  64. Description *string
  65. Address *string
  66. Lat float64
  67. Lng float64
  68. CoverImage *string
  69. AccessInfo *string
  70. HourlyRate *int
  71. Currency string
  72. MinHours int
  73. Tags []models.Tag
  74. Features []models.Feature
  75. Status string
  76. }
  77. func (s *PlaceService) Create(ctx context.Context, input CreatePlaceInput) (*models.Place, error) {
  78. status := input.Status
  79. if status == "" {
  80. status = "pending_moderation"
  81. }
  82. place := &models.Place{
  83. Type: input.Type,
  84. OwnerID: input.OwnerID,
  85. Title: input.Title,
  86. Description: input.Description,
  87. Address: input.Address,
  88. Lat: input.Lat,
  89. Lng: input.Lng,
  90. CoverImage: input.CoverImage,
  91. AccessInfo: input.AccessInfo,
  92. Status: status,
  93. HourlyRate: input.HourlyRate,
  94. Currency: input.Currency,
  95. MinHours: input.MinHours,
  96. Tags: input.Tags,
  97. Features: input.Features,
  98. }
  99. if err := s.placeRepo.Create(ctx, place); err != nil {
  100. return nil, fmt.Errorf("create place: %w", err)
  101. }
  102. return place, nil
  103. }
  104. func (s *PlaceService) GetByID(ctx context.Context, id string, fetchTags bool) (*models.Place, error) {
  105. place, err := s.placeRepo.GetByID(ctx, id)
  106. if err != nil {
  107. return nil, err
  108. }
  109. if place == nil {
  110. return nil, nil
  111. }
  112. if fetchTags {
  113. tags, err := s.placeRepo.GetTags(ctx, id)
  114. if err != nil {
  115. return nil, err
  116. }
  117. place.Tags = tags
  118. features, err := s.placeRepo.GetFeatures(ctx, id)
  119. if err != nil {
  120. return nil, err
  121. }
  122. place.Features = features
  123. }
  124. return place, nil
  125. }
  126. func (s *PlaceService) List(ctx context.Context, filter models.PlaceFilter) (*models.PaginatedPlaces, error) {
  127. places, err := s.placeRepo.List(ctx, filter)
  128. if err != nil {
  129. return nil, err
  130. }
  131. if filter.IncludeTagsFeatures && len(places) > 0 {
  132. placeIDs := make([]string, len(places))
  133. for i, p := range places {
  134. placeIDs[i] = p.ID
  135. }
  136. tagsMap, err := s.placeRepo.GetTagsBatch(ctx, placeIDs)
  137. if err != nil {
  138. return nil, err
  139. }
  140. featuresMap, err := s.placeRepo.GetFeaturesBatch(ctx, placeIDs)
  141. if err != nil {
  142. return nil, err
  143. }
  144. for _, p := range places {
  145. if tags, ok := tagsMap[p.ID]; ok {
  146. p.Tags = tags
  147. }
  148. if features, ok := featuresMap[p.ID]; ok {
  149. p.Features = features
  150. }
  151. }
  152. }
  153. limit := filter.Limit()
  154. hasMore := len(places) > limit
  155. if hasMore {
  156. places = places[:limit]
  157. }
  158. var nextCursor string
  159. if hasMore && len(places) > 0 {
  160. nextCursor = encodeCursor(places[len(places)-1])
  161. }
  162. return &models.PaginatedPlaces{
  163. Data: places,
  164. NextCursor: nextCursor,
  165. HasMore: hasMore,
  166. }, nil
  167. }
  168. type UpdatePlaceInput struct {
  169. ID string
  170. OwnerID string
  171. Title *string
  172. Description *string
  173. Address *string
  174. Lat *float64
  175. Lng *float64
  176. CoverImage *string
  177. AccessInfo *string
  178. HourlyRate *int
  179. Currency *string
  180. MinHours *int
  181. }
  182. func (s *PlaceService) Update(ctx context.Context, input UpdatePlaceInput, isModerator bool) (*models.Place, error) {
  183. place, err := s.placeRepo.GetByID(ctx, input.ID)
  184. if err != nil {
  185. return nil, err
  186. }
  187. if place == nil {
  188. return nil, nil
  189. }
  190. if !isModerator && place.OwnerID != input.OwnerID {
  191. return nil, fmt.Errorf("%w: user %s tried to update place %s", ErrNotYourPlace, input.OwnerID, input.ID)
  192. }
  193. if input.Title != nil { place.Title = *input.Title }
  194. if input.Description != nil { place.Description = input.Description }
  195. if input.Address != nil { place.Address = input.Address }
  196. if input.Lat != nil { place.Lat = *input.Lat }
  197. if input.Lng != nil { place.Lng = *input.Lng }
  198. if input.CoverImage != nil { place.CoverImage = input.CoverImage }
  199. if input.AccessInfo != nil { place.AccessInfo = input.AccessInfo }
  200. if input.HourlyRate != nil { place.HourlyRate = input.HourlyRate }
  201. if input.Currency != nil { place.Currency = *input.Currency }
  202. if input.MinHours != nil { place.MinHours = *input.MinHours }
  203. if !isModerator {
  204. place.Status = "pending_moderation"
  205. }
  206. if err := s.placeRepo.Update(ctx, place); err != nil {
  207. return nil, err
  208. }
  209. return place, nil
  210. }
  211. func (s *PlaceService) Moderate(ctx context.Context, id, action, comment, moderatorID string) error {
  212. var status string
  213. switch action {
  214. case "approve":
  215. status = "published"
  216. case "reject":
  217. status = "rejected"
  218. case "rework":
  219. status = "revision"
  220. case "revoke":
  221. status = "pending_moderation"
  222. default:
  223. return fmt.Errorf("unknown action: %s", action)
  224. }
  225. return s.placeRepo.UpdateStatus(ctx, id, status, comment)
  226. }
  227. func (s *PlaceService) Delete(ctx context.Context, id string) error {
  228. return s.placeRepo.SoftDelete(ctx, id)
  229. }