diff --git a/src/main/java/com/krylov/refound/controller/MapController.java b/src/main/java/com/krylov/refound/controller/MapController.java new file mode 100644 index 0000000..8c04039 --- /dev/null +++ b/src/main/java/com/krylov/refound/controller/MapController.java @@ -0,0 +1,24 @@ +package com.krylov.refound.controller; + +import com.krylov.refound.dto.MapMarkerDto; +import com.krylov.refound.service.MapService; +import java.util.List; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/api/v1") +public class MapController { + + private final MapService mapService; + + @GetMapping("/posts/map") + public List getMapMarkers(@RequestParam(required = false) String type, @RequestParam(required = false) String category) { + return mapService.findAllWithCoordinates(type, category); + } + +} diff --git a/src/main/java/com/krylov/refound/dto/ChatPostDto.java b/src/main/java/com/krylov/refound/dto/ChatPostDto.java new file mode 100644 index 0000000..a84ac61 --- /dev/null +++ b/src/main/java/com/krylov/refound/dto/ChatPostDto.java @@ -0,0 +1,12 @@ +package com.krylov.refound.dto; + +import java.util.List; +import lombok.Data; + +/** Краткая информация об объявлении для списка чатов. */ +@Data +public class ChatPostDto { + private Long id; + private String title; + private List images; +} diff --git a/src/main/java/com/krylov/refound/dto/MapMarkerDto.java b/src/main/java/com/krylov/refound/dto/MapMarkerDto.java new file mode 100644 index 0000000..7d86a3a --- /dev/null +++ b/src/main/java/com/krylov/refound/dto/MapMarkerDto.java @@ -0,0 +1,12 @@ +package com.krylov.refound.dto; + +import com.krylov.refound.enums.PostType; + +public record MapMarkerDto( + Long id, + Double latitude, + Double longitude, + String title, + PostType type, + String imageUrl +) {} diff --git a/src/main/java/com/krylov/refound/dto/PostSearchRequest.java b/src/main/java/com/krylov/refound/dto/PostSearchRequest.java new file mode 100644 index 0000000..dfd22cb --- /dev/null +++ b/src/main/java/com/krylov/refound/dto/PostSearchRequest.java @@ -0,0 +1,12 @@ +package com.krylov.refound.dto; + +import java.util.List; +import lombok.Data; + +@Data +public class PostSearchRequest { + private String search; + private List cities; + private int page = 0; + private int size = 20; +} diff --git a/src/main/java/com/krylov/refound/repository/PostRepository.java b/src/main/java/com/krylov/refound/repository/PostRepository.java index bae7790..24b8fe6 100644 --- a/src/main/java/com/krylov/refound/repository/PostRepository.java +++ b/src/main/java/com/krylov/refound/repository/PostRepository.java @@ -1,5 +1,6 @@ package com.krylov.refound.repository; +import com.krylov.refound.dto.MapMarkerDto; import com.krylov.refound.entity.Post; import com.krylov.refound.enums.PostStatus; import java.time.LocalDateTime; @@ -26,11 +27,27 @@ public interface PostRepository extends JpaRepository, JpaSpecificat @Modifying @Transactional @Query("update Post p set p.status = :status where p.id = :id") - int updateStatus(@Param("id") Long id, @Param("status") PostStatus status); - - List findByCityContainingIgnoreCaseOrDistrictContainingIgnoreCase(String city, String district); + void updateStatus(@Param("id") Long id, @Param("status") PostStatus status); @Query("select distinct p.city from Post p where p.city is not null and p.status in :statuses order by p.city") List findDistinctCities(@Param("statuses") Collection statuses); + @Query(""" + select new com.krylov.refound.dto.MapMarkerDto( + p.id, + p.latitude, + p.longitude, + p.title, + p.type, + (select i.url from Image i + where i.post = p + and i.id = (select min(i2.id) from Image i2 where i2.post = p)) + ) + from Post p + where p.status not in :excludedStatuses + and p.latitude is not null + and p.longitude is not null + """) + List findMapMarkers(@Param("excludedStatuses") Collection excludedStatuses); + } \ No newline at end of file diff --git a/src/main/java/com/krylov/refound/service/MapService.java b/src/main/java/com/krylov/refound/service/MapService.java new file mode 100644 index 0000000..56f1e34 --- /dev/null +++ b/src/main/java/com/krylov/refound/service/MapService.java @@ -0,0 +1,19 @@ +package com.krylov.refound.service; + +import com.krylov.refound.dto.MapMarkerDto; +import com.krylov.refound.enums.PostStatus; +import com.krylov.refound.repository.PostRepository; +import java.util.List; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class MapService { + + private final PostRepository postRepository; + + public List findAllWithCoordinates(String type, String category) { + return postRepository.findMapMarkers(List.of(PostStatus.REJECTED, PostStatus.MODERATION)); + } +} diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index bea3f1e..9dea8c2 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -54,4 +54,4 @@ jwt: app: cors: - allowed-origins: http://localhost:5173,http://localhost:3000 \ No newline at end of file + allowed-origins: http://localhost:5173,http://localhost:3000,http://192.168.1.75:3000 \ No newline at end of file