-
Notifications
You must be signed in to change notification settings - Fork 6
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
backend/bang-ggood/src/main/java/com/bang_ggood/category/dto/CategoryQuestionsResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ang-ggood/src/main/java/com/bang_ggood/category/dto/WrittenCategoryQuestionsResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...end/bang-ggood/src/main/java/com/bang_ggood/checklist/controller/ChecklistController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
6 changes: 0 additions & 6 deletions
6
backend/bang-ggood/src/main/java/com/bang_ggood/checklist/dto/CategoryScoreReadResponse.java
This file was deleted.
Oops, something went wrong.
6 changes: 0 additions & 6 deletions
6
...ng-ggood/src/main/java/com/bang_ggood/checklist/dto/ChecklistsComparisonReadResponse.java
This file was deleted.
Oops, something went wrong.
6 changes: 0 additions & 6 deletions
6
.../bang-ggood/src/main/java/com/bang_ggood/checklist/dto/UserChecklistsPreviewResponse.java
This file was deleted.
Oops, something went wrong.
3 changes: 2 additions & 1 deletion
3
...checklist/dto/ChecklistCreateRequest.java → ...t/dto/request/ChecklistCreateRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../checklist/dto/QuestionCreateRequest.java → ...st/dto/request/QuestionCreateRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ng_ggood/checklist/dto/BadgeResponse.java → ...checklist/dto/response/BadgeResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...-ggood/src/main/java/com/bang_ggood/checklist/dto/response/CategoryScoreReadResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...klist/dto/ChecklistQuestionsResponse.java → .../response/ChecklistQuestionsResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
.../dto/ChecklistComparisonReadResponse.java → ...ponse/ChecklistWithScoreReadResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
.../src/main/java/com/bang_ggood/checklist/dto/response/ChecklistsWithScoreReadResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} |
2 changes: 1 addition & 1 deletion
2
...ggood/checklist/dto/QuestionResponse.java → ...cklist/dto/response/QuestionResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} |
2 changes: 1 addition & 1 deletion
2
...ist/dto/UserChecklistPreviewResponse.java → ...esponse/UserChecklistPreviewResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...od/src/main/java/com/bang_ggood/checklist/dto/response/UserChecklistsPreviewResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} |
2 changes: 1 addition & 1 deletion
2
...ecklist/dto/WrittenChecklistResponse.java → ...to/response/WrittenChecklistResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...hecklist/dto/WrittenQuestionResponse.java → ...dto/response/WrittenQuestionResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
backend/bang-ggood/src/main/java/com/bang_ggood/checklist/service/ChecklistService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
체크리스트 아이디가 존재하는지 확인하기 위해 사용되는 메서드인데요,
existsAllByIdIn 을 사용하고 싶었지만 동작하지 않더라구요 🥲
더 좋은 방식이 있다면 알려주세요~