-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from GDSC-snowflowerthon/feat/shelter-13
[feat] 대피소 목록 가져오기 및 검색 #13
- Loading branch information
Showing
6 changed files
with
150 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/main/java/com/ALGo/ALGo_server/repository/ShelterRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/ALGo/ALGo_server/shelter/controller/ShelterController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/ALGo/ALGo_server/shelter/dto/ShelterResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/ALGo/ALGo_server/shelter/service/ShelterService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
} |