Skip to content

Commit

Permalink
체크리스트 질문 조회 API를 구현한다. (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
JINU-CHANG authored Jul 25, 2024
2 parents 3b5c9cd + a1e26f1 commit c93534a
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
package com.bang_ggood.category.domain;

import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;

public enum Category {

CLEAN(1, "청결", Badge.CLEAN),
ROOM_CONDITION(2, "방 컨디션", Badge.ROOM_CONDITION),
AMENITY(3, "편의시설", Badge.AMENITY),
OPTION(4, "옵션", Badge.OPTION),
ENVIRONMENT(5, "주거환경", Badge.ENVIRONMENT),
SECURITY(6, "보안", Badge.SECURITY),
ECONOMIC(7, "경제적", Badge.ECONOMIC);
CLEAN(1, "청결", Badge.CLEAN, new LinkedHashSet<>(Set.of(1, 2, 3, 4, 5))),
ROOM_CONDITION(2, "방 컨디션", Badge.ROOM_CONDITION, new LinkedHashSet<>(Set.of(6, 7, 8, 9, 10, 11))),
AMENITY(3, "편의시설", Badge.AMENITY, new LinkedHashSet<>(Set.of(12, 13, 14))),
OPTION(4, "옵션", Badge.OPTION, new LinkedHashSet<>(Set.of(15, 16))),
ENVIRONMENT(5, "주거환경", Badge.ENVIRONMENT, new LinkedHashSet<>(Set.of(17, 18, 19, 20, 21, 22))),
SECURITY(6, "보안", Badge.SECURITY, new LinkedHashSet<>(Set.of(22, 23, 24, 25, 26, 27, 28, 29, 30))),
ECONOMIC(7, "경제적", Badge.ECONOMIC, new LinkedHashSet<>(Set.of(31, 32)));

private final int id;
private final String description;
private final Badge badge;
private final Set<Integer> questionIds;

Category(int id, String description, Badge badge) {
Category(int id, String description, Badge badge, Set<Integer> questionIds) {
this.id = id;
this.description = description;
this.badge = badge;
this.questionIds = questionIds;
}

public static boolean contains(int id) {
Expand All @@ -34,4 +38,8 @@ public int getId() {
public String getDescription() {
return description;
}

public Set<Integer> getQuestionIds() {
return questionIds;
}
}
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.QuestionResponse;
import java.util.List;

public record CategoryQuestionsResponse(Integer id, String categoryName, List<QuestionResponse> questions) {
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.bang_ggood.checklist.controller;

import com.bang_ggood.checklist.dto.ChecklistCreateRequest;
import com.bang_ggood.checklist.dto.ChecklistQuestionsResponse;
import com.bang_ggood.checklist.service.ChecklistService;
import jakarta.validation.Valid;
import java.net.URI;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -23,4 +25,9 @@ public ResponseEntity<Void> createChecklist(@Valid @RequestBody ChecklistCreateR
long checklistId = checklistService.createChecklist(checklistCreateRequest);
return ResponseEntity.created(URI.create("/checklists/" + checklistId)).build();
}

@GetMapping("/checklists/questions")
public ResponseEntity<ChecklistQuestionsResponse> readChecklistQuestions() {
return ResponseEntity.ok(checklistService.readChecklistQuestions());
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.bang_ggood.checklist.domain;

import com.bang_ggood.exception.BangggoodException;
import com.bang_ggood.exception.ExceptionCode;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Component;

@Component
public class Questionlist {

public Map<Integer, Question> questions;
private Map<Integer, Question> questions;

public Questionlist() {
questions = new HashMap<>();
Expand Down Expand Up @@ -76,4 +78,12 @@ private void initEconomic() {
questions.put(31, new Question("보증금/전월세 비용이 합리적인가요?", "관리비도 포함하여 고려하세요."));
questions.put(32, new Question("교통 비용(지하철, 버스, 자가용)이 추가적으로 들지 않나요?", "주차 비용을 확인하세요."));
}

public String getTitleByQuestionId(int questionId) {
return questions.get(questionId).getTitle();
}

public String getSubtitleByQuestionId(int questionId) {
return questions.get(questionId).getSubtitle();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.bang_ggood.checklist.dto;

import com.bang_ggood.category.dto.CategoryQuestionsResponse;
import java.util.List;

public record ChecklistQuestionsResponse(List<CategoryQuestionsResponse> categories) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.bang_ggood.checklist.dto;

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

import com.bang_ggood.category.domain.Category;
import com.bang_ggood.category.dto.CategoryQuestionsResponse;
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.Option;
import com.bang_ggood.checklist.domain.Questionlist;
import com.bang_ggood.checklist.dto.ChecklistCreateRequest;
import com.bang_ggood.checklist.dto.ChecklistInfo;
import com.bang_ggood.checklist.dto.ChecklistQuestionsResponse;
import com.bang_ggood.checklist.dto.QuestionCreateRequest;
import com.bang_ggood.checklist.dto.QuestionResponse;
import com.bang_ggood.checklist.repository.ChecklistOptionRepository;
import com.bang_ggood.checklist.repository.ChecklistQuestionRepository;
import com.bang_ggood.checklist.repository.ChecklistRepository;
Expand All @@ -16,6 +20,7 @@
import com.bang_ggood.room.domain.Room;
import com.bang_ggood.room.repository.RoomRepository;
import com.bang_ggood.user.domain.User;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -119,4 +124,24 @@ private void validateQuestionInvalid(List<QuestionCreateRequest> questions) {
}
}
}

public ChecklistQuestionsResponse readChecklistQuestions() {
List<CategoryQuestionsResponse> categoryQuestionsResponses = new ArrayList<>();
for (Category category : Category.values()) {
CategoryQuestionsResponse categoryQuestionsResponse =
new CategoryQuestionsResponse(category.getId(), category.getDescription(), readChecklistQuestion(category));
categoryQuestionsResponses.add(categoryQuestionsResponse);
}
return new ChecklistQuestionsResponse(categoryQuestionsResponses);
}

private List<QuestionResponse> readChecklistQuestion(Category category) {
List<QuestionResponse> questionResponses = new ArrayList<>();
category.getQuestionIds().stream()
.map(questionId -> new QuestionResponse(questionId,
questionList.getTitleByQuestionId(questionId),
questionList.getSubtitleByQuestionId(questionId)))
.forEach(questionResponses::add);
return questionResponses;
}
}
2 changes: 1 addition & 1 deletion backend/bang-ggood/src/main/resources/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ CREATE TABLE checklist_question
CREATE TABLE category_priority
(
id bigint generated by default as identity,
category_id tinyint not null,
category_id INTEGER not null,
user_id bigint not null,
created_at TIMESTAMP not null,
modified_at TIMESTAMP not null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.bang_ggood.checklist.controller;

import static org.assertj.core.api.Assertions.assertThat;

import com.bang_ggood.AcceptanceTest;
import com.bang_ggood.category.domain.Category;
import com.bang_ggood.checklist.ChecklistFixture;
import com.bang_ggood.checklist.dto.ChecklistQuestionsResponse;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import org.junit.jupiter.api.DisplayName;
Expand Down Expand Up @@ -41,4 +45,18 @@ void createChecklist_noQuestionId_exception() {
.then().log().all()
.statusCode(400);
}

@DisplayName("체크리스트 질문 조회 성공")
@Test
void readChecklistQuestions() {
ChecklistQuestionsResponse checklistQuestionsResponse = RestAssured.given().log().all()
.contentType(ContentType.JSON)
.when().get("/checklists/questions")
.then().log().all()
.statusCode(200)
.extract()
.as(ChecklistQuestionsResponse.class);

assertThat(checklistQuestionsResponse.categories().size()).isEqualTo(Category.values().length);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,18 @@ void contains_true() {
void contains_false() {
assertThat(questionList.contains(9999)).isFalse();
}

@DisplayName("질문 제목 조회 성공")
@Test
void getTitle() {
assertThat(questionList.getTitleByQuestionId(1))
.isEqualTo("방 안에 물이 새거나 곰팡이가 핀 곳은 없나요?");
}

@DisplayName("질문 부제목 조회 성공")
@Test
void getSubtitle() {
assertThat(questionList.getSubtitleByQuestionId(1))
.isEqualTo("천장, 벽면, 가구 뒤, 장판을 확인하세요.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.bang_ggood.IntegrationTestSupport;
import com.bang_ggood.category.domain.Category;
import com.bang_ggood.checklist.ChecklistFixture;
import com.bang_ggood.checklist.dto.ChecklistQuestionsResponse;
import com.bang_ggood.exception.BangggoodException;
import com.bang_ggood.exception.ExceptionCode;
import org.junit.jupiter.api.DisplayName;
Expand Down Expand Up @@ -66,4 +68,14 @@ void createChecklist_duplicatedOptionId_exception() {
.isInstanceOf(BangggoodException.class)
.hasMessage(ExceptionCode.OPTION_DUPLICATED.getMessage());
}

@DisplayName("체크리스트 질문 조회 성공")
@Test
void readChecklistQuestions() {
// given & when
ChecklistQuestionsResponse checklistQuestionsResponse = checklistService.readChecklistQuestions();

// then
assertThat(checklistQuestionsResponse.categories().size()).isEqualTo(Category.values().length);
}
}
2 changes: 1 addition & 1 deletion backend/bang-ggood/src/test/resources/schema-test.sql
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ CREATE TABLE checklist_question
CREATE TABLE if not exists category_priority
(
id bigint generated by default as identity,
category_id tinyint not null,
category_id INTEGER not null,
user_id bigint not null,
created_at TIMESTAMP not null,
modified_at TIMESTAMP not null,
Expand Down

0 comments on commit c93534a

Please sign in to comment.