Skip to content

Commit

Permalink
Merge pull request #30 from GDG-Hackathon-77ia/fix/changeDQ
Browse files Browse the repository at this point in the history
fix: change DQ DTO
  • Loading branch information
GitJIHO authored Nov 14, 2024
2 parents b9f90e7 + e1c8975 commit 542fffc
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ public ResponseEntity<StringTypeMessageResponse> addDailyQuestion(@RequestBody D
return ResponseEntity.status(HttpStatus.CREATED).body(new StringTypeMessageResponse("질문이 추가되었습니다."));
}

@Operation(summary = "랜덤한 하나의 질문 조회", description = "최근 조회된 질문 제외한 랜덤한 하나의 질문을 조회합니다.")
@Operation(summary = "랜덤한 하나의 질문 조회", description = "최근 조회된 질문 제외한 랜덤한 하나의 질문을 조회합니다. 사용자의 답변 여부도 함께 리턴되며, 만약 사용자의 답변이 있을 시 답변도 리턴됩니다.")
@GetMapping("/question")
public ResponseEntity<DailyQuestionResponse> getRandomQuestionExcludingRecent() {
DailyQuestionResponse dailyQuestionResponse = dailyQuestionService.getRandomQuestionExcludingRecent();
public ResponseEntity<DailyQuestionResponse> getRandomQuestionExcludingRecent(@RequestAttribute("memberId") Long memberId) {
DailyQuestionResponse dailyQuestionResponse = dailyQuestionService.getRandomQuestionExcludingRecent(memberId);
return ResponseEntity.ok().body(dailyQuestionResponse);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

public record DailyQuestionResponse(
Long id,
String question
String question,
boolean AnsweredToday,
String answer
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@

import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

public interface DailyResponseRepository extends JpaRepository<DailyResponse, Long> {

List<DailyResponse> findAllByMemberAndResponseDate(Member member, LocalDate localDate);

Optional<DailyResponse> findByMemberId(Long memberId);

boolean existsByMemberIdAndResponseDate(Long memberId, LocalDate localDate);

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import com.gdg.kkia.dailyresponse.dto.DailyQuestionRequest;
import com.gdg.kkia.dailyresponse.dto.DailyQuestionResponse;
import com.gdg.kkia.dailyresponse.entity.DailyQuestion;
import com.gdg.kkia.dailyresponse.entity.DailyResponse;
import com.gdg.kkia.dailyresponse.repository.DailyQuestionRepository;
import com.gdg.kkia.dailyresponse.repository.DailyResponseRepository;
import jakarta.annotation.PostConstruct;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
Expand All @@ -19,6 +21,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.time.LocalDate;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
Expand All @@ -33,6 +36,7 @@ public class DailyQuestionService {
private final static int NUMBER_OF_RECENTLY_QUESTION_TO_EXCLUDE = 3;

private final DailyQuestionRepository dailyQuestionRepository;
private final DailyResponseRepository dailyResponseRepository;

@PostConstruct
public void loadQuestionsFromFile() {
Expand All @@ -59,7 +63,14 @@ public void addDailyQuestion(DailyQuestionRequest dailyQuestionRequest) {
}

@Transactional
public DailyQuestionResponse getRandomQuestionExcludingRecent() {
public DailyQuestionResponse getRandomQuestionExcludingRecent(Long memberId) {
DailyResponse dailyResponse = dailyResponseRepository.findByMemberId(memberId)
.orElse(null);

if (dailyResponse != null && dailyResponse.getResponseDate().equals(LocalDate.now())) {
return new DailyQuestionResponse(dailyResponse.getDailyQuestion().getId(), dailyResponse.getDailyQuestion().getQuestion(), true, dailyResponse.getResponse());
}

HttpSession session = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest().getSession();

List<Long> recentlyFetchedQuestionIds = (List<Long>) session.getAttribute("recentlyQuestionIds");
Expand All @@ -83,10 +94,10 @@ public DailyQuestionResponse getRandomQuestionExcludingRecent() {
}
session.setAttribute("recentlyQuestionIds", recentlyFetchedQuestionIds);

return new DailyQuestionResponse(randomQuestion.getId(), randomQuestion.getQuestion());
return new DailyQuestionResponse(randomQuestion.getId(), randomQuestion.getQuestion(), false, "아직 응답이 없습니다.");
}

return new DailyQuestionResponse(null, "조회된 질문이 없습니다.");
return new DailyQuestionResponse(null, "조회된 질문이 없습니다.", false, null);
}

@Transactional(readOnly = true)
Expand All @@ -95,7 +106,9 @@ public List<DailyQuestionResponse> getAllDailyQuestionForManager() {
.stream()
.map(DailyQuestion -> new DailyQuestionResponse(
DailyQuestion.getId(),
DailyQuestion.getQuestion()))
DailyQuestion.getQuestion(),
false,
"아직 응답이 없습니다."))
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.gdg.kkia.dailyresponse.service;

import com.gdg.kkia.common.exception.BadRequestException;
import com.gdg.kkia.common.exception.NotFoundException;
import com.gdg.kkia.common.exception.UnauthorizedException;
import com.gdg.kkia.dailyresponse.dto.DailyResponseRequest;
Expand Down Expand Up @@ -28,12 +29,15 @@ public class DailyResponseService {
private final PointLogService pointLogService;

public void saveResponseOfDailyQuestion(Long memberId, DailyResponseRequest dailyResponseRequest) {
if (dailyResponseRepository.existsByMemberIdAndResponseDate(memberId, LocalDate.now())) {
throw new BadRequestException("이미 오늘 응답을 했습니다.");
}

Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new NotFoundException("memberId에 해당하는 멤버가 없습니다."));

DailyQuestion dailyQuestion = dailyQuestionRepository.findById(dailyResponseRequest.questionId())
.orElseThrow(() -> new NotFoundException("questionId에 해당하는 질문이 없습니다."));

DailyResponse dailyResponse = new DailyResponse(dailyResponseRequest.response(), member, dailyQuestion);
dailyResponseRepository.save(dailyResponse);

Expand Down

0 comments on commit 542fffc

Please sign in to comment.