Skip to content

Commit

Permalink
Merge pull request #26 from choboss00/feature/3-branch-mentoring
Browse files Browse the repository at this point in the history
멘토, 멘티 contact 화면 분리 및 기능 구현, postCount API 추가
  • Loading branch information
sjmjys954646 authored Oct 13, 2023
2 parents 147133a + 312e155 commit c1fc3e1
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 194 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@

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;

import java.util.List;

public interface MentorPostJPARepostiory extends JpaRepository<MentorPost, Integer> {

@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<MentorPost> 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<MentorPost> findAllByMenteeUserId(@Param("menteeUserId") int menteeUserId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 담는 순서
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,7 +21,28 @@ public class ContactRestController {
@GetMapping(value = "/contacts")
@Operation(summary = "", description = "")
public ResponseEntity<?> findAll(@AuthenticationPrincipal CustomUserDetails userDetails) {
List<ContactResponse.MentorPostDTO> responseDTO = contactService.findAll(userDetails.getUser());
if ( userDetails.getUser().getRole() == Role.MENTEE ) {
List<ContactResponse.MenteeContactDTO> responseDTO = contactService.findAllByMentee(userDetails.getUser().getId());
return ResponseEntity.ok(ApiUtils.success(responseDTO));
}
List<ContactResponse.MentorPostDTO> 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));
}

Expand Down
112 changes: 54 additions & 58 deletions src/main/java/com/example/demo/mentoring/contact/ContactService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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<ContactResponse.MentorPostDTO> 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<MenteeDTO> 를 만듬 )
* - 싹다 묶어서 reponseDTOs 로 전달
* **/
// Return 할 객체
List<ContactResponse.MentorPostDTO> responseDTOs = new ArrayList<>();

// 멘토의 id 에 해당하는 mentorPost 다 가져오기
List<MentorPost> mentorPosts = mentorPostJPARepostiory.findAllByWriter(id);

// 멘토 정보 가져오기 ( 나중에 User 로 바꿔야 함 )
User mentorUser = userJPARepository.findById(id);
// List<UserInterest> 가져오기 ( 멘토꺼 tag 목록 )
List<UserInterest> mentorInterests = userInterestJPARepository.findAllById(mentorUser.getId());

// MentorDTO 담기
ContactResponse.MentorDTO mentorDTO = new ContactResponse.MentorDTO(mentorUser, mentorInterests);

// 만약 3개의 글을 썼을 경우, 현재 mentorPosts 에는 3개의 글 목록이 존재함
for ( MentorPost mentorPost : mentorPosts ) {
// List<MenteeDTO> 만들기
// 신청한 멘티의 목록 ( postId 로 조회 )
List<NotConnectedRegisterUser> mentees = contactJPARepository.findAllByMentorPostId(mentorPost.getId());

// List<MenteeDTO> 생성
List<ContactResponse.MenteeDTO> menteeDTOs = mentees
.stream()
.map(this::createMenteeDTO)
.collect(Collectors.toList());
// responseDTO 에 담기
responseDTOs.add(new ContactResponse.MentorPostDTO(mentorPost, mentorDTO, menteeDTOs));

}
return responseDTOs;
}
public List<ContactResponse.MenteeContactDTO> 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<UserInterest> 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<ContactResponse.MentorPostDTO> findAllByMentor(int userId) {

User mentorUser = userJPARepository.findById(userId);
List<UserInterest> 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<ContactResponse.MenteeDTO> menteeDTOs = contactJPARepository.findAllByMentorPostId(mentorPost.getId())
.stream()
.map(this::createMenteeDTO)
.collect(Collectors.toList());

return new ContactResponse.MentorPostDTO(mentorPost, mentorDTO, menteeDTOs);
}

// 매핑 로직 분리 ( menteeDTO 생성 로직 )
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,24 @@
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 {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@OneToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.LAZY)
private MentorPost mentorPost;

@ManyToOne(fetch = FetchType.LAZY)
Expand All @@ -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;
Expand Down
19 changes: 12 additions & 7 deletions src/main/java/com/example/demo/mentoring/done/ConnectedUser.java
Original file line number Diff line number Diff line change
@@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<ConnectedUser, Integer> {

}
Loading

0 comments on commit c1fc3e1

Please sign in to comment.