From c9cd11cdd43e1b4f37a6ca986945a6d7ed9b661c Mon Sep 17 00:00:00 2001 From: Jin_00 Date: Wed, 11 Oct 2023 22:52:21 +0900 Subject: [PATCH 01/22] =?UTF-8?q?feat=20:=20postCountDTO=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20(=20contact,=20done=20=EC=98=86=20=EC=88=AB?= =?UTF-8?q?=EC=9E=90=20=ED=91=9C=EC=8B=9C=ED=95=B4=EC=A3=BC=EB=8A=94=20DTO?= =?UTF-8?q?=20)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../demo/mentoring/contact/ContactResponse.java | 11 +++++++++++ .../demo/mentoring/contact/ContactRestController.java | 9 +++++++++ 2 files changed, 20 insertions(+) diff --git a/src/main/java/com/example/demo/mentoring/contact/ContactResponse.java b/src/main/java/com/example/demo/mentoring/contact/ContactResponse.java index a6daaf6b..ea139eaa 100644 --- a/src/main/java/com/example/demo/mentoring/contact/ContactResponse.java +++ b/src/main/java/com/example/demo/mentoring/contact/ContactResponse.java @@ -11,6 +11,17 @@ import java.util.stream.Collectors; public class ContactResponse { + + @Getter @Setter + public static class postCountDTO { + private int contactCount; + private int doneCount; + + public postCountDTO(int contactCount, int doneCount) { + this.contactCount = contactCount; + this.doneCount = doneCount; + } + } /** * * DTO 담는 순서 diff --git a/src/main/java/com/example/demo/mentoring/contact/ContactRestController.java b/src/main/java/com/example/demo/mentoring/contact/ContactRestController.java index 1827c26f..2ea7cc6f 100644 --- a/src/main/java/com/example/demo/mentoring/contact/ContactRestController.java +++ b/src/main/java/com/example/demo/mentoring/contact/ContactRestController.java @@ -23,5 +23,14 @@ public ResponseEntity findAll(@AuthenticationPrincipal CustomUserDetails user List responseDTO = contactService.findAll(userDetails.getUser()); return ResponseEntity.ok(ApiUtils.success(responseDTO)); } + + @GetMapping(value = "/contacts/postCounts") + @Operation(summary = "", description = "") + public ResponseEntity postCounts(@AuthenticationPrincipal CustomUserDetails userDetails) { + // TO-DO : contact, done 옆 숫자를 띄우는 API 로직 만들기 ( 멘토, 멘티 나눠서 ) + + + return null; + } } From 908a4d9d34485caf7d93a7cc580d2e606926d6d9 Mon Sep 17 00:00:00 2001 From: sjmjys954646 Date: Thu, 12 Oct 2023 21:54:47 +0900 Subject: [PATCH 02/22] =?UTF-8?q?fix=20:=20requestBody=20Part=20->=20Body?= =?UTF-8?q?=20=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 13bfbd812e51e579690719a19f266bb931c3c958 Mon Sep 17 00:00:00 2001 From: Jin_00 Date: Thu, 12 Oct 2023 22:18:52 +0900 Subject: [PATCH 03/22] =?UTF-8?q?feat=20:=20[=20=EB=A9=98=ED=86=A0=20?= =?UTF-8?q?=EA=B8=B0=EC=A4=80=20]=20contact,=20done=20=EA=B2=8C=EC=8B=9C?= =?UTF-8?q?=EA=B8=80=EC=9D=98=20=EA=B0=AF=EC=88=98=EB=A5=BC=20=EC=84=B8?= =?UTF-8?q?=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mentoring/MentorPostJPARepostiory.java | 3 + .../contact/ContactRestController.java | 12 +- .../mentoring/contact/ContactService.java | 107 +++++++++--------- .../contact/NotConnectedRegisterUser.java | 4 + .../demo/mentoring/done/ConnectedUser.java | 7 +- .../mentoring/done/DoneJPARepository.java | 12 ++ 6 files changed, 89 insertions(+), 56 deletions(-) create mode 100644 src/main/java/com/example/demo/mentoring/done/DoneJPARepository.java diff --git a/src/main/java/com/example/demo/mentoring/MentorPostJPARepostiory.java b/src/main/java/com/example/demo/mentoring/MentorPostJPARepostiory.java index 766bcc6e..ceefeb24 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostJPARepostiory.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostJPARepostiory.java @@ -14,4 +14,7 @@ public interface MentorPostJPARepostiory extends JpaRepository findAllByWriter(@Param("writer") int writer); MentorPost findById(int id); + + @Query("select count(*) from MentorPost m where m.writer.id = :userId") + int countByMentorId(int userId); } diff --git a/src/main/java/com/example/demo/mentoring/contact/ContactRestController.java b/src/main/java/com/example/demo/mentoring/contact/ContactRestController.java index 2ea7cc6f..0cabc7b0 100644 --- a/src/main/java/com/example/demo/mentoring/contact/ContactRestController.java +++ b/src/main/java/com/example/demo/mentoring/contact/ContactRestController.java @@ -2,6 +2,7 @@ import com.example.demo.config.auth.CustomUserDetails; import com.example.demo.config.utils.ApiUtils; +import com.example.demo.user.Role; import io.swagger.v3.oas.annotations.Operation; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; @@ -20,7 +21,7 @@ public class ContactRestController { @GetMapping(value = "/contacts") @Operation(summary = "", description = "") public ResponseEntity findAll(@AuthenticationPrincipal CustomUserDetails userDetails) { - List responseDTO = contactService.findAll(userDetails.getUser()); + List responseDTO = contactService.findAll(userDetails.getUser().getId()); return ResponseEntity.ok(ApiUtils.success(responseDTO)); } @@ -29,8 +30,15 @@ public ResponseEntity findAll(@AuthenticationPrincipal CustomUserDetails user public ResponseEntity postCounts(@AuthenticationPrincipal CustomUserDetails userDetails) { // TO-DO : contact, done 옆 숫자를 띄우는 API 로직 만들기 ( 멘토, 멘티 나눠서 ) + // 멘토, 멘티 구분 + Role role = userDetails.getUser().getRole(); - return null; + if ( role == Role.MENTEE ) { + return null; + } + + ContactResponse.postCountDTO responseDTO = contactService.postCountsByMentor(userDetails.getUser().getId()); + return ResponseEntity.ok(ApiUtils.success(responseDTO)); } } diff --git a/src/main/java/com/example/demo/mentoring/contact/ContactService.java b/src/main/java/com/example/demo/mentoring/contact/ContactService.java index 69ed6bd8..b9e1f6f3 100644 --- a/src/main/java/com/example/demo/mentoring/contact/ContactService.java +++ b/src/main/java/com/example/demo/mentoring/contact/ContactService.java @@ -2,6 +2,8 @@ import com.example.demo.mentoring.MentorPost; import com.example.demo.mentoring.MentorPostJPARepostiory; +import com.example.demo.mentoring.done.ConnectedUser; +import com.example.demo.mentoring.done.DoneJPARepository; import com.example.demo.user.Role; import com.example.demo.user.User; import com.example.demo.user.UserJPARepository; @@ -24,65 +26,55 @@ public class ContactService { private final UserJPARepository userJPARepository; private final ContactJPARepository contactJPARepository; private final UserInterestJPARepository userInterestJPARepository; + private final DoneJPARepository doneJPARepository; /** * findAll : dashboard - contact 부분 화면에 필요한 DTO를 응답해주는 함수 * parameter : Account ( 유저의 계정 ) * **/ - public List findAll(User user) { - // user 의 id 값 - int id = user.getId(); - - // 분기 - 유저가 멘토인지, 멘티인지 - if ( user.getRole() == Role.MENTEE ) { - // TO-DO : Mentee 입장에서 조회하는 부분 작성하기 - return null; - } - else { - /** - * 흐름 ( 멘토 입장 ) - * 1. 내가 작성한 게시글 ( mentorPosts ) 들을 조회해서 가져온다. - * 2. 멘토의 정보는 mentorPosts 와 별개로 한번에 조회되는 값이니, for문 밖으로 빼서 조회한다. - * - 멘토의 정보를 만들기 위해 필요한 값 : mentor 의 user, mentor 의 interests - * - user 는 조회하면 되니, 바로 구할 수 있다. - * - userInterest 에서 멘토의 interest 값들을 가져올 수 있기 때문에, userInterest 와 interest 를 join 후 tag 들을 가져온다. - * 3. mentorPosts 를 활용하여 for문을 돌면서 mentorPost 1개 + mentor 정보 1개 + mentee 정보 여러개 의 꼴로 DTO 를 만든다. - * - mentee 의 정보를 만들기 위해 필요한 값 : notConnectedRegisterUser 에서 mentee 의 정보, mentee 의 interests - * - 신청한 멘티들의 정보를 가져오기 위해 notConnectedRegisterUser 의 테이블과 , 각 멘티에 해당하는 interests 들을 묶어서 DTO 로 만들기 ( 그래서 List 를 만듬 ) - * - 싹다 묶어서 reponseDTOs 로 전달 - * **/ - // Return 할 객체 - List responseDTOs = new ArrayList<>(); - - // 멘토의 id 에 해당하는 mentorPost 다 가져오기 - List mentorPosts = mentorPostJPARepostiory.findAllByWriter(id); - - // 멘토 정보 가져오기 ( 나중에 User 로 바꿔야 함 ) - User mentorUser = userJPARepository.findById(id); - // List 가져오기 ( 멘토꺼 tag 목록 ) - List mentorInterests = userInterestJPARepository.findAllById(mentorUser.getId()); - - // MentorDTO 담기 - ContactResponse.MentorDTO mentorDTO = new ContactResponse.MentorDTO(mentorUser, mentorInterests); - - // 만약 3개의 글을 썼을 경우, 현재 mentorPosts 에는 3개의 글 목록이 존재함 - for ( MentorPost mentorPost : mentorPosts ) { - // List 만들기 - // 신청한 멘티의 목록 ( postId 로 조회 ) - List mentees = contactJPARepository.findAllByMentorPostId(mentorPost.getId()); - - // List 생성 - List menteeDTOs = mentees - .stream() - .map(this::createMenteeDTO) - .collect(Collectors.toList()); - // responseDTO 에 담기 - responseDTOs.add(new ContactResponse.MentorPostDTO(mentorPost, mentorDTO, menteeDTOs)); - - } - return responseDTOs; - } + public List findAll(int id) { + /** + * 흐름 ( 멘토 입장 ) + * 1. 내가 작성한 게시글 ( mentorPosts ) 들을 조회해서 가져온다. + * 2. 멘토의 정보는 mentorPosts 와 별개로 한번에 조회되는 값이니, for문 밖으로 빼서 조회한다. + * - 멘토의 정보를 만들기 위해 필요한 값 : mentor 의 user, mentor 의 interests + * - user 는 조회하면 되니, 바로 구할 수 있다. + * - userInterest 에서 멘토의 interest 값들을 가져올 수 있기 때문에, userInterest 와 interest 를 join 후 tag 들을 가져온다. + * 3. mentorPosts 를 활용하여 for문을 돌면서 mentorPost 1개 + mentor 정보 1개 + mentee 정보 여러개 의 꼴로 DTO 를 만든다. + * - mentee 의 정보를 만들기 위해 필요한 값 : notConnectedRegisterUser 에서 mentee 의 정보, mentee 의 interests + * - 신청한 멘티들의 정보를 가져오기 위해 notConnectedRegisterUser 의 테이블과 , 각 멘티에 해당하는 interests 들을 묶어서 DTO 로 만들기 ( 그래서 List 를 만듬 ) + * - 싹다 묶어서 reponseDTOs 로 전달 + * **/ + // Return 할 객체 + List responseDTOs = new ArrayList<>(); + + // 멘토의 id 에 해당하는 mentorPost 다 가져오기 + List mentorPosts = mentorPostJPARepostiory.findAllByWriter(id); + + // 멘토 정보 가져오기 ( 나중에 User 로 바꿔야 함 ) + User mentorUser = userJPARepository.findById(id); + // List 가져오기 ( 멘토꺼 tag 목록 ) + List mentorInterests = userInterestJPARepository.findAllById(mentorUser.getId()); + + // MentorDTO 담기 + ContactResponse.MentorDTO mentorDTO = new ContactResponse.MentorDTO(mentorUser, mentorInterests); + + // 만약 3개의 글을 썼을 경우, 현재 mentorPosts 에는 3개의 글 목록이 존재함 + for ( MentorPost mentorPost : mentorPosts ) { + // List 만들기 + // 신청한 멘티의 목록 ( postId 로 조회 ) + List mentees = contactJPARepository.findAllByMentorPostId(mentorPost.getId()); + + // List 생성 + List menteeDTOs = mentees + .stream() + .map(this::createMenteeDTO) + .collect(Collectors.toList()); + // responseDTO 에 담기 + responseDTOs.add(new ContactResponse.MentorPostDTO(mentorPost, mentorDTO, menteeDTOs)); + } + return responseDTOs; } // 매핑 로직 분리 ( menteeDTO 생성 로직 ) @@ -94,4 +86,13 @@ private ContactResponse.MenteeDTO createMenteeDTO(NotConnectedRegisterUser mente return new ContactResponse.MenteeDTO(mentee, menteeInterests); } + // contact, done 화면에서 게시글을 조회해서 갯수를 전달해주는 함수 + public ContactResponse.postCountDTO postCountsByMentor(int userId) { + // contact 화면에서 게시글을 조회 ( 나중에 where 조건에 state 를 달아야 함 ) + int contactCount = mentorPostJPARepostiory.countByMentorId(userId); + // done 화면에서 게시글을 조회 + int doneCount = doneJPARepository.countByMentorId(userId); + + return new ContactResponse.postCountDTO(contactCount, doneCount); + } } 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 b4537021..1ed18dfb 100644 --- a/src/main/java/com/example/demo/mentoring/contact/NotConnectedRegisterUser.java +++ b/src/main/java/com/example/demo/mentoring/contact/NotConnectedRegisterUser.java @@ -7,12 +7,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 not_connected_register_user SET deleted_at = CURRENT_TIMESTAMP, isDeleted = TRUE where id = ?") @Table(name = "notConnectedRegisterUser_tb") public class NotConnectedRegisterUser extends BaseTime { diff --git a/src/main/java/com/example/demo/mentoring/done/ConnectedUser.java b/src/main/java/com/example/demo/mentoring/done/ConnectedUser.java index 46f4bca2..b84ba0c7 100644 --- a/src/main/java/com/example/demo/mentoring/done/ConnectedUser.java +++ b/src/main/java/com/example/demo/mentoring/done/ConnectedUser.java @@ -1,18 +1,23 @@ package com.example.demo.mentoring.done; +import com.example.demo.config.utils.BaseTime; import com.example.demo.user.User; import lombok.AccessLevel; 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 connected_user_tb SET deleted_at = CURRENT_TIMESTAMP, isDeleted = TRUE where id = ?") @Table(name = "connectedUser_tb") -public class ConnectedUser { +public class ConnectedUser extends BaseTime { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) diff --git a/src/main/java/com/example/demo/mentoring/done/DoneJPARepository.java b/src/main/java/com/example/demo/mentoring/done/DoneJPARepository.java new file mode 100644 index 00000000..ff86e054 --- /dev/null +++ b/src/main/java/com/example/demo/mentoring/done/DoneJPARepository.java @@ -0,0 +1,12 @@ +package com.example.demo.mentoring.done; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; + +import java.util.List; + +public interface DoneJPARepository extends JpaRepository { + @Query("select count(*) from ConnectedUser cu where cu.mentorUser.id = :userId") + int countByMentorId(@Param("userId") int userId); +} From 3d525011ab677cad611b89545d46296b54bea857 Mon Sep 17 00:00:00 2001 From: Jin_00 Date: Thu, 12 Oct 2023 22:19:41 +0900 Subject: [PATCH 04/22] test : /contacts, /contacts/countPosts API Test --- src/main/resources/db/teardown.sql | 18 ++- .../java/com/example/demo/contactTest.java | 148 ++++++------------ 2 files changed, 58 insertions(+), 108 deletions(-) diff --git a/src/main/resources/db/teardown.sql b/src/main/resources/db/teardown.sql index 949b96e4..f20d62d4 100644 --- a/src/main/resources/db/teardown.sql +++ b/src/main/resources/db/teardown.sql @@ -4,6 +4,7 @@ TRUNCATE TABLE interest_tb; TRUNCATE TABLE mentor_post_tb; TRUNCATE TABLE user_interest_tb; TRUNCATE TABLE not_connected_register_user_tb; +TRUNCATE TABLE connected_user_tb; SET REFERENTIAL_INTEGRITY TRUE; -- user Table @@ -12,7 +13,8 @@ INSERT INTO user_tb (created_at, first_name, last_name, email, password, country (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'); + (NOW(), 'Admin', 'Adminson', 'admin22222@example.com', '{bcrypt}$2a$10$8H0OT8wgtALJkig6fmypi.Y7jzI5Y7W9PGgRKqnVeS2cLWGifwHF2', 'USA', 'I am the admin.', 35, 'admin.jpg', 'MENTEE'), + (NOW(), 'Adminnnn', 'Adminsonnnnn', 'admin33333@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 @@ -36,7 +38,7 @@ INSERT INTO mentor_post_tb (created_at, writer_id, title, content) VALUES (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.');; + (NOW(), 2, 'Mathematics Tutoring', 'I can help you understand and excel in math.'); -- notConnectedRegisterUser Table @@ -45,6 +47,13 @@ INSERT INTO not_connected_register_user_tb (created_at, mentor_post_id, mentee_u (NOW(), 1, 4, 'ACCEPT'), (NOW(), 2, 5, 'AWAIT'); +-- connectedUser Table +INSERT INTO connected_user_tb (created_at, mentor_user_id, mentee_user_id) +VALUES + (NOW(), 1, 5), + (NOW(), 1, 6), + (NOW(), 2, 3); + -- userInterest Table INSERT INTO user_interest_tb (created_at, user_id, interest_id) VALUES (NOW(), 1, 1), @@ -56,5 +65,6 @@ INSERT INTO user_interest_tb (created_at, user_id, interest_id) VALUES (NOW(), 4, 1), (NOW(), 4, 3), (NOW(), 5, 3), - (NOW(), 5, 4) -; \ No newline at end of file + (NOW(), 5, 4), + (NOW(), 5, 3), + (NOW(), 5, 4); diff --git a/src/test/java/com/example/demo/contactTest.java b/src/test/java/com/example/demo/contactTest.java index 18c072e0..bb761da9 100644 --- a/src/test/java/com/example/demo/contactTest.java +++ b/src/test/java/com/example/demo/contactTest.java @@ -14,28 +14,28 @@ import com.example.demo.user.userInterest.UserInterest; import com.example.demo.user.userInterest.UserInterestJPARepository; import com.fasterxml.jackson.databind.ObjectMapper; +import io.swagger.models.Contact; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +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 org.springframework.test.web.servlet.result.MockMvcResultHandlers; import java.util.List; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; + @SpringBootTest @ActiveProfiles("test") -public class contactTest { - - @Autowired - private MentorPostJPARepostiory mentorPostJPARepostiory; - @Autowired - private UserJPARepository userJPARepository; - @Autowired - private ContactJPARepository contactJPARepository; - @Autowired - private UserInterestJPARepository userInterestJPARepository; - @Autowired - private InterestJPARepository interestJPARepository; +@Sql("classpath:db/teardown.sql") +public class contactTest extends RestDoc { @Autowired private ContactService contactService; @@ -43,104 +43,44 @@ public class contactTest { @Autowired private ObjectMapper om; - User user1; - - @BeforeEach - void setUp() { - user1 = User.builder() - .email("anjdal65@gmail.com") - .password("asdf1234!") - .firstName("aaa") - .lastName("Seung") - .country("Korea") - .age(21) - .role(Role.MENTOR) - .build(); - User user2 = User.builder() - .email("anjdal44@gmail.com") - .password("asdf1234!") - .firstName("bbb") - .lastName("Seung") - .country("Korea") - .age(21) - .role(Role.MENTEE) - .build(); - User user3 = User.builder() - .email("anjdal66@gmail.com") - .password("asdf1234!") - .firstName("ccc") - .lastName("Seung") - .country("Korea") - .age(21) - .role(Role.MENTEE) - .build(); - - userJPARepository.save(user1); - userJPARepository.save(user2); - userJPARepository.save(user3); - - Interest interest = Interest.builder() - .category("K-POP") - .build(); - - interestJPARepository.save(interest); - - - UserInterest userInterest1 = UserInterest.builder() - .user(user1) - .interest(interest) - .build(); - UserInterest userInterest2 = UserInterest.builder() - .user(user2) - .interest(interest) - .build(); - UserInterest userInterest3 = UserInterest.builder() - .user(user3) - .interest(interest) - .build(); - - userInterestJPARepository.save(userInterest1); - userInterestJPARepository.save(userInterest2); - userInterestJPARepository.save(userInterest3); - - MentorPost mentorPost = MentorPost.builder() - .writer(user1) - .title("가나다라마바사") - .build(); - - MentorPost mentorPost2 = MentorPost.builder() - .writer(user1) - .title("아자차카타파하") - .build(); - - mentorPostJPARepostiory.save(mentorPost); - mentorPostJPARepostiory.save(mentorPost2); - - - NotConnectedRegisterUser mentee1 = NotConnectedRegisterUser.builder() - .mentorPost(mentorPost) - .menteeUser(user2) - .state(NotConnectedRegisterUser.State.AWAIT) - .build(); - NotConnectedRegisterUser mentee2 = NotConnectedRegisterUser.builder() - .mentorPost(mentorPost) - .menteeUser(user3) - .state(NotConnectedRegisterUser.State.AWAIT) - .build(); - // 문제점 : save 를 하기 위해 notConnected~ table 을 조회, mentorPost table 을 조회, account table 을 조회함 - // 나중에 고쳐야할 듯 하다. - contactJPARepository.save(mentee1); - contactJPARepository.save(mentee2); + @Test + @WithUserDetails("john@example.com") + @DisplayName("contact - mentor") + void contactTest() throws Exception { + + // given + + // when + ResultActions resultActions = mvc.perform( + get("/contacts") + ); + + // console + String responseBody = resultActions.andReturn().getResponse().getContentAsString(); + System.out.println("테스트 : "+responseBody); + + // verify + resultActions.andExpect(jsonPath("$.status").value("success")); + } @Test - void test() throws Exception { + @WithUserDetails("john@example.com") + @DisplayName("contact, done - count") + void countTest() throws Exception { + + // when + ResultActions resultActions = mvc.perform( + get("/contacts/postCounts") + ); - List responseDTOs = contactService.findAll(user1); + // console + String responseBody = resultActions.andReturn().getResponse().getContentAsString(); + System.out.println("테스트 : "+responseBody); - String responseBody = om.writeValueAsString(responseDTOs); + // verify + resultActions.andExpect(jsonPath("$.status").value("success")); - System.out.println("test : " + responseBody); } } From 4b92acfe9379e1cbede1e7ce975592cb56a23fae Mon Sep 17 00:00:00 2001 From: Jin_00 Date: Thu, 12 Oct 2023 22:21:52 +0900 Subject: [PATCH 05/22] =?UTF-8?q?feat=20:=20/contacts=20mentor,=20mentee?= =?UTF-8?q?=20=ED=99=94=EB=A9=B4=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../demo/mentoring/contact/ContactRestController.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/example/demo/mentoring/contact/ContactRestController.java b/src/main/java/com/example/demo/mentoring/contact/ContactRestController.java index 0cabc7b0..4f9b4e9d 100644 --- a/src/main/java/com/example/demo/mentoring/contact/ContactRestController.java +++ b/src/main/java/com/example/demo/mentoring/contact/ContactRestController.java @@ -21,9 +21,14 @@ public class ContactRestController { @GetMapping(value = "/contacts") @Operation(summary = "", description = "") public ResponseEntity findAll(@AuthenticationPrincipal CustomUserDetails userDetails) { - List responseDTO = contactService.findAll(userDetails.getUser().getId()); + if ( userDetails.getUser().getRole() == Role.MENTEE ) { + List responseDTO = contactService.findAllByMentee(userDetails.getUser()); + return ResponseEntity.ok(ApiUtils.success(responseDTO)); + } + List responseDTO = contactService.findAllByMentor(userDetails.getUser()); return ResponseEntity.ok(ApiUtils.success(responseDTO)); } +} @GetMapping(value = "/contacts/postCounts") @Operation(summary = "", description = "") From 4c197e55cd285974df5fea1c16d53b3170fa5fb6 Mon Sep 17 00:00:00 2001 From: Jin_00 Date: Thu, 12 Oct 2023 22:22:32 +0900 Subject: [PATCH 06/22] =?UTF-8?q?feat=20:=20mentee=20-=20contact=20?= =?UTF-8?q?=ED=99=94=EB=A9=B4=EC=9D=84=20=EC=9C=84=ED=95=9C=20DTO=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../demo/mentoring/contact/ContactResponse.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/main/java/com/example/demo/mentoring/contact/ContactResponse.java b/src/main/java/com/example/demo/mentoring/contact/ContactResponse.java index ea139eaa..74923a0c 100644 --- a/src/main/java/com/example/demo/mentoring/contact/ContactResponse.java +++ b/src/main/java/com/example/demo/mentoring/contact/ContactResponse.java @@ -22,6 +22,19 @@ public postCountDTO(int contactCount, int doneCount) { this.doneCount = doneCount; } } + + @Getter @Setter + public static class MenteeContactDTO { + private int postId; + private String title; + private MentorDTO mentor; + + public MenteeContactDTO(MentorPost mentorPost, MentorDTO mentor) { + this.postId = mentorPost.getId(); + this.title = mentorPost.getTitle(); + this.mentor = mentor; + } + } /** * * DTO 담는 순서 From c02ca17d6ea2cb756da326d38b1f3b4079fb12b2 Mon Sep 17 00:00:00 2001 From: sjmjys954646 Date: Thu, 12 Oct 2023 22:24:07 +0900 Subject: [PATCH 07/22] =?UTF-8?q?feat=20:=20add=20Enum=20at=20MentorPost?= =?UTF-8?q?=20mentorPost=EC=97=90=203=EA=B0=80=EC=A7=80=20=EC=83=81?= =?UTF-8?q?=ED=83=9C=20ACTIVE=20DONE=20DELETED=EB=A5=BC=20=EB=8D=94?= =?UTF-8?q?=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); From 2630e3320dbc5921ada49517574a04a3df832d51 Mon Sep 17 00:00:00 2001 From: Jin_00 Date: Thu, 12 Oct 2023 23:03:02 +0900 Subject: [PATCH 08/22] =?UTF-8?q?fix=20:=20conntectedUser=20=EC=9D=98=20me?= =?UTF-8?q?ntorUserId=20->=20mentorPostId=20=EB=B3=80=EA=B2=BD=EC=97=90=20?= =?UTF-8?q?=EB=94=B0=EB=A5=B8=20=EC=BD=94=EB=93=9C=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/done/ConnectedUser.java | 10 +++++----- .../example/demo/mentoring/done/DoneJPARepository.java | 3 +-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/example/demo/mentoring/done/ConnectedUser.java b/src/main/java/com/example/demo/mentoring/done/ConnectedUser.java index b84ba0c7..a6ff7f78 100644 --- a/src/main/java/com/example/demo/mentoring/done/ConnectedUser.java +++ b/src/main/java/com/example/demo/mentoring/done/ConnectedUser.java @@ -1,6 +1,7 @@ package com.example.demo.mentoring.done; import com.example.demo.config.utils.BaseTime; +import com.example.demo.mentoring.MentorPost; import com.example.demo.user.User; import lombok.AccessLevel; import lombok.Builder; @@ -23,16 +24,15 @@ public class ConnectedUser extends BaseTime { @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; - @ManyToOne - private User mentorUser; + @ManyToOne(fetch = FetchType.LAZY) + private MentorPost mentorPost; @ManyToOne private User menteeUser; @Builder - public ConnectedUser(int id, User mentorUser, User menteeUser) { - this.id = id; - this.mentorUser = mentorUser; + public ConnectedUser(MentorPost mentorPost, User menteeUser) { + this.mentorPost = mentorPost; this.menteeUser = menteeUser; } diff --git a/src/main/java/com/example/demo/mentoring/done/DoneJPARepository.java b/src/main/java/com/example/demo/mentoring/done/DoneJPARepository.java index ff86e054..ba5c2626 100644 --- a/src/main/java/com/example/demo/mentoring/done/DoneJPARepository.java +++ b/src/main/java/com/example/demo/mentoring/done/DoneJPARepository.java @@ -7,6 +7,5 @@ import java.util.List; public interface DoneJPARepository extends JpaRepository { - @Query("select count(*) from ConnectedUser cu where cu.mentorUser.id = :userId") - int countByMentorId(@Param("userId") int userId); + } From f148c7f95ba006ab9b5599b34c95d755d008db9a Mon Sep 17 00:00:00 2001 From: Jin_00 Date: Thu, 12 Oct 2023 23:04:49 +0900 Subject: [PATCH 09/22] =?UTF-8?q?fix=20:=20mentorPost=20=EA=B0=84=20?= =?UTF-8?q?=EA=B4=80=EA=B3=84=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../demo/mentoring/contact/NotConnectedRegisterUser.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) 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 1ed18dfb..94014f15 100644 --- a/src/main/java/com/example/demo/mentoring/contact/NotConnectedRegisterUser.java +++ b/src/main/java/com/example/demo/mentoring/contact/NotConnectedRegisterUser.java @@ -24,7 +24,7 @@ public class NotConnectedRegisterUser extends BaseTime { @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; - @OneToOne(fetch = FetchType.LAZY) + @ManyToOne(fetch = FetchType.LAZY) private MentorPost mentorPost; @ManyToOne(fetch = FetchType.LAZY) @@ -39,8 +39,7 @@ public enum State { } @Builder - public NotConnectedRegisterUser(int id, MentorPost mentorPost, User menteeUser, State state) { - this.id = id; + public NotConnectedRegisterUser(MentorPost mentorPost, User menteeUser, State state) { this.mentorPost = mentorPost; this.menteeUser = menteeUser; this.state = state; From 93740aab4df87195dbda207bac5b2c0b2bb44f62 Mon Sep 17 00:00:00 2001 From: Jin_00 Date: Thu, 12 Oct 2023 23:05:41 +0900 Subject: [PATCH 10/22] =?UTF-8?q?fix=20:=20Lazy=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/example/demo/mentoring/done/ConnectedUser.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/example/demo/mentoring/done/ConnectedUser.java b/src/main/java/com/example/demo/mentoring/done/ConnectedUser.java index a6ff7f78..010926e5 100644 --- a/src/main/java/com/example/demo/mentoring/done/ConnectedUser.java +++ b/src/main/java/com/example/demo/mentoring/done/ConnectedUser.java @@ -27,7 +27,7 @@ public class ConnectedUser extends BaseTime { @ManyToOne(fetch = FetchType.LAZY) private MentorPost mentorPost; - @ManyToOne + @ManyToOne(fetch = FetchType.LAZY) private User menteeUser; @Builder From 78b86147f474f5233fde1187f39b74e335ab987c Mon Sep 17 00:00:00 2001 From: Jin_00 Date: Thu, 12 Oct 2023 23:06:14 +0900 Subject: [PATCH 11/22] =?UTF-8?q?feat=20:=20=EB=A9=98=ED=86=A0,=20?= =?UTF-8?q?=EB=A9=98=ED=8B=B0=20contact=20=ED=99=94=EB=A9=B4=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80=20=EB=B0=8F?= =?UTF-8?q?=20=EC=BD=94=EB=93=9C=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mentoring/MentorPostJPARepostiory.java | 11 ++- .../contact/ContactRestController.java | 6 +- .../mentoring/contact/ContactService.java | 89 +++++++++---------- 3 files changed, 53 insertions(+), 53 deletions(-) diff --git a/src/main/java/com/example/demo/mentoring/MentorPostJPARepostiory.java b/src/main/java/com/example/demo/mentoring/MentorPostJPARepostiory.java index ceefeb24..74b95746 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostJPARepostiory.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostJPARepostiory.java @@ -2,7 +2,6 @@ import org.springframework.data.jpa.repository.JpaRepository; -import java.util.Optional; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; @@ -15,6 +14,12 @@ public interface MentorPostJPARepostiory extends JpaRepository findAllByMenteeUserId(@Param("menteeUserId") int menteeUserId); } diff --git a/src/main/java/com/example/demo/mentoring/contact/ContactRestController.java b/src/main/java/com/example/demo/mentoring/contact/ContactRestController.java index 4f9b4e9d..4470be70 100644 --- a/src/main/java/com/example/demo/mentoring/contact/ContactRestController.java +++ b/src/main/java/com/example/demo/mentoring/contact/ContactRestController.java @@ -22,13 +22,13 @@ public class ContactRestController { @Operation(summary = "", description = "") public ResponseEntity findAll(@AuthenticationPrincipal CustomUserDetails userDetails) { if ( userDetails.getUser().getRole() == Role.MENTEE ) { - List responseDTO = contactService.findAllByMentee(userDetails.getUser()); + List responseDTO = contactService.findAllByMentee(userDetails.getUser().getId()); return ResponseEntity.ok(ApiUtils.success(responseDTO)); } - List responseDTO = contactService.findAllByMentor(userDetails.getUser()); + List responseDTO = contactService.findAllByMentor(userDetails.getUser().getId()); return ResponseEntity.ok(ApiUtils.success(responseDTO)); } -} + @GetMapping(value = "/contacts/postCounts") @Operation(summary = "", description = "") diff --git a/src/main/java/com/example/demo/mentoring/contact/ContactService.java b/src/main/java/com/example/demo/mentoring/contact/ContactService.java index b9e1f6f3..ab36bcef 100644 --- a/src/main/java/com/example/demo/mentoring/contact/ContactService.java +++ b/src/main/java/com/example/demo/mentoring/contact/ContactService.java @@ -2,9 +2,7 @@ import com.example.demo.mentoring.MentorPost; import com.example.demo.mentoring.MentorPostJPARepostiory; -import com.example.demo.mentoring.done.ConnectedUser; import com.example.demo.mentoring.done.DoneJPARepository; -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; @@ -13,7 +11,6 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @@ -22,59 +19,57 @@ @Service public class ContactService { - private final MentorPostJPARepostiory mentorPostJPARepostiory; + private final MentorPostJPARepostiory mentorPostJPARepository; private final UserJPARepository userJPARepository; private final ContactJPARepository contactJPARepository; private final UserInterestJPARepository userInterestJPARepository; private final DoneJPARepository doneJPARepository; /** - * findAll : dashboard - contact 부분 화면에 필요한 DTO를 응답해주는 함수 - * parameter : Account ( 유저의 계정 ) + * contact - mentee 화면에서 mentor 가 작성한 글에 신청을 누른 게시글들을 가져오는 함수 * **/ - public List findAll(int id) { - /** - * 흐름 ( 멘토 입장 ) - * 1. 내가 작성한 게시글 ( mentorPosts ) 들을 조회해서 가져온다. - * 2. 멘토의 정보는 mentorPosts 와 별개로 한번에 조회되는 값이니, for문 밖으로 빼서 조회한다. - * - 멘토의 정보를 만들기 위해 필요한 값 : mentor 의 user, mentor 의 interests - * - user 는 조회하면 되니, 바로 구할 수 있다. - * - userInterest 에서 멘토의 interest 값들을 가져올 수 있기 때문에, userInterest 와 interest 를 join 후 tag 들을 가져온다. - * 3. mentorPosts 를 활용하여 for문을 돌면서 mentorPost 1개 + mentor 정보 1개 + mentee 정보 여러개 의 꼴로 DTO 를 만든다. - * - mentee 의 정보를 만들기 위해 필요한 값 : notConnectedRegisterUser 에서 mentee 의 정보, mentee 의 interests - * - 신청한 멘티들의 정보를 가져오기 위해 notConnectedRegisterUser 의 테이블과 , 각 멘티에 해당하는 interests 들을 묶어서 DTO 로 만들기 ( 그래서 List 를 만듬 ) - * - 싹다 묶어서 reponseDTOs 로 전달 - * **/ - // Return 할 객체 - List responseDTOs = new ArrayList<>(); - - // 멘토의 id 에 해당하는 mentorPost 다 가져오기 - List mentorPosts = mentorPostJPARepostiory.findAllByWriter(id); - - // 멘토 정보 가져오기 ( 나중에 User 로 바꿔야 함 ) - User mentorUser = userJPARepository.findById(id); - // List 가져오기 ( 멘토꺼 tag 목록 ) + public List findAllByMentee(int userId) { + + return mentorPostJPARepository.findAllByMenteeUserId(userId).stream() + .map(this::createMenteeContactDTO) + .collect(Collectors.toList()); + } + + // contact - mentee 부분 리팩토링 ( DTO 를 만드는 부분 ) + private ContactResponse.MenteeContactDTO createMenteeContactDTO(MentorPost mentorPost) { + User mentorUser = userJPARepository.findById(mentorPost.getWriter().getId()); List mentorInterests = userInterestJPARepository.findAllById(mentorUser.getId()); - // MentorDTO 담기 ContactResponse.MentorDTO mentorDTO = new ContactResponse.MentorDTO(mentorUser, mentorInterests); - // 만약 3개의 글을 썼을 경우, 현재 mentorPosts 에는 3개의 글 목록이 존재함 - for ( MentorPost mentorPost : mentorPosts ) { - // List 만들기 - // 신청한 멘티의 목록 ( postId 로 조회 ) - List mentees = contactJPARepository.findAllByMentorPostId(mentorPost.getId()); - - // List 생성 - List menteeDTOs = mentees - .stream() - .map(this::createMenteeDTO) - .collect(Collectors.toList()); - // responseDTO 에 담기 - responseDTOs.add(new ContactResponse.MentorPostDTO(mentorPost, mentorDTO, menteeDTOs)); - - } - return responseDTOs; + return new ContactResponse.MenteeContactDTO(mentorPost, mentorDTO); + } + + // --------------------------------------------------------------------------------------------- + + /** + * contact - mentor 화면에서 post 와 mentee 간 엮인 정보들을 조회해서 가져오는 함수 + * **/ + public List findAllByMentor(int userId) { + + User mentorUser = userJPARepository.findById(userId); + List mentorInterests = userInterestJPARepository.findAllById(mentorUser.getId()); + ContactResponse.MentorDTO mentorDTO = new ContactResponse.MentorDTO(mentorUser, mentorInterests); + + return mentorPostJPARepository.findAllByWriter(userId).stream() + .map(mentorPost -> createMentorPostDTO(mentorPost, mentorDTO)) + .collect(Collectors.toList()); + + } + + // MentorPostDTO 생성 로직 + private ContactResponse.MentorPostDTO createMentorPostDTO(MentorPost mentorPost, ContactResponse.MentorDTO mentorDTO) { + List menteeDTOs = contactJPARepository.findAllByMentorPostId(mentorPost.getId()) + .stream() + .map(this::createMenteeDTO) + .collect(Collectors.toList()); + + return new ContactResponse.MentorPostDTO(mentorPost, mentorDTO, menteeDTOs); } // 매핑 로직 분리 ( menteeDTO 생성 로직 ) @@ -89,9 +84,9 @@ private ContactResponse.MenteeDTO createMenteeDTO(NotConnectedRegisterUser mente // contact, done 화면에서 게시글을 조회해서 갯수를 전달해주는 함수 public ContactResponse.postCountDTO postCountsByMentor(int userId) { // contact 화면에서 게시글을 조회 ( 나중에 where 조건에 state 를 달아야 함 ) - int contactCount = mentorPostJPARepostiory.countByMentorId(userId); + int contactCount = mentorPostJPARepository.countContactByMentorId(userId); // done 화면에서 게시글을 조회 - int doneCount = doneJPARepository.countByMentorId(userId); + int doneCount = mentorPostJPARepository.countDoneByMentorId(userId); return new ContactResponse.postCountDTO(contactCount, doneCount); } From 1f3931d3e2472551e9526a78a84399643cf54415 Mon Sep 17 00:00:00 2001 From: Jin_00 Date: Thu, 12 Oct 2023 23:06:39 +0900 Subject: [PATCH 12/22] =?UTF-8?q?feat=20:=20connectedUser=20table=20?= =?UTF-8?q?=EC=97=90=20=EB=8D=94=EB=AF=B8=EB=8D=B0=EC=9D=B4=ED=84=B0=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/db/teardown.sql | 41 +++++++++++++++--------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/src/main/resources/db/teardown.sql b/src/main/resources/db/teardown.sql index f20d62d4..76d0c4a3 100644 --- a/src/main/resources/db/teardown.sql +++ b/src/main/resources/db/teardown.sql @@ -24,35 +24,34 @@ INSERT INTO interest_tb (created_at, category) VALUES (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.'); +INSERT INTO mentor_post_tb (created_at, writer_id, title, content, state) VALUES + (NOW(), 1, 'Teaching Programming', 'I can teach you how to code.', 'ACTIVE'), + (NOW(), 1, 'Art Workshop', 'Let''s create beautiful art together.', 'DONE'), + (NOW(), 2, 'Software Development Mentorship', 'I can mentor you in software development.', 'ACTIVE'), + (NOW(), 2, 'Art and Painting Mentorship', 'Learn the art of painting with me.', 'ACTIVE'), + (NOW(), 1, 'Web Development Mentorship', 'I can teach you web development from scratch.', 'ACTIVE'), + (NOW(), 2, 'Fitness and Health Mentorship', 'Get in shape and stay healthy with my guidance.', 'ACTIVE'), + (NOW(), 1, 'Data Science Mentorship', 'Learn data science and machine learning with me.', 'ACTIVE'), + (NOW(), 2, 'Music Production Mentorship', 'Produce your own music with professional tips.', 'ACTIVE'), + (NOW(), 2, 'Cooking and Culinary Arts Mentorship', 'Master the art of cooking and culinary skills.', 'ACTIVE'), + (NOW(), 2, 'Entrepreneurship Mentorship', 'Start and grow your own business.', 'ACTIVE'), + (NOW(), 1, 'Graphic Design Mentorship', 'Create stunning graphics and designs.', 'ACTIVE'), + (NOW(), 1, 'Yoga and Mindfulness Mentorship', 'Find inner peace and balance through yoga.', 'ACTIVE'), + (NOW(), 1, 'Photography Mentorship', 'Capture amazing moments with your camera.', 'ACTIVE'), + (NOW(), 2, 'Mathematics Tutoring', 'I can help you understand and excel in math.', 'ACTIVE'); -- 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'); + (NOW(), 3, 5, 'AWAIT'); -- connectedUser Table -INSERT INTO connected_user_tb (created_at, mentor_user_id, mentee_user_id) -VALUES - (NOW(), 1, 5), - (NOW(), 1, 6), - (NOW(), 2, 3); +INSERT INTO connected_user_tb (created_at, mentor_post_id, mentee_user_id) VALUES + (NOW(), 2, 3), + (NOW(), 2, 5), + (NOW(), 2, 6); -- userInterest Table INSERT INTO user_interest_tb (created_at, user_id, interest_id) VALUES From 02edc88304237dbd31f1b1c3459ebcde6238bc18 Mon Sep 17 00:00:00 2001 From: Jin_00 Date: Thu, 12 Oct 2023 23:12:07 +0900 Subject: [PATCH 13/22] =?UTF-8?q?fix=20:=20mentor=20=EA=B8=B0=EC=A4=80=20c?= =?UTF-8?q?ontact=20=ED=99=94=EB=A9=B4=20=EC=A1=B0=ED=9A=8C=20=EC=BF=BC?= =?UTF-8?q?=EB=A6=AC=20=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/MentorPostJPARepostiory.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/example/demo/mentoring/MentorPostJPARepostiory.java b/src/main/java/com/example/demo/mentoring/MentorPostJPARepostiory.java index 74b95746..8e7579ff 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostJPARepostiory.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostJPARepostiory.java @@ -9,7 +9,7 @@ public interface MentorPostJPARepostiory extends JpaRepository { - @Query("select m from MentorPost m where m.writer.id = :writer") + @Query("select m from MentorPost m where m.writer.id = :writer and m.state = 'ACTIVE'") List findAllByWriter(@Param("writer") int writer); MentorPost findById(int id); From 312e155e7e65137b8d2872918e593cc20676e9a0 Mon Sep 17 00:00:00 2001 From: Jin_00 Date: Thu, 12 Oct 2023 23:25:30 +0900 Subject: [PATCH 14/22] =?UTF-8?q?fix=20:=20=EC=97=90=EB=84=88=ED=85=8C?= =?UTF-8?q?=EC=9D=B4=EC=85=98=20=EC=86=8D=20=EC=BD=94=EB=93=9C=20=EC=9D=B4?= =?UTF-8?q?=EB=A6=84=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jin_00 --- .../demo/mentoring/contact/NotConnectedRegisterUser.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 94014f15..c1e686f0 100644 --- a/src/main/java/com/example/demo/mentoring/contact/NotConnectedRegisterUser.java +++ b/src/main/java/com/example/demo/mentoring/contact/NotConnectedRegisterUser.java @@ -16,7 +16,7 @@ @NoArgsConstructor(access = AccessLevel.PROTECTED) @Entity @Where(clause = "deleted_at IS NULL") -@SQLDelete(sql = "UPDATE not_connected_register_user SET deleted_at = CURRENT_TIMESTAMP, isDeleted = TRUE where id = ?") +@SQLDelete(sql = "UPDATE not_connected_register_user_tb SET deleted_at = CURRENT_TIMESTAMP, isDeleted = TRUE where id = ?") @Table(name = "notConnectedRegisterUser_tb") public class NotConnectedRegisterUser extends BaseTime { From 4ed374bb4926cabe11eb4fb5867c44ecd4acbc8b Mon Sep 17 00:00:00 2001 From: sjmjys954646 Date: Fri, 13 Oct 2023 09:29:05 +0900 Subject: [PATCH 15/22] =?UTF-8?q?feat=20:=20createPost=20ENUM=20Automatica?= =?UTF-8?q?lly=20post=EC=83=9D=EC=84=B1=EC=8B=9C=20enum=20ACTIVE=20?= =?UTF-8?q?=EC=9E=90=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/example/demo/mentoring/MentorPost.java | 2 +- src/main/java/com/example/demo/mentoring/MentorPostRequest.java | 2 -- src/test/java/com/example/demo/mentoringtest/MentoringTest.java | 1 - 3 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/main/java/com/example/demo/mentoring/MentorPost.java b/src/main/java/com/example/demo/mentoring/MentorPost.java index bc165ace..a9dfabda 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPost.java +++ b/src/main/java/com/example/demo/mentoring/MentorPost.java @@ -35,7 +35,7 @@ public class MentorPost extends BaseTime { @Convert(converter = StateConverter.class) @Column(name = "state", nullable = false) - private StateEnum state; + private StateEnum state = StateEnum.ACTIVE; @Builder public MentorPost(User writer, String title, String content){ diff --git a/src/main/java/com/example/demo/mentoring/MentorPostRequest.java b/src/main/java/com/example/demo/mentoring/MentorPostRequest.java index ec2586f3..bcbfe0ce 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostRequest.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostRequest.java @@ -15,7 +15,5 @@ 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 b1adf452..6fe0a944 100644 --- a/src/test/java/com/example/demo/mentoringtest/MentoringTest.java +++ b/src/test/java/com/example/demo/mentoringtest/MentoringTest.java @@ -91,7 +91,6 @@ 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); From b473434b47341d79f5ca5f7d9bdb76031a2d87c4 Mon Sep 17 00:00:00 2001 From: sjmjys954646 Date: Fri, 13 Oct 2023 10:06:54 +0900 Subject: [PATCH 16/22] =?UTF-8?q?feat=20:=20testcode=20=EC=9E=91=EC=84=B1?= =?UTF-8?q?=20=EB=B0=8F=20=EC=88=98=EC=A0=95=20testcode=20=ED=99=95?= =?UTF-8?q?=EC=9D=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../demo/mentoring/MentorPostResponse.java | 5 +++++ src/main/resources/db/teardown.sql | 12 ++++++------ .../example/demo/mentoringtest/MentoringTest.java | 15 ++++++++------- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/example/demo/mentoring/MentorPostResponse.java b/src/main/java/com/example/demo/mentoring/MentorPostResponse.java index 0343f3e5..56f249b9 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostResponse.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostResponse.java @@ -1,6 +1,7 @@ package com.example.demo.mentoring; +import com.example.demo.config.utils.StateEnum; import com.example.demo.mentoring.contact.NotConnectedRegisterUser; import com.example.demo.user.Role; import com.example.demo.user.User; @@ -25,12 +26,14 @@ public static class MentorPostAllDTO { private int postId; private String title; private String content; + private StateEnum stateEnum; private WriterDTO writerDTO; public MentorPostAllDTO(MentorPost mentorPost, List userInterests) { this.postId = mentorPost.getId(); this.title = mentorPost.getTitle(); this.content = mentorPost.getContent(); + this.stateEnum = mentorPost.getState(); WriterDTO writerDTO = new MentorPostAllDTO.WriterDTO(mentorPost.getWriter(), userInterests); this.writerDTO = writerDTO; } @@ -75,12 +78,14 @@ public static class MentorPostDTO { private String title; private String content; private WriterDTO writerDTO; + private StateEnum stateEnum; private List menteeDTOList; public MentorPostDTO(MentorPost mentorPost, List mentorFavorites, List mentees, List menteeInterest) { this.postId = mentorPost.getId(); this.title = mentorPost.getTitle(); this.content = mentorPost.getContent(); + this.stateEnum = mentorPost.getState(); MentorPostDTO.WriterDTO writerDTO = new MentorPostDTO.WriterDTO(mentorPost.getWriter(), mentorFavorites); this.writerDTO = writerDTO; List menteeDTOList = mentees.stream() diff --git a/src/main/resources/db/teardown.sql b/src/main/resources/db/teardown.sql index 949b96e4..0b430d35 100644 --- a/src/main/resources/db/teardown.sql +++ b/src/main/resources/db/teardown.sql @@ -7,12 +7,12 @@ 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'); +INSERT INTO user_tb (created_at, first_name, last_name, email, password, country, introduction, age, profile_image, role, state) VALUES + (NOW(), 'John', 'Doe', 'john@example.com', '{bcrypt}$2a$10$8H0OT8wgtALJkig6fmypi.Y7jzI5Y7W9PGgRKqnVeS2cLWGifwHF2', 'USA', 'Hello, I am John.', 25, 'profile.jpg', 'MENTOR', 'ACTIVE'), + (NOW(), 'Alice', 'Smith', 'alice.smith@example.com', '{bcrypt}$2a$10$8H0OT8wgtALJkig6fmypi.Y7jzI5Y7W9PGgRKqnVeS2cLWGifwHF2', 'Canada', 'I love painting.', 25, 'image2.jpg', 'MENTOR', 'ACTIVE'), + (NOW(), 'Admin', 'Admin', 'admin@example.com', '{bcrypt}$2a$10$8H0OT8wgtALJkig6fmypi.Y7jzI5Y7W9PGgRKqnVeS2cLWGifwHF2', 'USA', 'I am an admin user.', 35, 'admin.jpg', 'MENTEE', 'ACTIVE'), + (NOW(), 'Jane', 'Smith', 'jane@example.com', '{bcrypt}$2a$10$8H0OT8wgtALJkig6fmypi.Y7jzI5Y7W9PGgRKqnVeS2cLWGifwHF2', 'Canada', 'I love coding.', 30, 'profile.jpg', 'MENTEE', 'ACTIVE'), + (NOW(), 'Admin', 'Adminson', 'admin22222@example.com', '{bcrypt}$2a$10$8H0OT8wgtALJkig6fmypi.Y7jzI5Y7W9PGgRKqnVeS2cLWGifwHF2', 'USA', 'I am the admin.', 35, 'admin.jpg', 'MENTEE', 'ACTIVE'); -- interst Table INSERT INTO interest_tb (created_at, category) VALUES diff --git a/src/test/java/com/example/demo/mentoringtest/MentoringTest.java b/src/test/java/com/example/demo/mentoringtest/MentoringTest.java index 6fe0a944..297fa39a 100644 --- a/src/test/java/com/example/demo/mentoringtest/MentoringTest.java +++ b/src/test/java/com/example/demo/mentoringtest/MentoringTest.java @@ -29,6 +29,8 @@ import org.springframework.test.web.servlet.ResultActions; +import java.util.List; + 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; @@ -60,7 +62,7 @@ void CreateMentorPostTest() { //given User writer = User.builder() - .email("anjdal6664@gmail.com") + .email("john@example.com") .password("asdf1234!") .firstName("Jin") .lastName("Seung") @@ -241,10 +243,10 @@ void updateMentorPostTest() userJPARepository.save(writer); mentorPostService.createMentorPost(mentorPostRequest, writer); - mentorPostService.updateMentorPost(mentorPostUpdated,1); + mentorPostService.updateMentorPost(mentorPostUpdated,2); - MentorPost mentorPostFind = mentorPostJPARepostiory.findById(1); - Assertions.assertThat(1) + MentorPost mentorPostFind = mentorPostJPARepostiory.findById(2); + Assertions.assertThat(2) .isEqualTo(mentorPostFind.getId()); Assertions.assertThat(mentorPostUpdated.getTitle()) .isEqualTo(mentorPostFind.getTitle()); @@ -255,11 +257,11 @@ void updateMentorPostTest() @Test void mentorPostServiceTest() throws Exception { - MentorPostResponse.MentorPostDTO mentorPostFind = mentorPostService.findMentorPost(1); + List mentorPostFind = mentorPostService.findAllMentorPost(0); String responseBody = om.writeValueAsString(mentorPostFind); - System.out.println("test : " + responseBody); + System.out.println("테스트 : " + responseBody); } @WithUserDetails(value = "john@example.com") @@ -287,7 +289,6 @@ public void CreateMentorPostTestMVC() throws Exception { //resultActions.andDo(MockMvcResultHandlers.print()).andDo(document); } - @WithUserDetails(value = "john@example.com") @Test public void GetMentorPostTestMVC() throws Exception { From eeb808dcc5463de7cc715afabcb7da7c39eb86a1 Mon Sep 17 00:00:00 2001 From: sjmjys954646 Date: Fri, 13 Oct 2023 10:32:09 +0900 Subject: [PATCH 17/22] =?UTF-8?q?feat=20:=20mentorPostDelete=20restcontrol?= =?UTF-8?q?ler=20delete=20=EC=9A=94=EC=B2=AD=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 66860fce..b06564cb 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostRestController.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostRestController.java @@ -40,4 +40,10 @@ public ResponseEntity updateMentorPost(@PathVariable int id, @RequestBody @Va mentorPostService.updateMentorPost(requestDTO, id); return ResponseEntity.status(HttpStatus.OK).body(ApiUtils.successWithNoContent()); } + + @DeleteMapping(value = "/mentorings/post/{id}") + public ResponseEntity deleteMentorPost(@PathVariable int id, Errors errors, @AuthenticationPrincipal CustomUserDetails userDetails) { + mentorPostService.deleteMentorPost(id); + return ResponseEntity.status(HttpStatus.OK).body(ApiUtils.successWithNoContent()); + } } From 032d51e24dcee808ae43f97f297a126a1ab47159 Mon Sep 17 00:00:00 2001 From: sjmjys954646 Date: Fri, 13 Oct 2023 13:23:39 +0900 Subject: [PATCH 18/22] =?UTF-8?q?feat=20:=20delete=20Request=20softDelete?= =?UTF-8?q?=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/demo/config/utils/BaseTime.java | 4 ++ .../example/demo/mentoring/MentorPost.java | 4 +- .../demo/mentoring/MentorPostResponse.java | 47 +++++++++++++++++++ .../demo/mentoring/MentorPostService.java | 17 +++++++ .../demo/mentoringtest/MentoringTest.java | 27 ++++++----- 5 files changed, 86 insertions(+), 13 deletions(-) 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 26e578de..ce12ce25 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,7 @@ package com.example.demo.config.utils; +import lombok.Getter; +import lombok.Setter; import org.hibernate.annotations.ColumnDefault; import org.springframework.data.annotation.CreatedDate; import org.springframework.data.annotation.LastModifiedDate; @@ -11,6 +13,8 @@ import java.time.LocalDateTime; import java.time.LocalTime; +@Getter +@Setter @MappedSuperclass @EntityListeners(AuditingEntityListener.class) public abstract class BaseTime { diff --git a/src/main/java/com/example/demo/mentoring/MentorPost.java b/src/main/java/com/example/demo/mentoring/MentorPost.java index a9dfabda..8c7aaa76 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPost.java +++ b/src/main/java/com/example/demo/mentoring/MentorPost.java @@ -18,8 +18,8 @@ @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") +@SQLDelete(sql = "UPDATE mentor_post_tb SET deleted_at = CURRENT_TIMESTAMP, is_deleted = TRUE where id = ?") +@Table(name = "mentor_post_tb") public class MentorPost extends BaseTime { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) diff --git a/src/main/java/com/example/demo/mentoring/MentorPostResponse.java b/src/main/java/com/example/demo/mentoring/MentorPostResponse.java index 56f249b9..84a9d647 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostResponse.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostResponse.java @@ -9,6 +9,7 @@ import lombok.Getter; import lombok.Setter; +import java.time.LocalDateTime; import java.util.List; import java.util.stream.Collectors; @@ -145,4 +146,50 @@ public MenteeDTO(User user, List userInterests) { } } } + + @Getter + @Setter + public static class MentorPostAllWithTimeStampDTO { + private int postId; + private String title; + private String content; + private StateEnum stateEnum; + private WriterDTO writerDTO; + private LocalDateTime createdAt; + private LocalDateTime deletedAt; + private boolean isDeleted; + + public MentorPostAllWithTimeStampDTO(MentorPost mentorPost, List userInterests) { + this.postId = mentorPost.getId(); + this.title = mentorPost.getTitle(); + this.content = mentorPost.getContent(); + this.stateEnum = mentorPost.getState(); + WriterDTO writerDTO = new MentorPostAllWithTimeStampDTO.WriterDTO(mentorPost.getWriter(), userInterests); + this.writerDTO = writerDTO; + this.createdAt = mentorPost.getCreatedAt(); + this.deletedAt = mentorPost.getDeletedAt(); + this.isDeleted = mentorPost.isDeleted(); + } + + @Getter @Setter + public static class WriterDTO { + private int mentorId; + private String profileImage; + private String name; + private String country; + private Role role; + private List interests; + + 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.interests = 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 82593bbb..d9d03f7e 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostService.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostService.java @@ -82,6 +82,23 @@ public void updateMentorPost(MentorPostRequest.CreateDTO createDTO, int id) } } + + public void deleteMentorPost(int id) { + mentorPostJPARepository.deleteById(id); + } + + //생성 시간까지 조회하는 test service 코드 입니다 + public List findAllMentorPostWithTimeStamp() { + List pageContent = mentorPostJPARepository.findAll(); + //List mentorPostList = mentorPostJPARepostiory.findAll(); + List mentorPostDTOList = pageContent.stream().map( + mentorPost -> { + List writerInterests = userInterestJPARepository.findAllById(mentorPost.getWriter().getId()); + return new MentorPostResponse.MentorPostAllWithTimeStampDTO(mentorPost,writerInterests); + } + ).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 297fa39a..32204150 100644 --- a/src/test/java/com/example/demo/mentoringtest/MentoringTest.java +++ b/src/test/java/com/example/demo/mentoringtest/MentoringTest.java @@ -254,16 +254,6 @@ void updateMentorPostTest() .isEqualTo(mentorPostFind.getContent()); } - - @Test - void mentorPostServiceTest() throws Exception { - List mentorPostFind = mentorPostService.findAllMentorPost(0); - - String responseBody = om.writeValueAsString(mentorPostFind); - - System.out.println("테스트 : " + responseBody); - } - @WithUserDetails(value = "john@example.com") @Test public void CreateMentorPostTestMVC() throws Exception { @@ -292,7 +282,7 @@ public void CreateMentorPostTestMVC() throws Exception { @Test public void GetMentorPostTestMVC() throws Exception { - int id = 1; + int id = 2; // when ResultActions resultActions = mvc.perform( @@ -304,4 +294,19 @@ public void GetMentorPostTestMVC() throws Exception { System.out.println("테스트 : "+responseBody); } + @Test + @DisplayName("DeleteTest") + public void DeleteMentorPost() throws Exception{ + int id = 2; + mentorPostService.deleteMentorPost(id); + } + + @Test + void mentorPostServiceTest() throws Exception { + List mentorPostFind = mentorPostService.findAllMentorPostWithTimeStamp(); + + String responseBody = om.writeValueAsString(mentorPostFind); + + System.out.println("전체조회테스트 : " + responseBody); + } } From 3fc08ca9eccebe99821c5ff86b7aff75ae4e93e1 Mon Sep 17 00:00:00 2001 From: sjmjys954646 Date: Fri, 13 Oct 2023 13:37:56 +0900 Subject: [PATCH 19/22] =?UTF-8?q?fix=20:=20tableName=20=ED=85=8C=EC=9D=B4?= =?UTF-8?q?=EB=B8=94=EC=9D=B4=EB=A6=84=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/example/demo/mentoring/MentorPost.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/example/demo/mentoring/MentorPost.java b/src/main/java/com/example/demo/mentoring/MentorPost.java index 8c7aaa76..6e2b54f3 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPost.java +++ b/src/main/java/com/example/demo/mentoring/MentorPost.java @@ -19,7 +19,7 @@ @Entity @Where(clause = "deleted_at IS NULL") @SQLDelete(sql = "UPDATE mentor_post_tb SET deleted_at = CURRENT_TIMESTAMP, is_deleted = TRUE where id = ?") -@Table(name = "mentor_post_tb") +@Table(name = "mentorPost_tb") public class MentorPost extends BaseTime { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) From 6bb1813db42fa37704b474b1dd2bc31c91342869 Mon Sep 17 00:00:00 2001 From: sjmjys954646 Date: Fri, 13 Oct 2023 15:20:13 +0900 Subject: [PATCH 20/22] =?UTF-8?q?feat=20:=20patch=20restcontroller=20state?= =?UTF-8?q?=20patch=20=EC=9A=94=EC=B2=AD=20restcontroller?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/example/demo/mentoring/MentorPostRequest.java | 6 ++++++ .../example/demo/mentoring/MentorPostRestController.java | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/src/main/java/com/example/demo/mentoring/MentorPostRequest.java b/src/main/java/com/example/demo/mentoring/MentorPostRequest.java index bcbfe0ce..a32c24f9 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostRequest.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostRequest.java @@ -16,4 +16,10 @@ public static class CreateDTO { private String content; } + + @Getter + @Setter + public static class StateDTO { + private StateEnum stateEnum; + } } diff --git a/src/main/java/com/example/demo/mentoring/MentorPostRestController.java b/src/main/java/com/example/demo/mentoring/MentorPostRestController.java index b06564cb..2862220c 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostRestController.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostRestController.java @@ -46,4 +46,11 @@ public ResponseEntity deleteMentorPost(@PathVariable int id, Errors errors, @ mentorPostService.deleteMentorPost(id); return ResponseEntity.status(HttpStatus.OK).body(ApiUtils.successWithNoContent()); } + + @PatchMapping(value = "/mentorings/post/{id}/done") + public ResponseEntity changeMentorPostStatus(@PathVariable int id,@RequestBody @Valid MentorPostRequest.StateDTO requestDTO, Errors errors, @AuthenticationPrincipal CustomUserDetails userDetails) { + mentorPostService.changeMentorPostStatus(id); + return ResponseEntity.status(HttpStatus.OK).body(ApiUtils.successWithNoContent()); + } + } From d5eaf15e60232c34481706e3e17da134a2d61792 Mon Sep 17 00:00:00 2001 From: sjmjys954646 Date: Fri, 13 Oct 2023 16:08:09 +0900 Subject: [PATCH 21/22] feat : changeMentorPostStatusService patch Service --- .../com/example/demo/mentoring/MentorPost.java | 4 ++++ .../demo/mentoring/MentorPostRestController.java | 2 +- .../demo/mentoring/MentorPostService.java | 16 ++++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/example/demo/mentoring/MentorPost.java b/src/main/java/com/example/demo/mentoring/MentorPost.java index 6e2b54f3..ab65d1bf 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPost.java +++ b/src/main/java/com/example/demo/mentoring/MentorPost.java @@ -50,4 +50,8 @@ public void update(String title, String content) this.content = content; } + public void changeStatus(StateEnum stateEnum) + { + this.state = stateEnum; + } } diff --git a/src/main/java/com/example/demo/mentoring/MentorPostRestController.java b/src/main/java/com/example/demo/mentoring/MentorPostRestController.java index 2862220c..baa88154 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostRestController.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostRestController.java @@ -49,7 +49,7 @@ public ResponseEntity deleteMentorPost(@PathVariable int id, Errors errors, @ @PatchMapping(value = "/mentorings/post/{id}/done") public ResponseEntity changeMentorPostStatus(@PathVariable int id,@RequestBody @Valid MentorPostRequest.StateDTO requestDTO, Errors errors, @AuthenticationPrincipal CustomUserDetails userDetails) { - mentorPostService.changeMentorPostStatus(id); + mentorPostService.changeMentorPostStatus(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 d9d03f7e..18550d91 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostService.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostService.java @@ -99,6 +99,22 @@ public List findAllMentorPostW ).collect(Collectors.toList()); return mentorPostDTOList; } + + public void changeMentorPostStatus(MentorPostRequest.StateDTO stateDTO, int id) + { + Optional optionalMentorPost = Optional.ofNullable(mentorPostJPARepository.findById(id)); + + if(optionalMentorPost.isPresent()) + { + MentorPost mentorPost = optionalMentorPost.get(); + mentorPost.changeStatus(stateDTO.getStateEnum()); + } + else + { + // 예외처리 + + } + } } From 4648ff9ffd59f793ede4f4915c5d0ffa03753905 Mon Sep 17 00:00:00 2001 From: sjmjys954646 Date: Fri, 13 Oct 2023 17:00:23 +0900 Subject: [PATCH 22/22] =?UTF-8?q?feat=20:=20patch=20testcode=20post=20stat?= =?UTF-8?q?e=EB=B3=80=EA=B2=BD=20=EC=9A=94=EC=B2=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/demo/mentoringtest/MentoringTest.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/test/java/com/example/demo/mentoringtest/MentoringTest.java b/src/test/java/com/example/demo/mentoringtest/MentoringTest.java index 32204150..c55c79ec 100644 --- a/src/test/java/com/example/demo/mentoringtest/MentoringTest.java +++ b/src/test/java/com/example/demo/mentoringtest/MentoringTest.java @@ -31,6 +31,7 @@ import java.util.List; +import static org.junit.jupiter.api.Assertions.assertNull; 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; @@ -299,6 +300,19 @@ public void GetMentorPostTestMVC() throws Exception { public void DeleteMentorPost() throws Exception{ int id = 2; mentorPostService.deleteMentorPost(id); + + MentorPost mentorPostFind = mentorPostJPARepostiory.findById(2); + assertNull(mentorPostFind, "mentorPostNotFound"); + } + + @Test + @DisplayName("DoneTest") + public void PatchDoneMentorPost() throws Exception{ + int id = 1; + + MentorPostRequest.StateDTO stateDTO = new MentorPostRequest.StateDTO(); + stateDTO.setStateEnum(StateEnum.DONE); + mentorPostService.changeMentorPostStatus(stateDTO, id); } @Test