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 bc165ace..ab65d1bf 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPost.java +++ b/src/main/java/com/example/demo/mentoring/MentorPost.java @@ -18,7 +18,7 @@ @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 = ?") +@SQLDelete(sql = "UPDATE mentor_post_tb SET deleted_at = CURRENT_TIMESTAMP, is_deleted = TRUE where id = ?") @Table(name = "mentorPost_tb") public class MentorPost extends BaseTime { @Id @@ -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){ @@ -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/MentorPostRequest.java b/src/main/java/com/example/demo/mentoring/MentorPostRequest.java index ec2586f3..a32c24f9 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostRequest.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostRequest.java @@ -15,7 +15,11 @@ public static class CreateDTO { private String title; private String content; + } - private StateEnum state; + @Getter + @Setter + public static class StateDTO { + private StateEnum stateEnum; } } diff --git a/src/main/java/com/example/demo/mentoring/MentorPostResponse.java b/src/main/java/com/example/demo/mentoring/MentorPostResponse.java index 0343f3e5..84a9d647 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; @@ -8,6 +9,7 @@ import lombok.Getter; import lombok.Setter; +import java.time.LocalDateTime; import java.util.List; import java.util.stream.Collectors; @@ -25,12 +27,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 +79,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() @@ -140,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/MentorPostRestController.java b/src/main/java/com/example/demo/mentoring/MentorPostRestController.java index 66860fce..baa88154 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostRestController.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostRestController.java @@ -40,4 +40,17 @@ 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()); + } + + @PatchMapping(value = "/mentorings/post/{id}/done") + public ResponseEntity changeMentorPostStatus(@PathVariable int id,@RequestBody @Valid MentorPostRequest.StateDTO requestDTO, Errors errors, @AuthenticationPrincipal CustomUserDetails userDetails) { + 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 82593bbb..18550d91 100644 --- a/src/main/java/com/example/demo/mentoring/MentorPostService.java +++ b/src/main/java/com/example/demo/mentoring/MentorPostService.java @@ -82,6 +82,39 @@ 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; + } + + 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 + { + // 예외처리 + + } + } } diff --git a/src/test/java/com/example/demo/mentoringtest/MentoringTest.java b/src/test/java/com/example/demo/mentoringtest/MentoringTest.java index b1adf452..c55c79ec 100644 --- a/src/test/java/com/example/demo/mentoringtest/MentoringTest.java +++ b/src/test/java/com/example/demo/mentoringtest/MentoringTest.java @@ -29,6 +29,9 @@ import org.springframework.test.web.servlet.ResultActions; +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; @@ -60,7 +63,7 @@ void CreateMentorPostTest() { //given User writer = User.builder() - .email("anjdal6664@gmail.com") + .email("john@example.com") .password("asdf1234!") .firstName("Jin") .lastName("Seung") @@ -91,7 +94,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); @@ -242,10 +244,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()); @@ -253,16 +255,6 @@ void updateMentorPostTest() .isEqualTo(mentorPostFind.getContent()); } - - @Test - void mentorPostServiceTest() throws Exception { - MentorPostResponse.MentorPostDTO mentorPostFind = mentorPostService.findMentorPost(1); - - String responseBody = om.writeValueAsString(mentorPostFind); - - System.out.println("test : " + responseBody); - } - @WithUserDetails(value = "john@example.com") @Test public void CreateMentorPostTestMVC() throws Exception { @@ -288,11 +280,10 @@ public void CreateMentorPostTestMVC() throws Exception { //resultActions.andDo(MockMvcResultHandlers.print()).andDo(document); } - @WithUserDetails(value = "john@example.com") @Test public void GetMentorPostTestMVC() throws Exception { - int id = 1; + int id = 2; // when ResultActions resultActions = mvc.perform( @@ -304,4 +295,32 @@ public void GetMentorPostTestMVC() throws Exception { System.out.println("테스트 : "+responseBody); } + @Test + @DisplayName("DeleteTest") + 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 + void mentorPostServiceTest() throws Exception { + List mentorPostFind = mentorPostService.findAllMentorPostWithTimeStamp(); + + String responseBody = om.writeValueAsString(mentorPostFind); + + System.out.println("전체조회테스트 : " + responseBody); + } }