From f28f86382fc334f52a22efdb34db481cb3ad551b Mon Sep 17 00:00:00 2001 From: sjmjys954646 Date: Sun, 8 Oct 2023 22:46:03 +0900 Subject: [PATCH 01/18] =?UTF-8?q?feat=20:=20mentorPostRest=20Get=EC=9A=94?= =?UTF-8?q?=EC=B2=AD=20Paging=20restcontroller=20=EC=B6=94=EA=B0=80=20?= =?UTF-8?q?=EB=B0=8F=20paging=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../demo/mentoring/MentorPostResponse.java | 4 ++-- .../demo/mentoring/MentorPostRestController.java | 12 +++++++++--- .../example/demo/mentoring/MentorPostService.java | 15 ++++++++++----- .../example/demo/mentoringtest/MentoringTest.java | 6 ++++-- 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/example/demo/mentoring/MentorPostResponse.java b/src/main/java/com/example/demo/mentoring/MentorPostResponse.java index d5c81b97..0f8d3102 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostResponse.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostResponse.java @@ -14,13 +14,13 @@ public class MentorPostResponse { @Getter @Setter - public static class MentorPostDTO { + public static class MentorPostAllDTO { private int postId; private String title; private String content; private WriterDTO writerDTO; - public MentorPostDTO(MentorPost mentorPost, WriterDTO writerDTO) { + public MentorPostAllDTO(MentorPost mentorPost, WriterDTO writerDTO) { this.postId = mentorPost.getId(); this.title = mentorPost.getTitle(); this.content = mentorPost.getContent(); diff --git a/src/main/java/com/example/demo/mentoring/MentorPostRestController.java b/src/main/java/com/example/demo/mentoring/MentorPostRestController.java index 2654e342..d0e8336c 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostRestController.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostRestController.java @@ -6,9 +6,9 @@ import org.springframework.http.ResponseEntity; import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.validation.Errors; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestPart; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; + +import java.util.List; @RequiredArgsConstructor @RestController @@ -20,4 +20,10 @@ public ResponseEntity createMentorPost(@RequestPart MentorPostRequest.CreateD mentorPostService.createMentorPost(requestDTO, userDetails.getUser()); return ResponseEntity.ok().body(ApiUtils.success(true)); } + + @GetMapping("/mentorings") + public ResponseEntity getMentorPost(@RequestParam(value = "page", defaultValue = "0") Integer page) { + List responseDTOs = mentorPostService.findAllMentorPost(page); + return ResponseEntity.ok(ApiUtils.success(responseDTOs)); + } } diff --git a/src/main/java/com/example/demo/mentoring/MentorPostService.java b/src/main/java/com/example/demo/mentoring/MentorPostService.java index 84957e60..7652c84e 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostService.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostService.java @@ -4,6 +4,9 @@ import com.example.demo.user.userInterest.UserInterest; import com.example.demo.user.userInterest.UserInterestJPARepository; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -26,13 +29,15 @@ public void createMentorPost(MentorPostRequest.CreateDTO createDTO, User writer) /* 1. mentorPostList를 조회 2. 각 List당 writer별 writerInterests를 조회 3. MentorPostDTO 생성*/ - public List findAllMentorPost() { - List mentorPostList = mentorPostJPARepostiory.findAll(); - List mentorPostDTOList = mentorPostList.stream().map( + public List findAllMentorPost(int page) { + Pageable pageable = PageRequest.of(page,5); + + Page mentorPostList = mentorPostJPARepostiory.findAll(pageable); + List mentorPostDTOList = mentorPostList.stream().map( mentorPost -> { List writerInterests = userInterestJPARepository.findAllById(mentorPost.getWriter().getId()); - MentorPostResponse.MentorPostDTO.WriterDTO writerDTO = new MentorPostResponse.MentorPostDTO.WriterDTO(mentorPost.getWriter(), writerInterests); - return new MentorPostResponse.MentorPostDTO(mentorPost,writerDTO); + MentorPostResponse.MentorPostAllDTO.WriterDTO writerDTO = new MentorPostResponse.MentorPostAllDTO.WriterDTO(mentorPost.getWriter(), writerInterests); + return new MentorPostResponse.MentorPostAllDTO(mentorPost,writerDTO); } ).collect(Collectors.toList()); return mentorPostDTOList; diff --git a/src/test/java/com/example/demo/mentoringtest/MentoringTest.java b/src/test/java/com/example/demo/mentoringtest/MentoringTest.java index 03693896..6e347202 100644 --- a/src/test/java/com/example/demo/mentoringtest/MentoringTest.java +++ b/src/test/java/com/example/demo/mentoringtest/MentoringTest.java @@ -1,6 +1,7 @@ package com.example.demo.mentoringtest; +import com.example.demo.interest.InterestJPARepository; import com.example.demo.mentoring.MentorPost; import com.example.demo.mentoring.MentorPostJPARepostiory; import com.example.demo.mentoring.MentorPostResponse; @@ -25,7 +26,8 @@ public class MentoringTest { private UserJPARepository userJPARepository; @Autowired private MentorPostJPARepostiory mentorPostJPARepostiory; - + @Autowired + private InterestJPARepository interestJPARepository; @Autowired private MentorPostService mentorPostService; @@ -67,7 +69,7 @@ void CreateMentorPostTest() { @Test void test() throws Exception { - List responseDTOs = mentorPostService.findAllMentorPost(); + List responseDTOs = mentorPostService.findAllMentorPost(10); String responseBody = om.writeValueAsString(responseDTOs); From fa98b88cd4e5da2520763eab6af450e30302bbbf Mon Sep 17 00:00:00 2001 From: sjmjys954646 Date: Sun, 8 Oct 2023 23:46:12 +0900 Subject: [PATCH 02/18] fix : mentorPostRestController --- .../com/example/demo/mentoring/MentorPostRestController.java | 4 ++-- 1 file changed, 2 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 d0e8336c..ee33c5ba 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostRestController.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostRestController.java @@ -15,13 +15,13 @@ public class MentorPostRestController { private final MentorPostService mentorPostService; - @PostMapping(value = "/mentorings") + @PostMapping(value = "/mentorings/post") public ResponseEntity createMentorPost(@RequestPart MentorPostRequest.CreateDTO requestDTO, Errors errors, @AuthenticationPrincipal CustomUserDetails userDetails) { mentorPostService.createMentorPost(requestDTO, userDetails.getUser()); return ResponseEntity.ok().body(ApiUtils.success(true)); } - @GetMapping("/mentorings") + @GetMapping("/mentorings/post") public ResponseEntity getMentorPost(@RequestParam(value = "page", defaultValue = "0") Integer page) { List responseDTOs = mentorPostService.findAllMentorPost(page); return ResponseEntity.ok(ApiUtils.success(responseDTOs)); From f3dbb6129cc874b99c2477ed5a8f47a98b2eff02 Mon Sep 17 00:00:00 2001 From: sjmjys954646 Date: Mon, 9 Oct 2023 00:49:56 +0900 Subject: [PATCH 03/18] fix : pagination --- .../demo/mentoring/MentorPostResponse.java | 70 ++++++++++++++++++- .../demo/mentoring/MentorPostService.java | 5 +- .../demo/mentoringtest/MentoringTest.java | 9 ++- 3 files changed, 80 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/example/demo/mentoring/MentorPostResponse.java b/src/main/java/com/example/demo/mentoring/MentorPostResponse.java index 0f8d3102..5f2d449c 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostResponse.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostResponse.java @@ -1,17 +1,25 @@ package com.example.demo.mentoring; +import com.example.demo.mentoring.contact.NotConnectedRegisterUser; import com.example.demo.user.Role; import com.example.demo.user.User; import com.example.demo.user.userInterest.UserInterest; import lombok.Getter; import lombok.Setter; +import org.springframework.web.bind.annotation.PostMapping; import java.util.List; import java.util.stream.Collectors; public class MentorPostResponse { +/* + 페이지에서 멘토post 전체를 조회 + MentorPostAllDTO + 클릭시 들어간 페이지에서 보여지는 Post + MentorPostDTO + */ @Getter @Setter public static class MentorPostAllDTO { @@ -43,7 +51,67 @@ public WriterDTO(User user, List userInterests) { this.country = user.getCountry(); this.role = user.getRole(); this.favorites = userInterests.stream() - .filter(userInterest -> userInterest.getUser().getId() == user.getId()) + .map(userInterest -> userInterest.getInterest().getCategory()) + .collect(Collectors.toList()); + } + } + } + + @Getter + @Setter + public static class MentorPostDTO { + private int postId; + private String title; + private String content; + private WriterDTO writerDTO; + private List menteeDTOList; + + public MentorPostDTO(MentorPost mentorPost, MentorPostDTO.WriterDTO writerDTO) { + this.postId = mentorPost.getId(); + this.title = mentorPost.getTitle(); + this.content = mentorPost.getContent(); + this.writerDTO = writerDTO; + } + + @Getter @Setter + public static class WriterDTO { + private int mentorId; + private String profileImage; + private String name; + private String country; + private Role role; + private List favorites; + + public WriterDTO(User user, List userInterests) { + this.mentorId = user.getId(); + this.profileImage = user.getProfileImage(); + this.name = user.getFirstName() + " " + user.getLastName(); + this.country = user.getCountry(); + this.role = user.getRole(); + this.favorites = userInterests.stream() + .map(userInterest -> userInterest.getInterest().getCategory()) + .collect(Collectors.toList()); + } + } + + @Getter @Setter + public static class MenteeDTO{ + private int menteeId; + private String profileImage; + private String name; + private String country; + private Role role; + private int age; + private List favorites; + + public MenteeDTO(User user, List userInterests) { + this.menteeId = user.getId(); + this.profileImage = user.getProfileImage(); + this.name = user.getFirstName() + " " + user.getLastName(); + this.country = user.getCountry(); + this.role = user.getRole(); + this.age = user.getAge(); + this.favorites = userInterests.stream() .map(userInterest -> userInterest.getInterest().getCategory()) .collect(Collectors.toList()); } diff --git a/src/main/java/com/example/demo/mentoring/MentorPostService.java b/src/main/java/com/example/demo/mentoring/MentorPostService.java index 7652c84e..2a79c322 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostService.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostService.java @@ -32,8 +32,9 @@ public void createMentorPost(MentorPostRequest.CreateDTO createDTO, User writer) public List findAllMentorPost(int page) { Pageable pageable = PageRequest.of(page,5); - Page mentorPostList = mentorPostJPARepostiory.findAll(pageable); - List mentorPostDTOList = mentorPostList.stream().map( + Page pageContent = mentorPostJPARepostiory.findAll(pageable); + //List mentorPostList = mentorPostJPARepostiory.findAll(); + List mentorPostDTOList = pageContent.getContent().stream().map( mentorPost -> { List writerInterests = userInterestJPARepository.findAllById(mentorPost.getWriter().getId()); MentorPostResponse.MentorPostAllDTO.WriterDTO writerDTO = new MentorPostResponse.MentorPostAllDTO.WriterDTO(mentorPost.getWriter(), writerInterests); diff --git a/src/test/java/com/example/demo/mentoringtest/MentoringTest.java b/src/test/java/com/example/demo/mentoringtest/MentoringTest.java index 6e347202..b63e4e27 100644 --- a/src/test/java/com/example/demo/mentoringtest/MentoringTest.java +++ b/src/test/java/com/example/demo/mentoringtest/MentoringTest.java @@ -57,9 +57,16 @@ void CreateMentorPostTest() { .content("content") .build(); + MentorPost mentorPost2 = MentorPost.builder() + .writer(writer) + .title("title2") + .content("content2") + .build(); + // then userJPARepository.save(writer); mentorPostJPARepostiory.save(mentorPost); + mentorPostJPARepostiory.save(mentorPost2); MentorPost mentorPostFind = mentorPostJPARepostiory.findById(mentorPost.getId()); Assertions.assertThat(mentorPost.getId()) @@ -69,7 +76,7 @@ void CreateMentorPostTest() { @Test void test() throws Exception { - List responseDTOs = mentorPostService.findAllMentorPost(10); + List responseDTOs = mentorPostService.findAllMentorPost(0); String responseBody = om.writeValueAsString(responseDTOs); From 2792ff29d9948c14c962d2695efae177a61f4b82 Mon Sep 17 00:00:00 2001 From: sjmjys954646 Date: Mon, 9 Oct 2023 12:58:47 +0900 Subject: [PATCH 04/18] =?UTF-8?q?fix=20:=20findAllMentorPost,=20MentorPost?= =?UTF-8?q?Response=20DTO=20=EC=88=98=EC=A0=95=20=EC=A2=80=EB=8D=94=20?= =?UTF-8?q?=EB=B3=B4=EA=B8=B0=20=EC=A7=81=EA=B4=80=EC=A0=81=EC=9D=B8=20?= =?UTF-8?q?=EB=B0=A9=EC=8B=9D=EC=9C=BC=EB=A1=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../demo/mentoring/MentorPostResponse.java | 3 ++- .../mentoring/MentorPostRestController.java | 5 ++-- .../demo/mentoring/MentorPostService.java | 24 +++++++++++++++++-- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/example/demo/mentoring/MentorPostResponse.java b/src/main/java/com/example/demo/mentoring/MentorPostResponse.java index 5f2d449c..1f07ae48 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostResponse.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostResponse.java @@ -28,10 +28,11 @@ public static class MentorPostAllDTO { private String content; private WriterDTO writerDTO; - public MentorPostAllDTO(MentorPost mentorPost, WriterDTO writerDTO) { + public MentorPostAllDTO(MentorPost mentorPost, List favorites) { this.postId = mentorPost.getId(); this.title = mentorPost.getTitle(); this.content = mentorPost.getContent(); + WriterDTO writerDTO = new MentorPostAllDTO.WriterDTO(mentorPost.getWriter(), favorites); this.writerDTO = writerDTO; } diff --git a/src/main/java/com/example/demo/mentoring/MentorPostRestController.java b/src/main/java/com/example/demo/mentoring/MentorPostRestController.java index ee33c5ba..f001b8bc 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostRestController.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostRestController.java @@ -3,6 +3,7 @@ import com.example.demo.config.auth.CustomUserDetails; import com.example.demo.config.utils.ApiUtils; import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.validation.Errors; @@ -18,11 +19,11 @@ public class MentorPostRestController { @PostMapping(value = "/mentorings/post") public ResponseEntity createMentorPost(@RequestPart MentorPostRequest.CreateDTO requestDTO, Errors errors, @AuthenticationPrincipal CustomUserDetails userDetails) { mentorPostService.createMentorPost(requestDTO, userDetails.getUser()); - return ResponseEntity.ok().body(ApiUtils.success(true)); + return ResponseEntity.status(HttpStatus.OK).body(ApiUtils.successWithNoContent()); } @GetMapping("/mentorings/post") - public ResponseEntity getMentorPost(@RequestParam(value = "page", defaultValue = "0") Integer page) { + public ResponseEntity getMentorPost(@RequestParam(value = "page", defaultValue = "0") Integer page, @AuthenticationPrincipal CustomUserDetails userDetails) { List responseDTOs = mentorPostService.findAllMentorPost(page); return ResponseEntity.ok(ApiUtils.success(responseDTOs)); } diff --git a/src/main/java/com/example/demo/mentoring/MentorPostService.java b/src/main/java/com/example/demo/mentoring/MentorPostService.java index 2a79c322..c9989709 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostService.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostService.java @@ -1,5 +1,7 @@ package com.example.demo.mentoring; +import com.example.demo.mentoring.contact.ContactJPARepository; +import com.example.demo.mentoring.contact.NotConnectedRegisterUser; import com.example.demo.user.User; import com.example.demo.user.userInterest.UserInterest; import com.example.demo.user.userInterest.UserInterestJPARepository; @@ -19,6 +21,7 @@ public class MentorPostService { private final MentorPostJPARepostiory mentorPostJPARepostiory; private final UserInterestJPARepository userInterestJPARepository; + private final ContactJPARepository contactJPARepository; //mentorPost생성 @Transactional @@ -37,10 +40,27 @@ public List findAllMentorPost(int page) { List mentorPostDTOList = pageContent.getContent().stream().map( mentorPost -> { List writerInterests = userInterestJPARepository.findAllById(mentorPost.getWriter().getId()); - MentorPostResponse.MentorPostAllDTO.WriterDTO writerDTO = new MentorPostResponse.MentorPostAllDTO.WriterDTO(mentorPost.getWriter(), writerInterests); - return new MentorPostResponse.MentorPostAllDTO(mentorPost,writerDTO); + return new MentorPostResponse.MentorPostAllDTO(mentorPost,writerInterests); } ).collect(Collectors.toList()); return mentorPostDTOList; } + +// public MentorPostResponse.MentorPostDTO findMentorPost(int id){ +// MentorPost mentorPost = mentorPostJPARepostiory.findById(id); +// +// //writer 데이터 +// User mentor = mentorPost.getWriter(); +// //mentee들 데이터 +// List menteeList = contactJPARepository.findAllByMentorPostId(id); +// //writer Interest데이터 +// List writerInterests = userInterestJPARepository.findAllById(mentor.getId()); +// +// +// //mentee들 Interest데이터는 내부에서 +// +// MentorPostResponse.MentorPostDTO mentorPostDTO = new MentorPostResponse.MentorPostDTO(mentorPost,); +// +// return mentorPostDTO; +// } } From f9b70a7a574d81a42e913353da3a4e1db9165c1e Mon Sep 17 00:00:00 2001 From: sjmjys954646 Date: Mon, 9 Oct 2023 13:43:51 +0900 Subject: [PATCH 05/18] =?UTF-8?q?feat=20:=20MentorPostEach=20Service,=20Re?= =?UTF-8?q?sponse=20=EA=B0=81=20=ED=8F=AC=EC=8A=A4=ED=8A=B8=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=ED=95=A0=20=EC=88=98=20=EC=9E=88=EB=8A=94=20DTO?= =?UTF-8?q?=EC=99=80=20Service=20=EA=B0=9C=EB=B0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../demo/mentoring/MentorPostResponse.java | 28 +++++++++++++-- .../demo/mentoring/MentorPostService.java | 35 ++++++++++--------- .../demo/mentoringtest/MentoringTest.java | 1 + 3 files changed, 44 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/example/demo/mentoring/MentorPostResponse.java b/src/main/java/com/example/demo/mentoring/MentorPostResponse.java index 1f07ae48..df8cb28d 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostResponse.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostResponse.java @@ -28,11 +28,11 @@ public static class MentorPostAllDTO { private String content; private WriterDTO writerDTO; - public MentorPostAllDTO(MentorPost mentorPost, List favorites) { + public MentorPostAllDTO(MentorPost mentorPost, List userInterests) { this.postId = mentorPost.getId(); this.title = mentorPost.getTitle(); this.content = mentorPost.getContent(); - WriterDTO writerDTO = new MentorPostAllDTO.WriterDTO(mentorPost.getWriter(), favorites); + WriterDTO writerDTO = new MentorPostAllDTO.WriterDTO(mentorPost.getWriter(), userInterests); this.writerDTO = writerDTO; } @@ -58,6 +58,17 @@ public WriterDTO(User user, List userInterests) { } } + /* + param : + MentorPost mentorPost 멘토 데이터 + List mentorInterests 멘토의 Interest + List mentees 멘티들 데이터 + List menteeInterests : 멘티들 각각의 Interest 리스트 전체 + + dto : + writerDTO : 작성자인 멘토의 DTO + menteeDTO : 멘티들의 DTO + */ @Getter @Setter public static class MentorPostDTO { @@ -67,11 +78,22 @@ public static class MentorPostDTO { private WriterDTO writerDTO; private List menteeDTOList; - public MentorPostDTO(MentorPost mentorPost, MentorPostDTO.WriterDTO writerDTO) { + public MentorPostDTO(MentorPost mentorPost, List mentorFavorites, List mentees, List menteeInterest) { this.postId = mentorPost.getId(); this.title = mentorPost.getTitle(); this.content = mentorPost.getContent(); + MentorPostDTO.WriterDTO writerDTO = new MentorPostDTO.WriterDTO(mentorPost.getWriter(), mentorFavorites); this.writerDTO = writerDTO; + List menteeDTOList = mentees.stream() + .map(mentee -> { + List eachMenteeFavorite = menteeInterest.stream().filter( + userInterest -> mentee.getId() == userInterest.getId() + ).collect(Collectors.toList()); + MentorPostDTO.MenteeDTO menteeDTO = new MentorPostDTO.MenteeDTO(mentee.getMenteeUser(), eachMenteeFavorite); + return menteeDTO; + }) + .collect(Collectors.toList()); + this.menteeDTOList = menteeDTOList; } @Getter @Setter diff --git a/src/main/java/com/example/demo/mentoring/MentorPostService.java b/src/main/java/com/example/demo/mentoring/MentorPostService.java index c9989709..acbbafba 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostService.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostService.java @@ -46,21 +46,22 @@ public List findAllMentorPost(int page) { return mentorPostDTOList; } -// public MentorPostResponse.MentorPostDTO findMentorPost(int id){ -// MentorPost mentorPost = mentorPostJPARepostiory.findById(id); -// -// //writer 데이터 -// User mentor = mentorPost.getWriter(); -// //mentee들 데이터 -// List menteeList = contactJPARepository.findAllByMentorPostId(id); -// //writer Interest데이터 -// List writerInterests = userInterestJPARepository.findAllById(mentor.getId()); -// -// -// //mentee들 Interest데이터는 내부에서 -// -// MentorPostResponse.MentorPostDTO mentorPostDTO = new MentorPostResponse.MentorPostDTO(mentorPost,); -// -// return mentorPostDTO; -// } + public MentorPostResponse.MentorPostDTO findMentorPost(int id){ + MentorPost mentorPost = mentorPostJPARepostiory.findById(id); + + //writer 데이터 + User mentor = mentorPost.getWriter(); + //mentee들 데이터 + List menteeList = contactJPARepository.findAllByMentorPostId(id); + //writer Interest데이터 + List mentorInterests = userInterestJPARepository.findAllById(mentor.getId()); + //mentee들 Interest데이터 + List menteeInterests = menteeList.stream() + .flatMap(mentee -> userInterestJPARepository.findAllById(mentee.getId()).stream()) + .collect(Collectors.toList()); + + MentorPostResponse.MentorPostDTO mentorPostDTO = new MentorPostResponse.MentorPostDTO(mentorPost, mentorInterests, menteeList, menteeInterests); + + return mentorPostDTO; + } } diff --git a/src/test/java/com/example/demo/mentoringtest/MentoringTest.java b/src/test/java/com/example/demo/mentoringtest/MentoringTest.java index b63e4e27..3c9194e6 100644 --- a/src/test/java/com/example/demo/mentoringtest/MentoringTest.java +++ b/src/test/java/com/example/demo/mentoringtest/MentoringTest.java @@ -73,6 +73,7 @@ void CreateMentorPostTest() { .isEqualTo(mentorPostFind.getId()); } + @Test void test() throws Exception { From 8aae3dd709f8361e7b326786ef458ec540376fe2 Mon Sep 17 00:00:00 2001 From: sjmjys954646 Date: Mon, 9 Oct 2023 14:38:17 +0900 Subject: [PATCH 06/18] =?UTF-8?q?feat=20:=20findMentorPost=20Testcode=20?= =?UTF-8?q?=ED=8F=AC=EC=8A=A4=ED=8A=B8=20=EA=B0=9C=EB=B3=84=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1=20fav?= =?UTF-8?q?orites=20=EB=B2=84=EA=B7=B8=20=EB=B0=9C=EA=B2=AC=EB=90=98?= =?UTF-8?q?=EC=96=B4=20=EC=88=98=EC=A0=95=EC=98=88=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../demo/mentoringtest/MentoringTest.java | 125 +++++++++++++++++- 1 file changed, 121 insertions(+), 4 deletions(-) diff --git a/src/test/java/com/example/demo/mentoringtest/MentoringTest.java b/src/test/java/com/example/demo/mentoringtest/MentoringTest.java index 3c9194e6..1c1ec202 100644 --- a/src/test/java/com/example/demo/mentoringtest/MentoringTest.java +++ b/src/test/java/com/example/demo/mentoringtest/MentoringTest.java @@ -1,14 +1,19 @@ package com.example.demo.mentoringtest; +import com.example.demo.interest.Interest; import com.example.demo.interest.InterestJPARepository; import com.example.demo.mentoring.MentorPost; import com.example.demo.mentoring.MentorPostJPARepostiory; import com.example.demo.mentoring.MentorPostResponse; import com.example.demo.mentoring.MentorPostService; +import com.example.demo.mentoring.contact.ContactJPARepository; +import com.example.demo.mentoring.contact.NotConnectedRegisterUser; import com.example.demo.user.Role; import com.example.demo.user.User; import com.example.demo.user.UserJPARepository; +import com.example.demo.user.userInterest.UserInterest; +import com.example.demo.user.userInterest.UserInterestJPARepository; import com.fasterxml.jackson.databind.ObjectMapper; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; @@ -18,6 +23,7 @@ import org.springframework.test.context.ActiveProfiles; import java.util.List; +import java.util.stream.Collectors; @SpringBootTest @ActiveProfiles("test") @@ -30,6 +36,10 @@ public class MentoringTest { private InterestJPARepository interestJPARepository; @Autowired private MentorPostService mentorPostService; + @Autowired + private UserInterestJPARepository userInterestJPARepository; + @Autowired + private ContactJPARepository contactJPARepository; @Autowired private ObjectMapper om; @@ -74,14 +84,121 @@ void CreateMentorPostTest() { } + @Test - void test() throws Exception { + @DisplayName("findMentorPostTest") + void findMentorPostSaveTest() { - List responseDTOs = mentorPostService.findAllMentorPost(0); + //given + User mentor = User.builder() + .email("anjdal6612312364@gmail.com") + .password("as123df1234!") + .firstName("Jin123") + .lastName("Seun123g") + .country("Korea") + .age(21) + .role(Role.MENTOR) + .build(); - String responseBody = om.writeValueAsString(responseDTOs); + User mentee1 = User.builder() + .email("anjda22l6664@gmail.com") + .password("asdf221234!") + .firstName("Jin11") + .lastName("Seung11") + .country("Korea") + .age(21) + .role(Role.MENTEE) + .build(); - System.out.println("test : " + responseBody); + User mentee2 = User.builder() + .email("anjdal66111164@gmail.com") + .password("asdf122222234!") + .firstName("Jin22") + .lastName("Seung22") + .country("Korea") + .age(21) + .role(Role.MENTEE) + .build(); + + Interest interest1 = Interest.builder() + .category("test1") + .build(); + + Interest interest2 = Interest.builder() + .category("test2") + .build(); + + Interest interest3 = Interest.builder() + .category("test3") + .build(); + UserInterest userInterest1 = UserInterest.builder() + .user(mentor) + .interest(interest2) + .build(); + + UserInterest userInterest2 = UserInterest.builder() + .user(mentee1) + .interest(interest2) + .build(); + + UserInterest userInterest3 = UserInterest.builder() + .user(mentee1) + .interest(interest3) + .build(); + + UserInterest userInterest4 = UserInterest.builder() + .user(mentee2) + .interest(interest1) + .build(); + + UserInterest userInterest5 = UserInterest.builder() + .user(mentee2) + .interest(interest2) + .build(); + + MentorPost mentorPost3 = MentorPost.builder() + .writer(mentor) + .title("title") + .content("content") + .build(); + + NotConnectedRegisterUser menteeNotConnected1 = NotConnectedRegisterUser.builder() + .mentorPost(mentorPost3) + .menteeUser(mentee1) + .state(NotConnectedRegisterUser.State.AWAIT) + .build(); + + NotConnectedRegisterUser menteeNotConnected2 = NotConnectedRegisterUser.builder() + .mentorPost(mentorPost3) + .menteeUser(mentee2) + .state(NotConnectedRegisterUser.State.AWAIT) + .build(); + + //when + interestJPARepository.save(interest1); + interestJPARepository.save(interest2); + interestJPARepository.save(interest3); + userJPARepository.save(mentor); + userJPARepository.save(mentee1); + userJPARepository.save(mentee2); + mentorPostJPARepostiory.save(mentorPost3); + userInterestJPARepository.save(userInterest1); + userInterestJPARepository.save(userInterest2); + userInterestJPARepository.save(userInterest3); + userInterestJPARepository.save(userInterest4); + userInterestJPARepository.save(userInterest5); + contactJPARepository.save(menteeNotConnected1); + contactJPARepository.save(menteeNotConnected2); + + } + + @Test + void mentorPostServiceTest() throws Exception { + MentorPostResponse.MentorPostDTO mentorPostFind = mentorPostService.findMentorPost(1); + + String responseBody = om.writeValueAsString(mentorPostFind); + + System.out.println("test : " + responseBody); } } From b29cc3fade9d0640516d9cbe2ad8eb803f2a3034 Mon Sep 17 00:00:00 2001 From: sjmjys954646 Date: Mon, 9 Oct 2023 15:33:51 +0900 Subject: [PATCH 07/18] =?UTF-8?q?fix=20:=20mentorPost=20Bug=20favorite?= =?UTF-8?q?=EC=9D=B4=20=EC=9E=98=EB=AA=BB=20=EC=A1=B0=ED=9A=8C=EB=90=98?= =?UTF-8?q?=EB=8A=94=20=EB=B2=84=EA=B7=B8=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../demo/mentoring/MentorPostResponse.java | 3 +- .../demo/mentoring/MentorPostService.java | 2 +- .../demo/mentoringtest/MentoringTest.java | 30 +++++++++++-------- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/example/demo/mentoring/MentorPostResponse.java b/src/main/java/com/example/demo/mentoring/MentorPostResponse.java index df8cb28d..a5f4f33a 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostResponse.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostResponse.java @@ -87,8 +87,9 @@ public MentorPostDTO(MentorPost mentorPost, List mentorFavorites, List menteeDTOList = mentees.stream() .map(mentee -> { List eachMenteeFavorite = menteeInterest.stream().filter( - userInterest -> mentee.getId() == userInterest.getId() + userInterest -> mentee.getMenteeUser().getId() == userInterest.getUser().getId() ).collect(Collectors.toList()); + MentorPostDTO.MenteeDTO menteeDTO = new MentorPostDTO.MenteeDTO(mentee.getMenteeUser(), eachMenteeFavorite); return menteeDTO; }) diff --git a/src/main/java/com/example/demo/mentoring/MentorPostService.java b/src/main/java/com/example/demo/mentoring/MentorPostService.java index acbbafba..3291b7f0 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostService.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostService.java @@ -57,7 +57,7 @@ public MentorPostResponse.MentorPostDTO findMentorPost(int id){ List mentorInterests = userInterestJPARepository.findAllById(mentor.getId()); //mentee들 Interest데이터 List menteeInterests = menteeList.stream() - .flatMap(mentee -> userInterestJPARepository.findAllById(mentee.getId()).stream()) + .flatMap(mentee -> userInterestJPARepository.findAllById(mentee.getMenteeUser().getId()).stream()) .collect(Collectors.toList()); MentorPostResponse.MentorPostDTO mentorPostDTO = new MentorPostResponse.MentorPostDTO(mentorPost, mentorInterests, menteeList, menteeInterests); diff --git a/src/test/java/com/example/demo/mentoringtest/MentoringTest.java b/src/test/java/com/example/demo/mentoringtest/MentoringTest.java index 1c1ec202..83849d78 100644 --- a/src/test/java/com/example/demo/mentoringtest/MentoringTest.java +++ b/src/test/java/com/example/demo/mentoringtest/MentoringTest.java @@ -100,20 +100,20 @@ void findMentorPostSaveTest() { .role(Role.MENTOR) .build(); - User mentee1 = User.builder() + User mentee_One = User.builder() .email("anjda22l6664@gmail.com") .password("asdf221234!") - .firstName("Jin11") + .firstName("mentee1") .lastName("Seung11") .country("Korea") .age(21) .role(Role.MENTEE) .build(); - User mentee2 = User.builder() + User mentee_Two = User.builder() .email("anjdal66111164@gmail.com") .password("asdf122222234!") - .firstName("Jin22") + .firstName("mentee2") .lastName("Seung22") .country("Korea") .age(21) @@ -138,25 +138,30 @@ void findMentorPostSaveTest() { .build(); UserInterest userInterest2 = UserInterest.builder() - .user(mentee1) + .user(mentee_One) .interest(interest2) .build(); UserInterest userInterest3 = UserInterest.builder() - .user(mentee1) + .user(mentee_One) .interest(interest3) .build(); UserInterest userInterest4 = UserInterest.builder() - .user(mentee2) + .user(mentee_Two) .interest(interest1) .build(); UserInterest userInterest5 = UserInterest.builder() - .user(mentee2) + .user(mentee_Two) .interest(interest2) .build(); + UserInterest userInterest6 = UserInterest.builder() + .user(mentor) + .interest(interest3) + .build(); + MentorPost mentorPost3 = MentorPost.builder() .writer(mentor) .title("title") @@ -165,13 +170,13 @@ void findMentorPostSaveTest() { NotConnectedRegisterUser menteeNotConnected1 = NotConnectedRegisterUser.builder() .mentorPost(mentorPost3) - .menteeUser(mentee1) + .menteeUser(mentee_One) .state(NotConnectedRegisterUser.State.AWAIT) .build(); NotConnectedRegisterUser menteeNotConnected2 = NotConnectedRegisterUser.builder() .mentorPost(mentorPost3) - .menteeUser(mentee2) + .menteeUser(mentee_Two) .state(NotConnectedRegisterUser.State.AWAIT) .build(); @@ -180,14 +185,15 @@ void findMentorPostSaveTest() { interestJPARepository.save(interest2); interestJPARepository.save(interest3); userJPARepository.save(mentor); - userJPARepository.save(mentee1); - userJPARepository.save(mentee2); + userJPARepository.save(mentee_One); + userJPARepository.save(mentee_Two); mentorPostJPARepostiory.save(mentorPost3); userInterestJPARepository.save(userInterest1); userInterestJPARepository.save(userInterest2); userInterestJPARepository.save(userInterest3); userInterestJPARepository.save(userInterest4); userInterestJPARepository.save(userInterest5); + userInterestJPARepository.save(userInterest6); contactJPARepository.save(menteeNotConnected1); contactJPARepository.save(menteeNotConnected2); From 3d2f97f1080083608459d121d397efe329364b7a Mon Sep 17 00:00:00 2001 From: sjmjys954646 Date: Mon, 9 Oct 2023 16:28:20 +0900 Subject: [PATCH 08/18] =?UTF-8?q?feat=20:=20mentorPostIdRestController=20R?= =?UTF-8?q?estController=EC=97=90=20api=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/demo/mentoring/MentorPostRestController.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/com/example/demo/mentoring/MentorPostRestController.java b/src/main/java/com/example/demo/mentoring/MentorPostRestController.java index f001b8bc..29251eee 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostRestController.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostRestController.java @@ -27,4 +27,10 @@ public ResponseEntity getMentorPost(@RequestParam(value = "page", defaultValu List responseDTOs = mentorPostService.findAllMentorPost(page); return ResponseEntity.ok(ApiUtils.success(responseDTOs)); } + + @GetMapping("/mentorings/post/{id}") + public ResponseEntity getMentorPostId(@PathVariable int id, @AuthenticationPrincipal CustomUserDetails userDetails) { + MentorPostResponse.MentorPostDTO responseDTO = mentorPostService.findMentorPost(id); + return ResponseEntity.ok(ApiUtils.success(responseDTO)); + } } From 92a9ef3854fb4bf2045ce6bfbcf67331e0d42989 Mon Sep 17 00:00:00 2001 From: Jin_00 Date: Wed, 11 Oct 2023 22:18:17 +0900 Subject: [PATCH 09/18] =?UTF-8?q?feat=20:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EB=8D=B0=EC=9D=B4=ED=84=B0=20=ED=8C=8C=EC=9D=BC=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/db/teardown.sql | 60 ++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/main/resources/db/teardown.sql diff --git a/src/main/resources/db/teardown.sql b/src/main/resources/db/teardown.sql new file mode 100644 index 00000000..949b96e4 --- /dev/null +++ b/src/main/resources/db/teardown.sql @@ -0,0 +1,60 @@ +SET REFERENTIAL_INTEGRITY FALSE; +TRUNCATE TABLE user_tb; +TRUNCATE TABLE interest_tb; +TRUNCATE TABLE mentor_post_tb; +TRUNCATE TABLE user_interest_tb; +TRUNCATE TABLE not_connected_register_user_tb; +SET REFERENTIAL_INTEGRITY TRUE; + +-- user Table +INSERT INTO user_tb (created_at, first_name, last_name, email, password, country, introduction, age, profile_image, role) VALUES + (NOW(), 'John', 'Doe', 'john@example.com', '{bcrypt}$2a$10$8H0OT8wgtALJkig6fmypi.Y7jzI5Y7W9PGgRKqnVeS2cLWGifwHF2', 'USA', 'Hello, I am John.', 25, 'profile.jpg', 'MENTOR'), + (NOW(), 'Alice', 'Smith', 'alice.smith@example.com', '{bcrypt}$2a$10$8H0OT8wgtALJkig6fmypi.Y7jzI5Y7W9PGgRKqnVeS2cLWGifwHF2', 'Canada', 'I love painting.', 25, 'image2.jpg', 'MENTOR'), + (NOW(), 'Admin', 'Admin', 'admin@example.com', '{bcrypt}$2a$10$8H0OT8wgtALJkig6fmypi.Y7jzI5Y7W9PGgRKqnVeS2cLWGifwHF2', 'USA', 'I am an admin user.', 35, 'admin.jpg', 'MENTEE'), + (NOW(), 'Jane', 'Smith', 'jane@example.com', '{bcrypt}$2a$10$8H0OT8wgtALJkig6fmypi.Y7jzI5Y7W9PGgRKqnVeS2cLWGifwHF2', 'Canada', 'I love coding.', 30, 'profile.jpg', 'MENTEE'), + (NOW(), 'Admin', 'Adminson', 'admin22222@example.com', '{bcrypt}$2a$10$8H0OT8wgtALJkig6fmypi.Y7jzI5Y7W9PGgRKqnVeS2cLWGifwHF2', 'USA', 'I am the admin.', 35, 'admin.jpg', 'MENTEE'); + +-- interst Table +INSERT INTO interest_tb (created_at, category) VALUES + (NOW(), 'IDOL'), + (NOW(), 'Game'), + (NOW(), 'K-POP'), + (NOW(), 'Sports'); + +-- mentorPost Table +INSERT INTO mentor_post_tb (created_at, writer_id, title, content) VALUES + (NOW(), 1, 'Teaching Programming', 'I can teach you how to code.'), + (NOW(), 1, 'Art Workshop', 'Let''s create beautiful art together.'), + (NOW(), 2, 'Software Development Mentorship', 'I can mentor you in software development.'), + (NOW(), 2, 'Art and Painting Mentorship', 'Learn the art of painting with me.'), + (NOW(), 1, 'Web Development Mentorship', 'I can teach you web development from scratch.'), + (NOW(), 2, 'Fitness and Health Mentorship', 'Get in shape and stay healthy with my guidance.'), + (NOW(), 1, 'Data Science Mentorship', 'Learn data science and machine learning with me.'), + (NOW(), 2, 'Music Production Mentorship', 'Produce your own music with professional tips.'), + (NOW(), 2, 'Cooking and Culinary Arts Mentorship', 'Master the art of cooking and culinary skills.'), + (NOW(), 2, 'Entrepreneurship Mentorship', 'Start and grow your own business.'), + (NOW(), 1, 'Graphic Design Mentorship', 'Create stunning graphics and designs.'), + (NOW(), 1, 'Yoga and Mindfulness Mentorship', 'Find inner peace and balance through yoga.'), + (NOW(), 1, 'Photography Mentorship', 'Capture amazing moments with your camera.'), + (NOW(), 2, 'Mathematics Tutoring', 'I can help you understand and excel in math.');; + + +-- notConnectedRegisterUser Table +INSERT INTO not_connected_register_user_tb (created_at, mentor_post_id, mentee_user_id, state) VALUES + (NOW(), 1, 3, 'AWAIT'), + (NOW(), 1, 4, 'ACCEPT'), + (NOW(), 2, 5, 'AWAIT'); + +-- userInterest Table +INSERT INTO user_interest_tb (created_at, user_id, interest_id) VALUES + (NOW(), 1, 1), + (NOW(), 1, 2), + (NOW(), 2, 1), + (NOW(), 2, 3), + (NOW(), 3, 3), + (NOW(), 3, 4), + (NOW(), 4, 1), + (NOW(), 4, 3), + (NOW(), 5, 3), + (NOW(), 5, 4) +; \ No newline at end of file From 109fe670e29efdbe263221e30ed728da90e52404 Mon Sep 17 00:00:00 2001 From: Jin_00 Date: Wed, 11 Oct 2023 22:18:46 +0900 Subject: [PATCH 10/18] =?UTF-8?q?feat=20:=20interest=20table=20=EC=97=90?= =?UTF-8?q?=20baseTime=20=EC=83=81=EC=86=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/example/demo/interest/Interest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/example/demo/interest/Interest.java b/src/main/java/com/example/demo/interest/Interest.java index 67048693..84dbb3f1 100644 --- a/src/main/java/com/example/demo/interest/Interest.java +++ b/src/main/java/com/example/demo/interest/Interest.java @@ -1,5 +1,6 @@ package com.example.demo.interest; +import com.example.demo.config.utils.BaseTime; import lombok.AccessLevel; import lombok.Builder; import lombok.Getter; @@ -11,7 +12,7 @@ @NoArgsConstructor(access = AccessLevel.PROTECTED) @Entity @Table(name = "interest_tb") -public class Interest { +public class Interest extends BaseTime { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; From 957d98a27e82da72c68f1be37d6a8bea60c035b3 Mon Sep 17 00:00:00 2001 From: Jin_00 Date: Wed, 11 Oct 2023 22:19:16 +0900 Subject: [PATCH 11/18] =?UTF-8?q?feat=20:=20state=20=EC=97=90=20=EC=97=90?= =?UTF-8?q?=EB=84=88=ED=85=8C=EC=9D=B4=EC=85=98=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../demo/mentoring/contact/NotConnectedRegisterUser.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/com/example/demo/mentoring/contact/NotConnectedRegisterUser.java b/src/main/java/com/example/demo/mentoring/contact/NotConnectedRegisterUser.java index 62b945fc..b4537021 100644 --- a/src/main/java/com/example/demo/mentoring/contact/NotConnectedRegisterUser.java +++ b/src/main/java/com/example/demo/mentoring/contact/NotConnectedRegisterUser.java @@ -26,6 +26,8 @@ public class NotConnectedRegisterUser extends BaseTime { @ManyToOne(fetch = FetchType.LAZY) private User menteeUser; + @Column(nullable = false) + @Enumerated(value = EnumType.STRING) private State state; public enum State { From decdbb9e2041a6c140022a3927cb9cefeb49deb0 Mon Sep 17 00:00:00 2001 From: Jin_00 Date: Wed, 11 Oct 2023 22:19:41 +0900 Subject: [PATCH 12/18] =?UTF-8?q?test=20:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EB=8D=B0=EC=9D=B4=ED=84=B0=EA=B0=80=20=EC=9E=98=20=EC=8B=A4?= =?UTF-8?q?=ED=96=89=EB=90=98=EB=8A=94=EC=A7=80=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/com/example/demo/GardenBeApplicationTests.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/test/java/com/example/demo/GardenBeApplicationTests.java b/src/test/java/com/example/demo/GardenBeApplicationTests.java index 4da8df8c..0c5a59c6 100644 --- a/src/test/java/com/example/demo/GardenBeApplicationTests.java +++ b/src/test/java/com/example/demo/GardenBeApplicationTests.java @@ -3,9 +3,11 @@ import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.jdbc.Sql; @SpringBootTest @ActiveProfiles("test") +@Sql("classpath:db/teardown.sql") class GardenBeApplicationTests { @Test From 3f2beae412c7ae5dfad0d65408156d3e43fe1b1d Mon Sep 17 00:00:00 2001 From: sjmjys954646 Date: Thu, 12 Oct 2023 13:21:30 +0900 Subject: [PATCH 13/18] =?UTF-8?q?feat=20:=20softdelete=20softdelete=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84=20=EB=B0=8F=20mentorPost=EC=97=90=20?= =?UTF-8?q?=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/example/demo/config/utils/BaseTime.java | 6 +++++- src/main/java/com/example/demo/mentoring/MentorPost.java | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/example/demo/config/utils/BaseTime.java b/src/main/java/com/example/demo/config/utils/BaseTime.java index edbd9c70..27410f55 100644 --- a/src/main/java/com/example/demo/config/utils/BaseTime.java +++ b/src/main/java/com/example/demo/config/utils/BaseTime.java @@ -15,8 +15,12 @@ public abstract class BaseTime { @CreatedDate @Column(nullable = false) - private LocalTime createdAt; + private LocalDateTime createdAt; @LastModifiedDate private LocalDateTime updatedAt; + + private LocalDateTime deletedAt; + + private boolean isDeleted; } diff --git a/src/main/java/com/example/demo/mentoring/MentorPost.java b/src/main/java/com/example/demo/mentoring/MentorPost.java index a8709fb2..85a136f6 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPost.java +++ b/src/main/java/com/example/demo/mentoring/MentorPost.java @@ -6,12 +6,16 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; +import org.hibernate.annotations.SQLDelete; +import org.hibernate.annotations.Where; import javax.persistence.*; @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) @Entity +@Where(clause = "deleted_at IS NULL") +@SQLDelete(sql = "UPDATE mentorPost_tb SET deleted_at = CURRENT_TIMESTAMP, isDeleted = TRUE where id = ?") @Table(name = "mentorPost_tb") public class MentorPost extends BaseTime { @Id From a890e94b2e164fde4a6a77bb088a69d167f42cb4 Mon Sep 17 00:00:00 2001 From: sjmjys954646 Date: Thu, 12 Oct 2023 15:17:39 +0900 Subject: [PATCH 14/18] =?UTF-8?q?feat=20:=20mvcTest=20post=EB=8A=94=20?= =?UTF-8?q?=EC=98=A4=EB=A5=98=EB=82=98=EB=8A=94=EB=8D=B0=20=ED=99=95?= =?UTF-8?q?=EC=9D=B8=EC=A4=91=20get=EC=9A=94=EC=B2=AD=EB=93=A4=EC=9D=80=20?= =?UTF-8?q?=ED=99=95=EC=9D=B8=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/demo/config/utils/BaseTime.java | 2 + .../demo/mentoring/MentorPostRequest.java | 2 +- src/test/java/com/example/demo/RestDoc.java | 37 +++++++++++ .../demo/mentoringtest/MentoringTest.java | 64 ++++++++++++++++--- 4 files changed, 96 insertions(+), 9 deletions(-) create mode 100644 src/test/java/com/example/demo/RestDoc.java diff --git a/src/main/java/com/example/demo/config/utils/BaseTime.java b/src/main/java/com/example/demo/config/utils/BaseTime.java index 27410f55..26e578de 100644 --- a/src/main/java/com/example/demo/config/utils/BaseTime.java +++ b/src/main/java/com/example/demo/config/utils/BaseTime.java @@ -1,5 +1,6 @@ package com.example.demo.config.utils; +import org.hibernate.annotations.ColumnDefault; import org.springframework.data.annotation.CreatedDate; import org.springframework.data.annotation.LastModifiedDate; import org.springframework.data.jpa.domain.support.AuditingEntityListener; @@ -22,5 +23,6 @@ public abstract class BaseTime { private LocalDateTime deletedAt; + @ColumnDefault("false") private boolean isDeleted; } diff --git a/src/main/java/com/example/demo/mentoring/MentorPostRequest.java b/src/main/java/com/example/demo/mentoring/MentorPostRequest.java index 96434478..489daeda 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostRequest.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostRequest.java @@ -9,7 +9,7 @@ public class MentorPostRequest { @Getter @Setter - public class CreateDTO { + public static class CreateDTO { @NotNull private String title; diff --git a/src/test/java/com/example/demo/RestDoc.java b/src/test/java/com/example/demo/RestDoc.java new file mode 100644 index 00000000..dea8bb38 --- /dev/null +++ b/src/test/java/com/example/demo/RestDoc.java @@ -0,0 +1,37 @@ +package com.example.demo; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.restdocs.RestDocumentationContextProvider; +import org.springframework.restdocs.RestDocumentationExtension; +import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation; +import org.springframework.restdocs.mockmvc.RestDocumentationResultHandler; +import org.springframework.restdocs.operation.preprocess.Preprocessors; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.filter.CharacterEncodingFilter; + +import java.nio.charset.StandardCharsets; + +@ExtendWith({ SpringExtension.class, RestDocumentationExtension.class }) +public class RestDoc { + protected MockMvc mvc; + protected RestDocumentationResultHandler document; + + @BeforeEach + private void setup(WebApplicationContext webApplicationContext, + RestDocumentationContextProvider restDocumentation) { + this.document = MockMvcRestDocumentation.document("{class-name}/{method-name}", + Preprocessors.preprocessRequest(Preprocessors.prettyPrint()), + Preprocessors.preprocessResponse(Preprocessors.prettyPrint())); + + mvc = MockMvcBuilders.webAppContextSetup(webApplicationContext) + .addFilter(new CharacterEncodingFilter(StandardCharsets.UTF_8.name(), true)) + .apply(MockMvcRestDocumentation.documentationConfiguration(restDocumentation)) + // .apply(SecurityMockMvcConfigurers.springSecurity()) + .alwaysDo(document) + .build(); + } +} diff --git a/src/test/java/com/example/demo/mentoringtest/MentoringTest.java b/src/test/java/com/example/demo/mentoringtest/MentoringTest.java index 83849d78..68894fe2 100644 --- a/src/test/java/com/example/demo/mentoringtest/MentoringTest.java +++ b/src/test/java/com/example/demo/mentoringtest/MentoringTest.java @@ -1,12 +1,10 @@ package com.example.demo.mentoringtest; +import com.example.demo.RestDoc; import com.example.demo.interest.Interest; import com.example.demo.interest.InterestJPARepository; -import com.example.demo.mentoring.MentorPost; -import com.example.demo.mentoring.MentorPostJPARepostiory; -import com.example.demo.mentoring.MentorPostResponse; -import com.example.demo.mentoring.MentorPostService; +import com.example.demo.mentoring.*; import com.example.demo.mentoring.contact.ContactJPARepository; import com.example.demo.mentoring.contact.NotConnectedRegisterUser; import com.example.demo.user.Role; @@ -19,15 +17,24 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.security.test.context.support.WithUserDetails; import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.jdbc.Sql; +import org.springframework.test.web.servlet.ResultActions; -import java.util.List; -import java.util.stream.Collectors; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; @SpringBootTest +@Sql(value = "classpath:db/teardown.sql") +@AutoConfigureMockMvc @ActiveProfiles("test") -public class MentoringTest { +public class MentoringTest extends RestDoc { @Autowired private UserJPARepository userJPARepository; @Autowired @@ -88,7 +95,6 @@ void CreateMentorPostTest() { @Test @DisplayName("findMentorPostTest") void findMentorPostSaveTest() { - //given User mentor = User.builder() .email("anjdal6612312364@gmail.com") @@ -207,4 +213,46 @@ void mentorPostServiceTest() throws Exception { System.out.println("test : " + responseBody); } + + @WithUserDetails(value = "john@example.com") + @Test + public void CreateMentorPostTestMVC() throws Exception { + + MentorPostRequest.CreateDTO createDTO = new MentorPostRequest.CreateDTO(); + createDTO.setTitle("asfd"); + createDTO.setContent("afaffafa"); + + String requestBody = om.writeValueAsString(createDTO); + + // when + ResultActions resultActions = mvc.perform( + post("/mentorings/post") + .content(requestBody) + .contentType(MediaType.APPLICATION_JSON_VALUE) + ); + + String responseBody = resultActions.andReturn().getResponse().getContentAsString(); + System.out.println("테스트 : " + responseBody); + + // verify + //resultActions.andExpect(jsonPath("$.success").value("true")); + //resultActions.andDo(MockMvcResultHandlers.print()).andDo(document); + } + + @WithUserDetails(value = "john@example.com") + @Test + public void GetMentorPostTestMVC() throws Exception { + + int id = 1; + + // when + ResultActions resultActions = mvc.perform( + get("/mentorings/post") + ); + + // console + String responseBody = resultActions.andReturn().getResponse().getContentAsString(); + System.out.println("테스트 : "+responseBody); + } + } From 322ef1878decf25c999a7afe6f8b11846f45fdf1 Mon Sep 17 00:00:00 2001 From: sjmjys954646 Date: Thu, 12 Oct 2023 16:09:22 +0900 Subject: [PATCH 15/18] =?UTF-8?q?feat=20:=20putRequest=20MentorPostRestCon?= =?UTF-8?q?troller=20=EC=97=90=20=EC=9A=94=EC=B2=AD=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/demo/mentoring/MentorPostResponse.java | 13 ++++++------- .../demo/mentoring/MentorPostRestController.java | 6 ++++++ .../example/demo/mentoring/MentorPostService.java | 7 +++++++ .../example/demo/mentoringtest/MentoringTest.java | 2 ++ 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/example/demo/mentoring/MentorPostResponse.java b/src/main/java/com/example/demo/mentoring/MentorPostResponse.java index a5f4f33a..0343f3e5 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostResponse.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostResponse.java @@ -7,7 +7,6 @@ import com.example.demo.user.userInterest.UserInterest; import lombok.Getter; import lombok.Setter; -import org.springframework.web.bind.annotation.PostMapping; import java.util.List; import java.util.stream.Collectors; @@ -43,7 +42,7 @@ public static class WriterDTO { private String name; private String country; private Role role; - private List favorites; + private List interests; public WriterDTO(User user, List userInterests) { this.mentorId = user.getId(); @@ -51,7 +50,7 @@ public WriterDTO(User user, List userInterests) { this.name = user.getFirstName() + " " + user.getLastName(); this.country = user.getCountry(); this.role = user.getRole(); - this.favorites = userInterests.stream() + this.interests = userInterests.stream() .map(userInterest -> userInterest.getInterest().getCategory()) .collect(Collectors.toList()); } @@ -104,7 +103,7 @@ public static class WriterDTO { private String name; private String country; private Role role; - private List favorites; + private List interests; public WriterDTO(User user, List userInterests) { this.mentorId = user.getId(); @@ -112,7 +111,7 @@ public WriterDTO(User user, List userInterests) { this.name = user.getFirstName() + " " + user.getLastName(); this.country = user.getCountry(); this.role = user.getRole(); - this.favorites = userInterests.stream() + this.interests = userInterests.stream() .map(userInterest -> userInterest.getInterest().getCategory()) .collect(Collectors.toList()); } @@ -126,7 +125,7 @@ public static class MenteeDTO{ private String country; private Role role; private int age; - private List favorites; + private List interests; public MenteeDTO(User user, List userInterests) { this.menteeId = user.getId(); @@ -135,7 +134,7 @@ public MenteeDTO(User user, List userInterests) { this.country = user.getCountry(); this.role = user.getRole(); this.age = user.getAge(); - this.favorites = userInterests.stream() + this.interests = userInterests.stream() .map(userInterest -> userInterest.getInterest().getCategory()) .collect(Collectors.toList()); } diff --git a/src/main/java/com/example/demo/mentoring/MentorPostRestController.java b/src/main/java/com/example/demo/mentoring/MentorPostRestController.java index 29251eee..ab9e887a 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostRestController.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostRestController.java @@ -33,4 +33,10 @@ public ResponseEntity getMentorPostId(@PathVariable int id, @AuthenticationPr MentorPostResponse.MentorPostDTO responseDTO = mentorPostService.findMentorPost(id); return ResponseEntity.ok(ApiUtils.success(responseDTO)); } + + @PutMapping(value = "/mentorings/post/{id}") + public ResponseEntity updateMentorPost(@PathVariable int id, @RequestPart MentorPostRequest.CreateDTO requestDTO, Errors errors, @AuthenticationPrincipal CustomUserDetails userDetails) { + mentorPostService.updateMentorPost(requestDTO, userDetails.getUser(), id); + return ResponseEntity.status(HttpStatus.OK).body(ApiUtils.successWithNoContent()); + } } diff --git a/src/main/java/com/example/demo/mentoring/MentorPostService.java b/src/main/java/com/example/demo/mentoring/MentorPostService.java index 3291b7f0..fd96634e 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostService.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostService.java @@ -64,4 +64,11 @@ public MentorPostResponse.MentorPostDTO findMentorPost(int id){ return mentorPostDTO; } + @Transactional + public void updateMentorPost(MentorPostRequest.CreateDTO createDTO, User writer, int id) + { + + } } + + diff --git a/src/test/java/com/example/demo/mentoringtest/MentoringTest.java b/src/test/java/com/example/demo/mentoringtest/MentoringTest.java index 68894fe2..7421a96b 100644 --- a/src/test/java/com/example/demo/mentoringtest/MentoringTest.java +++ b/src/test/java/com/example/demo/mentoringtest/MentoringTest.java @@ -20,6 +20,8 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.test.context.support.WithUserDetails; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.jdbc.Sql; From c0b65cb26774dde0f695b0ef83e44ad0bc0ce47b Mon Sep 17 00:00:00 2001 From: sjmjys954646 Date: Thu, 12 Oct 2023 16:57:31 +0900 Subject: [PATCH 16/18] =?UTF-8?q?feat=20:=20putRequest=20update=EC=9A=94?= =?UTF-8?q?=EC=B2=AD=20service=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/demo/mentoring/MentorPost.java | 7 ++++++ .../mentoring/MentorPostRestController.java | 2 +- .../demo/mentoring/MentorPostService.java | 22 ++++++++++++++----- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/example/demo/mentoring/MentorPost.java b/src/main/java/com/example/demo/mentoring/MentorPost.java index 85a136f6..1d5c9feb 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPost.java +++ b/src/main/java/com/example/demo/mentoring/MentorPost.java @@ -36,4 +36,11 @@ public MentorPost(User writer, String title, String content){ this.title = title; this.content = content; } + + public void update(String title, String content) + { + this.title = title; + this.content = content; + } + } diff --git a/src/main/java/com/example/demo/mentoring/MentorPostRestController.java b/src/main/java/com/example/demo/mentoring/MentorPostRestController.java index ab9e887a..2d8d3465 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostRestController.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostRestController.java @@ -36,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) { - mentorPostService.updateMentorPost(requestDTO, userDetails.getUser(), id); + mentorPostService.updateMentorPost(requestDTO, id); return ResponseEntity.status(HttpStatus.OK).body(ApiUtils.successWithNoContent()); } } diff --git a/src/main/java/com/example/demo/mentoring/MentorPostService.java b/src/main/java/com/example/demo/mentoring/MentorPostService.java index fd96634e..0bb9c7ee 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostService.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostService.java @@ -13,20 +13,21 @@ import org.springframework.transaction.annotation.Transactional; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; @Transactional @RequiredArgsConstructor @Service public class MentorPostService { - private final MentorPostJPARepostiory mentorPostJPARepostiory; + private final MentorPostJPARepostiory mentorPostJPARepository; private final UserInterestJPARepository userInterestJPARepository; private final ContactJPARepository contactJPARepository; //mentorPost생성 @Transactional public void createMentorPost(MentorPostRequest.CreateDTO createDTO, User writer) { - mentorPostJPARepostiory.save(createDTO.toEntity(writer)); + mentorPostJPARepository.save(createDTO.toEntity(writer)); } /* 1. mentorPostList를 조회 @@ -35,7 +36,7 @@ public void createMentorPost(MentorPostRequest.CreateDTO createDTO, User writer) public List findAllMentorPost(int page) { Pageable pageable = PageRequest.of(page,5); - Page pageContent = mentorPostJPARepostiory.findAll(pageable); + Page pageContent = mentorPostJPARepository.findAll(pageable); //List mentorPostList = mentorPostJPARepostiory.findAll(); List mentorPostDTOList = pageContent.getContent().stream().map( mentorPost -> { @@ -47,7 +48,7 @@ public List findAllMentorPost(int page) { } public MentorPostResponse.MentorPostDTO findMentorPost(int id){ - MentorPost mentorPost = mentorPostJPARepostiory.findById(id); + MentorPost mentorPost = mentorPostJPARepository.findById(id); //writer 데이터 User mentor = mentorPost.getWriter(); @@ -65,9 +66,20 @@ public MentorPostResponse.MentorPostDTO findMentorPost(int id){ return mentorPostDTO; } @Transactional - public void updateMentorPost(MentorPostRequest.CreateDTO createDTO, User writer, int id) + public void updateMentorPost(MentorPostRequest.CreateDTO createDTO, int id) { + Optional optionalMentorPost = Optional.ofNullable(mentorPostJPARepository.findById(id)); + if(optionalMentorPost.isPresent()) + { + MentorPost mentorPost = optionalMentorPost.get(); + mentorPost.update(createDTO.getTitle(), createDTO.getContent()); + } + else + { + // 예외처리 + + } } } From 0370f6f317abef5214e8966792cbe99495499a75 Mon Sep 17 00:00:00 2001 From: sjmjys954646 Date: Thu, 12 Oct 2023 17:34:34 +0900 Subject: [PATCH 17/18] =?UTF-8?q?fix=20:=20postRequest=20post=20=EC=9A=94?= =?UTF-8?q?=EC=B2=AD=20=EC=A0=84=EC=B2=B4=EA=B0=80=20=EC=9E=98=EB=AA=BB?= =?UTF-8?q?=EB=90=98=EC=84=9C=20=EB=8B=A4=EB=9C=AF=EC=96=B4=EA=B3=A0?= =?UTF-8?q?=EC=B9=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../demo/mentoring/MentorPostRequest.java | 8 ----- .../demo/mentoring/MentorPostService.java | 5 +-- .../demo/mentoringtest/MentoringTest.java | 35 ++++++++++++++++--- 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/example/demo/mentoring/MentorPostRequest.java b/src/main/java/com/example/demo/mentoring/MentorPostRequest.java index 489daeda..a95762ec 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostRequest.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostRequest.java @@ -14,13 +14,5 @@ public static class CreateDTO { private String title; private String content; - - public MentorPost toEntity(User writer) { - return MentorPost.builder() - .writer(writer) - .title(title) - .content(content) - .build(); - } } } diff --git a/src/main/java/com/example/demo/mentoring/MentorPostService.java b/src/main/java/com/example/demo/mentoring/MentorPostService.java index 0bb9c7ee..82593bbb 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostService.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostService.java @@ -27,7 +27,8 @@ public class MentorPostService { //mentorPost생성 @Transactional public void createMentorPost(MentorPostRequest.CreateDTO createDTO, User writer) { - mentorPostJPARepository.save(createDTO.toEntity(writer)); + MentorPost mentorPost = new MentorPost( writer, createDTO.getTitle(), createDTO.getContent()); + mentorPostJPARepository.save(mentorPost); } /* 1. mentorPostList를 조회 @@ -78,7 +79,7 @@ public void updateMentorPost(MentorPostRequest.CreateDTO createDTO, int id) else { // 예외처리 - + } } } diff --git a/src/test/java/com/example/demo/mentoringtest/MentoringTest.java b/src/test/java/com/example/demo/mentoringtest/MentoringTest.java index 7421a96b..74cfbf23 100644 --- a/src/test/java/com/example/demo/mentoringtest/MentoringTest.java +++ b/src/test/java/com/example/demo/mentoringtest/MentoringTest.java @@ -33,7 +33,6 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; @SpringBootTest -@Sql(value = "classpath:db/teardown.sql") @AutoConfigureMockMvc @ActiveProfiles("test") public class MentoringTest extends RestDoc { @@ -83,13 +82,23 @@ void CreateMentorPostTest() { .build(); // then +// userJPARepository.save(writer); +// mentorPostJPARepostiory.save(mentorPost); +// mentorPostJPARepostiory.save(mentorPost2); + userJPARepository.save(writer); - mentorPostJPARepostiory.save(mentorPost); - mentorPostJPARepostiory.save(mentorPost2); + MentorPostRequest.CreateDTO mentorPostRequest = new MentorPostRequest.CreateDTO(); + mentorPostRequest.setTitle("title"); + mentorPostRequest.setContent("content"); + mentorPostService.createMentorPost(mentorPostRequest, writer); - MentorPost mentorPostFind = mentorPostJPARepostiory.findById(mentorPost.getId()); - Assertions.assertThat(mentorPost.getId()) + MentorPost mentorPostFind = mentorPostJPARepostiory.findById(1); + Assertions.assertThat(1) .isEqualTo(mentorPostFind.getId()); + Assertions.assertThat(mentorPostRequest.getTitle()) + .isEqualTo(mentorPostFind.getTitle()); + Assertions.assertThat(mentorPostRequest.getContent()) + .isEqualTo(mentorPostFind.getContent()); } @@ -207,6 +216,22 @@ void findMentorPostSaveTest() { } + @Test + @DisplayName("updateMentorPostTest") + void updateMentorPostTest() + { + //when + MentorPost mentorPostUpdated = MentorPost.builder() + .title("updated!!!!") + .content("content!!!!") + .build(); + + MentorPostRequest.CreateDTO mentorPostRequest = new MentorPostRequest.CreateDTO(); + //mentorPostRequest. + mentorPostService.updateMentorPost(mentorPostRequest,1); + } + + @Test void mentorPostServiceTest() throws Exception { MentorPostResponse.MentorPostDTO mentorPostFind = mentorPostService.findMentorPost(1); From da26d8ce198b4940e270fee7f1b7367107929c10 Mon Sep 17 00:00:00 2001 From: sjmjys954646 Date: Thu, 12 Oct 2023 17:42:32 +0900 Subject: [PATCH 18/18] =?UTF-8?q?feat=20:=20update=20testcode=20put=20?= =?UTF-8?q?=EC=9A=94=EC=B2=AD=20=ED=85=8C=EC=8A=A4=ED=8A=B8=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../demo/mentoringtest/MentoringTest.java | 34 +++++++++++++++---- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/src/test/java/com/example/demo/mentoringtest/MentoringTest.java b/src/test/java/com/example/demo/mentoringtest/MentoringTest.java index 74cfbf23..6af5632e 100644 --- a/src/test/java/com/example/demo/mentoringtest/MentoringTest.java +++ b/src/test/java/com/example/demo/mentoringtest/MentoringTest.java @@ -220,15 +220,35 @@ void findMentorPostSaveTest() { @DisplayName("updateMentorPostTest") void updateMentorPostTest() { - //when - MentorPost mentorPostUpdated = MentorPost.builder() - .title("updated!!!!") - .content("content!!!!") - .build(); + User writer = User.builder() + .email("anjdal6664@gmail.com") + .password("asdf1234!") + .firstName("Jin") + .lastName("Seung") + .country("Korea") + .age(21) + .role(Role.MENTOR) + .build(); MentorPostRequest.CreateDTO mentorPostRequest = new MentorPostRequest.CreateDTO(); - //mentorPostRequest. - mentorPostService.updateMentorPost(mentorPostRequest,1); + mentorPostRequest.setTitle("tittttttle"); + mentorPostRequest.setContent("content"); + + MentorPostRequest.CreateDTO mentorPostUpdated = new MentorPostRequest.CreateDTO(); + mentorPostUpdated.setTitle("updated!!!!"); + mentorPostUpdated.setContent("contenttt"); + + userJPARepository.save(writer); + mentorPostService.createMentorPost(mentorPostRequest, writer); + mentorPostService.updateMentorPost(mentorPostUpdated,1); + + MentorPost mentorPostFind = mentorPostJPARepostiory.findById(1); + Assertions.assertThat(1) + .isEqualTo(mentorPostFind.getId()); + Assertions.assertThat(mentorPostUpdated.getTitle()) + .isEqualTo(mentorPostFind.getTitle()); + Assertions.assertThat(mentorPostUpdated.getContent()) + .isEqualTo(mentorPostFind.getContent()); }