Skip to content

Commit

Permalink
Merge pull request #27 from sjmjys954646/feature/3-branch-mentoring
Browse files Browse the repository at this point in the history
Feature/3 branch mentoring
  • Loading branch information
choboss00 authored Oct 14, 2023
2 parents c1fc3e1 + 2fa2610 commit 936a019
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 20 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/example/demo/config/utils/BaseTime.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,6 +13,8 @@
import java.time.LocalDateTime;
import java.time.LocalTime;

@Getter
@Setter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseTime {
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/com/example/demo/mentoring/MentorPost.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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){
Expand All @@ -50,4 +50,8 @@ public void update(String title, String content)
this.content = content;
}

public void changeStatus(StateEnum stateEnum)
{
this.state = stateEnum;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
52 changes: 52 additions & 0 deletions src/main/java/com/example/demo/mentoring/MentorPostResponse.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
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;
import com.example.demo.user.userInterest.UserInterest;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -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<UserInterest> 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;
}
Expand Down Expand Up @@ -75,12 +79,14 @@ public static class MentorPostDTO {
private String title;
private String content;
private WriterDTO writerDTO;
private StateEnum stateEnum;
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();
MentorPostDTO.WriterDTO writerDTO = new MentorPostDTO.WriterDTO(mentorPost.getWriter(), mentorFavorites);
this.writerDTO = writerDTO;
List<MentorPostDTO.MenteeDTO> menteeDTOList = mentees.stream()
Expand Down Expand Up @@ -140,4 +146,50 @@ public MenteeDTO(User user, List<UserInterest> 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<UserInterest> 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<String> interests;

public WriterDTO(User user, List<UserInterest> 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());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

}
33 changes: 33 additions & 0 deletions src/main/java/com/example/demo/mentoring/MentorPostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,39 @@ public void updateMentorPost(MentorPostRequest.CreateDTO createDTO, int id)

}
}

public void deleteMentorPost(int id) {
mentorPostJPARepository.deleteById(id);
}

//생성 시간까지 조회하는 test service 코드 입니다
public List<MentorPostResponse.MentorPostAllWithTimeStampDTO> findAllMentorPostWithTimeStamp() {
List<MentorPost> pageContent = mentorPostJPARepository.findAll();
//List<MentorPost> mentorPostList = mentorPostJPARepostiory.findAll();
List<MentorPostResponse.MentorPostAllWithTimeStampDTO> mentorPostDTOList = pageContent.stream().map(
mentorPost -> {
List<UserInterest> 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<MentorPost> optionalMentorPost = Optional.ofNullable(mentorPostJPARepository.findById(id));

if(optionalMentorPost.isPresent())
{
MentorPost mentorPost = optionalMentorPost.get();
mentorPost.changeStatus(stateDTO.getStateEnum());
}
else
{
// 예외처리

}
}
}


53 changes: 36 additions & 17 deletions src/test/java/com/example/demo/mentoringtest/MentoringTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -242,27 +244,17 @@ 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());
Assertions.assertThat(mentorPostUpdated.getContent())
.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 = "[email protected]")
@Test
public void CreateMentorPostTestMVC() throws Exception {
Expand All @@ -288,11 +280,10 @@ public void CreateMentorPostTestMVC() throws Exception {
//resultActions.andDo(MockMvcResultHandlers.print()).andDo(document);
}

@WithUserDetails(value = "[email protected]")
@Test
public void GetMentorPostTestMVC() throws Exception {

int id = 1;
int id = 2;

// when
ResultActions resultActions = mvc.perform(
Expand All @@ -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<MentorPostResponse.MentorPostAllWithTimeStampDTO> mentorPostFind = mentorPostService.findAllMentorPostWithTimeStamp();

String responseBody = om.writeValueAsString(mentorPostFind);

System.out.println("전체조회테스트 : " + responseBody);
}
}

0 comments on commit 936a019

Please sign in to comment.