Skip to content

Commit

Permalink
Merge pull request #156 from UMC-WOWMARKET/feat/notice
Browse files Browse the repository at this point in the history
[feat] 공지, 문의, 문의답변 수정/삭제 구현
  • Loading branch information
minji1289 authored Jan 10, 2024
2 parents 5164018 + e7e6613 commit c53e76a
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public ResponseEntity createNotice(@PathVariable Long project_id, @RequestBody N
return noticeService.createNotice(project_id, requestDto);
}


// 주문폼: 공지 전체 조회
@GetMapping("/{project_id}/notice")
public NoticePageResponseDto getNoticeList(@PathVariable Long project_id) {
Expand All @@ -33,8 +32,21 @@ public NoticePageResponseDto getNoticeList(@PathVariable Long project_id) {

// 주문폼: 공지 선택 조회
@GetMapping("/{project_id}/notice/{notice_id}")
public NoticeSelectResponseDto getNotice(@PathVariable String project_id, @PathVariable Long notice_id) {
public NoticeSelectResponseDto getNotice(@PathVariable Long project_id, @PathVariable Long notice_id) {
return noticeService.getNotice(notice_id);
}

//주문폼: 공지 수정 (판매자만 가능)
@PatchMapping("/{project_id}/notice/{notice_id}")
public ResponseEntity updateNotice(@PathVariable Long project_id, @PathVariable Long notice_id, @RequestBody NoticeRequestDto requestDto)
{
return noticeService.updateNotice(project_id, notice_id, requestDto);
}

@DeleteMapping("/{project_id}/notice/{notice_id}")
public ResponseEntity deleteNotice(@PathVariable Long project_id, @PathVariable Long notice_id)
{
return noticeService.deleteNotice(project_id, notice_id);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import wowmarket.wow_server.repository.UserRepository;

import java.util.List;
import java.util.Optional;

@Service
public class NoticeService {
Expand Down Expand Up @@ -73,4 +74,34 @@ public NoticeSelectResponseDto getNotice(Long notice_id) {
// 해당 id 가 있을 경우
return new NoticeSelectResponseDto(notice);
}

//공지 수정 (판매자만 가능)
public ResponseEntity updateNotice(Long project_id, Long notice_id, NoticeRequestDto requestDto) {
User user = userRepository.findByEmail(SecurityUtil.getLoginUsername())
.orElseThrow(()->new ResponseStatusException(HttpStatus.BAD_REQUEST));
Project project = projectRepository.findByProject_Id(project_id);
//판매자만 공지 수정 가능
if (user.getEmail() == project.getUser().getEmail()){
Notice notice = noticeRepository.findByNoticeId(notice_id)
.orElseThrow(()->new ResponseStatusException(HttpStatus.BAD_REQUEST));
notice.update(requestDto.getTitle(), requestDto.getContent());
noticeRepository.save(notice);
return new ResponseEntity(HttpStatus.OK);
}
else return new ResponseEntity(HttpStatus.BAD_REQUEST); //판매자 아닐 경우
}

//공지 삭제 (판매자만 가능)
public ResponseEntity deleteNotice(Long project_id, Long notice_id) {
User user = userRepository.findByEmail(SecurityUtil.getLoginUsername())
.orElseThrow(()->new ResponseStatusException(HttpStatus.BAD_REQUEST));
Project project = projectRepository.findByProject_Id(project_id);
//판매자만 공지 삭제 가능
if (user.getEmail() == project.getUser().getEmail()){
noticeRepository.deleteById(notice_id);
return new ResponseEntity(HttpStatus.OK);
}
else return new ResponseEntity(HttpStatus.BAD_REQUEST); //판매자 아닐 경우
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package wowmarket.wow_server.detail.project.question.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import wowmarket.wow_server.detail.project.notice.dto.NoticeRequestDto;
import wowmarket.wow_server.detail.project.question.dto.AnswerRequestDto;
import wowmarket.wow_server.detail.project.question.dto.AnswerResponseDto;
import wowmarket.wow_server.detail.project.question.dto.QuestionRequestDto;
Expand All @@ -23,6 +25,21 @@ public QuestionResponseDto createQuestion(@PathVariable Long project_id, @Reques
return questionService.createQuestion(project_id, requestDto);
}

//문의글 수정 (작성자만 가능)
@PatchMapping("/question/{question_id}")
public ResponseEntity updateQuestion(@PathVariable Long question_id, @RequestBody QuestionRequestDto requestDto)
{
return questionService.updateQuestion(question_id, requestDto);
}

//문의글 삭제 (작성자만 가능)
@DeleteMapping("/question/{question_id}")
public ResponseEntity deleteQuestion(@PathVariable Long question_id)
{
return questionService.deleteQuestion(question_id);
}


// 문의 전체 조회
@GetMapping("/{project_id}/question")
public List<QuestionResponseDto> getQuestionList(@PathVariable Long project_id) {
Expand All @@ -41,4 +58,18 @@ public AnswerResponseDto createAnswer(@PathVariable Long project_id, @PathVariab
return questionService.createAnswer(project_id, question_id, requestDto);
}

// 문의 답변 수정 (판매자만 가능)
@PatchMapping("/{project_id}/question/{question_id}/answer")
public ResponseEntity updateAnswer(@PathVariable Long project_id, @PathVariable Long question_id, @RequestBody AnswerRequestDto requestDto)
{
return questionService.updateAnswer(project_id, question_id, requestDto);
}

//문의 답변 삭제 (판매자만 가능)
@DeleteMapping("/{project_id}/question/{question_id}/answer")
public ResponseEntity deleteAnswer(@PathVariable Long project_id, @PathVariable Long question_id)
{
return questionService.deleteAnswer(project_id, question_id);
}

}
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package wowmarket.wow_server.detail.project.question.service;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;
import wowmarket.wow_server.detail.project.notice.dto.NoticeRequestDto;
import wowmarket.wow_server.detail.project.question.dto.AnswerRequestDto;
import wowmarket.wow_server.detail.project.question.dto.AnswerResponseDto;
import wowmarket.wow_server.detail.project.question.dto.QuestionRequestDto;
import wowmarket.wow_server.detail.project.question.dto.QuestionResponseDto;
import wowmarket.wow_server.detail.project.question.dto.QuestionSelectResponseDto;
import wowmarket.wow_server.domain.Answer;
import wowmarket.wow_server.domain.Project;
import wowmarket.wow_server.domain.Question;
import wowmarket.wow_server.domain.User;
import wowmarket.wow_server.domain.*;
import wowmarket.wow_server.global.jwt.SecurityUtil;
import wowmarket.wow_server.repository.ProjectRepository;
import wowmarket.wow_server.repository.QuestionRepository;
Expand Down Expand Up @@ -47,6 +46,39 @@ public QuestionResponseDto createQuestion(Long project_id, QuestionRequestDto re
return questionResponseDto;
}

//문의 수정 (글 작성자만 가능)
public ResponseEntity updateQuestion(Long question_id, QuestionRequestDto requestDto) {
User user = userRepository.findByEmail(SecurityUtil.getLoginUsername())
.orElseThrow(()->new ResponseStatusException(HttpStatus.BAD_REQUEST));
Question question = questionRepository.findByQuestion_Id(question_id);
//문의글 작성자만 글 수정 가능
if (user.getEmail() == question.getUser().getEmail()){
question.update(requestDto.getTitle(), requestDto.getContent(), requestDto.isSecret());
questionRepository.save(question);
return new ResponseEntity(HttpStatus.OK);
}
else return new ResponseEntity(HttpStatus.BAD_REQUEST); //문의글 작성자가 아닐 경우
}

//문의 삭제 (작성자만 가능)
public ResponseEntity deleteQuestion(Long question_id) {
User user = userRepository.findByEmail(SecurityUtil.getLoginUsername())
.orElseThrow(()->new ResponseStatusException(HttpStatus.BAD_REQUEST));
Question question = questionRepository.findByQuestion_Id(question_id);
//판매자만 문의 삭제 가능
if (user.getEmail() == question.getUser().getEmail()){
//문의 삭제시 문의 답변글도 함께 삭제
if (answerRepository.existsByQuestionId(question_id)) //문의 답변이 존재하는 경우
{
Answer answer = answerRepository.findByQuestionId(question_id);
answerRepository.deleteById(answer.getId());
}
questionRepository.deleteById(question_id);
return new ResponseEntity(HttpStatus.OK);
}
else return new ResponseEntity(HttpStatus.BAD_REQUEST); //판매자 아닐 경우
}


// 문의 전체 조회 (비밀글 여부 Response로 출력 ok)
public List<QuestionResponseDto> getQuestionList(Long project_id) {
Expand Down Expand Up @@ -131,7 +163,35 @@ public AnswerResponseDto createAnswer(Long project_id, Long question_id, AnswerR

return answerResponseDto;
}

return null;
}

//문의 답변 수정 (판매자만 가능)
public ResponseEntity updateAnswer(Long project_id, Long question_id, AnswerRequestDto requestDto) {
User user = userRepository.findByEmail(SecurityUtil.getLoginUsername())
.orElseThrow(()->new ResponseStatusException(HttpStatus.BAD_REQUEST));
Project project = projectRepository.findByProject_Id(project_id);
//판매자만 문의 답변 수정 가능
if (user.getEmail() == project.getUser().getEmail()){
Answer answer = answerRepository.findByQuestionId(question_id);
answer.update(requestDto.getContent());
answerRepository.save(answer);
return new ResponseEntity(HttpStatus.OK);
}
else return new ResponseEntity(HttpStatus.BAD_REQUEST); //판매자 아닐 경우
}

//문의 답변 삭제 (판매자만 가능)
public ResponseEntity deleteAnswer(Long project_id, Long question_id) {
User user = userRepository.findByEmail(SecurityUtil.getLoginUsername())
.orElseThrow(()->new ResponseStatusException(HttpStatus.BAD_REQUEST));
Project project = projectRepository.findByProject_Id(project_id);
//판매자만 문의 답변 삭제 가능
if (user.getEmail() == project.getUser().getEmail()){
Answer answer = answerRepository.findByQuestionId(question_id);
answerRepository.deleteById(answer.getId());
return new ResponseEntity(HttpStatus.OK);
}
else return new ResponseEntity(HttpStatus.BAD_REQUEST); //판매자 아닐 경우
}
}
4 changes: 4 additions & 0 deletions src/main/java/wowmarket/wow_server/domain/Answer.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,8 @@ public Answer(Project project, Question question, User user, AnswerRequestDto re
this.content = requestDto.getContent();
}

public void update(String content) {
this.content = content;
}

}
5 changes: 5 additions & 0 deletions src/main/java/wowmarket/wow_server/domain/Notice.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ public Notice(Project project, User user, NoticeRequestDto requestDto) {
this.content = requestDto.getContent();
}

public void update(String title, String content) {
this.title = title;
this.content = content;
}

}
6 changes: 6 additions & 0 deletions src/main/java/wowmarket/wow_server/domain/Question.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,10 @@ public Question(Project project, User user, QuestionRequestDto requestDto){
this.content = requestDto.getContent();
}

public void update(String title, String content, boolean secret) {
this.title = title;
this.content = content;
this.secret = secret;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import wowmarket.wow_server.domain.Notice;
import wowmarket.wow_server.domain.Project;
import wowmarket.wow_server.domain.Question;

import java.util.List;
Expand All @@ -17,4 +18,7 @@ public interface QuestionRepository extends JpaRepository<Question, Long> {

@Query(nativeQuery = true, value = "select * from question where project_id=? and question_id=?")
Optional<Question> findByProjectIdAndQuestionId(Long project_id, Long question_id);

@Query(nativeQuery = true, value = "select * from question where question_id =?")
Question findByQuestion_Id(Long questionId);
}

0 comments on commit c53e76a

Please sign in to comment.