Skip to content

Commit

Permalink
방 비교 결과 조회 API를 만든다. (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
tkdgur0906 authored Jul 25, 2024
2 parents c93534a + 943753e commit 32ee0d9
Show file tree
Hide file tree
Showing 16 changed files with 343 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public enum Badge {
SECURITY("보안", "안전해요", "🔒"),
ECONOMIC("경제적", "경제적이에요", "💰")
;


private static final String DESCRIPTION_FORMAT = "%s %s";
private final String shortDescription;
private final String longDescription;
private final String emoji;
Expand All @@ -22,10 +23,10 @@ public enum Badge {
}

public String getShortDescriptionWithEmoji() {
return this.emoji + this.shortDescription;
return String.format(DESCRIPTION_FORMAT, this.emoji, this.shortDescription);
}

public String getLongDescriptionWithEmoji() {
return this.emoji + this.longDescription;
return String.format(DESCRIPTION_FORMAT, this.emoji, this.longDescription);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.bang_ggood.category.domain;

import com.bang_ggood.checklist.domain.ChecklistQuestion;
import com.bang_ggood.checklist.domain.Grade;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public enum Category {
Expand Down Expand Up @@ -31,13 +34,45 @@ public static boolean contains(int id) {
.anyMatch(category -> category.id == id);
}

// 2. 뱃지 부여
public static List<Badge> getBadges(List<ChecklistQuestion> questions) {
return Arrays.stream(values())
.filter(category -> category.calculateTotalScore(questions) >= 80)
.map(Category::getBadge)
.toList();
}

// 1. 총점 : score * 100 / maxScore
public int calculateTotalScore(List<ChecklistQuestion> questions) {
List<ChecklistQuestion> filteredQuestions = filterQuestion(questions);

if (filteredQuestions.isEmpty()) {
return 0;
}

int maxScore = Grade.calculateMaxScore(filteredQuestions.size());
int score = filteredQuestions.stream()
.mapToInt(question -> Grade.getScore(question.getAnswer()))
.sum();

return score * 100 / maxScore;
}

private List<ChecklistQuestion> filterQuestion(List<ChecklistQuestion> questions) {
return questions.stream()
.filter(checklistQuestion -> questionIds.contains(checklistQuestion.getQuestionId()))
.toList();
}

public int getId() {
return id;
}

public String getDescription() {
return description;
}

public Badge getBadge() { return badge; }

public Set<Integer> getQuestionIds() {
return questionIds;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

import com.bang_ggood.checklist.dto.ChecklistCreateRequest;
import com.bang_ggood.checklist.dto.ChecklistQuestionsResponse;
import com.bang_ggood.checklist.dto.ChecklistsComparisonReadResponse;
import com.bang_ggood.checklist.dto.UserChecklistsPreviewResponse;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.net.URI;
import java.util.List;

@RestController
public class ChecklistController {
Expand All @@ -26,8 +30,18 @@ public ResponseEntity<Void> createChecklist(@Valid @RequestBody ChecklistCreateR
return ResponseEntity.created(URI.create("/checklists/" + checklistId)).build();
}

@GetMapping("/checklists")
public ResponseEntity<UserChecklistsPreviewResponse> readUserChecklistsPreview() {
return ResponseEntity.ok(checklistService.readUserChecklistsPreview());
}

@GetMapping("/checklists/questions")
public ResponseEntity<ChecklistQuestionsResponse> readChecklistQuestions() {
return ResponseEntity.ok(checklistService.readChecklistQuestions());
}

@GetMapping("/checklists/comparison")
public ResponseEntity<ChecklistsComparisonReadResponse> readChecklistsComparison(@RequestParam("id")List<Long> checklistIds) {
return ResponseEntity.ok(checklistService.readChecklistsComparison(checklistIds));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne;
import java.util.List;
import java.util.Objects;


Expand All @@ -34,6 +36,9 @@ public class Checklist extends BaseEntity {

private String realEstate;

@OneToMany(mappedBy = "checklist")
private List<ChecklistQuestion> questions;

public Checklist(User user, Room room, Integer deposit, Integer rent, Integer contractTerm, String realEstate) {
this.user = user;
this.room = room;
Expand All @@ -58,10 +63,20 @@ public User getUser() {
return user;
}

public Room getRoom() {
return room;
public String getRoomName() {
return room.getName();
}

public String getRoomAddress() {
return room.getAddress();
}

public Integer getRoomFloor() { return room.getFloor(); }

public String getRoomStation() { return room.getStation(); }

public Integer getRoomWalkingTime() { return room.getWalkingTime(); }

public Integer getDeposit() {
return deposit;
}
Expand All @@ -78,6 +93,10 @@ public String getRealEstate() {
return realEstate;
}

public List<ChecklistQuestion> getQuestions() {
return questions;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
package com.bang_ggood.checklist.domain;

import java.util.Arrays;

public enum Grade {

GOOD(3),
SOSO(2),
BAD(1);

private final int score;

Grade(int score) {
this.score = score;
}

public static int calculateMaxScore(int size) {
return GOOD.score * size;
}

public static int getScore(String answer) {
return Arrays.stream(values())
.filter(grade -> grade.name().equals(answer))
.findAny()
.get()
.score;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.bang_ggood.checklist.dto;

import com.bang_ggood.category.domain.Badge;

public record BadgeResponse(String shortName, String longName) {

public static BadgeResponse from(Badge badge) {
return new BadgeResponse(badge.getShortDescriptionWithEmoji(), badge.getLongDescriptionWithEmoji());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.bang_ggood.checklist.dto;

public record CategoryScoreReadResponse(
Integer categoryId, String categoryName, Integer score
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.bang_ggood.checklist.dto;

import com.bang_ggood.checklist.domain.Checklist;
import java.util.List;

public record ChecklistComparisonReadResponse(
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(
checklist.getId(),
checklist.getRoomName(),
checklist.getRoomAddress(),
checklist.getRoomFloor(),
checklist.getDeposit(),
checklist.getRent(),
checklist.getContractTerm(),
checklist.getRoomStation(),
checklist.getRoomWalkingTime(),
checklistOptionCount,
checklistScore,
categoryScores
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.bang_ggood.checklist.dto;

import java.util.List;

public record ChecklistsComparisonReadResponse(List<ChecklistComparisonReadResponse> checklists) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.bang_ggood.checklist.dto;

import com.bang_ggood.checklist.domain.Checklist;
import java.time.LocalDateTime;
import java.util.List;

public record UserChecklistPreviewResponse(
Long checklistId, String roomName, String address,
Integer deposit, Integer rent, LocalDateTime createdAt,
List<BadgeResponse> badge) {

public static UserChecklistPreviewResponse of(Checklist checklist, List<BadgeResponse> badges) {
return new UserChecklistPreviewResponse(
checklist.getId(),
checklist.getRoomName(),
checklist.getRoomAddress(),
checklist.getDeposit(),
checklist.getRent(),
checklist.getCreatedAt(),
badges);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.bang_ggood.checklist.dto;

import java.util.List;

public record UserChecklistsPreviewResponse(List<UserChecklistPreviewResponse> checklists) {
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.bang_ggood.checklist.repository;

import com.bang_ggood.checklist.domain.Checklist;
import com.bang_ggood.checklist.domain.ChecklistOption;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ChecklistOptionRepository extends JpaRepository<ChecklistOption, Long> {

Integer countByChecklist(Checklist checklist);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.bang_ggood.checklist.repository;

import com.bang_ggood.checklist.domain.Checklist;
import com.bang_ggood.user.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;

public interface ChecklistRepository extends JpaRepository<Checklist, Long> {

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

0 comments on commit 32ee0d9

Please sign in to comment.