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에 기능을 추가 구현한다. #215

Merged
merged 19 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
f538567
feat: 체크리스트 점수에 따라 등수 구하는 메서드 구현
tkdgur0906 Aug 2, 2024
b36ff94
feat: 방 정보 response 형식 변경
tkdgur0906 Aug 2, 2024
e2466d9
feat: 방 비교 결과 조회 기능 랭킹 구하도록 추가 구현
tkdgur0906 Aug 2, 2024
e581a50
feat: Grade가 null일 경우 점수 계산 0점으로 처리하도록 구현
tkdgur0906 Aug 3, 2024
1c7e10b
feat: 방 비교 시 모든 카테고리에 대한 점수 반환하도록 기능 추가
tkdgur0906 Aug 3, 2024
51a8121
feat: 방 비교 시 필요한 db 조회 로직에 논리적 삭제 도입
tkdgur0906 Aug 3, 2024
0d15ffe
style: 코드 포맷팅
tkdgur0906 Aug 3, 2024
9e3d1ea
refactor: dto equals, hashcode, toString 삭제
tkdgur0906 Aug 4, 2024
3c4d63a
style: 메서드 위치 getter를 맨 아래로 이동
tkdgur0906 Aug 4, 2024
f247693
refactor: 방 비교하는 체크리스트가 유저의 것인지 검증하는 메서드 리팩토링
tkdgur0906 Aug 4, 2024
0be2c6b
refactor: 방 비교 랭킹 구하는 메서드 리팩토링
tkdgur0906 Aug 5, 2024
b18b3ae
test: 체크리스트 비교 테스트에 값 테스트 추가
tkdgur0906 Aug 5, 2024
0ef6035
feat: grade에 NONE 추가하여 null 처리 삭제
tkdgur0906 Aug 5, 2024
7378345
style: 에러 코드 컨벤션에 맞게 코드 정렬
tkdgur0906 Aug 5, 2024
483c7cb
refactor: 방 비교 메서드 검증 로직 기존 체크리스트 데이터로 수행하도록 변경
tkdgur0906 Aug 5, 2024
905a6cf
fix: 머지 충돌 해결
tkdgur0906 Aug 5, 2024
7178c39
test: 직접 쿼리 작성한 findByUserAndIdIn db 메서드 테스트
tkdgur0906 Aug 5, 2024
8abc291
feat: 방 비교 결과 조회 서비스 로직 인자로 user 받도록 추가
tkdgur0906 Aug 5, 2024
2ba0997
Merge remote-tracking branch 'refs/remotes/origin/dev-be' into feat/1…
tkdgur0906 Aug 5, 2024
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
Expand Up @@ -2,8 +2,6 @@

import com.bang_ggood.category.dto.request.CategoryPriorityCreateRequest;
import com.bang_ggood.category.dto.response.CategoriesReadResponse;
import com.bang_ggood.category.dto.response.CategoriesReadResponse;
import com.bang_ggood.category.dto.request.CategoryPriorityCreateRequest;
import com.bang_ggood.category.service.CategoryService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
import com.bang_ggood.category.dto.request.CategoryPriorityCreateRequest;
import com.bang_ggood.category.dto.response.CategoriesReadResponse;
import com.bang_ggood.category.dto.response.CategoryReadResponse;
import com.bang_ggood.category.dto.response.CategoriesReadResponse;
import com.bang_ggood.category.dto.request.CategoryPriorityCreateRequest;
import com.bang_ggood.category.dto.response.CategoryReadResponse;
import com.bang_ggood.category.repository.CategoryPriorityRepository;
import com.bang_ggood.exception.BangggoodException;
import com.bang_ggood.user.domain.User;
Expand Down Expand Up @@ -65,7 +62,9 @@ private void validateCategoryId(CategoryPriorityCreateRequest request) {
request.categoryIds().stream()
.filter(id -> !Category.contains(id))
.findAny()
.ifPresent(id -> { throw new BangggoodException(CATEGORY_NOT_FOUND); });
.ifPresent(id -> {
throw new BangggoodException(CATEGORY_NOT_FOUND);
});
}

public CategoriesReadResponse readCategories() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public ResponseEntity<UserChecklistsPreviewResponse> readUserChecklistsPreview()
@GetMapping("/checklists/comparison")
public ResponseEntity<ChecklistsWithScoreReadResponse> readChecklistsComparison(
@RequestParam("id") List<Long> checklistIds) {
return ResponseEntity.ok(checklistService.readChecklistsComparison(checklistIds));
User user = new User(1L, "방끗", "[email protected]");
return ResponseEntity.ok(checklistService.readChecklistsComparison(user, checklistIds));
}

@PutMapping("/checklists/{id}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.bang_ggood.checklist.domain;

import com.bang_ggood.BaseEntity;
import com.bang_ggood.room.domain.FloorLevel;
import com.bang_ggood.room.domain.Room;
import com.bang_ggood.room.domain.Structure;
import com.bang_ggood.room.domain.Type;
import com.bang_ggood.user.domain.User;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
Expand Down Expand Up @@ -95,6 +98,22 @@ public Integer getRoomWalkingTime() {
return room.getWalkingTime();
}

public Type getRoomType() {
return room.getType();
}

public Integer getRoomSize() {
return room.getSize();
}

public FloorLevel getRoomFloorLevel() {
return room.getFloorLevel();
}

public Structure getRoomStructure() {
return room.getStructure();
}

public Integer getDeposit() {
return deposit;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.bang_ggood.checklist.domain;

import java.util.List;

public class ChecklistRank {

private ChecklistRank() {
}

public static int calculateRanks(int targetScore, List<Integer> scores) {
return (int) scores.stream()
.filter(score -> score > targetScore)
.count() + 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ public enum Grade {
GOOD(3),
SOSO(2),
BAD(1),
NONE(0)
;
NONE(0);

private final int score;

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

import com.bang_ggood.checklist.domain.Checklist;
import com.bang_ggood.room.dto.response.SelectedRoomResponse;
import java.util.List;

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 ChecklistWithScoreReadResponse of(Checklist checklist, int checklistOptionCount, int checklistScore,
public class ChecklistWithScoreReadResponse {
Copy link
Contributor

Choose a reason for hiding this comment

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

record에서 class로 바꾼 이유가 있으신가요?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

객체 생성시 rank를 할당할 수가 없는데 Record를 사용할 경우 setter 사용이 불가했습니다.
rank 계산 후 값을 할당해주기 위해 class로 변경하였습니다!


private final Long checklistId;
private final Integer score;
private final SelectedRoomResponse room;
private final List<SelectedOptionResponse> options;
private final List<CategoryScoreReadResponse> categories;
private Integer rank;

public ChecklistWithScoreReadResponse(Long checklistId, Integer score,
SelectedRoomResponse room, List<SelectedOptionResponse> options,
List<CategoryScoreReadResponse> categories) {
this.checklistId = checklistId;
this.score = score;
this.room = room;
this.options = options;
this.categories = categories;
}

public static ChecklistWithScoreReadResponse of(Checklist checklist, int checklistScore,
SelectedRoomResponse room, List<SelectedOptionResponse> options,
List<CategoryScoreReadResponse> categoryScores) {
return new ChecklistWithScoreReadResponse(
checklist.getId(),
checklist.getRoomName(),
checklist.getRoomAddress(),
checklist.getRoomFloor(),
checklist.getDeposit(),
checklist.getRent(),
checklist.getContractTerm(),
checklist.getRoomStation(),
checklist.getRoomWalkingTime(),
checklistOptionCount,
checklistScore,
room,
options,
categoryScores
);
}

public void assignRank(int rank) {
this.rank = rank;
}

public Long getChecklistId() {
return checklistId;
}

public Integer getScore() {
return score;
}

public SelectedRoomResponse getRoom() {
return room;
}

public List<SelectedOptionResponse> getOptions() {
return options;
}

public List<CategoryScoreReadResponse> getCategories() {
return categories;
}

public Integer getRank() {
return rank;
}
tsulocalize marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.bang_ggood.checklist.dto.response;

import com.bang_ggood.checklist.domain.Checklist;

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

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ default Checklist getById(long id) {
//TODO: 논리적 삭제 리팩토링
List<Checklist> findByUser(User user);

//TODO: 논리적 삭제 리팩토링
List<Checklist> findByUserAndIdIn(User user, List<Long> checklistIds);

//TODO: 논리적 삭제 리팩토링
long countAllByIdIn(List<Long> ids);
@Query("SELECT c FROM Checklist c "
Copy link
Contributor

Choose a reason for hiding this comment

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

쿼리를 따로 작성한 코드에 대해서는 테스트를 작성해 보아도 좋을 것 같습니다~

Copy link
Contributor Author

Choose a reason for hiding this comment

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

작성 완료!

+ "JOIN FETCH c.user u "
+ "JOIN FETCH c.room r "
+ "WHERE u = :user "
+ "AND c.id IN :checklistIds "
+ "AND c.deleted = false")
List<Checklist> findByUserAndIdIn(@Param("user") User user,
@Param("checklistIds") List<Long> checklistIds);

@Query("SELECT COUNT(c) > 0 FROM Checklist c "
+ "WHERE c.id = :id "
Expand All @@ -44,5 +47,4 @@ default Checklist getById(long id) {
+ "SET c.deleted = true "
+ "WHERE c.id = :id")
void deleteById(@Param("id") long id);

}
Loading
Loading