Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import NextLevel.demo.common.SuccessResponse;
import NextLevel.demo.project.community.dto.request.SaveCommunityDto;
import NextLevel.demo.project.community.service.ProjectCommunityAnswerService;
import NextLevel.demo.project.community.dto.response.ResponseCommunityListDto;
import NextLevel.demo.project.community.service.ProjectCommunityAskService;
import NextLevel.demo.util.jwt.JWTUtil;
Expand All @@ -18,35 +19,65 @@
@RequiredArgsConstructor
public class ProjectCommunityController {

private final ProjectCommunityAskService communityService;
private final ProjectCommunityAskService askService;
private final ProjectCommunityAnswerService answerService;

// ask 관련

// list
@GetMapping("/public/project/{projectId}/community")
public ResponseEntity getProjectCommunity(@PathVariable Long projectId) {
return ResponseEntity.ok().body(new SuccessResponse("success", new ResponseCommunityListDto(communityService.selectAll(projectId))));
return ResponseEntity.ok().body(new SuccessResponse("success", new ResponseCommunityListDto(askService.selectAll(projectId))));
}

// 생성만
// 생성
@PostMapping("/api1/project/{projectId}/community")
public ResponseEntity<?> saveProjectCommunity(@PathVariable("projectId") Long projectId, @RequestBody @Valid SaveCommunityDto dto) {
public ResponseEntity<?> saveProjectCommunityAsk(@PathVariable("projectId") Long projectId, @RequestBody @Valid SaveCommunityDto dto) {
dto.setUserId(JWTUtil.getUserIdFromSecurityContext());
dto.setId(projectId);
communityService.create(dto);
askService.create(dto);
return ResponseEntity.status(HttpStatus.OK).body(new SuccessResponse("success", null));
}

// 수정
@PostMapping("/api1/project/community/{communityId}")
public ResponseEntity<?> updateProjectCommunity(@PathVariable("communityId") Long communityId, @RequestBody @Valid SaveCommunityDto dto) {
public ResponseEntity<?> updateProjectCommunityAsk(@PathVariable("communityId") Long communityId, @RequestBody @Valid SaveCommunityDto dto) {
dto.setUserId(JWTUtil.getUserIdFromSecurityContext());
dto.setId(communityId);
communityService.update(dto);
askService.update(dto);
return ResponseEntity.status(HttpStatus.OK).body(new SuccessResponse("success", null));
}

@DeleteMapping("/api1/project/community/{communityId}")
public ResponseEntity<?> deleteProjectCommunity(@PathVariable("communityId") Long communityId) {
communityService.delete(communityId,JWTUtil.getUserIdFromSecurityContext());
askService.delete(communityId,JWTUtil.getUserIdFromSecurityContext());
return ResponseEntity.status(HttpStatus.OK).body(new SuccessResponse("success", null));
}

// answer 관련

// 생성
@PostMapping("/api1/project/{askId}/community/answer")
public ResponseEntity<?> saveProjectCommunityAnswer(@PathVariable("askId") Long askId, @RequestBody @Valid SaveCommunityDto dto) {
dto.setUserId(JWTUtil.getUserIdFromSecurityContext());
dto.setId(askId);
answerService.addAnswer(dto);
return ResponseEntity.status(HttpStatus.OK).body(new SuccessResponse("success", null));
}

// 수정
@PostMapping("/api1/project/community/{answerId}/answer")
public ResponseEntity<?> updateProjectCommunityAnswer(@PathVariable("answerId") Long answerId, @RequestBody @Valid SaveCommunityDto dto) {
dto.setUserId(JWTUtil.getUserIdFromSecurityContext());
dto.setId(answerId);
answerService.updateAnswer(dto);
return ResponseEntity.status(HttpStatus.OK).body(new SuccessResponse("success", null));
}

@DeleteMapping("/api1/project/community/{answerId}/answer")
public ResponseEntity<?> deleteProjectCommunityAnswer(@PathVariable("answerId") Long answerId) {
answerService.deleteAnswer(answerId,JWTUtil.getUserIdFromSecurityContext());
return ResponseEntity.status(HttpStatus.OK).body(new SuccessResponse("success", null));
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package NextLevel.demo.project.community.entity;

import NextLevel.demo.BasedEntity;
import NextLevel.demo.project.community.dto.request.SaveCommunityDto;
import NextLevel.demo.user.entity.UserEntity;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -31,4 +32,9 @@ public class ProjectCommunityAnswerEntity extends BasedEntity {
@JoinColumn(name = "ask_id", nullable = false)
private ProjectCommunityAskEntity ask;

public void update(SaveCommunityDto dto) {
if(dto.getContent() != null && !dto.getContent().isEmpty())
this.content = dto.getContent();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ public class ProjectCommunityAskEntity extends BasedEntity {
@JoinColumn(name = "user_id", nullable = false)
private UserEntity user;

@OneToOne(mappedBy = "ask")
@OneToOne(mappedBy = "ask", cascade = {CascadeType.REMOVE})
private ProjectCommunityAnswerEntity answer;

public void update(SaveCommunityDto dto) {
if(dto.getContent()!=null && !dto.getContent().isEmpty())
this.content = dto.getContent();
}

public void setNoAnswer() { answer = null; }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package NextLevel.demo.project.community.service;

import NextLevel.demo.exception.CustomException;
import NextLevel.demo.exception.ErrorCode;
import NextLevel.demo.project.community.dto.request.SaveCommunityDto;
import NextLevel.demo.project.community.entity.ProjectCommunityAnswerEntity;
import NextLevel.demo.project.community.entity.ProjectCommunityAskEntity;
import NextLevel.demo.project.community.repository.ProjectCommunityAnswerRepository;
import NextLevel.demo.project.community.repository.ProjectCommunityAskRepository;
import NextLevel.demo.project.project.service.ProjectValidateService;
import NextLevel.demo.user.entity.UserEntity;
import NextLevel.demo.user.service.UserValidateService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class ProjectCommunityAnswerService {

private final ProjectValidateService projectValidateService;
private final UserValidateService userValidateService;

private final ProjectCommunityAnswerRepository projectCommunityAnswerRepository;
private final ProjectCommunityAskRepository projectCommunityAskRepository;

@Transactional
public void addAnswer(SaveCommunityDto dto) {
Long askId = dto.getId();
ProjectCommunityAskEntity ask = projectCommunityAskRepository.findById(askId).orElseThrow(
()->{return new CustomException(ErrorCode.NOT_FOUND, "community");}
);
UserEntity user = userValidateService.getUserInfo(dto.getUserId());

projectValidateService.validateAuthor(ask.getProject(), user);

if(ask.getAnswer() != null) {
dto.setId(ask.getAnswer().getId());
updateAnswer(dto);
return;
}

projectCommunityAnswerRepository.save(dto.toAnswerEntity(user, ask));
}

@Transactional
public void updateAnswer(SaveCommunityDto dto) {
Long answerId = dto.getId();
ProjectCommunityAnswerEntity answer = projectCommunityAnswerRepository.findById(answerId).orElseThrow(
()->{return new CustomException(ErrorCode.NOT_FOUND, "community");}
);
UserEntity user = userValidateService.getUserInfo(dto.getUserId());
projectValidateService.validateAuthor(answer.getAsk().getProject(), user);
answer.update(dto);
}

@Transactional
public void deleteAnswer(Long answerId, Long userId) {
ProjectCommunityAnswerEntity answer = projectCommunityAnswerRepository.findById(answerId).orElseThrow(
()->{return new CustomException(ErrorCode.NOT_FOUND, "community");}
);
UserEntity user = userValidateService.getUserInfo(userId);
projectValidateService.validateAuthor(answer.getAsk().getProject(), user);
answer.getAsk().setNoAnswer();
projectCommunityAnswerRepository.deleteById(answerId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import NextLevel.demo.exception.ErrorCode;
import NextLevel.demo.project.project.entity.ProjectEntity;
import NextLevel.demo.project.project.repository.ProjectRepository;
import NextLevel.demo.user.entity.UserEntity;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

Expand All @@ -20,9 +21,21 @@ public ProjectEntity getProjectEntity(Long id) {

public ProjectEntity validateAuthor(Long projectId, Long userId) {
ProjectEntity project = getProjectEntity(projectId);
if(project.getUser().getId() != userId){
if(!project.getUser().getId().equals(userId)){
throw new CustomException(ErrorCode.NOT_AUTHOR);
}
return project;
}

public void validateAuthor(ProjectEntity project, Long userId) {
if(!project.getUser().getId().equals(userId)){
throw new CustomException(ErrorCode.NOT_AUTHOR);
}
}

public void validateAuthor(ProjectEntity project, UserEntity user) {
if(!project.getUser().getId().equals(user.getId())){
throw new CustomException(ErrorCode.NOT_AUTHOR);
}
}
}
Loading