From 60fe865e60f4d8a0895d2ab9e688b0c39791b5db Mon Sep 17 00:00:00 2001 From: sung-silver Date: Sun, 21 May 2023 04:49:10 +0900 Subject: [PATCH 1/2] [FEAT] save question and answer] --- .../sopkathon/server/ServerApplication.java | 2 + .../common/exception/SuccessStatus.java | 6 +- .../server/controller/AnswerController.java | 14 ++++- .../server/controller/ResultController.java | 27 ++++++++ .../com/sopkathon/server/domain/Result.java | 8 ++- .../server/dto/request/ResultDto.java | 10 +++ .../server/dto/request/SaveResultsDto.java | 11 ++++ .../dto/request/UpdateUserPointDto.java | 7 +-- .../dto/response/AnswerDetailResponseDto.java | 22 +++++++ .../dto/response/AnswerResponseDto.java | 14 +++++ .../server/dto/response/UserResponseDto.java | 1 + .../server/repository/AnswerRepository.java | 5 +- .../server/repository/QuestionRepository.java | 10 +++ .../server/repository/ResultRepository.java | 13 ++++ .../server/service/AnswerService.java | 1 + .../server/service/ResultService.java | 62 +++++++++++++++++++ .../sopkathon/server/service/UserService.java | 1 + 17 files changed, 200 insertions(+), 14 deletions(-) create mode 100644 src/main/java/com/sopkathon/server/controller/ResultController.java create mode 100644 src/main/java/com/sopkathon/server/dto/request/ResultDto.java create mode 100644 src/main/java/com/sopkathon/server/dto/request/SaveResultsDto.java create mode 100644 src/main/java/com/sopkathon/server/dto/response/AnswerDetailResponseDto.java create mode 100644 src/main/java/com/sopkathon/server/dto/response/AnswerResponseDto.java create mode 100644 src/main/java/com/sopkathon/server/repository/QuestionRepository.java create mode 100644 src/main/java/com/sopkathon/server/repository/ResultRepository.java create mode 100644 src/main/java/com/sopkathon/server/service/ResultService.java diff --git a/src/main/java/com/sopkathon/server/ServerApplication.java b/src/main/java/com/sopkathon/server/ServerApplication.java index 119da93..055b7ab 100644 --- a/src/main/java/com/sopkathon/server/ServerApplication.java +++ b/src/main/java/com/sopkathon/server/ServerApplication.java @@ -2,7 +2,9 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.data.jpa.repository.config.EnableJpaAuditing; +@EnableJpaAuditing @SpringBootApplication public class ServerApplication { diff --git a/src/main/java/com/sopkathon/server/common/exception/SuccessStatus.java b/src/main/java/com/sopkathon/server/common/exception/SuccessStatus.java index 277935f..faec8cd 100644 --- a/src/main/java/com/sopkathon/server/common/exception/SuccessStatus.java +++ b/src/main/java/com/sopkathon/server/common/exception/SuccessStatus.java @@ -18,7 +18,11 @@ public enum SuccessStatus { */ DELETE_RESULT(HttpStatus.OK, "결과 조회 완료후, 삭제 하였습니다."), - CHANGE_POINT_SUCCESS(HttpStatus.CREATED, "해당 사용자의 포인트가 변경되었습니다") + CHANGE_POINT_SUCCESS(HttpStatus.OK, "해당 사용자의 포인트가 변경되었습니다"), + + SAVE_ANSWERS(HttpStatus.CREATED, "해당 사용자의 포인트가 변경되었습니다"), + + VIEW_ANASWER(HttpStatus.OK, "답변 상세보기 조회 성공") ; diff --git a/src/main/java/com/sopkathon/server/controller/AnswerController.java b/src/main/java/com/sopkathon/server/controller/AnswerController.java index 109861b..12d915c 100644 --- a/src/main/java/com/sopkathon/server/controller/AnswerController.java +++ b/src/main/java/com/sopkathon/server/controller/AnswerController.java @@ -3,17 +3,18 @@ import com.sopkathon.server.common.dto.ApiResponseDto; import com.sopkathon.server.common.exception.SuccessStatus; import com.sopkathon.server.dto.request.DeleteResultRequestDto; +import com.sopkathon.server.dto.request.SaveResultsDto; import com.sopkathon.server.service.AnswerService; +import com.sopkathon.server.service.ResultService; import lombok.RequiredArgsConstructor; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.RequestHeader; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; @RestController @RequiredArgsConstructor public class AnswerController { private final AnswerService answerService; + private final ResultService resultService; @DeleteMapping("/answer") public ApiResponseDto deleteAnswer( @@ -23,4 +24,11 @@ public ApiResponseDto deleteAnswer( answerService.deleteAnswer(userId, requestDto); return ApiResponseDto.success(SuccessStatus.DELETE_RESULT); } + + @PostMapping("/answer") + public ApiResponseDto saveResult( @RequestBody SaveResultsDto request){ + resultService.createResult(request); + return ApiResponseDto.success(SuccessStatus.SAVE_ANSWERS); + } + } diff --git a/src/main/java/com/sopkathon/server/controller/ResultController.java b/src/main/java/com/sopkathon/server/controller/ResultController.java new file mode 100644 index 0000000..872f0c3 --- /dev/null +++ b/src/main/java/com/sopkathon/server/controller/ResultController.java @@ -0,0 +1,27 @@ +package com.sopkathon.server.controller; + +import com.sopkathon.server.common.dto.ApiResponseDto; +import com.sopkathon.server.common.exception.SuccessStatus; +import com.sopkathon.server.dto.request.UpdateUserPointDto; +import com.sopkathon.server.service.ResultService; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.*; + +import javax.validation.Valid; + +import static com.sopkathon.server.common.exception.ErrorStatus.INTERNAL_SERVER_ERROR; +import static com.sopkathon.server.common.exception.SuccessStatus.VIEW_ANASWER; + +@RestController +@RequiredArgsConstructor +public class ResultController { + + private final ResultService resultService; + + @GetMapping("/result/{answerId}/detail") + public ApiResponseDto viewResult(@RequestHeader("user-id") @Valid Long userId, @Valid @PathVariable Long answerId){ + resultService.viewResult(userId, answerId); + return ApiResponseDto.success(VIEW_ANASWER); + } + +} diff --git a/src/main/java/com/sopkathon/server/domain/Result.java b/src/main/java/com/sopkathon/server/domain/Result.java index 4424ee5..d145e01 100644 --- a/src/main/java/com/sopkathon/server/domain/Result.java +++ b/src/main/java/com/sopkathon/server/domain/Result.java @@ -27,11 +27,13 @@ public class Result extends AuditingTimeEntity { @JoinColumn(name = "answer_id") private Answer answer; - @Builder - public Result(Long id, User sender, User receiver, Answer answer) { - this.id = id; + public Result(User sender, User receiver, Answer answer) { this.sender = sender; this.receiver = receiver; this.answer = answer; } + + public static Result newInstance(User sender, User receiver, Answer answer) { + return new Result(sender, receiver, answer); + } } diff --git a/src/main/java/com/sopkathon/server/dto/request/ResultDto.java b/src/main/java/com/sopkathon/server/dto/request/ResultDto.java new file mode 100644 index 0000000..a718da8 --- /dev/null +++ b/src/main/java/com/sopkathon/server/dto/request/ResultDto.java @@ -0,0 +1,10 @@ +package com.sopkathon.server.dto.request; + +import lombok.Data; + +@Data +public class ResultDto { + Long receiverId; + Long senderId; + Long answerId; +} diff --git a/src/main/java/com/sopkathon/server/dto/request/SaveResultsDto.java b/src/main/java/com/sopkathon/server/dto/request/SaveResultsDto.java new file mode 100644 index 0000000..b0f339e --- /dev/null +++ b/src/main/java/com/sopkathon/server/dto/request/SaveResultsDto.java @@ -0,0 +1,11 @@ +package com.sopkathon.server.dto.request; + + +import lombok.Data; + +import java.util.List; + +@Data +public class SaveResultsDto { + List results; +} diff --git a/src/main/java/com/sopkathon/server/dto/request/UpdateUserPointDto.java b/src/main/java/com/sopkathon/server/dto/request/UpdateUserPointDto.java index 2ac8870..15f845b 100644 --- a/src/main/java/com/sopkathon/server/dto/request/UpdateUserPointDto.java +++ b/src/main/java/com/sopkathon/server/dto/request/UpdateUserPointDto.java @@ -1,11 +1,8 @@ package com.sopkathon.server.dto.request; +import lombok.Data; -import lombok.AccessLevel; -import lombok.Getter; -import lombok.NoArgsConstructor; -@Getter -@NoArgsConstructor(access = AccessLevel.PRIVATE) +@Data public class UpdateUserPointDto { Boolean changeStatus; } diff --git a/src/main/java/com/sopkathon/server/dto/response/AnswerDetailResponseDto.java b/src/main/java/com/sopkathon/server/dto/response/AnswerDetailResponseDto.java new file mode 100644 index 0000000..8eae417 --- /dev/null +++ b/src/main/java/com/sopkathon/server/dto/response/AnswerDetailResponseDto.java @@ -0,0 +1,22 @@ +package com.sopkathon.server.dto.response; + +import lombok.Builder; + +import java.util.List; + +public class AnswerDetailResponseDto { + + Long senderId; + String senderName; + Long answerId; + List answers; + + @Builder + + public AnswerDetailResponseDto(Long senderId, String senderName, Long answerId, List answers) { + this.senderId = senderId; + this.senderName = senderName; + this.answerId = answerId; + this.answers = answers; + } +} diff --git a/src/main/java/com/sopkathon/server/dto/response/AnswerResponseDto.java b/src/main/java/com/sopkathon/server/dto/response/AnswerResponseDto.java new file mode 100644 index 0000000..696141c --- /dev/null +++ b/src/main/java/com/sopkathon/server/dto/response/AnswerResponseDto.java @@ -0,0 +1,14 @@ +package com.sopkathon.server.dto.response; + +import lombok.Builder; + +public class AnswerResponseDto { + Long id; + String answer; + + @Builder + public AnswerResponseDto(Long id, String answer) { + this.id = id; + this.answer = answer; + } +} diff --git a/src/main/java/com/sopkathon/server/dto/response/UserResponseDto.java b/src/main/java/com/sopkathon/server/dto/response/UserResponseDto.java index 48811b5..c15be54 100644 --- a/src/main/java/com/sopkathon/server/dto/response/UserResponseDto.java +++ b/src/main/java/com/sopkathon/server/dto/response/UserResponseDto.java @@ -2,6 +2,7 @@ import lombok.AccessLevel; import lombok.AllArgsConstructor; +import lombok.Data; import lombok.Getter; @Getter diff --git a/src/main/java/com/sopkathon/server/repository/AnswerRepository.java b/src/main/java/com/sopkathon/server/repository/AnswerRepository.java index f9357ef..0173d8b 100644 --- a/src/main/java/com/sopkathon/server/repository/AnswerRepository.java +++ b/src/main/java/com/sopkathon/server/repository/AnswerRepository.java @@ -1,11 +1,12 @@ package com.sopkathon.server.repository; import com.sopkathon.server.domain.Answer; -import com.sopkathon.server.domain.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.List; + @Repository public interface AnswerRepository extends JpaRepository { - + List findAllByQuestionId(Long id); } diff --git a/src/main/java/com/sopkathon/server/repository/QuestionRepository.java b/src/main/java/com/sopkathon/server/repository/QuestionRepository.java new file mode 100644 index 0000000..e0f8ee2 --- /dev/null +++ b/src/main/java/com/sopkathon/server/repository/QuestionRepository.java @@ -0,0 +1,10 @@ +package com.sopkathon.server.repository; + +import com.sopkathon.server.domain.Question; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.Optional; + +public interface QuestionRepository extends JpaRepository { + Optional findById(Long id); +} diff --git a/src/main/java/com/sopkathon/server/repository/ResultRepository.java b/src/main/java/com/sopkathon/server/repository/ResultRepository.java new file mode 100644 index 0000000..7bb30f2 --- /dev/null +++ b/src/main/java/com/sopkathon/server/repository/ResultRepository.java @@ -0,0 +1,13 @@ +package com.sopkathon.server.repository; + +import com.sopkathon.server.domain.Result; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.Optional; + +@Repository +public interface ResultRepository extends JpaRepository { + Result save(Result result); + Optional findByReceiverIdAndAnswerId(Long receiverId, Long answerId); +} diff --git a/src/main/java/com/sopkathon/server/service/AnswerService.java b/src/main/java/com/sopkathon/server/service/AnswerService.java index 93c17cb..70392bc 100644 --- a/src/main/java/com/sopkathon/server/service/AnswerService.java +++ b/src/main/java/com/sopkathon/server/service/AnswerService.java @@ -8,6 +8,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; + @Service @RequiredArgsConstructor @Transactional diff --git a/src/main/java/com/sopkathon/server/service/ResultService.java b/src/main/java/com/sopkathon/server/service/ResultService.java new file mode 100644 index 0000000..3098064 --- /dev/null +++ b/src/main/java/com/sopkathon/server/service/ResultService.java @@ -0,0 +1,62 @@ +package com.sopkathon.server.service; + +import com.sopkathon.server.domain.Answer; +import com.sopkathon.server.domain.Question; +import com.sopkathon.server.domain.Result; +import com.sopkathon.server.domain.User; +import com.sopkathon.server.dto.request.ResultDto; +import com.sopkathon.server.dto.request.SaveResultsDto; +import com.sopkathon.server.dto.response.AnswerDetailResponseDto; +import com.sopkathon.server.dto.response.AnswerResponseDto; +import com.sopkathon.server.repository.AnswerRepository; +import com.sopkathon.server.repository.QuestionRepository; +import com.sopkathon.server.repository.ResultRepository; +import com.sopkathon.server.repository.UserRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; + + +@RequiredArgsConstructor +@Service +public class ResultService { + private final ResultRepository resultRepository; + private final UserRepository userRepository; + private final AnswerRepository answerRepository; + private final QuestionRepository questionRepository; + + public void createResult(SaveResultsDto results) { + + User sender; + User receiver; + Answer answer; + + for (ResultDto resultDto : results.getResults()) { + sender = userRepository.findById(resultDto.getSenderId()).orElse(null); + receiver = userRepository.findById(resultDto.getReceiverId()).orElse(null); + answer = answerRepository.findById(resultDto.getAnswerId()).orElse(null); + resultRepository.save(Result.newInstance(sender,receiver,answer)); + } + } + + public AnswerDetailResponseDto viewResult(Long userId, Long answerId){ + Answer answer = answerRepository.findById(answerId).orElse(null); + Question question = questionRepository.findById(answer.getQuestion().getId()).orElse(null); + List answers = answerRepository.findAllByQuestionId(question.getId()); + + List results = new ArrayList<>(); + + for(Answer addAnswer: answers){ + results.add(AnswerResponseDto.builder().id(addAnswer.getId()).answer(addAnswer.getAnswer()).build()); + } + Result result = resultRepository.findByReceiverIdAndAnswerId(userId, answerId).orElse(null); + User user = result.getSender(); + + System.out.println(user.getId()); + String userName = user.getName(); + + return AnswerDetailResponseDto.builder().senderId(user.getId()).senderName(userName).answerId(answer.getId()).answers(results).build(); + } +} diff --git a/src/main/java/com/sopkathon/server/service/UserService.java b/src/main/java/com/sopkathon/server/service/UserService.java index 853d8c3..9c7dbfa 100644 --- a/src/main/java/com/sopkathon/server/service/UserService.java +++ b/src/main/java/com/sopkathon/server/service/UserService.java @@ -28,4 +28,5 @@ public boolean subUserPoint(Long userId){ user.subPoint(); return true; } + } From 43cf439cc12aa268fd06da6182bee80efaea7ac1 Mon Sep 17 00:00:00 2001 From: sung-silver Date: Sun, 21 May 2023 05:13:11 +0900 Subject: [PATCH 2/2] [FIX] get Answer details --- .../sopkathon/server/service/ResultService.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/sopkathon/server/service/ResultService.java b/src/main/java/com/sopkathon/server/service/ResultService.java index 3098064..3aeb710 100644 --- a/src/main/java/com/sopkathon/server/service/ResultService.java +++ b/src/main/java/com/sopkathon/server/service/ResultService.java @@ -45,18 +45,18 @@ public AnswerDetailResponseDto viewResult(Long userId, Long answerId){ Answer answer = answerRepository.findById(answerId).orElse(null); Question question = questionRepository.findById(answer.getQuestion().getId()).orElse(null); List answers = answerRepository.findAllByQuestionId(question.getId()); - List results = new ArrayList<>(); - for(Answer addAnswer: answers){ + System.out.println(addAnswer.getAnswer()+"답변이다리"); results.add(AnswerResponseDto.builder().id(addAnswer.getId()).answer(addAnswer.getAnswer()).build()); } - Result result = resultRepository.findByReceiverIdAndAnswerId(userId, answerId).orElse(null); - User user = result.getSender(); - - System.out.println(user.getId()); + System.out.println(userId); + System.out.println(answerId); + Result result = resultRepository.findByReceiverIdAndAnswerId(userId, answerId) + .orElseThrow(() -> new RuntimeException("ASDASDASD")); + System.out.println(result.getReceiver()); + User user = userRepository.findById(result.getReceiver().getId()).orElse(null); String userName = user.getName(); - return AnswerDetailResponseDto.builder().senderId(user.getId()).senderName(userName).answerId(answer.getId()).answers(results).build(); } }