From 04e70b61598995dd8617eeb7faada71ee7cfd9f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=B0=95=EC=84=9C=EC=A7=84?= <109809242+PicturePark1101@users.noreply.github.com> Date: Tue, 12 Nov 2024 17:29:24 +0900 Subject: [PATCH] =?UTF-8?q?[feat]=20=EB=8C=80=ED=95=99=EA=B5=90=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=20API=20=EA=B5=AC=ED=98=84=20(#214)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [feat] make command, request DTO * [feat] add static method in UniversityEntity * [feat] implements controller * [feat] implements service * [chore] update submodule * [refactor] change package * [chore] update submodule * [refactor] add a line break --- server-yml | 2 +- .../controller/UniversityController.java | 12 ++++++++++ .../request/UniversitiesPostRequest.java | 7 ++++++ .../service/UniversityCommandService.java | 18 +++++++++++++++ .../university/service/UniversityUpdater.java | 18 +++++++++++++++ .../command/UniversitiesPostCommand.java | 23 +++++++++++++++++++ .../domain/university/model/University.java | 20 +++++++++++++++- 7 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/hankki/hankkiserver/api/university/controller/request/UniversitiesPostRequest.java create mode 100644 src/main/java/org/hankki/hankkiserver/api/university/service/UniversityCommandService.java create mode 100644 src/main/java/org/hankki/hankkiserver/api/university/service/UniversityUpdater.java create mode 100644 src/main/java/org/hankki/hankkiserver/api/university/service/command/UniversitiesPostCommand.java diff --git a/server-yml b/server-yml index 1e7e3c37..ffaf66c6 160000 --- a/server-yml +++ b/server-yml @@ -1 +1 @@ -Subproject commit 1e7e3c37e950d3890bd22b55be8f3d6c91f0a524 +Subproject commit ffaf66c6224b53675d85223154b72999317670d7 diff --git a/src/main/java/org/hankki/hankkiserver/api/university/controller/UniversityController.java b/src/main/java/org/hankki/hankkiserver/api/university/controller/UniversityController.java index e652e481..62dc2308 100644 --- a/src/main/java/org/hankki/hankkiserver/api/university/controller/UniversityController.java +++ b/src/main/java/org/hankki/hankkiserver/api/university/controller/UniversityController.java @@ -2,10 +2,15 @@ import lombok.RequiredArgsConstructor; import org.hankki.hankkiserver.api.dto.HankkiResponse; +import org.hankki.hankkiserver.api.university.controller.request.UniversitiesPostRequest; +import org.hankki.hankkiserver.api.university.service.UniversityCommandService; import org.hankki.hankkiserver.api.university.service.UniversityQueryService; +import org.hankki.hankkiserver.api.university.service.command.UniversitiesPostCommand; import org.hankki.hankkiserver.api.university.service.response.UniversitiesResponse; import org.hankki.hankkiserver.common.code.CommonSuccessCode; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -15,9 +20,16 @@ public class UniversityController { private final UniversityQueryService universityQueryService; + private final UniversityCommandService universityCommandService; @GetMapping("/universities") public HankkiResponse getUniversities() { return HankkiResponse.success(CommonSuccessCode.OK, universityQueryService.findAllByOrderByName()); } + + @PostMapping("/universities") + public HankkiResponse createUniversities(@RequestBody UniversitiesPostRequest universitiesPostRequest) { + universityCommandService.createUniversities(UniversitiesPostCommand.of(universitiesPostRequest)); + return HankkiResponse.success(CommonSuccessCode.CREATED); + } } diff --git a/src/main/java/org/hankki/hankkiserver/api/university/controller/request/UniversitiesPostRequest.java b/src/main/java/org/hankki/hankkiserver/api/university/controller/request/UniversitiesPostRequest.java new file mode 100644 index 00000000..52ec5459 --- /dev/null +++ b/src/main/java/org/hankki/hankkiserver/api/university/controller/request/UniversitiesPostRequest.java @@ -0,0 +1,7 @@ +package org.hankki.hankkiserver.api.university.controller.request; + +import java.util.List; + +public record UniversitiesPostRequest(List universities) { + public record UniversityRequest(String name, double longitude, double latitude) { } +} diff --git a/src/main/java/org/hankki/hankkiserver/api/university/service/UniversityCommandService.java b/src/main/java/org/hankki/hankkiserver/api/university/service/UniversityCommandService.java new file mode 100644 index 00000000..1f387b05 --- /dev/null +++ b/src/main/java/org/hankki/hankkiserver/api/university/service/UniversityCommandService.java @@ -0,0 +1,18 @@ +package org.hankki.hankkiserver.api.university.service; + +import lombok.RequiredArgsConstructor; +import org.hankki.hankkiserver.api.university.service.command.UniversitiesPostCommand; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@RequiredArgsConstructor +public class UniversityCommandService { + + private final UniversityUpdater universityUpdater; + + @Transactional + public void createUniversities(UniversitiesPostCommand universitiesPostCommand) { + universityUpdater.saveAll(universitiesPostCommand.toEntityList()); + } +} diff --git a/src/main/java/org/hankki/hankkiserver/api/university/service/UniversityUpdater.java b/src/main/java/org/hankki/hankkiserver/api/university/service/UniversityUpdater.java new file mode 100644 index 00000000..9eb157f6 --- /dev/null +++ b/src/main/java/org/hankki/hankkiserver/api/university/service/UniversityUpdater.java @@ -0,0 +1,18 @@ +package org.hankki.hankkiserver.api.university.service; + +import java.util.List; +import lombok.RequiredArgsConstructor; +import org.hankki.hankkiserver.domain.university.model.University; +import org.hankki.hankkiserver.domain.university.repository.UniversityRepository; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class UniversityUpdater { + + private final UniversityRepository universityRepository; + + void saveAll(List universities){ + universityRepository.saveAll(universities); + } +} diff --git a/src/main/java/org/hankki/hankkiserver/api/university/service/command/UniversitiesPostCommand.java b/src/main/java/org/hankki/hankkiserver/api/university/service/command/UniversitiesPostCommand.java new file mode 100644 index 00000000..bf1c13be --- /dev/null +++ b/src/main/java/org/hankki/hankkiserver/api/university/service/command/UniversitiesPostCommand.java @@ -0,0 +1,23 @@ +package org.hankki.hankkiserver.api.university.service.command; + +import java.util.List; +import org.hankki.hankkiserver.api.university.controller.request.UniversitiesPostRequest; +import org.hankki.hankkiserver.domain.common.Point; +import org.hankki.hankkiserver.domain.university.model.University; + +public record UniversitiesPostCommand(List universityCommands) { + + record UniversityCommand(String name, double longitude, double latitude) { + University toEntity() { + return University.create(name, new Point(latitude, longitude)); + } + } + + public static UniversitiesPostCommand of(UniversitiesPostRequest universitiesPostRequest) { + return new UniversitiesPostCommand(universitiesPostRequest.universities().stream().map(it -> new UniversityCommand(it.name(), it.longitude(), it.latitude())).toList()); + } + + public List toEntityList() { + return universityCommands.stream().map(UniversityCommand::toEntity).toList(); + } +} diff --git a/src/main/java/org/hankki/hankkiserver/domain/university/model/University.java b/src/main/java/org/hankki/hankkiserver/domain/university/model/University.java index f60cd0b7..238cc3b7 100644 --- a/src/main/java/org/hankki/hankkiserver/domain/university/model/University.java +++ b/src/main/java/org/hankki/hankkiserver/domain/university/model/University.java @@ -1,7 +1,13 @@ package org.hankki.hankkiserver.domain.university.model; -import jakarta.persistence.*; +import jakarta.persistence.Column; +import jakarta.persistence.Embedded; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; import lombok.AccessLevel; +import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; import org.hankki.hankkiserver.domain.common.Point; @@ -22,4 +28,16 @@ public class University { @Column(nullable = false) private String name; + public static University create(String name, Point point) { + return University.builder() + .name(name) + .point(point) + .build(); + } + + @Builder + private University(String name, Point point) { + this.name = name; + this.point = point; + } }