-
Notifications
You must be signed in to change notification settings - Fork 0
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 #98 from UMC-ON/feat/country
Feature: 국가와 도시를 조회할 수 있는 api
- Loading branch information
Showing
11 changed files
with
224 additions
and
20 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
24 changes: 24 additions & 0 deletions
24
src/main/java/com/on/server/domain/country/application/CityService.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,24 @@ | ||
package com.on.server.domain.country.application; | ||
|
||
import com.on.server.domain.country.domain.City; | ||
import com.on.server.domain.country.domain.repository.CityRepository; | ||
import com.on.server.domain.country.dto.CityDTO; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class CityService { | ||
|
||
private final CityRepository cityRepository; | ||
|
||
public List<CityDTO> getCitiesByCountry(String countryName) { | ||
List<City> cities = cityRepository.findByCountryName(countryName); | ||
return cities.stream() | ||
.map(CityDTO::fromEntity) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/on/server/domain/country/application/CountryService.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,24 @@ | ||
package com.on.server.domain.country.application; | ||
|
||
import com.on.server.domain.country.domain.Country; | ||
import com.on.server.domain.country.domain.repository.CountryRepository; | ||
import com.on.server.domain.country.dto.CountryDTO; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class CountryService { | ||
|
||
private final CountryRepository countryRepository; | ||
|
||
public List<CountryDTO> getAllCountries() { | ||
List<Country> countries = countryRepository.findAll(); | ||
return countries.stream() | ||
.map(CountryDTO::fromEntity) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/on/server/domain/country/domain/City.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,23 @@ | ||
package com.on.server.domain.country.domain; | ||
|
||
import com.on.server.global.domain.BaseEntity; | ||
import jakarta.persistence.*; | ||
import lombok.*; | ||
|
||
@Getter | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Entity | ||
@Table(name = "city") | ||
public class City extends BaseEntity { | ||
@Column(name = "city_code", nullable = false) | ||
private String code; | ||
|
||
@Column(name = "city_name", nullable = false) | ||
private String name; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "country_id") | ||
private Country country; | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/on/server/domain/country/domain/Country.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,28 @@ | ||
package com.on.server.domain.country.domain; | ||
|
||
import com.on.server.global.domain.BaseEntity; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Table; | ||
import lombok.*; | ||
|
||
@Getter | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Entity | ||
@Table(name = "country") | ||
public class Country extends BaseEntity { | ||
|
||
@Column(name = "country_code", nullable = false) | ||
private String code; | ||
|
||
@Column(name = "country_name", nullable = false) | ||
private String name; | ||
|
||
@Column(name = "country_flag", nullable = false) | ||
private String flag; | ||
|
||
@Column(name = "continent", nullable = false) | ||
private String continent; | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/on/server/domain/country/domain/repository/CityRepository.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,10 @@ | ||
package com.on.server.domain.country.domain.repository; | ||
|
||
import com.on.server.domain.country.domain.City; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
|
||
public interface CityRepository extends JpaRepository<City, String> { | ||
List<City> findByCountryName(String countryName); | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/on/server/domain/country/domain/repository/CountryRepository.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,7 @@ | ||
package com.on.server.domain.country.domain.repository; | ||
|
||
import com.on.server.domain.country.domain.Country; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface CountryRepository extends JpaRepository<Country, String> { | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/on/server/domain/country/dto/CityDTO.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,23 @@ | ||
package com.on.server.domain.country.dto; | ||
|
||
import com.on.server.domain.country.domain.City; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class CityDTO { | ||
private String code; | ||
private String name; | ||
|
||
public static CityDTO fromEntity(City city) { | ||
return CityDTO.builder() | ||
.code(city.getCode()) | ||
.name(city.getName()) | ||
.build(); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/on/server/domain/country/dto/CountryDTO.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,27 @@ | ||
package com.on.server.domain.country.dto; | ||
|
||
import com.on.server.domain.country.domain.Country; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class CountryDTO { | ||
private String code; | ||
private String name; | ||
private String flag; | ||
private String continent; | ||
|
||
public static CountryDTO fromEntity(Country country) { | ||
return CountryDTO.builder() | ||
.code(country.getCode()) | ||
.name(country.getName()) | ||
.flag(country.getFlag()) | ||
.continent(country.getContinent()) | ||
.build(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/on/server/domain/country/presentation/CityController.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,30 @@ | ||
package com.on.server.domain.country.presentation; | ||
|
||
import com.on.server.domain.country.application.CityService; | ||
import com.on.server.domain.country.dto.CityDTO; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.List; | ||
|
||
@Tag(name = "도시") | ||
@RestController | ||
@RequestMapping("/api/v1/city") | ||
@RequiredArgsConstructor | ||
public class CityController { | ||
|
||
private final CityService cityService; | ||
|
||
@Operation(summary = "특정 국가에 대한 도시 목록 조회") | ||
@GetMapping("/{countryName}") | ||
public ResponseEntity<List<CityDTO>> getCitiesByCountry(@PathVariable("countryName") String countryName) { | ||
List<CityDTO> cities = cityService.getCitiesByCountry(countryName); | ||
return ResponseEntity.ok(cities); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/on/server/domain/country/presentation/CountryController.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,28 @@ | ||
package com.on.server.domain.country.presentation; | ||
|
||
import com.on.server.domain.country.application.CountryService; | ||
import com.on.server.domain.country.dto.CountryDTO; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
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.RestController; | ||
|
||
import java.util.List; | ||
|
||
@Tag(name = "국가") | ||
@RestController | ||
@RequestMapping("/api/v1/country") | ||
@RequiredArgsConstructor | ||
public class CountryController { | ||
|
||
private final CountryService countryService; | ||
|
||
@Operation(summary = "전체 국가 조회") | ||
@GetMapping | ||
public ResponseEntity<List<CountryDTO>> getAllCountries() { | ||
return ResponseEntity.ok(countryService.getAllCountries()); | ||
} | ||
} |