diff --git a/src/main/java/com/example/demo/mentoring/MentorPostJPARepostiory.java b/src/main/java/com/example/demo/mentoring/MentorPostJPARepostiory.java index 766bcc6e..8e7579ff 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; @@ -10,8 +9,17 @@ 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); + + @Query("select count(*) from MentorPost m where m.writer.id = :userId and m.state = 'ACTIVE'") + int countContactByMentorId(int userId); + + @Query("select count(*) from MentorPost m where m.writer.id = :userId and m.state = 'DONE'") + int countDoneByMentorId(int userId); + + @Query("SELECT m FROM MentorPost m INNER JOIN NotConnectedRegisterUser ncru ON m.id = ncru.mentorPost.id WHERE ncru.menteeUser.id = :menteeUserId") + List findAllByMenteeUserId(@Param("menteeUserId") int menteeUserId); } 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..74923a0c 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,30 @@ 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; + } + } + + @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 담는 순서 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..4470be70 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,28 @@ public class ContactRestController { @GetMapping(value = "/contacts") @Operation(summary = "", description = "") public ResponseEntity findAll(@AuthenticationPrincipal CustomUserDetails userDetails) { - List responseDTO = contactService.findAll(userDetails.getUser()); + if ( userDetails.getUser().getRole() == Role.MENTEE ) { + List responseDTO = contactService.findAllByMentee(userDetails.getUser().getId()); + return ResponseEntity.ok(ApiUtils.success(responseDTO)); + } + List responseDTO = contactService.findAllByMentor(userDetails.getUser().getId()); + return ResponseEntity.ok(ApiUtils.success(responseDTO)); + } + + + @GetMapping(value = "/contacts/postCounts") + @Operation(summary = "", description = "") + public ResponseEntity postCounts(@AuthenticationPrincipal CustomUserDetails userDetails) { + // TO-DO : contact, done 옆 숫자를 띄우는 API 로직 만들기 ( 멘토, 멘티 나눠서 ) + + // 멘토, 멘티 구분 + Role role = userDetails.getUser().getRole(); + + 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..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,7 +2,7 @@ import com.example.demo.mentoring.MentorPost; import com.example.demo.mentoring.MentorPostJPARepostiory; -import com.example.demo.user.Role; +import com.example.demo.mentoring.done.DoneJPARepository; import com.example.demo.user.User; import com.example.demo.user.UserJPARepository; import com.example.demo.user.userInterest.UserInterest; @@ -11,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; @@ -20,69 +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(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 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()); + + ContactResponse.MentorDTO mentorDTO = new ContactResponse.MentorDTO(mentorUser, mentorInterests); + + 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 생성 로직 ) @@ -94,4 +81,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 = mentorPostJPARepository.countContactByMentorId(userId); + // done 화면에서 게시글을 조회 + int doneCount = mentorPostJPARepository.countDoneByMentorId(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..c1e686f0 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_tb SET deleted_at = CURRENT_TIMESTAMP, isDeleted = TRUE where id = ?") @Table(name = "notConnectedRegisterUser_tb") public class NotConnectedRegisterUser extends BaseTime { @@ -20,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) @@ -35,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; 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..010926e5 100644 --- a/src/main/java/com/example/demo/mentoring/done/ConnectedUser.java +++ b/src/main/java/com/example/demo/mentoring/done/ConnectedUser.java @@ -1,33 +1,38 @@ 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; 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) private int id; - @ManyToOne - private User mentorUser; + @ManyToOne(fetch = FetchType.LAZY) + private MentorPost mentorPost; - @ManyToOne + @ManyToOne(fetch = FetchType.LAZY) 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 new file mode 100644 index 00000000..ba5c2626 --- /dev/null +++ b/src/main/java/com/example/demo/mentoring/done/DoneJPARepository.java @@ -0,0 +1,11 @@ +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 { + +} diff --git a/src/main/resources/db/teardown.sql b/src/main/resources/db/teardown.sql index 949b96e4..76d0c4a3 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 @@ -22,28 +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_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 @@ -56,5 +64,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); } }