From 908a4d9d34485caf7d93a7cc580d2e606926d6d9 Mon Sep 17 00:00:00 2001 From: sjmjys954646 Date: Thu, 12 Oct 2023 21:54:47 +0900 Subject: [PATCH 1/2] =?UTF-8?q?fix=20:=20requestBody=20Part=20->=20Body=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/demo/mentoring/MentorPostRestController.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/example/demo/mentoring/MentorPostRestController.java b/src/main/java/com/example/demo/mentoring/MentorPostRestController.java index 2d8d3465..66860fce 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostRestController.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostRestController.java @@ -9,6 +9,7 @@ import org.springframework.validation.Errors; import org.springframework.web.bind.annotation.*; +import javax.validation.Valid; import java.util.List; @RequiredArgsConstructor @@ -17,7 +18,7 @@ public class MentorPostRestController { private final MentorPostService mentorPostService; @PostMapping(value = "/mentorings/post") - public ResponseEntity createMentorPost(@RequestPart MentorPostRequest.CreateDTO requestDTO, Errors errors, @AuthenticationPrincipal CustomUserDetails userDetails) { + public ResponseEntity createMentorPost(@RequestBody @Valid MentorPostRequest.CreateDTO requestDTO, Errors errors, @AuthenticationPrincipal CustomUserDetails userDetails) { mentorPostService.createMentorPost(requestDTO, userDetails.getUser()); return ResponseEntity.status(HttpStatus.OK).body(ApiUtils.successWithNoContent()); } @@ -35,7 +36,7 @@ public ResponseEntity getMentorPostId(@PathVariable int id, @AuthenticationPr } @PutMapping(value = "/mentorings/post/{id}") - public ResponseEntity updateMentorPost(@PathVariable int id, @RequestPart MentorPostRequest.CreateDTO requestDTO, Errors errors, @AuthenticationPrincipal CustomUserDetails userDetails) { + public ResponseEntity updateMentorPost(@PathVariable int id, @RequestBody @Valid MentorPostRequest.CreateDTO requestDTO, Errors errors, @AuthenticationPrincipal CustomUserDetails userDetails) { mentorPostService.updateMentorPost(requestDTO, id); return ResponseEntity.status(HttpStatus.OK).body(ApiUtils.successWithNoContent()); } From c02ca17d6ea2cb756da326d38b1f3b4079fb12b2 Mon Sep 17 00:00:00 2001 From: sjmjys954646 Date: Thu, 12 Oct 2023 22:24:07 +0900 Subject: [PATCH 2/2] =?UTF-8?q?feat=20:=20add=20Enum=20at=20MentorPost=20m?= =?UTF-8?q?entorPost=EC=97=90=203=EA=B0=80=EC=A7=80=20=EC=83=81=ED=83=9C?= =?UTF-8?q?=20ACTIVE=20DONE=20DELETED=EB=A5=BC=20=EB=8D=94=ED=95=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../demo/config/utils/StateConverter.java | 27 ++++++++++++++++ .../example/demo/config/utils/StateEnum.java | 32 +++++++++++++++++++ .../example/demo/mentoring/MentorPost.java | 7 ++++ .../demo/mentoring/MentorPostRequest.java | 3 ++ .../demo/mentoringtest/MentoringTest.java | 2 ++ 5 files changed, 71 insertions(+) create mode 100644 src/main/java/com/example/demo/config/utils/StateConverter.java create mode 100644 src/main/java/com/example/demo/config/utils/StateEnum.java diff --git a/src/main/java/com/example/demo/config/utils/StateConverter.java b/src/main/java/com/example/demo/config/utils/StateConverter.java new file mode 100644 index 00000000..e109d5bf --- /dev/null +++ b/src/main/java/com/example/demo/config/utils/StateConverter.java @@ -0,0 +1,27 @@ +package com.example.demo.config.utils; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; +import org.springframework.util.StringUtils; + +import javax.persistence.AttributeConverter; +import java.util.Optional; + +@RequiredArgsConstructor +@Component +public class StateConverter implements AttributeConverter { + + @Override + public String convertToDatabaseColumn(StateEnum attribute) { + return Optional.ofNullable(attribute).orElse(StateEnum.NULL).getValue(); + } + + @Override + public StateEnum convertToEntityAttribute(String dbData) { + if (dbData == null || dbData.length() == 0) { + return StateEnum.NULL; + } + return StateEnum.findOf(dbData); + } +} diff --git a/src/main/java/com/example/demo/config/utils/StateEnum.java b/src/main/java/com/example/demo/config/utils/StateEnum.java new file mode 100644 index 00000000..af51d0c3 --- /dev/null +++ b/src/main/java/com/example/demo/config/utils/StateEnum.java @@ -0,0 +1,32 @@ +package com.example.demo.config.utils; + +import lombok.Getter; + +import java.util.Collections; +import java.util.Map; +import java.util.Optional; +import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +@Getter +public enum StateEnum { + NULL("NULL", ""), + ACTIVE("ACTIVE", "ACTIVE"), + DONE("DONE", "DONE"), + DELETED("DELETED", "DELETED"); + + private final String desc; + private final String value; + + StateEnum(String desc, String value) { + this.desc = desc; + this.value = value; + } + + private static final Map descriptions = Collections.unmodifiableMap(Stream.of(values()).collect(Collectors.toMap(StateEnum::getValue, Function.identity()))); + + public static StateEnum findOf(String findValue) { + return Optional.ofNullable(descriptions.get(findValue)).orElse(NULL); + } +} diff --git a/src/main/java/com/example/demo/mentoring/MentorPost.java b/src/main/java/com/example/demo/mentoring/MentorPost.java index 1d5c9feb..bc165ace 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPost.java +++ b/src/main/java/com/example/demo/mentoring/MentorPost.java @@ -1,11 +1,14 @@ package com.example.demo.mentoring; import com.example.demo.config.utils.BaseTime; +import com.example.demo.config.utils.StateConverter; +import com.example.demo.config.utils.StateEnum; import com.example.demo.user.User; import lombok.AccessLevel; import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; +import org.hibernate.annotations.ColumnDefault; import org.hibernate.annotations.SQLDelete; import org.hibernate.annotations.Where; @@ -30,6 +33,10 @@ public class MentorPost extends BaseTime { private String content; + @Convert(converter = StateConverter.class) + @Column(name = "state", nullable = false) + private StateEnum state; + @Builder public MentorPost(User writer, String title, String content){ this.writer = writer; diff --git a/src/main/java/com/example/demo/mentoring/MentorPostRequest.java b/src/main/java/com/example/demo/mentoring/MentorPostRequest.java index a95762ec..ec2586f3 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostRequest.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostRequest.java @@ -1,5 +1,6 @@ package com.example.demo.mentoring; +import com.example.demo.config.utils.StateEnum; import com.example.demo.user.User; import lombok.Getter; import lombok.Setter; @@ -14,5 +15,7 @@ public static class CreateDTO { private String title; private String content; + + private StateEnum state; } } diff --git a/src/test/java/com/example/demo/mentoringtest/MentoringTest.java b/src/test/java/com/example/demo/mentoringtest/MentoringTest.java index 6af5632e..b1adf452 100644 --- a/src/test/java/com/example/demo/mentoringtest/MentoringTest.java +++ b/src/test/java/com/example/demo/mentoringtest/MentoringTest.java @@ -2,6 +2,7 @@ import com.example.demo.RestDoc; +import com.example.demo.config.utils.StateEnum; import com.example.demo.interest.Interest; import com.example.demo.interest.InterestJPARepository; import com.example.demo.mentoring.*; @@ -90,6 +91,7 @@ void CreateMentorPostTest() { MentorPostRequest.CreateDTO mentorPostRequest = new MentorPostRequest.CreateDTO(); mentorPostRequest.setTitle("title"); mentorPostRequest.setContent("content"); + mentorPostRequest.setState(StateEnum.ACTIVE); mentorPostService.createMentorPost(mentorPostRequest, writer); MentorPost mentorPostFind = mentorPostJPARepostiory.findById(1);