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 4f22716..5dce450 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 c1e3d37..ce6ab3f 100644 --- a/src/main/java/com/sopkathon/server/controller/AnswerController.java +++ b/src/main/java/com/sopkathon/server/controller/AnswerController.java @@ -3,18 +3,19 @@ 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.RequestBody; -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( @@ -24,4 +25,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 de1e731..e4eccdc 100644 --- a/src/main/java/com/sopkathon/server/repository/AnswerRepository.java +++ b/src/main/java/com/sopkathon/server/repository/AnswerRepository.java @@ -1,8 +1,10 @@ package com.sopkathon.server.repository; import com.sopkathon.server.domain.Answer; + import com.sopkathon.server.domain.Question; import com.sopkathon.server.domain.User; + import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -10,5 +12,5 @@ @Repository public interface AnswerRepository extends JpaRepository { - List findAllByQuestion(Question question); + 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 index cbf18e5..f14164b 100644 --- a/src/main/java/com/sopkathon/server/repository/QuestionRepository.java +++ b/src/main/java/com/sopkathon/server/repository/QuestionRepository.java @@ -1,14 +1,11 @@ package com.sopkathon.server.repository; -import com.sopkathon.server.domain.Answer; + import com.sopkathon.server.domain.Question; -import com.sopkathon.server.domain.User; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.stereotype.Repository; -import java.util.List; +import java.util.Optional; -@Repository 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 index 7ce62f3..2753623 100644 --- a/src/main/java/com/sopkathon/server/repository/ResultRepository.java +++ b/src/main/java/com/sopkathon/server/repository/ResultRepository.java @@ -1,13 +1,14 @@ package com.sopkathon.server.repository; import com.sopkathon.server.domain.Result; -import com.sopkathon.server.domain.User; + import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; -import java.util.List; +import java.util.Optional; @Repository public interface ResultRepository extends JpaRepository { - List findAllByReceiver(User user); + 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 b374bf2..c7d6481 100644 --- a/src/main/java/com/sopkathon/server/service/AnswerService.java +++ b/src/main/java/com/sopkathon/server/service/AnswerService.java @@ -9,6 +9,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..3aeb710 --- /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){ + System.out.println(addAnswer.getAnswer()+"답변이다리"); + results.add(AnswerResponseDto.builder().id(addAnswer.getId()).answer(addAnswer.getAnswer()).build()); + } + 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(); + } +} 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; } + }