Skip to content

Commit

Permalink
Merge pull request #49 from sjmjys954646/feature/3-branch-mentoring
Browse files Browse the repository at this point in the history
멘토링 글 필터링 추가
  • Loading branch information
choboss00 authored Oct 28, 2023
2 parents de4b282 + 35d01fd commit cf6799f
Show file tree
Hide file tree
Showing 12 changed files with 206 additions and 96 deletions.
27 changes: 0 additions & 27 deletions src/main/java/com/example/demo/config/utils/StateConverter.java

This file was deleted.

32 changes: 0 additions & 32 deletions src/main/java/com/example/demo/config/utils/StateEnum.java

This file was deleted.

11 changes: 4 additions & 7 deletions src/main/java/com/example/demo/mentoring/MentorPost.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
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;

Expand All @@ -34,9 +31,9 @@ public class MentorPost extends BaseTime {
@Column(length = 300)
private String content;

@Convert(converter = StateConverter.class)
@Convert(converter = MentorPostStateConverter.class)
@Column(name = "state", nullable = false)
private StateEnum state = StateEnum.ACTIVE;
private MentorPostStateEnum state = MentorPostStateEnum.ACTIVE;

@Builder
public MentorPost(User writer, String title, String content){
Expand All @@ -51,8 +48,8 @@ public void update(String title, String content)
this.content = content;
}

public void changeStatus(StateEnum stateEnum)
public void changeStatus(MentorPostStateEnum mentorPostStateEnum)
{
this.state = stateEnum;
this.state = mentorPostStateEnum;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.example.demo.mentoring;

public enum MentorPostCategoryEnum {
NULL, TITLE, WRITER, INTEREST;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.example.demo.mentoring;

import org.springframework.data.domain.Page;
import org.springframework.data.jpa.repository.JpaRepository;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.Optional;
Expand All @@ -13,6 +15,15 @@ public interface MentorPostJPARepostiory extends JpaRepository<MentorPost, Integ
@Query("select m from MentorPost m where m.writer.id = :writer and m.state = 'ACTIVE'")
List<MentorPost> findAllByWriter(@Param("writer") int writer);

@Query("select m from MentorPost m where m.title like :keyword")
Page<MentorPost> findAllByTitleKeyword(@Param("keyword") String keyword, Pageable pageable);

@Query("select m from MentorPost m where m.writer.firstName like :keyword or m.writer.firstName like :keyword")
Page<MentorPost> findAllByWriterKeyword(@Param("keyword") String keyword, Pageable pageable);

@Query("select m from MentorPost m inner join UserInterest ui ON m.writer.id = ui.user.id where ui.interest.category like :keyword")
Page<MentorPost> findAllByInterestKeyword(@Param("keyword") String keyword, Pageable pageable);

Optional<MentorPost> findById(int id);

@Query("select count(*) from MentorPost m where m.writer.id = :userId and m.state = 'ACTIVE'")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.example.demo.mentoring;

import com.example.demo.config.utils.StateEnum;
import com.example.demo.user.User;
import lombok.Getter;
import lombok.Setter;
import javax.validation.constraints.NotNull;
Expand All @@ -20,6 +18,6 @@ public static class CreateDTO {
@Getter
@Setter
public static class StateDTO {
private StateEnum stateEnum;
private MentorPostStateEnum mentorPostStateEnum;
}
}
13 changes: 6 additions & 7 deletions src/main/java/com/example/demo/mentoring/MentorPostResponse.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
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;
Expand All @@ -27,14 +26,14 @@ public static class MentorPostAllDTO {
private int postId;
private String title;
private String content;
private StateEnum stateEnum;
private MentorPostStateEnum mentorPostStateEnum;
private WriterDTO writerDTO;

public MentorPostAllDTO(MentorPost mentorPost, List<UserInterest> userInterests) {
this.postId = mentorPost.getId();
this.title = mentorPost.getTitle();
this.content = mentorPost.getContent();
this.stateEnum = mentorPost.getState();
this.mentorPostStateEnum = mentorPost.getState();
WriterDTO writerDTO = new MentorPostAllDTO.WriterDTO(mentorPost.getWriter(), userInterests);
this.writerDTO = writerDTO;
}
Expand Down Expand Up @@ -79,14 +78,14 @@ public static class MentorPostDTO {
private String title;
private String content;
private WriterDTO writerDTO;
private StateEnum stateEnum;
private MentorPostStateEnum mentorPostStateEnum;
private List<MenteeDTO> menteeDTOList;

public MentorPostDTO(MentorPost mentorPost, List<UserInterest> mentorFavorites, List<NotConnectedRegisterUser> mentees, List<UserInterest> menteeInterest) {
this.postId = mentorPost.getId();
this.title = mentorPost.getTitle();
this.content = mentorPost.getContent();
this.stateEnum = mentorPost.getState();
this.mentorPostStateEnum = mentorPost.getState();
MentorPostDTO.WriterDTO writerDTO = new MentorPostDTO.WriterDTO(mentorPost.getWriter(), mentorFavorites);
this.writerDTO = writerDTO;
List<MentorPostDTO.MenteeDTO> menteeDTOList = mentees.stream()
Expand Down Expand Up @@ -153,7 +152,7 @@ public static class MentorPostAllWithTimeStampDTO {
private int postId;
private String title;
private String content;
private StateEnum stateEnum;
private MentorPostStateEnum mentorPostStateEnum;
private WriterDTO writerDTO;
private LocalDateTime createdAt;
private LocalDateTime deletedAt;
Expand All @@ -163,7 +162,7 @@ public MentorPostAllWithTimeStampDTO(MentorPost mentorPost, List<UserInterest> u
this.postId = mentorPost.getId();
this.title = mentorPost.getTitle();
this.content = mentorPost.getContent();
this.stateEnum = mentorPost.getState();
this.mentorPostStateEnum = mentorPost.getState();
WriterDTO writerDTO = new MentorPostAllWithTimeStampDTO.WriterDTO(mentorPost.getWriter(), userInterests);
this.writerDTO = writerDTO;
this.createdAt = mentorPost.getCreatedAt();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,16 @@ public ResponseEntity<?> createMentorPost(@RequestBody @Valid MentorPostRequest.
}

@GetMapping("/mentorings/post")
public ResponseEntity<?> getMentorPost(@RequestParam(value = "page", defaultValue = "0") Integer page, @AuthenticationPrincipal CustomUserDetails userDetails) {
List<MentorPostResponse.MentorPostAllDTO> responseDTOs = mentorPostService.findAllMentorPost(page);
public ResponseEntity<?> getMentorPost(
@RequestParam(value = "category", required = false) MentorPostCategoryEnum category,
@RequestParam(value = "search", defaultValue = "") String keyword,
@RequestParam(value = "page", defaultValue = "0") Integer page,
@AuthenticationPrincipal CustomUserDetails userDetails) {

if(category == null)
category = MentorPostCategoryEnum.NULL;

List<MentorPostResponse.MentorPostAllDTO> responseDTOs = mentorPostService.findAllMentorPost(category, keyword, page);
return ResponseEntity.ok(ApiUtils.success(responseDTOs));
}

Expand Down
58 changes: 47 additions & 11 deletions src/main/java/com/example/demo/mentoring/MentorPostService.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.example.demo.mentoring;

import com.example.demo.config.errors.exception.Exception401;
import com.example.demo.config.errors.exception.Exception500;
import com.example.demo.config.errors.exception.Exception400;
import com.example.demo.config.errors.exception.Exception404;
import com.example.demo.mentoring.contact.ContactJPARepository;
import com.example.demo.mentoring.contact.NotConnectedRegisterUser;
import com.example.demo.user.Role;
import com.example.demo.user.User;
import com.example.demo.user.userInterest.UserInterest;
import com.example.demo.user.userInterest.UserInterestJPARepository;
Expand All @@ -14,11 +15,8 @@
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.example.demo.config.errors.exception.Exception400;
import com.example.demo.config.errors.exception.Exception404;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Transactional
Expand All @@ -32,6 +30,15 @@ public class MentorPostService {
//mentorPost생성
@Transactional
public void createMentorPost(MentorPostRequest.CreateDTO createDTO, User writer) {
if ( writer.getRole() == Role.MENTEE ) {
throw new Exception401("해당 사용자는 멘티입니다.");
}

//글자수 확인
if(createDTO.getContent().length() > 300){
throw new Exception404("글자수가 300자를 넘어갑니다.");
}

MentorPost mentorPost = new MentorPost( writer, createDTO.getTitle(), createDTO.getContent());
try {
mentorPostJPARepository.save(mentorPost);
Expand All @@ -43,10 +50,32 @@ public void createMentorPost(MentorPostRequest.CreateDTO createDTO, User writer)
/* 1. mentorPostList를 조회
2. 각 List당 writer별 writerInterests를 조회
3. MentorPostDTO 생성*/
public List<MentorPostResponse.MentorPostAllDTO> findAllMentorPost(int page) {
public List<MentorPostResponse.MentorPostAllDTO> findAllMentorPost(MentorPostCategoryEnum searchCategory, String keyword, int page) {
Pageable pageable = PageRequest.of(page,5);
Page<MentorPost> pageContent = null;

//검색별 pageContent 검색
if(searchCategory == MentorPostCategoryEnum.NULL)
{
pageContent = mentorPostJPARepository.findAll(pageable);
}
else if(searchCategory == MentorPostCategoryEnum.TITLE)
{
pageContent = mentorPostJPARepository.findAllByTitleKeyword("%" + keyword + "%", pageable);
}
else if(searchCategory == MentorPostCategoryEnum.WRITER)
{
pageContent = mentorPostJPARepository.findAllByWriterKeyword("%" + keyword + "%", pageable);
}
else if(searchCategory == MentorPostCategoryEnum.INTEREST)
{
pageContent = mentorPostJPARepository.findAllByInterestKeyword("%" + keyword + "%", pageable);
}
else
{
throw new Exception404("검색 분류가 잘못되었습니다.");
}

Page<MentorPost> pageContent = mentorPostJPARepository.findAll(pageable);

if(pageContent.getTotalPages() == 0){
throw new Exception404("해당 글들이 존재하지 않습니다");
Expand All @@ -56,10 +85,6 @@ public List<MentorPostResponse.MentorPostAllDTO> findAllMentorPost(int page) {
List<MentorPostResponse.MentorPostAllDTO> mentorPostDTOList = pageContent.getContent().stream().map(
mentorPost -> {
List<UserInterest> writerInterests = userInterestJPARepository.findAllById(mentorPost.getWriter().getId());
if(writerInterests.isEmpty()){
throw new Exception404("해당 카테고리는 존재하지 않습니다");
}

return new MentorPostResponse.MentorPostAllDTO(mentorPost,writerInterests);
}
).collect(Collectors.toList());
Expand All @@ -71,6 +96,7 @@ public MentorPostResponse.MentorPostDTO findMentorPost(int id){
.orElseThrow(() -> new Exception404("해당 글이 존재하지 않습니다.\n" + "id : " + id));

//writer 데이터
//writer를 제외하고는 다 null 가능
User mentor = mentorPost.getWriter();
//mentee들 데이터
List<NotConnectedRegisterUser> menteeList = contactJPARepository.findAllByMentorPostId(id);
Expand All @@ -91,6 +117,11 @@ public void updateMentorPost(MentorPostRequest.CreateDTO createDTO, int id)
MentorPost mentorPost = mentorPostJPARepository.findById(id).
orElseThrow(() -> new Exception404("해당 글이 존재하지 않습니다."));

//글자수 확인
if(createDTO.getContent().length() > 300){
throw new Exception404("글자수가 300자를 넘어갑니다.");
}

try {
mentorPost.update(createDTO.getTitle(), createDTO.getContent());
} catch (Exception e) {
Expand Down Expand Up @@ -127,7 +158,12 @@ public void changeMentorPostStatus(MentorPostRequest.StateDTO stateDTO, int id)
{
MentorPost mentorPost = mentorPostJPARepository.findById(id)
.orElseThrow(() -> new Exception404("해당 글이 존재하지 않습니다."));;
mentorPost.changeStatus(stateDTO.getStateEnum());

try {
mentorPost.changeStatus(stateDTO.getMentorPostStateEnum());
} catch (Exception e) {
throw new Exception500("unknown server error");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.demo.mentoring;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import javax.persistence.AttributeConverter;
import java.util.Optional;

@RequiredArgsConstructor
@Component
public class MentorPostStateConverter implements AttributeConverter<MentorPostStateEnum, String> {

@Override
public String convertToDatabaseColumn(MentorPostStateEnum attribute) {
return attribute.getValue();
}

@Override
public MentorPostStateEnum convertToEntityAttribute(String dbData) {
return MentorPostStateEnum.findOf(dbData);
}
}
Loading

0 comments on commit cf6799f

Please sign in to comment.