Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] 체크리스트리스트 API, 방 비교 API 리팩토링 반영 #170

Merged
merged 2 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.bang_ggood.category.dto;

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

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

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

public record WrittenCategoryQuestionsResponse(Integer categoryId, String categoryName, List<WrittenQuestionResponse> questions) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.bang_ggood.checklist.controller;

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 {

private final ChecklistService checklistService;

public ChecklistController(ChecklistService checklistService) {
this.checklistService = checklistService;
}

@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));
}
}

This file was deleted.

This file was deleted.

This file was deleted.

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

import com.bang_ggood.checklist.dto.ChecklistInfo;
import com.bang_ggood.room.domain.Room;
import com.bang_ggood.room.dto.RoomCreateRequest;
import jakarta.validation.Valid;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.bang_ggood.checklist.dto;
package com.bang_ggood.checklist.dto.request;

import jakarta.validation.constraints.NotNull;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.bang_ggood.checklist.dto;
package com.bang_ggood.checklist.dto.response;

import com.bang_ggood.category.domain.Badge;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +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
@@ -1,4 +1,4 @@
package com.bang_ggood.checklist.dto;
package com.bang_ggood.checklist.dto.response;

import com.bang_ggood.category.dto.CategoryQuestionsResponse;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package com.bang_ggood.checklist.dto;
package com.bang_ggood.checklist.dto.response;

import com.bang_ggood.checklist.domain.Checklist;
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
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,4 +1,4 @@
package com.bang_ggood.checklist.dto;
package com.bang_ggood.checklist.dto.response;

public record QuestionResponse(Integer questionId, String title, String subtitle) {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.bang_ggood.checklist.dto;
package com.bang_ggood.checklist.dto.response;

import com.bang_ggood.checklist.domain.Checklist;
import java.time.LocalDateTime;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
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) {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.bang_ggood.checklist.dto;
package com.bang_ggood.checklist.dto.response;

import com.bang_ggood.category.dto.WrittenCategoryQuestionsResponse;
import com.bang_ggood.room.dto.WrittenRoomResponse;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.bang_ggood.checklist.dto;
package com.bang_ggood.checklist.dto.response;

public record WrittenQuestionResponse(Integer questionId, String title, String subtitle, String answer) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
import com.bang_ggood.exception.BangggoodException;
import com.bang_ggood.exception.ExceptionCode;
import com.bang_ggood.user.domain.User;

import java.util.Optional;
import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
import java.util.Optional;

public interface ChecklistRepository extends JpaRepository<Checklist, Long> {

Expand All @@ -27,4 +25,6 @@ default Checklist getById(long id) {
List<Checklist> findByUser(User user);

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

long countAllByIdIn(List<Long> ids);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

체크리스트 아이디가 존재하는지 확인하기 위해 사용되는 메서드인데요,
existsAllByIdIn 을 사용하고 싶었지만 동작하지 않더라구요 🥲
더 좋은 방식이 있다면 알려주세요~

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package com.bang_ggood.checklist.service;

import com.bang_ggood.category.domain.Badge;
import com.bang_ggood.category.domain.Category;
import com.bang_ggood.checklist.domain.Checklist;
import com.bang_ggood.checklist.domain.ChecklistQuestion;
import com.bang_ggood.checklist.domain.ChecklistScore;
import com.bang_ggood.checklist.dto.response.BadgeResponse;
import com.bang_ggood.checklist.dto.response.CategoryScoreReadResponse;
import com.bang_ggood.checklist.dto.response.ChecklistWithScoreReadResponse;
import com.bang_ggood.checklist.dto.response.ChecklistsWithScoreReadResponse;
import com.bang_ggood.checklist.dto.response.UserChecklistPreviewResponse;
import com.bang_ggood.checklist.dto.response.UserChecklistsPreviewResponse;
import com.bang_ggood.checklist.repository.ChecklistOptionRepository;
import com.bang_ggood.checklist.repository.ChecklistQuestionRepository;
import com.bang_ggood.checklist.repository.ChecklistRepository;
import com.bang_ggood.exception.BangggoodException;
import com.bang_ggood.exception.ExceptionCode;
import com.bang_ggood.room.repository.RoomRepository;
import com.bang_ggood.user.domain.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

@Service
public class ChecklistService {

private final ChecklistRepository checklistRepository;
private final RoomRepository roomRepository;
private final ChecklistOptionRepository checklistOptionRepository;
private final ChecklistQuestionRepository checklistQuestionRepository;

public ChecklistService(ChecklistRepository checklistRepository, RoomRepository roomRepository,
ChecklistOptionRepository checklistOptionRepository,
ChecklistQuestionRepository checklistQuestionRepository) {
this.checklistRepository = checklistRepository;
this.roomRepository = roomRepository;
this.checklistOptionRepository = checklistOptionRepository;
this.checklistQuestionRepository = checklistQuestionRepository;
}

@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
4 changes: 2 additions & 2 deletions backend/bang-ggood/src/main/resources/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ CREATE TABLE checklist_option
CREATE TABLE checklist_question
(
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
question_id INTEGER NOT NULL,
question VARCHAR(22) NOT NULL,
checklist_id BIGINT NOT NULL,
created_at TIMESTAMP(6),
modified_at TIMESTAMP(6),
answer VARCHAR(255),
grade VARCHAR(255),
FOREIGN KEY (checklist_id) REFERENCES checklist (id)
);

Expand Down
Loading
Loading