From b9f74d79fd84fc13531d7eb677ab8a4366ae0e25 Mon Sep 17 00:00:00 2001 From: nohy6630 Date: Fri, 3 Nov 2023 16:41:15 +0900 Subject: [PATCH] =?UTF-8?q?Feat(#43):=20=EC=9E=AC=EB=82=9C=20=EB=AC=B8?= =?UTF-8?q?=EC=9E=90=20=EC=9E=84=EC=8B=9C=20api=20=EA=B5=AC=ED=98=84=20(?= =?UTF-8?q?=EB=8D=94=EB=AF=B8=EB=8D=B0=EC=9D=B4=ED=84=B0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/DisasterController.java | 18 +++++ .../dto/request/LatestDisasterRequest.java | 23 +++++++ .../dto/response/LatestDisasterResponse.java | 27 ++++++++ .../domain/disaster/entity/Disaster.java | 44 +++++++++++++ .../disaster/service/DisasterService.java | 25 +++++++ .../domain/disaster/util/DisasterType.java | 65 +++++++++++++++++++ .../InvalidDisasterTypeException.java | 9 +++ .../context/CustomExceptionContext.java | 5 +- 8 files changed, 215 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/numberone/backend/domain/disaster/controller/DisasterController.java create mode 100644 src/main/java/com/numberone/backend/domain/disaster/dto/request/LatestDisasterRequest.java create mode 100644 src/main/java/com/numberone/backend/domain/disaster/dto/response/LatestDisasterResponse.java create mode 100644 src/main/java/com/numberone/backend/domain/disaster/entity/Disaster.java create mode 100644 src/main/java/com/numberone/backend/domain/disaster/service/DisasterService.java create mode 100644 src/main/java/com/numberone/backend/domain/disaster/util/DisasterType.java create mode 100644 src/main/java/com/numberone/backend/exception/badrequest/InvalidDisasterTypeException.java diff --git a/src/main/java/com/numberone/backend/domain/disaster/controller/DisasterController.java b/src/main/java/com/numberone/backend/domain/disaster/controller/DisasterController.java new file mode 100644 index 00000000..399f4ce5 --- /dev/null +++ b/src/main/java/com/numberone/backend/domain/disaster/controller/DisasterController.java @@ -0,0 +1,18 @@ +package com.numberone.backend.domain.disaster.controller; + +import com.numberone.backend.domain.disaster.dto.request.LatestDisasterRequest; +import com.numberone.backend.domain.disaster.dto.response.LatestDisasterResponse; +import com.numberone.backend.domain.disaster.service.DisasterService; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/api/disaster") +public class DisasterController { + private final DisasterService disasterService; + @PostMapping("/latest") + public LatestDisasterResponse getLatestDisaster(@RequestBody LatestDisasterRequest latestDisasterRequest){ + return disasterService.getLatestDisaster(latestDisasterRequest); + } +} diff --git a/src/main/java/com/numberone/backend/domain/disaster/dto/request/LatestDisasterRequest.java b/src/main/java/com/numberone/backend/domain/disaster/dto/request/LatestDisasterRequest.java new file mode 100644 index 00000000..e9269d00 --- /dev/null +++ b/src/main/java/com/numberone/backend/domain/disaster/dto/request/LatestDisasterRequest.java @@ -0,0 +1,23 @@ +package com.numberone.backend.domain.disaster.dto.request; + +import com.numberone.backend.domain.disaster.dto.response.LatestDisasterResponse; +import com.numberone.backend.domain.disaster.entity.Disaster; +import com.numberone.backend.domain.disaster.util.DisasterType; +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotNull; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class LatestDisasterRequest { + @Schema(defaultValue = "126.9723") + @NotNull(message = "경도 정보는 null 일 수 없습니다.") + private Double longitude; + + @Schema(defaultValue = "37.5559") + @NotNull(message = "위도 정보는 null 일 수 없습니다.") + private Double latitude; +} diff --git a/src/main/java/com/numberone/backend/domain/disaster/dto/response/LatestDisasterResponse.java b/src/main/java/com/numberone/backend/domain/disaster/dto/response/LatestDisasterResponse.java new file mode 100644 index 00000000..2fff2d5f --- /dev/null +++ b/src/main/java/com/numberone/backend/domain/disaster/dto/response/LatestDisasterResponse.java @@ -0,0 +1,27 @@ +package com.numberone.backend.domain.disaster.dto.response; + +import com.numberone.backend.domain.disaster.entity.Disaster; +import com.numberone.backend.domain.disaster.util.DisasterType; +import lombok.*; + +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +@Builder +public class LatestDisasterResponse { + private String disasterType; + private Integer severity; + private String title; + private String msg; + private String info; + + public static LatestDisasterResponse of(Disaster disaster) { + return LatestDisasterResponse.builder() + .disasterType(disaster.getDisasterType().getDescription()) + .severity(disaster.getSeverity()) + .title(disaster.getTitle()) + .msg(disaster.getMsg()) + .info(disaster.getInfo()) + .build(); + } +} diff --git a/src/main/java/com/numberone/backend/domain/disaster/entity/Disaster.java b/src/main/java/com/numberone/backend/domain/disaster/entity/Disaster.java new file mode 100644 index 00000000..03387e1d --- /dev/null +++ b/src/main/java/com/numberone/backend/domain/disaster/entity/Disaster.java @@ -0,0 +1,44 @@ +package com.numberone.backend.domain.disaster.entity; + +import com.numberone.backend.domain.disaster.util.DisasterType; +import jakarta.persistence.*; +import lombok.*; + +@Entity +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class Disaster { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Enumerated(EnumType.STRING) + private DisasterType disasterType; + + private Integer severity; + + private String title; + + private String msg; + + private String info; + + @Builder + public Disaster(DisasterType disasterType, Integer severity, String title, String msg, String info) { + this.disasterType = disasterType; + this.severity = severity; + this.title = title; + this.msg = msg; + this.info = info; + } + + public static Disaster of(DisasterType disasterType, Integer severity, String title, String msg, String info) { + return Disaster.builder() + .disasterType(disasterType) + .severity(severity) + .title(title) + .msg(msg) + .info(info) + .build(); + } +} diff --git a/src/main/java/com/numberone/backend/domain/disaster/service/DisasterService.java b/src/main/java/com/numberone/backend/domain/disaster/service/DisasterService.java new file mode 100644 index 00000000..75dc58e7 --- /dev/null +++ b/src/main/java/com/numberone/backend/domain/disaster/service/DisasterService.java @@ -0,0 +1,25 @@ +package com.numberone.backend.domain.disaster.service; + +import com.numberone.backend.domain.disaster.dto.request.LatestDisasterRequest; +import com.numberone.backend.domain.disaster.dto.response.LatestDisasterResponse; +import com.numberone.backend.domain.disaster.entity.Disaster; +import com.numberone.backend.domain.disaster.util.DisasterType; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@RequiredArgsConstructor +@Transactional(readOnly = true) +public class DisasterService { + public LatestDisasterResponse getLatestDisaster(LatestDisasterRequest latestDisasterRequest) { + Disaster disaster = Disaster.of( + DisasterType.kor2code("화재"), + 2, + "서울특별시 강남구 동작동 화재 발생", + "금일 10.23. 19:39경 소촌동 855 화재 발생, 인근주민은 안전유의 및 차량우회바랍니다. 960-8222", + "서울특별시 강남구 ・ 오후 2시 46분" + ); + return LatestDisasterResponse.of(disaster); + } +} diff --git a/src/main/java/com/numberone/backend/domain/disaster/util/DisasterType.java b/src/main/java/com/numberone/backend/domain/disaster/util/DisasterType.java new file mode 100644 index 00000000..107c1acc --- /dev/null +++ b/src/main/java/com/numberone/backend/domain/disaster/util/DisasterType.java @@ -0,0 +1,65 @@ +package com.numberone.backend.domain.disaster.util; + +import com.numberone.backend.exception.badrequest.InvalidDisasterTypeException; +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@Getter +@RequiredArgsConstructor +public enum DisasterType { + TYPHOON("태풍"), + DRYNESS("건조"), + WILDFIRE("산불"), + LANDSLIDE("산사태"), + FLOOD("홍수"), + HEAVY_RAIN("호우"), + HEATWAVE("폭염"), + FOG("안개"), + ROUGH_SEA("풍랑"), + FINE_DUST("미세먼지"), + TIDAL_WAVE("대조기"), + DROUGHT("가뭄"), + HEAVY_SNOWFALL("대설"), + TSUNAMI("지진해일"), + EARTHQUAKE("지진"), + COLD_WAVE("한파"), + YELLOW_DUST("황사"), + STRONG_WIND("강풍"), + TRAFFIC_CONTROL("교통통제"), + FIRE("화재"), + COLLAPSE("붕괴"), + EXPLOSION("폭발"), + TRAFFIC_ACCIDENT("교통사고"), + ENVIRONMENTAL_POLLUTION("환경오염사고"), + ENERGY("에너지"), + COMMUNICATION("통신"), + TRAFFIC("교통"), + FINANCE("금융"), + MEDICAL("의료"), + WATER_SUPPLY("수도"), + INFECTIOUS_DISEASE("전염병"), + POWER_OUTAGE("정전"), + GAS("가스"), + AI("AI"), + CHEMICAL("화생방사고"), + RIOT("폭동"), + TERROR("테러"), + EMERGENCY("비상사태"), + CIVIL_DEFENSE("민방공"), + OTHERS("기타"); + + private final String description; + + public String getDescription() { + return description; + } + + public static DisasterType kor2code(String kor) { + for (DisasterType type : DisasterType.values()) { + if (type.getDescription().equals(kor)) { + return type; + } + } + throw new InvalidDisasterTypeException(); + } +} \ No newline at end of file diff --git a/src/main/java/com/numberone/backend/exception/badrequest/InvalidDisasterTypeException.java b/src/main/java/com/numberone/backend/exception/badrequest/InvalidDisasterTypeException.java new file mode 100644 index 00000000..a1ceb2f9 --- /dev/null +++ b/src/main/java/com/numberone/backend/exception/badrequest/InvalidDisasterTypeException.java @@ -0,0 +1,9 @@ +package com.numberone.backend.exception.badrequest; + +import static com.numberone.backend.exception.context.CustomExceptionContext.INVALID_DISASTER_TYPE; + +public class InvalidDisasterTypeException extends BadRequestException { + public InvalidDisasterTypeException() { + super(INVALID_DISASTER_TYPE); + } +} diff --git a/src/main/java/com/numberone/backend/exception/context/CustomExceptionContext.java b/src/main/java/com/numberone/backend/exception/context/CustomExceptionContext.java index b822b34a..db44f27e 100644 --- a/src/main/java/com/numberone/backend/exception/context/CustomExceptionContext.java +++ b/src/main/java/com/numberone/backend/exception/context/CustomExceptionContext.java @@ -21,7 +21,10 @@ public enum CustomExceptionContext implements ExceptionContext { // S3 관련 예외 S3_FILE_UPLOAD_FAILED("S3 파일 업로드에 실패했습니다.", 4000), - S3_MULTIPART_MISSING("Multipart 파일이 null 이거나 없습니다.", 4001) + S3_MULTIPART_MISSING("Multipart 파일이 null 이거나 없습니다.", 4001), + + // DISASTER 관련 예외 + INVALID_DISASTER_TYPE("존재하지 않는 대피소 유형입니다.",5000) ;