Skip to content

Commit

Permalink
Merge pull request #15 from GOSOPT-SOPKATHON-5/feat/#10-saveQuestionA…
Browse files Browse the repository at this point in the history
…ndAnswer

Feat/#10 save question and answer
  • Loading branch information
sung-silver authored May 20, 2023
2 parents c33de89 + 3b79674 commit e9a482c
Show file tree
Hide file tree
Showing 17 changed files with 185 additions and 23 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/sopkathon/server/ServerApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, "답변 상세보기 조회 성공")

;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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);
}

}
8 changes: 5 additions & 3 deletions src/main/java/com/sopkathon/server/domain/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/sopkathon/server/dto/request/ResultDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.sopkathon.server.dto.request;

import lombok.Data;

@Data
public class ResultDto {
Long receiverId;
Long senderId;
Long answerId;
}
11 changes: 11 additions & 0 deletions src/main/java/com/sopkathon/server/dto/request/SaveResultsDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.sopkathon.server.dto.request;


import lombok.Data;

import java.util.List;

@Data
public class SaveResultsDto {
List<ResultDto> results;
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<AnswerResponseDto> answers;

@Builder

public AnswerDetailResponseDto(Long senderId, String senderName, Long answerId, List<AnswerResponseDto> answers) {
this.senderId = senderId;
this.senderName = senderName;
this.answerId = answerId;
this.answers = answers;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;

@Getter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
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;

@Repository
public interface AnswerRepository extends JpaRepository<Answer, Long> {
List<Answer> findAllByQuestion(Question question);
List<Answer> findAllByQuestionId(Long id);
}
Original file line number Diff line number Diff line change
@@ -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<Question, Long> {

Optional<Question> findById(Long id);
}
Original file line number Diff line number Diff line change
@@ -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<Result, Long> {
List<Result> findAllByReceiver(User user);
Result save(Result result);
Optional<Result> findByReceiverIdAndAnswerId(Long receiverId, Long answerId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@RequiredArgsConstructor
@Transactional
Expand Down
62 changes: 62 additions & 0 deletions src/main/java/com/sopkathon/server/service/ResultService.java
Original file line number Diff line number Diff line change
@@ -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<Answer> answers = answerRepository.findAllByQuestionId(question.getId());
List<AnswerResponseDto> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ public boolean subUserPoint(Long userId){
user.subPoint();
return true;
}

}

0 comments on commit e9a482c

Please sign in to comment.