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를 리팩토링한다. #167

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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.bang_ggood.category.dto;
package com.bang_ggood.category.dto.request;

import java.util.List;

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

import java.util.List;

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

import com.bang_ggood.checklist.dto.response.QuestionResponse;

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

import com.bang_ggood.category.domain.Category;

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

import com.bang_ggood.category.domain.Category;
import com.bang_ggood.checklist.dto.response.WrittenQuestionResponse;

import java.util.List;

public record WrittenCategoryQuestionsResponse(Integer categoryId, String categoryName,
List<WrittenQuestionResponse> questions) {

public static WrittenCategoryQuestionsResponse of(Category category, List<WrittenQuestionResponse> questions) {
return new WrittenCategoryQuestionsResponse(
category.getId(),
category.getName(),
questions
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.bang_ggood.checklist.controller;

import com.bang_ggood.checklist.dto.response.WrittenChecklistResponse;
import com.bang_ggood.checklist.service.ChecklistService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ChecklistController {

private final ChecklistService checklistService;

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

@GetMapping("/checklists/{id}")
public ResponseEntity<WrittenChecklistResponse> readChecklistById(@PathVariable long id) {
return ResponseEntity.ok(checklistService.readChecklistById(id));
}
}

This file was deleted.

This file was deleted.

This file was deleted.

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

import com.bang_ggood.room.domain.Room;
import com.bang_ggood.room.dto.RoomCreateRequest;
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,4 +1,4 @@
package com.bang_ggood.checklist.dto;
package com.bang_ggood.checklist.dto.request;

public record ChecklistInfo(Integer deposit, Integer rent, Integer contractTerm, String realEstate) {
}
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
@@ -1,4 +1,4 @@
package com.bang_ggood.checklist.dto;
package com.bang_ggood.checklist.dto.response;

public record CategoryScoreReadResponse(
Integer categoryId, String categoryName, Integer score
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.bang_ggood.checklist.dto.response;

import com.bang_ggood.category.dto.response.CategoryQuestionsResponse;

import java.util.List;

public record ChecklistQuestionsResponse(List<CategoryQuestionsResponse> categories) {
}
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 java.util.List;

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 QuestionResponse(Integer questionId, String title, String subtitle) {
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bang_ggood.checklist.dto;
package com.bang_ggood.checklist.dto.response;

import com.bang_ggood.checklist.domain.Checklist;

import java.time.LocalDateTime;
import java.util.List;

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 java.util.List;

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

import com.bang_ggood.category.dto.response.WrittenCategoryQuestionsResponse;
import com.bang_ggood.room.dto.response.WrittenRoomResponse;

import java.util.List;

public record WrittenChecklistResponse(WrittenRoomResponse room, List<Integer> options,
List<WrittenCategoryQuestionsResponse> categories) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.bang_ggood.checklist.dto.response;

import com.bang_ggood.checklist.domain.ChecklistQuestion;

public record WrittenQuestionResponse(Integer questionId, String title, String subtitle, String answer) {

public static WrittenQuestionResponse of(ChecklistQuestion checklistQuestion) {
return new WrittenQuestionResponse(
checklistQuestion.getQuestion().getId(),
checklistQuestion.getQuestion().getTitle(),
checklistQuestion.getQuestion().getSubtitle(),
checklistQuestion.getGrade().name()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public interface ChecklistRepository extends JpaRepository<Checklist, Long> {
Optional<Checklist> findById(long id);

default Checklist getById(long id) {
return findById(id).orElseThrow(() -> new BangggoodException(ExceptionCode.USER_NOT_FOUND));
return findById(id).orElseThrow(() -> new BangggoodException(ExceptionCode.CHECKLIST_NOT_FOUND));
}

List<Checklist> findByUser(User user);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.bang_ggood.checklist.service;

import com.bang_ggood.category.domain.Category;
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.Question;
import com.bang_ggood.checklist.dto.response.WrittenChecklistResponse;
import com.bang_ggood.checklist.dto.response.WrittenQuestionResponse;
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.room.dto.response.WrittenRoomResponse;
import com.bang_ggood.room.repository.RoomRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@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 WrittenChecklistResponse readChecklistById(long id) {
Checklist checklist = checklistRepository.getById(id);
WrittenRoomResponse writtenRoomResponse = WrittenRoomResponse.of(checklist);

List<Integer> optionIds = readOptionsByChecklistId(id);

List<WrittenCategoryQuestionsResponse> writtenCategoryQuestionsResponses =
readCategoryQuestionsByChecklistId(id);

return new WrittenChecklistResponse(writtenRoomResponse, optionIds, writtenCategoryQuestionsResponses);
}

private List<Integer> readOptionsByChecklistId(long checklistId) {
return checklistOptionRepository.findByChecklistId(checklistId)
.stream()
.map(ChecklistOption::getOptionId)
.toList();
}

private List<WrittenCategoryQuestionsResponse> readCategoryQuestionsByChecklistId(long checklistId) {
List<ChecklistQuestion> checklistQuestions = checklistQuestionRepository.findByChecklistId(checklistId);
return Arrays.stream(Category.values())
.map(category -> readQuestionsByCategory(category, checklistQuestions))
.collect(Collectors.toList());
}

private WrittenCategoryQuestionsResponse readQuestionsByCategory(Category category,
List<ChecklistQuestion> checklistQuestions) {
List<WrittenQuestionResponse> writtenQuestionResponses =
Question.filter(category, checklistQuestions).stream()
.map(WrittenQuestionResponse::of)
.collect(Collectors.toList());

return WrittenCategoryQuestionsResponse.of(category, writtenQuestionResponses);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.bang_ggood.room.dto;
package com.bang_ggood.room.dto.request;

import com.bang_ggood.room.domain.Room;
import jakarta.validation.constraints.NotNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.bang_ggood.room.dto.response;

import com.bang_ggood.checklist.domain.Checklist;

public record WrittenRoomResponse(String name, Integer deposit, Integer rent, Integer contractTerm, Integer floor,
String address, String station, Integer walkingTime, String realEstate) {

public static WrittenRoomResponse of(Checklist checklist) {
return new WrittenRoomResponse(checklist.getRoomName(), checklist.getDeposit(), checklist.getRent(),
checklist.getContractTerm(), checklist.getRoomFloor(), checklist.getRoomAddress(),
checklist.getRoomStation(), checklist.getRoomWalkingTime(), checklist.getRealEstate());
}
}
20 changes: 15 additions & 5 deletions backend/bang-ggood/src/main/resources/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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 Expand Up @@ -54,16 +55,15 @@ CREATE TABLE checklist_option
CREATE TABLE checklist_question
(
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
question_id INTEGER NOT NULL,
question VARCHAR(255) 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)
);


CREATE TABLE category_priority
CREATE TABLE if not exists category_priority
(
id bigint generated by default as identity,
category_id INTEGER not null,
Expand All @@ -72,4 +72,14 @@ CREATE TABLE category_priority
modified_at TIMESTAMP not null,
primary key (id),
foreign key (user_id) references users
);
);


CREATE TABLE test_entity
(
id bigint generated by default as identity,
name varchar(255) not null,
created_at TIMESTAMP not null,
modified_at TIMESTAMP not null,
primary key (id)
);
Loading
Loading