From e1f6337022e417308c3068bb7b4c9fe5efb87cf6 Mon Sep 17 00:00:00 2001 From: tinon1004 Date: Tue, 27 Feb 2024 09:01:02 +0900 Subject: [PATCH] =?UTF-8?q?#38=20refactor:=20=EC=98=A4=ED=83=80=20?= =?UTF-8?q?=EC=88=98=EC=A0=95=20=EB=B0=8F=20=ED=8C=8C=EC=9D=BC=EB=AA=85=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hertz/common/config/SecurityConfig.java | 5 +- .../AdministrativeAreaControllerV1.java | 90 +++++++++++++++++++ .../controller/SidoControllerV1.java | 23 ----- .../entity/AdministrativeAreaEmd.java | 36 ++++++++ .../entity/AdministrativeAreaSgg.java | 37 ++++++++ ...{Sido.java => AdministrativeAreaSido.java} | 15 ++-- .../administrative_area/entity/Emd.java | 4 - .../AdministrativeEmdRepository.java | 12 +++ .../AdministrativeSggRepository.java | 12 +++ .../AdministrativeSidoRepository.java | 8 ++ 10 files changed, 206 insertions(+), 36 deletions(-) create mode 100644 src/main/java/com/ajou/hertz/domain/administrative_area/controller/AdministrativeAreaControllerV1.java delete mode 100644 src/main/java/com/ajou/hertz/domain/administrative_area/controller/SidoControllerV1.java create mode 100644 src/main/java/com/ajou/hertz/domain/administrative_area/entity/AdministrativeAreaEmd.java create mode 100644 src/main/java/com/ajou/hertz/domain/administrative_area/entity/AdministrativeAreaSgg.java rename src/main/java/com/ajou/hertz/domain/administrative_area/entity/{Sido.java => AdministrativeAreaSido.java} (60%) delete mode 100644 src/main/java/com/ajou/hertz/domain/administrative_area/entity/Emd.java create mode 100644 src/main/java/com/ajou/hertz/domain/administrative_area/repository/AdministrativeEmdRepository.java create mode 100644 src/main/java/com/ajou/hertz/domain/administrative_area/repository/AdministrativeSggRepository.java create mode 100644 src/main/java/com/ajou/hertz/domain/administrative_area/repository/AdministrativeSidoRepository.java diff --git a/src/main/java/com/ajou/hertz/common/config/SecurityConfig.java b/src/main/java/com/ajou/hertz/common/config/SecurityConfig.java index 2e9831c..e5529ff 100644 --- a/src/main/java/com/ajou/hertz/common/config/SecurityConfig.java +++ b/src/main/java/com/ajou/hertz/common/config/SecurityConfig.java @@ -45,7 +45,10 @@ public class SecurityConfig { "/v*/users", POST, "/v*/users/existence", GET, "/v*/users/email", GET, - "/v*/administrative-area", GET + "/v*/administrative-area", GET, + "/v*/administrative-area/sgg", GET, + "/v*/administrative-area/sgg/emd", GET + ); @Bean diff --git a/src/main/java/com/ajou/hertz/domain/administrative_area/controller/AdministrativeAreaControllerV1.java b/src/main/java/com/ajou/hertz/domain/administrative_area/controller/AdministrativeAreaControllerV1.java new file mode 100644 index 0000000..fce7653 --- /dev/null +++ b/src/main/java/com/ajou/hertz/domain/administrative_area/controller/AdministrativeAreaControllerV1.java @@ -0,0 +1,90 @@ +package com.ajou.hertz.domain.administrative_area.controller; + + +import java.util.List; +import java.util.stream.Collectors; + +import org.springframework.beans.factory.annotation.Autowired; +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; + +import com.ajou.hertz.domain.administrative_area.entity.AdministrativeAreaEmd; +import com.ajou.hertz.domain.administrative_area.entity.AdministrativeAreaSgg; +import com.ajou.hertz.domain.administrative_area.entity.AdministrativeAreaSido; +import com.ajou.hertz.domain.administrative_area.repository.AdministrativeEmdRepository; +import com.ajou.hertz.domain.administrative_area.repository.AdministrativeSggRepository; +import com.ajou.hertz.domain.administrative_area.repository.AdministrativeSidoRepository; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.tags.Tag; + +@Tag(name = "행정구역 조회 API") +@RestController +@RequestMapping("/v1/administrative-area") +public class AdministrativeAreaControllerV1 { + private final AdministrativeSidoRepository administrativeSidoRepository; + private final AdministrativeSggRepository administrativeSggRepository; + private final AdministrativeEmdRepository administrativeEmdRepository; + + @Autowired + public AdministrativeAreaControllerV1(AdministrativeSidoRepository administrativeSidoRepository, AdministrativeSggRepository administrativeSggRepository, AdministrativeEmdRepository administrativeEmdRepository) { + this.administrativeSidoRepository = administrativeSidoRepository; + this.administrativeSggRepository = administrativeSggRepository; + this.administrativeEmdRepository = administrativeEmdRepository; + } + + @Operation( + summary = "행정구역 시도 조회", + description = "행정구역 시도를 조회합니다." + ) + + @GetMapping + public List getSidoList() { + List administrativeAreaSidoEntities = administrativeSidoRepository.findAll(); + return administrativeAreaSidoEntities.stream() + .map(AdministrativeAreaSido::getName) + .collect(Collectors.toList()); + } + + @Operation( + summary = "행정구역 시군구 조회", + description = "행정구역 시군구를 조회합니다." + ) + + @GetMapping("/sgg") + public List getSggList( + @Parameter( + description = "시도 id를 입력하면 됩니다", + example = "1" + )@RequestParam Long sidoId + ){ + AdministrativeAreaSido administrativeAreaSido = administrativeSidoRepository.findById(sidoId) + .orElseThrow(() -> new IllegalArgumentException("Invalid sidoId: " + sidoId)); + + return administrativeSggRepository.findByAdministrativeAreaSido(administrativeAreaSido).stream() + .map(AdministrativeAreaSgg::getName) + .collect(Collectors.toList()); + } + + @Operation( + summary = "행정구역 읍면동 조회", + description = "행정구역 읍면동을 조회합니다." + ) + @GetMapping("/sgg/emd") + public List getEmdList( + @Parameter( + description = "시군구 id를 입력하면 됩니다", + example = "1" + )@RequestParam Long sggId + ) { + AdministrativeAreaSgg administrativeAreaSgg = administrativeSggRepository.findById(sggId) + .orElseThrow(() -> new IllegalArgumentException("Invalid sggId: " + sggId)); + + return administrativeEmdRepository.findByAdministrativeAreaSgg(administrativeAreaSgg).stream() + .map(AdministrativeAreaEmd::getName) + .collect(Collectors.toList()); + } +} \ No newline at end of file diff --git a/src/main/java/com/ajou/hertz/domain/administrative_area/controller/SidoControllerV1.java b/src/main/java/com/ajou/hertz/domain/administrative_area/controller/SidoControllerV1.java deleted file mode 100644 index b1aa51f..0000000 --- a/src/main/java/com/ajou/hertz/domain/administrative_area/controller/SidoControllerV1.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.ajou.hertz.domain.administrative_area.controller; - -import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; - -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -import com.ajou.hertz.domain.administrative_area.constant.SidoList; - -@RestController -@RequestMapping("/v1/administrative-area") -public class SidoControllerV1 { - @GetMapping - public List getSidoList() { - return Arrays.stream(SidoList.values()) - .map(SidoList::name) - .collect(Collectors.toList()); - } - -} diff --git a/src/main/java/com/ajou/hertz/domain/administrative_area/entity/AdministrativeAreaEmd.java b/src/main/java/com/ajou/hertz/domain/administrative_area/entity/AdministrativeAreaEmd.java new file mode 100644 index 0000000..7e9e0b2 --- /dev/null +++ b/src/main/java/com/ajou/hertz/domain/administrative_area/entity/AdministrativeAreaEmd.java @@ -0,0 +1,36 @@ +package com.ajou.hertz.domain.administrative_area.entity; + +import com.ajou.hertz.common.entity.TimeTrackedBaseEntity; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@AllArgsConstructor(access = AccessLevel.PRIVATE) +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@Entity +@Table(name = "administrative_area_emd") +public class AdministrativeAreaEmd extends TimeTrackedBaseEntity { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name="administrative_area_emd_id", nullable = false, updatable = false) + private Long id; + + @Column(nullable = false) + private String name; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "administrative_area_sgg_id") + private AdministrativeAreaSgg administrativeAreaSgg; +} diff --git a/src/main/java/com/ajou/hertz/domain/administrative_area/entity/AdministrativeAreaSgg.java b/src/main/java/com/ajou/hertz/domain/administrative_area/entity/AdministrativeAreaSgg.java new file mode 100644 index 0000000..0c24daa --- /dev/null +++ b/src/main/java/com/ajou/hertz/domain/administrative_area/entity/AdministrativeAreaSgg.java @@ -0,0 +1,37 @@ +package com.ajou.hertz.domain.administrative_area.entity; + +import com.ajou.hertz.common.entity.TimeTrackedBaseEntity; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@AllArgsConstructor(access = AccessLevel.PRIVATE) +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@Entity +@Table(name = "administrative_area_sgg") +public class AdministrativeAreaSgg extends TimeTrackedBaseEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name="administrative_area_sgg_id", nullable = false, updatable = false) + private Long id; + + @Column(nullable = false) + private String name; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "administrative_area_sido_id") + private AdministrativeAreaSido administrativeAreaSido; +} diff --git a/src/main/java/com/ajou/hertz/domain/administrative_area/entity/Sido.java b/src/main/java/com/ajou/hertz/domain/administrative_area/entity/AdministrativeAreaSido.java similarity index 60% rename from src/main/java/com/ajou/hertz/domain/administrative_area/entity/Sido.java rename to src/main/java/com/ajou/hertz/domain/administrative_area/entity/AdministrativeAreaSido.java index d8dcf37..cd83923 100644 --- a/src/main/java/com/ajou/hertz/domain/administrative_area/entity/Sido.java +++ b/src/main/java/com/ajou/hertz/domain/administrative_area/entity/AdministrativeAreaSido.java @@ -1,12 +1,11 @@ package com.ajou.hertz.domain.administrative_area.entity; import com.ajou.hertz.common.entity.TimeTrackedBaseEntity; -import com.ajou.hertz.domain.administrative_area.constant.SidoList; import jakarta.persistence.Column; import jakarta.persistence.Entity; -import jakarta.persistence.EnumType; -import jakarta.persistence.Enumerated; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.Table; import lombok.AccessLevel; @@ -18,15 +17,15 @@ @AllArgsConstructor(access = AccessLevel.PRIVATE) @NoArgsConstructor(access = AccessLevel.PROTECTED) @Entity -@Table(name = "adminitrative_area_sido") -public class Sido extends TimeTrackedBaseEntity{ +@Table(name = "administrative_area_sido") +public class AdministrativeAreaSido extends TimeTrackedBaseEntity{ @Id - @Column(nullable = false, updatable = false) + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name="administrative_area_sido_id", nullable = false, updatable = false) private Long id; - @Enumerated(EnumType.STRING) @Column(nullable = false) - private SidoList name; + private String name; } diff --git a/src/main/java/com/ajou/hertz/domain/administrative_area/entity/Emd.java b/src/main/java/com/ajou/hertz/domain/administrative_area/entity/Emd.java deleted file mode 100644 index a626362..0000000 --- a/src/main/java/com/ajou/hertz/domain/administrative_area/entity/Emd.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.ajou.hertz.domain.administrative_area.entity; - -public class Emd { -} diff --git a/src/main/java/com/ajou/hertz/domain/administrative_area/repository/AdministrativeEmdRepository.java b/src/main/java/com/ajou/hertz/domain/administrative_area/repository/AdministrativeEmdRepository.java new file mode 100644 index 0000000..fb1e041 --- /dev/null +++ b/src/main/java/com/ajou/hertz/domain/administrative_area/repository/AdministrativeEmdRepository.java @@ -0,0 +1,12 @@ +package com.ajou.hertz.domain.administrative_area.repository; + +import java.util.List; + +import org.springframework.data.jpa.repository.JpaRepository; + +import com.ajou.hertz.domain.administrative_area.entity.AdministrativeAreaEmd; +import com.ajou.hertz.domain.administrative_area.entity.AdministrativeAreaSgg; + +public interface AdministrativeEmdRepository extends JpaRepository { + List findByAdministrativeAreaSgg(AdministrativeAreaSgg administrativeAreaSgg); +} diff --git a/src/main/java/com/ajou/hertz/domain/administrative_area/repository/AdministrativeSggRepository.java b/src/main/java/com/ajou/hertz/domain/administrative_area/repository/AdministrativeSggRepository.java new file mode 100644 index 0000000..49db3fb --- /dev/null +++ b/src/main/java/com/ajou/hertz/domain/administrative_area/repository/AdministrativeSggRepository.java @@ -0,0 +1,12 @@ +package com.ajou.hertz.domain.administrative_area.repository; + +import java.util.List; + +import org.springframework.data.jpa.repository.JpaRepository; + +import com.ajou.hertz.domain.administrative_area.entity.AdministrativeAreaSgg; +import com.ajou.hertz.domain.administrative_area.entity.AdministrativeAreaSido; + +public interface AdministrativeSggRepository extends JpaRepository { + List findByAdministrativeAreaSido(AdministrativeAreaSido administrativeAreaSido); +} diff --git a/src/main/java/com/ajou/hertz/domain/administrative_area/repository/AdministrativeSidoRepository.java b/src/main/java/com/ajou/hertz/domain/administrative_area/repository/AdministrativeSidoRepository.java new file mode 100644 index 0000000..5babae9 --- /dev/null +++ b/src/main/java/com/ajou/hertz/domain/administrative_area/repository/AdministrativeSidoRepository.java @@ -0,0 +1,8 @@ +package com.ajou.hertz.domain.administrative_area.repository; + +import org.springframework.data.jpa.repository.JpaRepository; + +import com.ajou.hertz.domain.administrative_area.entity.AdministrativeAreaSido; + +public interface AdministrativeSidoRepository extends JpaRepository { +}