Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] 지역 리스트 검색 API 구현 완료 #50

Merged
merged 2 commits into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.spoony.spoony_server.domain.location.controller;

import com.spoony.spoony_server.common.dto.ResponseDTO;
import com.spoony.spoony_server.domain.location.dto.response.LocationResponseListDTO;
import com.spoony.spoony_server.domain.location.service.LocationService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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/location")
public class LocationController {

private final LocationService locationService;

@GetMapping("/search")
public ResponseEntity<ResponseDTO<LocationResponseListDTO>> searchLocations(@RequestParam String query) {
LocationResponseListDTO locationResponseListDTO = locationService.searchLocationsByQuery(query);
return ResponseEntity.status(HttpStatus.OK).body(ResponseDTO.success(locationResponseListDTO));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.spoony.spoony_server.domain.location.dto.response;

public record LocationResponseDTO(Long locationId,
String locationName,
String locationAddress,
LocationTypeDTO locationType,
Double longitude,
Double latitude) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.spoony.spoony_server.domain.location.dto.response;

import java.util.List;

public record LocationResponseListDTO(List<LocationResponseDTO> locationResponseList) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.spoony.spoony_server.domain.location.dto.response;

public record LocationTypeDTO(Long locationTypeId, String locationTypeName, Double scope) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.spoony.spoony_server.domain.location.entity;

import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Table(name = "location")
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class LocationEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long locationId;
private String locationName;
private String locationAddress;
private Double latitude;
private Double longitude;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "location_type_id")
private LocationTypeEntity locationTypeEntity;

@Builder
public LocationEntity(Long locationId,
String locationName,
String locationAddress,
Double latitude,
Double longitude,
LocationTypeEntity locationTypeEntity) {
this.locationId = locationId;
this.locationName = locationName;
this.locationAddress = locationAddress;
this.latitude = latitude;
this.longitude = longitude;
this.locationTypeEntity = locationTypeEntity;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.spoony.spoony_server.domain.location.entity;

import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Table(name = "location_type")
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class LocationTypeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long locationTypeId;
private String locationTypeName;
private Double scope;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.spoony.spoony_server.domain.location.repository;

import com.spoony.spoony_server.domain.location.entity.LocationEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface LocationRepository extends JpaRepository<LocationEntity, Long> {
List<LocationEntity> findByLocationNameContaining(@Param("query") String query);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.spoony.spoony_server.domain.location.service;

import com.spoony.spoony_server.domain.location.dto.response.LocationResponseDTO;
import com.spoony.spoony_server.domain.location.dto.response.LocationResponseListDTO;
import com.spoony.spoony_server.domain.location.dto.response.LocationTypeDTO;
import com.spoony.spoony_server.domain.location.entity.LocationEntity;
import com.spoony.spoony_server.domain.location.repository.LocationRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class LocationService {

private final LocationRepository locationRepository;

public LocationResponseListDTO searchLocationsByQuery(String query) {
List<LocationEntity> locationEntityList = locationRepository.findByLocationNameContaining(query);

List<LocationResponseDTO> locationResponseList = locationEntityList.stream()
.map(locationEntity -> new LocationResponseDTO(
locationEntity.getLocationId(),
locationEntity.getLocationName(),
locationEntity.getLocationAddress(),
new LocationTypeDTO(
locationEntity.getLocationTypeEntity().getLocationTypeId(),
locationEntity.getLocationTypeEntity().getLocationTypeName(),
locationEntity.getLocationTypeEntity().getScope()
),
locationEntity.getLongitude(),
locationEntity.getLatitude()
))
.toList();

return new LocationResponseListDTO(locationResponseList);
}
}