Skip to content

Commit

Permalink
[feat] 대피소 목록 가져오기 및 검색 #13
Browse files Browse the repository at this point in the history
  • Loading branch information
ri-naa committed Jan 11, 2024
1 parent 7bb8e48 commit b1188f2
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.ALGo.ALGo_server.repository;

import com.ALGo.ALGo_server.entity.Shelter;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface ShelterRepository extends JpaRepository<Shelter, Long> {

List<Shelter> findAll();

List<Shelter> findByRegion(String region);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.ALGo.ALGo_server.shelter.controller;

import com.ALGo.ALGo_server.entity.Shelter;
import com.ALGo.ALGo_server.repository.ShelterRepository;
import com.ALGo.ALGo_server.shelter.dto.ShelterResponse;
import com.ALGo.ALGo_server.shelter.service.ShelterService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shelter")
public class ShelterController {

private final ShelterRepository shelterRepository;
private final ShelterService shelterService;

public ShelterController(ShelterRepository shelterRepository, ShelterService shelterService) {
this.shelterRepository = shelterRepository;
this.shelterService = shelterService;
}

@GetMapping
public List<Shelter> getAllShelters() {
return shelterRepository.findAll();
}

@PostMapping("/search/{region}")
public List<ShelterResponse> searchSheltersByRegion(@PathVariable String region) {
return shelterService.searchSheltersByRegion(region);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.ALGo.ALGo_server.shelter.dto;

import jakarta.persistence.Column;
import lombok.Data;

import java.util.List;

@Data
public class ShelterResponse {
private Long shelter_id;
private String region;
private String address;
private String description;

public ShelterResponse(Long shelter_id, String region, String address, String description){
this.shelter_id = shelter_id;
this.region = region;
this.address = address;
this.description = description;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.ALGo.ALGo_server.shelter.service;

import com.ALGo.ALGo_server.entity.Shelter;
import com.ALGo.ALGo_server.repository.ShelterRepository;
import com.ALGo.ALGo_server.shelter.dto.ShelterResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
@RequiredArgsConstructor
@Slf4j
public class ShelterService {

private final ShelterRepository shelterRepository;

public List<ShelterResponse> searchSheltersByRegion(String region) {
List<Shelter> shelters = shelterRepository.findByRegion(region);
return convertToShelterResponseList(shelters);
}

private List<ShelterResponse> convertToShelterResponseList(List<Shelter> shelters) {
List<ShelterResponse> shelterResponses = new ArrayList<>();
for (Shelter shelter : shelters) {
ShelterResponse shelterResponse = new ShelterResponse(
shelter.getShelter_id(),
shelter.getRegion(),
shelter.getAddress(),
shelter.getDescription()
);
shelterResponses.add(shelterResponse);
}
return shelterResponses;
}


}

0 comments on commit b1188f2

Please sign in to comment.