diff --git a/src/main/java/com/spoony/spoony_server/domain/location/controller/LocationController.java b/src/main/java/com/spoony/spoony_server/domain/location/controller/LocationController.java new file mode 100644 index 0000000..97a6925 --- /dev/null +++ b/src/main/java/com/spoony/spoony_server/domain/location/controller/LocationController.java @@ -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> searchLocations(@RequestParam String query) { + LocationResponseListDTO locationResponseListDTO = locationService.searchLocationsByQuery(query); + return ResponseEntity.status(HttpStatus.OK).body(ResponseDTO.success(locationResponseListDTO)); + } +} diff --git a/src/main/java/com/spoony/spoony_server/domain/location/dto/response/LocationResponseDTO.java b/src/main/java/com/spoony/spoony_server/domain/location/dto/response/LocationResponseDTO.java new file mode 100644 index 0000000..b131e5f --- /dev/null +++ b/src/main/java/com/spoony/spoony_server/domain/location/dto/response/LocationResponseDTO.java @@ -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) { +} diff --git a/src/main/java/com/spoony/spoony_server/domain/location/dto/response/LocationResponseListDTO.java b/src/main/java/com/spoony/spoony_server/domain/location/dto/response/LocationResponseListDTO.java new file mode 100644 index 0000000..caa15b4 --- /dev/null +++ b/src/main/java/com/spoony/spoony_server/domain/location/dto/response/LocationResponseListDTO.java @@ -0,0 +1,6 @@ +package com.spoony.spoony_server.domain.location.dto.response; + +import java.util.List; + +public record LocationResponseListDTO(List locationResponseList) { +} diff --git a/src/main/java/com/spoony/spoony_server/domain/location/dto/response/LocationTypeDTO.java b/src/main/java/com/spoony/spoony_server/domain/location/dto/response/LocationTypeDTO.java new file mode 100644 index 0000000..c9122c6 --- /dev/null +++ b/src/main/java/com/spoony/spoony_server/domain/location/dto/response/LocationTypeDTO.java @@ -0,0 +1,4 @@ +package com.spoony.spoony_server.domain.location.dto.response; + +public record LocationTypeDTO(Long locationTypeId, String locationTypeName, Double scope) { +} diff --git a/src/main/java/com/spoony/spoony_server/domain/location/entity/LocationEntity.java b/src/main/java/com/spoony/spoony_server/domain/location/entity/LocationEntity.java new file mode 100644 index 0000000..6af55c4 --- /dev/null +++ b/src/main/java/com/spoony/spoony_server/domain/location/entity/LocationEntity.java @@ -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; + } +} diff --git a/src/main/java/com/spoony/spoony_server/domain/location/entity/LocationTypeEntity.java b/src/main/java/com/spoony/spoony_server/domain/location/entity/LocationTypeEntity.java new file mode 100644 index 0000000..6c91f13 --- /dev/null +++ b/src/main/java/com/spoony/spoony_server/domain/location/entity/LocationTypeEntity.java @@ -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; +} diff --git a/src/main/java/com/spoony/spoony_server/domain/location/repository/LocationRepository.java b/src/main/java/com/spoony/spoony_server/domain/location/repository/LocationRepository.java new file mode 100644 index 0000000..45658c1 --- /dev/null +++ b/src/main/java/com/spoony/spoony_server/domain/location/repository/LocationRepository.java @@ -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 { + List findByLocationNameContaining(@Param("query") String query); +} diff --git a/src/main/java/com/spoony/spoony_server/domain/location/service/LocationService.java b/src/main/java/com/spoony/spoony_server/domain/location/service/LocationService.java new file mode 100644 index 0000000..14d1c27 --- /dev/null +++ b/src/main/java/com/spoony/spoony_server/domain/location/service/LocationService.java @@ -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 locationEntityList = locationRepository.findByLocationNameContaining(query); + + List 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); + } +}