|
|
@@ -38,18 +38,25 @@ func DecodeCursor(cursor string) (id string, createdAt time.Time, rating float64
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+type ObjectStorager interface {
|
|
|
+ DeleteObjects(ctx context.Context, urls []string) error
|
|
|
+}
|
|
|
+
|
|
|
var (
|
|
|
- ErrNotYourPlace = errors.New("not your place")
|
|
|
+ ErrNotYourPlace = errors.New("not your place")
|
|
|
ErrPlaceNotFound = errors.New("place not found")
|
|
|
)
|
|
|
|
|
|
type PlaceRepo interface {
|
|
|
Create(ctx context.Context, place *models.Place) error
|
|
|
GetByID(ctx context.Context, id string) (*models.Place, error)
|
|
|
+ GetByIDRaw(ctx context.Context, id string) (*models.Place, error)
|
|
|
List(ctx context.Context, filter models.PlaceFilter) ([]*models.Place, error)
|
|
|
Update(ctx context.Context, place *models.Place) error
|
|
|
UpdateStatus(ctx context.Context, id, status, comment string) error
|
|
|
SoftDelete(ctx context.Context, id string) error
|
|
|
+ HardDelete(ctx context.Context, id string) error
|
|
|
+ GetPlaceImages(ctx context.Context, placeID string) ([]models.PlaceImage, error)
|
|
|
SetTags(ctx context.Context, placeID string, tags []models.Tag) error
|
|
|
SetFeatures(ctx context.Context, placeID string, features []models.Feature) error
|
|
|
GetTags(ctx context.Context, placeID string) ([]models.Tag, error)
|
|
|
@@ -60,10 +67,11 @@ type PlaceRepo interface {
|
|
|
|
|
|
type PlaceService struct {
|
|
|
placeRepo PlaceRepo
|
|
|
+ storage ObjectStorager
|
|
|
}
|
|
|
|
|
|
-func NewPlaceService(placeRepo PlaceRepo) *PlaceService {
|
|
|
- return &PlaceService{placeRepo: placeRepo}
|
|
|
+func NewPlaceService(placeRepo PlaceRepo, storage ObjectStorager) *PlaceService {
|
|
|
+ return &PlaceService{placeRepo: placeRepo, storage: storage}
|
|
|
}
|
|
|
|
|
|
type CreatePlaceInput struct {
|
|
|
@@ -277,3 +285,34 @@ func (s *PlaceService) Moderate(ctx context.Context, id, action, comment, modera
|
|
|
func (s *PlaceService) Delete(ctx context.Context, id string) error {
|
|
|
return s.placeRepo.SoftDelete(ctx, id)
|
|
|
}
|
|
|
+
|
|
|
+func (s *PlaceService) HardDelete(ctx context.Context, id string) error {
|
|
|
+ place, err := s.placeRepo.GetByIDRaw(ctx, id)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ if place == nil {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ var urls []string
|
|
|
+ if place.CoverImage != nil {
|
|
|
+ urls = append(urls, *place.CoverImage)
|
|
|
+ }
|
|
|
+
|
|
|
+ images, err := s.placeRepo.GetPlaceImages(ctx, id)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ for _, img := range images {
|
|
|
+ urls = append(urls, img.URL)
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(urls) > 0 {
|
|
|
+ if err := s.storage.DeleteObjects(ctx, urls); err != nil {
|
|
|
+ return fmt.Errorf("delete place files: %w", err)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return s.placeRepo.HardDelete(ctx, id)
|
|
|
+}
|