diff --git a/src/main/java/com/example/demo/config/utils/StateConverter.java b/src/main/java/com/example/demo/config/utils/StateConverter.java deleted file mode 100644 index e109d5bf..00000000 --- a/src/main/java/com/example/demo/config/utils/StateConverter.java +++ /dev/null @@ -1,27 +0,0 @@ -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 deleted file mode 100644 index af51d0c3..00000000 --- a/src/main/java/com/example/demo/config/utils/StateEnum.java +++ /dev/null @@ -1,32 +0,0 @@ -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 0a53a704..1d543f24 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPost.java +++ b/src/main/java/com/example/demo/mentoring/MentorPost.java @@ -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; @@ -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){ @@ -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; } } diff --git a/src/main/java/com/example/demo/mentoring/MentorPostCategoryEnum.java b/src/main/java/com/example/demo/mentoring/MentorPostCategoryEnum.java new file mode 100644 index 00000000..0e4829eb --- /dev/null +++ b/src/main/java/com/example/demo/mentoring/MentorPostCategoryEnum.java @@ -0,0 +1,5 @@ +package com.example.demo.mentoring; + +public enum MentorPostCategoryEnum { + NULL, TITLE, WRITER, INTEREST; +} diff --git a/src/main/java/com/example/demo/mentoring/MentorPostJPARepostiory.java b/src/main/java/com/example/demo/mentoring/MentorPostJPARepostiory.java index bf6e112b..5f5aad56 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostJPARepostiory.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostJPARepostiory.java @@ -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; @@ -13,6 +15,15 @@ public interface MentorPostJPARepostiory extends JpaRepository findAllByWriter(@Param("writer") int writer); + @Query("select m from MentorPost m where m.title like :keyword") + Page 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 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 findAllByInterestKeyword(@Param("keyword") String keyword, Pageable pageable); + Optional findById(int id); @Query("select count(*) from MentorPost m where m.writer.id = :userId and m.state = 'ACTIVE'") diff --git a/src/main/java/com/example/demo/mentoring/MentorPostRequest.java b/src/main/java/com/example/demo/mentoring/MentorPostRequest.java index a32c24f9..a2fd23c9 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostRequest.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostRequest.java @@ -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; @@ -20,6 +18,6 @@ public static class CreateDTO { @Getter @Setter public static class StateDTO { - private StateEnum stateEnum; + private MentorPostStateEnum mentorPostStateEnum; } } diff --git a/src/main/java/com/example/demo/mentoring/MentorPostResponse.java b/src/main/java/com/example/demo/mentoring/MentorPostResponse.java index 84a9d647..6b72279d 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostResponse.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostResponse.java @@ -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; @@ -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 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; } @@ -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 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(); + this.mentorPostStateEnum = mentorPost.getState(); MentorPostDTO.WriterDTO writerDTO = new MentorPostDTO.WriterDTO(mentorPost.getWriter(), mentorFavorites); this.writerDTO = writerDTO; List menteeDTOList = mentees.stream() @@ -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; @@ -163,7 +162,7 @@ public MentorPostAllWithTimeStampDTO(MentorPost mentorPost, List 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(); diff --git a/src/main/java/com/example/demo/mentoring/MentorPostRestController.java b/src/main/java/com/example/demo/mentoring/MentorPostRestController.java index baa88154..ba3f2b67 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostRestController.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostRestController.java @@ -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 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 responseDTOs = mentorPostService.findAllMentorPost(category, keyword, page); return ResponseEntity.ok(ApiUtils.success(responseDTOs)); } diff --git a/src/main/java/com/example/demo/mentoring/MentorPostService.java b/src/main/java/com/example/demo/mentoring/MentorPostService.java index eabed99c..774f9f6e 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostService.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostService.java @@ -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; @@ -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 @@ -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); @@ -43,10 +50,32 @@ public void createMentorPost(MentorPostRequest.CreateDTO createDTO, User writer) /* 1. mentorPostList를 조회 2. 각 List당 writer별 writerInterests를 조회 3. MentorPostDTO 생성*/ - public List findAllMentorPost(int page) { + public List findAllMentorPost(MentorPostCategoryEnum searchCategory, String keyword, int page) { Pageable pageable = PageRequest.of(page,5); + Page 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 pageContent = mentorPostJPARepository.findAll(pageable); if(pageContent.getTotalPages() == 0){ throw new Exception404("해당 글들이 존재하지 않습니다"); @@ -56,10 +85,6 @@ public List findAllMentorPost(int page) { List mentorPostDTOList = pageContent.getContent().stream().map( mentorPost -> { List writerInterests = userInterestJPARepository.findAllById(mentorPost.getWriter().getId()); - if(writerInterests.isEmpty()){ - throw new Exception404("해당 카테고리는 존재하지 않습니다"); - } - return new MentorPostResponse.MentorPostAllDTO(mentorPost,writerInterests); } ).collect(Collectors.toList()); @@ -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 menteeList = contactJPARepository.findAllByMentorPostId(id); @@ -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) { @@ -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"); + } } } diff --git a/src/main/java/com/example/demo/mentoring/MentorPostStateConverter.java b/src/main/java/com/example/demo/mentoring/MentorPostStateConverter.java new file mode 100644 index 00000000..39c83ff8 --- /dev/null +++ b/src/main/java/com/example/demo/mentoring/MentorPostStateConverter.java @@ -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 { + + @Override + public String convertToDatabaseColumn(MentorPostStateEnum attribute) { + return attribute.getValue(); + } + + @Override + public MentorPostStateEnum convertToEntityAttribute(String dbData) { + return MentorPostStateEnum.findOf(dbData); + } +} diff --git a/src/main/java/com/example/demo/mentoring/MentorPostStateEnum.java b/src/main/java/com/example/demo/mentoring/MentorPostStateEnum.java new file mode 100644 index 00000000..3795e105 --- /dev/null +++ b/src/main/java/com/example/demo/mentoring/MentorPostStateEnum.java @@ -0,0 +1,30 @@ +package com.example.demo.mentoring; + +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 MentorPostStateEnum { + ACTIVE("ACTIVE", "ACTIVE"), + DONE("DONE", "DONE"); + + private final String desc; + private final String value; + + MentorPostStateEnum(String desc, String value) { + this.desc = desc; + this.value = value; + } + + private static final Map descriptions = Collections.unmodifiableMap(Stream.of(values()).collect(Collectors.toMap(MentorPostStateEnum::getValue, Function.identity()))); + + public static MentorPostStateEnum findOf(String findValue) { + return descriptions.get(findValue); + } +} diff --git a/src/test/java/com/example/demo/mentoringtest/MentoringTest.java b/src/test/java/com/example/demo/mentoringtest/MentoringTest.java index 5b4cc8b8..66fb3290 100644 --- a/src/test/java/com/example/demo/mentoringtest/MentoringTest.java +++ b/src/test/java/com/example/demo/mentoringtest/MentoringTest.java @@ -2,9 +2,8 @@ import com.example.demo.RestDoc; -import com.example.demo.config.errors.exception.Exception400; import com.example.demo.config.errors.exception.Exception404; -import com.example.demo.config.utils.StateEnum; +import com.example.demo.mentoring.MentorPostStateEnum; import com.example.demo.interest.Interest; import com.example.demo.interest.InterestJPARepository; import com.example.demo.mentoring.*; @@ -23,11 +22,8 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; -import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.test.context.support.WithUserDetails; import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.jdbc.Sql; import org.springframework.test.web.servlet.ResultActions; @@ -126,6 +122,17 @@ void findMentorPostSaveTest() { .phone("010-0000-0000") .build(); + User mentor2 = User.builder() + .email("anj2312364@gmail.com") + .password("adf1234!") + .firstName("Jin123") + .lastName("Seun123g") + .country("Korea") + .age(23) + .role(Role.MENTOR) + .phone("010-0000-0000") + .build(); + User mentee_One = User.builder() .email("anjda22l6664@gmail.com") .password("asdf221234!") @@ -190,12 +197,59 @@ void findMentorPostSaveTest() { .interest(interest3) .build(); + UserInterest userInterest7 = UserInterest.builder() + .user(mentor2) + .interest(interest1) + .build(); + MentorPost mentorPost3 = MentorPost.builder() .writer(mentor) .title("title") .content("content") .build(); + MentorPost mentorPost4 = MentorPost.builder() + .writer(mentor) + .title("abtitle4") + .content("content") + .build(); + + MentorPost mentorPost5 = MentorPost.builder() + .writer(mentor) + .title("title5") + .content("content") + .build(); + + MentorPost mentorPost6 = MentorPost.builder() + .writer(mentor) + .title("tabitle6") + .content("content") + .build(); + + MentorPost mentorPost7 = MentorPost.builder() + .writer(mentor) + .title("title7") + .content("content") + .build(); + + MentorPost mentorPost8 = MentorPost.builder() + .writer(mentor) + .title("abtitle8") + .content("content") + .build(); + + MentorPost mentorPost9 = MentorPost.builder() + .writer(mentor2) + .title("abtitle8") + .content("content") + .build(); + + MentorPost mentorPost10 = MentorPost.builder() + .writer(mentor2) + .title("abtitle83") + .content("content") + .build(); + NotConnectedRegisterUser menteeNotConnected1 = NotConnectedRegisterUser.builder() .mentorPost(mentorPost3) .menteeUser(mentee_One) @@ -213,15 +267,24 @@ void findMentorPostSaveTest() { interestJPARepository.save(interest2); interestJPARepository.save(interest3); userJPARepository.save(mentor); + userJPARepository.save(mentor2); userJPARepository.save(mentee_One); userJPARepository.save(mentee_Two); mentorPostJPARepostiory.save(mentorPost3); + mentorPostJPARepostiory.save(mentorPost4); + mentorPostJPARepostiory.save(mentorPost5); + mentorPostJPARepostiory.save(mentorPost6); + mentorPostJPARepostiory.save(mentorPost7); + mentorPostJPARepostiory.save(mentorPost8); + mentorPostJPARepostiory.save(mentorPost9); + mentorPostJPARepostiory.save(mentorPost10); userInterestJPARepository.save(userInterest1); userInterestJPARepository.save(userInterest2); userInterestJPARepository.save(userInterest3); userInterestJPARepository.save(userInterest4); userInterestJPARepository.save(userInterest5); userInterestJPARepository.save(userInterest6); + userInterestJPARepository.save(userInterest7); contactJPARepository.save(menteeNotConnected1); contactJPARepository.save(menteeNotConnected2); @@ -320,13 +383,13 @@ public void DeleteMentorPost() throws Exception{ public void PatchDoneMentorPost() throws Exception{ int id = 1; MentorPostRequest.StateDTO stateDTO = new MentorPostRequest.StateDTO(); - stateDTO.setStateEnum(StateEnum.DONE); + stateDTO.setMentorPostStateEnum(MentorPostStateEnum.DONE); mentorPostService.changeMentorPostStatus(stateDTO, id); } @Test void mentorPostServiceTest() throws Exception { - List mentorPostFind = mentorPostService.findAllMentorPostWithTimeStamp(); + List mentorPostFind = mentorPostService.findAllMentorPost(MentorPostCategoryEnum.NULL,"",2); String responseBody = om.writeValueAsString(mentorPostFind);