Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mentorPost 수정 #57

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/main/java/com/example/demo/mentoring/MentorPostResponse.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.demo.mentoring;


import com.example.demo.mentoring.contact.ContactStateEnum;
import com.example.demo.mentoring.contact.NotConnectedRegisterUser;
import com.example.demo.user.Role;
import com.example.demo.user.User;
Expand Down Expand Up @@ -94,7 +95,7 @@ public MentorPostDTO(MentorPost mentorPost, List<UserInterest> mentorFavorites,
userInterest -> mentee.getMenteeUser().getId() == userInterest.getUser().getId()
).collect(Collectors.toList());

MentorPostDTO.MenteeDTO menteeDTO = new MentorPostDTO.MenteeDTO(mentee.getMenteeUser(), eachMenteeFavorite);
MentorPostDTO.MenteeDTO menteeDTO = new MentorPostDTO.MenteeDTO(mentee, eachMenteeFavorite);
return menteeDTO;
})
.collect(Collectors.toList());
Expand Down Expand Up @@ -130,15 +131,17 @@ public static class MenteeDTO{
private String country;
private Role role;
private int age;
private ContactStateEnum state;
private List<String> interests;

public MenteeDTO(User user, List<UserInterest> userInterests) {
this.menteeId = user.getId();
this.profileImage = user.getProfileImage();
this.name = user.getFirstName() + " " + user.getLastName();
this.country = user.getCountry();
this.role = user.getRole();
this.age = user.getAge();
public MenteeDTO(NotConnectedRegisterUser notConnectedRegisterUser, List<UserInterest> userInterests) {
this.menteeId = notConnectedRegisterUser.getId();
this.profileImage = notConnectedRegisterUser.getMenteeUser().getProfileImage();
this.name = notConnectedRegisterUser.getMenteeUser().getFirstName() + " " + notConnectedRegisterUser.getMenteeUser().getLastName();
this.country = notConnectedRegisterUser.getMenteeUser().getCountry();
this.role = notConnectedRegisterUser.getMenteeUser().getRole();
this.age = notConnectedRegisterUser.getMenteeUser().getAge();
this.state = notConnectedRegisterUser.getState();
this.interests = userInterests.stream()
.map(userInterest -> userInterest.getInterest().getCategory())
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ public ResponseEntity<?> createMentorPost(@RequestBody @Valid MentorPostRequest.
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) {
@RequestParam(value = "page", defaultValue = "0") Integer page) {

if(category == null)
category = MentorPostCategoryEnum.NULL;
Expand All @@ -38,26 +37,26 @@ public ResponseEntity<?> getMentorPost(
}

@GetMapping("/mentorings/post/{id}")
public ResponseEntity<?> getMentorPostId(@PathVariable int id, @AuthenticationPrincipal CustomUserDetails userDetails) {
public ResponseEntity<?> getMentorPostId(@PathVariable int id) {
MentorPostResponse.MentorPostDTO responseDTO = mentorPostService.findMentorPost(id);
return ResponseEntity.ok(ApiUtils.success(responseDTO));
}

@PutMapping(value = "/mentorings/post/{id}")
public ResponseEntity<?> updateMentorPost(@PathVariable int id, @RequestBody @Valid MentorPostRequest.CreateDTO requestDTO, Errors errors, @AuthenticationPrincipal CustomUserDetails userDetails) {
mentorPostService.updateMentorPost(requestDTO, id);
mentorPostService.updateMentorPost(requestDTO, id, userDetails.getUser());
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);
public ResponseEntity<?> deleteMentorPost(@PathVariable int id, @AuthenticationPrincipal CustomUserDetails userDetails) {
mentorPostService.deleteMentorPost(id, userDetails.getUser());
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);
mentorPostService.changeMentorPostStatus(requestDTO, id, userDetails.getUser());
return ResponseEntity.status(HttpStatus.OK).body(ApiUtils.successWithNoContent());
}

Expand Down
22 changes: 17 additions & 5 deletions src/main/java/com/example/demo/mentoring/MentorPostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ public MentorPostResponse.MentorPostDTO findMentorPost(int id){
return mentorPostDTO;
}
@Transactional
public void updateMentorPost(MentorPostRequest.CreateDTO createDTO, int id)
{
public void updateMentorPost(MentorPostRequest.CreateDTO createDTO, int id, User writer) {
isMentor(writer);

MentorPost mentorPost = mentorPostJPARepository.findById(id).
orElseThrow(() -> new Exception404("해당 글이 존재하지 않습니다."));

Expand All @@ -130,7 +131,10 @@ public void updateMentorPost(MentorPostRequest.CreateDTO createDTO, int id)
}
}

public void deleteMentorPost(int id) {
public void deleteMentorPost(int id, User writer) {

isMentor(writer);

try {
mentorPostJPARepository.deleteById(id);
} catch (Exception e) {
Expand All @@ -155,8 +159,10 @@ public List<MentorPostResponse.MentorPostAllWithTimeStampDTO> findAllMentorPostW
return mentorPostDTOList;
}

public void changeMentorPostStatus(MentorPostRequest.StateDTO stateDTO, int id)
{
public void changeMentorPostStatus(MentorPostRequest.StateDTO stateDTO, int id, User writer) {

isMentor(writer);

MentorPost mentorPost = mentorPostJPARepository.findById(id)
.orElseThrow(() -> new Exception404("해당 글이 존재하지 않습니다."));;

Expand All @@ -166,6 +172,12 @@ public void changeMentorPostStatus(MentorPostRequest.StateDTO stateDTO, int id)
throw new Exception500("unknown server error");
}
}

private void isMentor(User writer) {
if ( writer.getRole() == Role.MENTEE ) {
throw new Exception401("해당 사용자는 멘티입니다.");
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ public void deleteContact(List<Integer> contactId, User mentee) {
}

// 멘토 인증을 위한 메소드
private void isMentor(User user) {
private void isMentor(User mentor) {
// 예외 처리
if ( user.getRole() != Role.MENTOR ) {
if ( mentor.getRole() != Role.MENTOR ) {
throw new Exception401("해당 사용자는 멘토가 아닙니다.");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ void updateMentorPostTest()

userJPARepository.save(writer);
mentorPostService.createMentorPost(mentorPostRequest, writer);
mentorPostService.updateMentorPost(mentorPostUpdated,2);
mentorPostService.updateMentorPost(mentorPostUpdated,2, writer);

MentorPost mentorPostFind = mentorPostJPARepostiory.findById(2)
.orElseThrow(() -> new Exception404("해당 게시글이 존재하지 않습니다."));
Expand Down Expand Up @@ -372,7 +372,7 @@ public void GetMentorPostTestMVC() throws Exception {
@DisplayName("DeleteTest")
public void DeleteMentorPost() throws Exception{
int id = 2;
mentorPostService.deleteMentorPost(id);
mentorPostService.deleteMentorPost(id, User.builder().id(1).build());

MentorPost mentorPostFind = mentorPostJPARepostiory.findById(2)
.orElse(null);
Expand All @@ -385,7 +385,7 @@ public void PatchDoneMentorPost() throws Exception{
int id = 1;
MentorPostRequest.StateDTO stateDTO = new MentorPostRequest.StateDTO();
stateDTO.setMentorPostStateEnum(MentorPostStateEnum.DONE);
mentorPostService.changeMentorPostStatus(stateDTO, id);
mentorPostService.changeMentorPostStatus(stateDTO, id, User.builder().id(1).build());
}

@Test
Expand Down
Loading