Skip to content

Commit

Permalink
fix: 머지 실패 시 오류 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
JINU-CHANG committed Jul 30, 2024
2 parents aa130ad + ea3c992 commit ea3a056
Show file tree
Hide file tree
Showing 15 changed files with 291 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.bang_ggood.category.dto;

import com.bang_ggood.checklist.dto.response.QuestionResponse;
import java.util.List;

public record CategoryQuestionsResponse(Integer categoryId, String categoryName, List<QuestionResponse> questions) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.bang_ggood.category.dto;

import com.bang_ggood.checklist.dto.response.WrittenQuestionResponse;
import java.util.List;

public record WrittenCategoryQuestionsResponse(Integer categoryId, String categoryName, List<WrittenQuestionResponse> questions) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.net.URI;
import com.bang_ggood.checklist.dto.response.ChecklistsWithScoreReadResponse;
import com.bang_ggood.checklist.dto.response.UserChecklistsPreviewResponse;
import com.bang_ggood.checklist.service.ChecklistService;
import com.bang_ggood.user.domain.User;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
public class ChecklistController {
Expand All @@ -37,4 +46,16 @@ public ResponseEntity<ChecklistQuestionsResponse> readChecklistQuestions() {
public ResponseEntity<WrittenChecklistResponse> readChecklistById(@PathVariable("id") long id) {
return ResponseEntity.ok(checklistService.readChecklistById(id));
}

@GetMapping("/checklists")
public ResponseEntity<UserChecklistsPreviewResponse> readUserChecklistsPreview() {
User user = new User(1L, "방방이");
return ResponseEntity.ok(checklistService.readUserChecklistsPreview(user));
}

@GetMapping("/checklists/comparison")
public ResponseEntity<ChecklistsWithScoreReadResponse> readChecklistsComparison(
@RequestParam("id") List<Long> checklistIds) {
return ResponseEntity.ok(checklistService.readChecklistsComparison(checklistIds));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.bang_ggood.room.domain.Room;
import com.bang_ggood.room.dto.request.RoomCreateRequest;
import jakarta.validation.Valid;

import java.util.List;

public record ChecklistCreateRequest(@Valid RoomCreateRequest room, List<Integer> options,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package com.bang_ggood.checklist.dto.response;

import com.bang_ggood.category.domain.Category;

public record CategoryScoreReadResponse(
Integer categoryId, String categoryName, Integer score
) {

public static CategoryScoreReadResponse of(Category category, int score) {
return new CategoryScoreReadResponse(category.getId(), category.getName(), score);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

import java.util.List;

public record ChecklistComparisonReadResponse(
public record ChecklistWithScoreReadResponse(
Long checklistId, String roomName, String address,
Integer floor, Integer deposit, Integer rent,
Integer contractTerm, String station, Integer walkingTime,
Integer optionCount, Integer score,
List<CategoryScoreReadResponse> categories
) {
public static ChecklistComparisonReadResponse of(Checklist checklist, int checklistOptionCount, int checklistScore, List<CategoryScoreReadResponse> categoryScores) {
return new ChecklistComparisonReadResponse(
public static ChecklistWithScoreReadResponse of(Checklist checklist, int checklistOptionCount, int checklistScore, List<CategoryScoreReadResponse> categoryScores) {
return new ChecklistWithScoreReadResponse(
checklist.getId(),
checklist.getRoomName(),
checklist.getRoomAddress(),
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.bang_ggood.checklist.dto.response;

import java.util.List;

public record ChecklistsWithScoreReadResponse(List<ChecklistWithScoreReadResponse> checklists) {
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bang_ggood.checklist.dto.response;

import com.bang_ggood.checklist.dto.response.UserChecklistPreviewResponse;
import java.util.List;

public record UserChecklistsPreviewResponse(List<UserChecklistPreviewResponse> checklists) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
import com.bang_ggood.room.dto.response.WrittenRoomResponse;
import java.util.List;

public record WrittenChecklistResponse(WrittenRoomResponse room, List<Integer> options, List<WrittenCategoryQuestionsResponse> categories) {
public record WrittenChecklistResponse(WrittenRoomResponse room, List<Integer> options,
List<WrittenCategoryQuestionsResponse> categories) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.springframework.data.repository.query.Param;
import java.util.List;
import java.util.Optional;
import java.util.List;
import java.util.Optional;

public interface ChecklistRepository extends JpaRepository<Checklist, Long> {

Expand All @@ -22,8 +24,10 @@ public interface ChecklistRepository extends JpaRepository<Checklist, Long> {
default Checklist getById(long id) {
return findById(id).orElseThrow(() -> new BangggoodException(ExceptionCode.CHECKLIST_NOT_FOUND));
}

List<Checklist> findByUser(User user);

List<Checklist> findByUserAndIdIn(User user, List<Long> checklistIds);

long countAllByIdIn(List<Long> ids);
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
package com.bang_ggood.checklist.service;

import com.bang_ggood.category.domain.Badge;
import com.bang_ggood.category.domain.Category;
import com.bang_ggood.category.dto.response.CategoryQuestionsResponse;
import com.bang_ggood.category.dto.response.WrittenCategoryQuestionsResponse;
import com.bang_ggood.checklist.domain.Checklist;
import com.bang_ggood.checklist.domain.ChecklistOption;
import com.bang_ggood.checklist.domain.ChecklistQuestion;
import com.bang_ggood.checklist.domain.ChecklistScore;
import com.bang_ggood.checklist.domain.Grade;
import com.bang_ggood.checklist.domain.Option;
import com.bang_ggood.checklist.domain.Question;
import com.bang_ggood.checklist.dto.request.ChecklistCreateRequest;
import com.bang_ggood.checklist.dto.request.ChecklistInfo;
import com.bang_ggood.checklist.dto.request.QuestionCreateRequest;
import com.bang_ggood.checklist.dto.response.BadgeResponse;
import com.bang_ggood.checklist.dto.response.CategoryScoreReadResponse;
import com.bang_ggood.checklist.dto.response.ChecklistQuestionsResponse;
import com.bang_ggood.checklist.dto.response.ChecklistWithScoreReadResponse;
import com.bang_ggood.checklist.dto.response.ChecklistsWithScoreReadResponse;
import com.bang_ggood.checklist.dto.response.QuestionResponse;
import com.bang_ggood.checklist.dto.response.UserChecklistPreviewResponse;
import com.bang_ggood.checklist.dto.response.UserChecklistsPreviewResponse;
import com.bang_ggood.checklist.dto.response.WrittenChecklistResponse;
import com.bang_ggood.checklist.dto.response.WrittenQuestionResponse;
import com.bang_ggood.checklist.repository.ChecklistOptionRepository;
Expand All @@ -29,6 +37,7 @@
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -108,7 +117,8 @@ private void createChecklistQuestions(ChecklistCreateRequest checklistCreateRequ
.map(question -> {
int questionId = question.getId();
return Optional.ofNullable(existQuestions.get(questionId))
.map(answer -> new ChecklistQuestion(checklist, Question.findById(questionId), Grade.from(answer)))
.map(answer -> new ChecklistQuestion(checklist, Question.findById(questionId),
Grade.from(answer)))
.orElseGet(() -> new ChecklistQuestion(checklist, Question.findById(questionId), null));
})
.collect(Collectors.toList());
Expand Down Expand Up @@ -187,4 +197,79 @@ private WrittenCategoryQuestionsResponse readQuestionsByCategory(Category catego

return WrittenCategoryQuestionsResponse.of(category, writtenQuestionResponses);
}

@Transactional
public UserChecklistsPreviewResponse readUserChecklistsPreview(User user) {
List<Checklist> checklists = checklistRepository.findByUser(user);
List<UserChecklistPreviewResponse> responses = checklists.stream()
.map(this::getChecklistPreview)
.toList();

return new UserChecklistsPreviewResponse(responses);
}

private UserChecklistPreviewResponse getChecklistPreview(Checklist checklist) {
return UserChecklistPreviewResponse.of(checklist, createBadges(checklist.getQuestions()));
}

private List<BadgeResponse> createBadges(List<ChecklistQuestion> questions) {
return Arrays.stream(Category.values())
.map(category -> category.provideBadge(questions))
.filter(badge -> badge != Badge.NONE)
.map(BadgeResponse::from)
.toList();
}

@Transactional
public ChecklistsWithScoreReadResponse readChecklistsComparison(List<Long> checklistIds) {
User user = new User(1L, "방끗");

validateChecklistComparison(checklistIds);

List<ChecklistWithScoreReadResponse> responses = checklistRepository.findByUserAndIdIn(user, checklistIds)
.stream()
.map(this::getChecklistWithScore)
.sorted(Comparator.comparing(ChecklistWithScoreReadResponse::score).reversed())
.toList();

return new ChecklistsWithScoreReadResponse(responses);
}

private void validateChecklistComparison(List<Long> checklistIds) {
validateChecklistComparisonCount(checklistIds);
validateChecklist(checklistIds);
}

private void validateChecklistComparisonCount(List<Long> checklistIds) {
if (checklistIds.size() > 3) {
throw new BangggoodException(ExceptionCode.CHECKLIST_COMPARISON_INVALID_COUNT);
}
}

private void validateChecklist(List<Long> checklistIds) {
if (checklistRepository.countAllByIdIn(checklistIds) != checklistIds.size()) {
throw new BangggoodException(ExceptionCode.CHECKLIST_NOT_FOUND);
}
}


private ChecklistWithScoreReadResponse getChecklistWithScore(Checklist checklist) {
List<CategoryScoreReadResponse> categoryScores = getCategoryScores(checklist.getQuestions());
int checklistScore = getChecklistScore(checklist.getQuestions());
int checklistOptionCount = checklistOptionRepository.countByChecklist(checklist);

return ChecklistWithScoreReadResponse.of(checklist, checklistOptionCount, checklistScore, categoryScores);
}

private List<CategoryScoreReadResponse> getCategoryScores(List<ChecklistQuestion> questions) {
return Arrays.stream(Category.values())
.map(category -> CategoryScoreReadResponse.of(category,
ChecklistScore.calculateCategoryScore(category, questions)))
.filter(response -> response.score() != 0)
.toList();
}

private int getChecklistScore(List<ChecklistQuestion> questions) {
return ChecklistScore.calculateTotalScore(questions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ public enum ExceptionCode {
CATEGORY_PRIORITY_INVALID_COUNT(HttpStatus.BAD_REQUEST, "카테고리 개수가 유효하지 않습니다."),
CATEGORY_NOT_FOUND(HttpStatus.BAD_REQUEST, "카테코리가 존재하지 않습니다."),
CATEGORY_DUPLICATED(HttpStatus.BAD_REQUEST, "중복된 카테고리가 존재합니다."),
GRADE_INVALID(HttpStatus.BAD_REQUEST, "점수가 유효하지 않습니다."),

// Checklist
CHECKLIST_COMPARISON_INVALID_COUNT(HttpStatus.BAD_REQUEST, "비교할 체크리스트 개수가 유효하지 않습니다."),
CHECKLIST_NOT_FOUND(HttpStatus.BAD_REQUEST, "체크리스트가 존재하지 않습니다."),
GRADE_INVALID(HttpStatus.BAD_REQUEST, "점수가 유효하지 않습니다.")
;

private final HttpStatus httpStatus;
Expand Down
1 change: 0 additions & 1 deletion backend/bang-ggood/src/main/resources/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ DROP TABLE IF EXISTS checklist_option CASCADE;
DROP TABLE IF EXISTS checklist_question CASCADE;
DROP TABLE IF EXISTS room CASCADE;
DROP TABLE IF EXISTS users CASCADE;
DROP TABLE IF EXISTS test_entity CASCADE;

-- Create tables
CREATE TABLE room
Expand Down
Loading

0 comments on commit ea3a056

Please sign in to comment.