-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…ndAnswer Feat/#10 save question and answer
- Loading branch information
Showing
17 changed files
with
185 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/com/sopkathon/server/controller/ResultController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/com/sopkathon/server/dto/request/ResultDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
src/main/java/com/sopkathon/server/dto/request/SaveResultsDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
7 changes: 2 additions & 5 deletions
7
src/main/java/com/sopkathon/server/dto/request/UpdateUserPointDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/sopkathon/server/dto/response/AnswerDetailResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/sopkathon/server/dto/response/AnswerResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
src/main/java/com/sopkathon/server/repository/AnswerRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
9 changes: 3 additions & 6 deletions
9
src/main/java/com/sopkathon/server/repository/QuestionRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
7 changes: 4 additions & 3 deletions
7
src/main/java/com/sopkathon/server/repository/ResultRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/main/java/com/sopkathon/server/service/ResultService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,4 +28,5 @@ public boolean subUserPoint(Long userId){ | |
user.subPoint(); | ||
return true; | ||
} | ||
|
||
} |