Skip to content

Commit

Permalink
Merge pull request #41 from UMC-HACKATHON-SnapSpot/feat/#25
Browse files Browse the repository at this point in the history
feat: 반경 500미터 필터링 조회
  • Loading branch information
2hy2on authored Jul 4, 2024
2 parents dd2e131 + e653fa9 commit 3f77940
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.umc.hackaton.snapspot.spot.controller;

import com.umc.hackaton.snapspot.spot.dto.SpotRequestDto;
import com.umc.hackaton.snapspot.spot.dto.SpotResponseDto;
import com.umc.hackaton.snapspot.spot.entity.Spot;
import com.umc.hackaton.snapspot.spot.service.SpotService;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -74,5 +75,14 @@ public ResponseEntity<?> patchSpot(
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("스팟 수정에 실패하였습니다.");
}
}

@GetMapping("list")
public ResponseEntity<List<SpotResponseDto>> readNearSpotList(@RequestParam("latitude") double latitude, @RequestParam("longitude") double longitude){
return ResponseEntity.ok(spotService.readNearSpotList(latitude, longitude));
}

@GetMapping("list/category")
public ResponseEntity<List<SpotResponseDto>> readCategoryNearSpotList(@RequestParam("latitude") double latitude, @RequestParam("longitude") double longitude, @RequestParam("categoryId") Long categoryId){
List<SpotResponseDto> list = spotService.readNearSpotList(latitude, longitude);
return ResponseEntity.ok(spotService.readCategoryNearSpotList(list, categoryId));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.umc.hackaton.snapspot.spot.converter;

import com.umc.hackaton.snapspot.spot.dto.SpotResponseDto;
import com.umc.hackaton.snapspot.spot.entity.Spot;

import java.util.List;
import java.util.stream.Collectors;

public class SpotConverter {
public static List<SpotResponseDto>toDtoList(List<Spot> spotList){
return spotList.stream()
.map(SpotConverter::toDto)
.collect(Collectors.toList());
}

public static SpotResponseDto toDto(Spot spot){
return SpotResponseDto.builder()
.spotId(spot.getId())
.imgUrl(spot.getImgUrl())
.likeNum(spot.getLikeNum())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
package com.umc.hackaton.snapspot.spot.dto;

import lombok.Builder;

@Builder
public class SpotResponseDto {
Long spotId;
String imgUrl;

Long likeNum;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@
import com.umc.hackaton.snapspot.spot.entity.Spot;
import com.umc.hackaton.snapspot.user.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface SpotRepository extends JpaRepository<Spot, Long> {
List<Spot> findAllByUser(User user);

@Query(value = "SELECT * FROM Spot s " +
"WHERE (6371 * 2 * ASIN(SQRT(POWER(SIN((RADIANS(:latitude) - RADIANS(s.latitude)) / 2), 2) + " +
"COS(RADIANS(:latitude)) * COS(RADIANS(s.latitude)) * POWER(SIN((RADIANS(:longitude) - RADIANS(s.longitude)) / 2), 2)))) < :distance", nativeQuery = true)
List<Spot> findSpotsWithinDistance(@Param("latitude") double latitude,
@Param("longitude") double longitude,
@Param("distance") double distance);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import com.umc.hackaton.snapspot.category.entity.CategorySpot;
import com.umc.hackaton.snapspot.category.repository.CategoryRepository;
import com.umc.hackaton.snapspot.category.repository.CategorySpotRepository;
import com.umc.hackaton.snapspot.spot.converter.SpotConverter;
import com.umc.hackaton.snapspot.spot.dto.SpotDto;
import com.umc.hackaton.snapspot.spot.dto.SpotRequestDto;
import com.umc.hackaton.snapspot.spot.dto.SpotResponseDto;
import com.umc.hackaton.snapspot.spot.entity.Spot;
import com.umc.hackaton.snapspot.spot.repository.SpotRepository;
import com.umc.hackaton.snapspot.user.entity.User;
Expand Down Expand Up @@ -77,4 +79,9 @@ public Spot updateSpot(Long spotId, SpotRequestDto dto) {
Spot spot = spotRepository.findById(spotId).orElse(null);
return spot.update(dto);
}

public List<SpotResponseDto> readNearSpotList(double latitude, double longitude) {
List<Spot> spotList = spotRepository.findSpotsWithinDistance(latitude, longitude, 0.5);
return SpotConverter.toDtoList(spotList);
}
}

0 comments on commit 3f77940

Please sign in to comment.