Skip to content

Commit

Permalink
[BE] feat: 모집 완료된 방 리스트 API 구현(woowacourse-teams#31) (woowacourse-tea…
Browse files Browse the repository at this point in the history
…ms#96)

* feat: 모집 완료된 방들을 조회하는 기능 구현

* refactor: 변수명 변경

* refactor: 변수명 변경

* refactor: 변수명 변경

* fix: 충돌 해결
  • Loading branch information
jcoding-play authored Jul 23, 2024
1 parent 117122d commit cf818d7
Show file tree
Hide file tree
Showing 15 changed files with 160 additions and 100 deletions.
21 changes: 21 additions & 0 deletions backend/src/main/java/corea/DataInitializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,27 @@ public void run(ApplicationArguments args) {
LocalDateTime.of(2024, 12, 25, 12, 0),
LocalDateTime.of(2024, 12, 30, 12, 0),
Classification.FRONTEND, RoomStatus.OPENED));
roomRepository.save(
new Room("방 제목 8", "방 설명 8", 3,
null, null, List.of("TDD", "클린코드"),
1, 20, member1,
LocalDateTime.of(2024, 12, 25, 12, 0),
LocalDateTime.of(2024, 12, 30, 12, 0),
Classification.BACKEND, RoomStatus.CLOSED));
roomRepository.save(
new Room("방 제목 9", "방 설명 9", 3,
null, null, List.of("TDD"),
1, 20, member3,
LocalDateTime.of(2024, 12, 25, 12, 0),
LocalDateTime.of(2024, 12, 30, 12, 0),
Classification.ANDROID, RoomStatus.CLOSED));
roomRepository.save(
new Room("방 제목 10", "방 설명 10", 3,
null, null, List.of("TDD"),
1, 20, member5,
LocalDateTime.of(2024, 12, 25, 12, 0),
LocalDateTime.of(2024, 12, 30, 12, 0),
Classification.FRONTEND, RoomStatus.CLOSED));

participationRepository.save(new Participation(room1.getId(), member2.getId()));
participationRepository.save(new Participation(room1.getId(), member3.getId()));
Expand Down
27 changes: 5 additions & 22 deletions backend/src/main/java/corea/auth/domain/AuthInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,23 @@
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.Objects;

@RequiredArgsConstructor
@Getter
public class AuthInfo {

private static final long NOT_EXIST_ID = -1L;
public static final AuthInfo ANONYMOUS = new AuthInfo(NOT_EXIST_ID, "", "");
private static final String EMPTY_STRING = "";
private static final AuthInfo ANONYMOUS = new AuthInfo(NOT_EXIST_ID, EMPTY_STRING, EMPTY_STRING);

private final long id;
private final String name;
private final String email;

public static AuthInfo from(Member member) {
return new AuthInfo(member.getId(), member.getUserName(), member.getEmail());
}

public boolean isAnonymous() {
return id == NOT_EXIST_ID;
}

public boolean isNotAnonymous() {
return id == NOT_EXIST_ID;
}

@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (!(o instanceof AuthInfo authInfo)) return false;
return Objects.equals(id, authInfo.id);
return new AuthInfo(member.getId(), member.getUsername(), member.getEmail());
}

@Override
public int hashCode() {
return Objects.hashCode(id);
public static AuthInfo getAnonymous() {
return ANONYMOUS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import corea.auth.RequestHandler;
import corea.auth.annotation.AccessedMember;
import corea.auth.domain.AuthInfo;
import corea.member.domain.Member;
import corea.member.repository.MemberRepository;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
Expand All @@ -30,8 +29,9 @@ public boolean supportsParameter(MethodParameter parameter) {
public AuthInfo resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) {
HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();
Member member = memberRepository.findByEmail(requestHandler.extract(request))
.orElse(null);
return member == null ? AuthInfo.ANONYMOUS : AuthInfo.from(member);

return memberRepository.findByEmail(requestHandler.extract(request))
.map(AuthInfo::from)
.orElse(AuthInfo.getAnonymous());
}
}
2 changes: 1 addition & 1 deletion backend/src/main/java/corea/matching/dto/ReviewInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
public record ReviewInfo(long userId, String username, String link, ReviewStatus isReviewed) {

public static ReviewInfo of(MatchResult matchResult, Member member) {
return new ReviewInfo(member.getId(), member.getUserName(), matchResult.getPrLink(), matchResult.getReviewStatus());
return new ReviewInfo(member.getId(), member.getUsername(), matchResult.getPrLink(), matchResult.getReviewStatus());
}
}
6 changes: 3 additions & 3 deletions backend/src/main/java/corea/member/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Member {
@GeneratedValue(strategy = IDENTITY)
private Long id;

private String userName;
private String username;

private String thumbnailUrl;

Expand All @@ -31,7 +31,7 @@ public class Member {

private String profileLink;

public Member(String userName, String thumbnailUrl, String name, String email, boolean isEmailAccepted, float attitude) {
this(null, userName, thumbnailUrl, name, email, isEmailAccepted, attitude, null);
public Member(String username, String thumbnailUrl, String name, String email, boolean isEmailAccepted, float attitude) {
this(null, username, thumbnailUrl, name, email, isEmailAccepted, attitude, null);
}
}
21 changes: 9 additions & 12 deletions backend/src/main/java/corea/room/controller/RoomController.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class RoomController implements RoomControllerSpecification {

@GetMapping("/{id}")
public ResponseEntity<RoomResponse> room(@PathVariable long id, @AccessedMember AuthInfo authInfo) {
RoomResponse response = roomService.findOne(id, authInfo);
RoomResponse response = roomService.findOne(id, authInfo.getId());
return ResponseEntity.ok(response);
}

Expand All @@ -38,12 +38,6 @@ public ResponseEntity<ReviewInfos> reviewees(@PathVariable long id, @LoginMember
return ResponseEntity.ok(response);
}

@GetMapping
public ResponseEntity<RoomResponses> rooms() {
RoomResponses response = roomService.findAll();
return ResponseEntity.ok(response);
}

@GetMapping("/participated")
public ResponseEntity<RoomResponses> participatedRooms(@LoginMember AuthInfo authInfo) {
RoomResponses response = roomService.findParticipatedRooms(authInfo.getId());
Expand All @@ -54,11 +48,14 @@ public ResponseEntity<RoomResponses> participatedRooms(@LoginMember AuthInfo aut
public ResponseEntity<RoomResponses> openedRooms(@AccessedMember AuthInfo authInfo,
@RequestParam(value = "classification", defaultValue = "all") String expression,
@RequestParam(defaultValue = "0") int page) {
if (authInfo.isAnonymous()) {
RoomResponses response = roomService.findOpenedRoomsWithoutMember(expression, page);
return ResponseEntity.ok(response);
}
RoomResponses response = roomService.findOpenedRoomsWithMember(authInfo.getId(), expression, page);
RoomResponses response = roomService.findOpenedRooms(authInfo.getId(), expression, page);
return ResponseEntity.ok(response);
}

@GetMapping("/closed")
public ResponseEntity<RoomResponses> closedRooms(@RequestParam(value = "classification", defaultValue = "all") String expression,
@RequestParam(defaultValue = "0") int page) {
RoomResponses response = roomService.findClosedRooms(expression, page);
return ResponseEntity.ok(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ public interface RoomControllerSpecification {

ResponseEntity<RoomResponse> room(long id, AuthInfo authInfo);

ResponseEntity<RoomResponses> rooms();

ResponseEntity<RoomResponses> participatedRooms(AuthInfo authInfo);

ResponseEntity<RoomResponses> openedRooms(AuthInfo authInfo, String expression, int page);
Expand Down
8 changes: 4 additions & 4 deletions backend/src/main/java/corea/room/domain/Room.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ public Room(String title, String content, int matchingSize, String repositoryLin
this(null, title, content, matchingSize, repositoryLink, thumbnailLink, keyword, currentParticipantsSize, limitedParticipantsSize, manager, recruitmentDeadline, reviewDeadline, classification, status);
}

public boolean isOpen() {
return status.isOpen();
}

public boolean isClosed() {
return status.isClosed();
}

public String getManagerName() {
return manager.getName();
}
}

23 changes: 2 additions & 21 deletions backend/src/main/java/corea/room/dto/RoomResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,13 @@ public record RoomResponse(
boolean isParticipated,
boolean isClosed
) {
public static RoomResponse from(Room room) {
return new RoomResponse(
room.getId(),
room.getTitle(),
room.getContent(),
room.getManager().getName(),
room.getRepositoryLink(),
room.getThumbnailLink(),
room.getMatchingSize(),
room.getKeyword(),
room.getCurrentParticipantsSize(),
room.getLimitedParticipantsSize(),
room.getRecruitmentDeadline(),
room.getReviewDeadline(),
false,
room.isOpen()
);
}

public static RoomResponse from(Room room, boolean isParticipated) {
public static RoomResponse of(Room room, boolean isParticipated) {
return new RoomResponse(
room.getId(),
room.getTitle(),
room.getContent(),
room.getManager()
.getName(),
room.getManagerName(),
room.getRepositoryLink(),
room.getThumbnailLink(),
room.getMatchingSize(),
Expand Down
6 changes: 3 additions & 3 deletions backend/src/main/java/corea/room/dto/RoomResponses.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@

public record RoomResponses(List<RoomResponse> rooms) {

public static RoomResponses from(List<Room> rooms) {
public static RoomResponses of(List<Room> rooms, boolean isParticipated) {
return rooms.stream()
.map(RoomResponse::from)
.map(room -> RoomResponse.of(room, isParticipated))
.collect(collectingAndThen(toList(), RoomResponses::new));
}

public static RoomResponses from(Page<Room> roomsWithPage) {
return from(roomsWithPage.getContent());
return of(roomsWithPage.getContent(), false);
}
}
33 changes: 13 additions & 20 deletions backend/src/main/java/corea/room/service/RoomService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package corea.room.service;

import corea.auth.domain.AuthInfo;
import corea.exception.CoreaException;
import corea.exception.ExceptionType;
import corea.matching.domain.Participation;
Expand Down Expand Up @@ -35,15 +34,14 @@ public class RoomService {

public RoomResponse create(RoomCreateRequest request) {
Room room = roomRepository.save(request.toEntity());
return RoomResponse.from(room);
return RoomResponse.of(room, true);
}

public RoomResponse findOne(long roomId, AuthInfo authInfo) {
public RoomResponse findOne(long roomId, long memberId) {
Room room = getRoom(roomId);
if (authInfo.isNotAnonymous() && participationRepository.existsByRoomIdAndMemberId(roomId, authInfo.getId())) {
return RoomResponse.from(room, true);
}
return RoomResponse.from(room);
boolean isParticipated = participationRepository.existsByRoomIdAndMemberId(roomId, memberId);

return RoomResponse.of(room, isParticipated);
}

public RoomResponses findParticipatedRooms(long memberId) {
Expand All @@ -52,40 +50,35 @@ public RoomResponses findParticipatedRooms(long memberId) {
return participations.stream()
.map(Participation::getRoomId)
.map(this::getRoom)
.collect(collectingAndThen(toList(), RoomResponses::from));
.collect(collectingAndThen(toList(), rooms -> RoomResponses.of(rooms, true)));
}

public RoomResponses findOpenedRoomsWithoutMember(String expression, int pageNumber) {
public RoomResponses findOpenedRooms(long memberId, String expression, int pageNumber) {
Classification classification = Classification.from(expression);
RoomStatus status = RoomStatus.OPENED;
PageRequest pageRequest = PageRequest.of(pageNumber, PAGE_SIZE);

if (classification.isAll()) {
Page<Room> roomsWithPage = roomRepository.findAllByStatus(status, pageRequest);
Page<Room> roomsWithPage = roomRepository.findAllByMemberAndStatus(memberId, status, pageRequest);
return RoomResponses.from(roomsWithPage);
}
Page<Room> roomsWithPage = roomRepository.findAllByClassificationAndStatus(classification, status, pageRequest);
Page<Room> roomsWithPage = roomRepository.findAllByMemberAndClassificationAndStatus(memberId, classification, status, pageRequest);
return RoomResponses.from(roomsWithPage);
}

public RoomResponses findOpenedRoomsWithMember(long memberId, String expression, int pageNumber) {
public RoomResponses findClosedRooms(String expression, int pageNumber) {
Classification classification = Classification.from(expression);
RoomStatus status = RoomStatus.OPENED;
RoomStatus status = RoomStatus.CLOSED;
PageRequest pageRequest = PageRequest.of(pageNumber, PAGE_SIZE);

if (classification.isAll()) {
Page<Room> roomsWithPage = roomRepository.findAllByMemberAndStatus(memberId, status, pageRequest);
Page<Room> roomsWithPage = roomRepository.findAllByStatus(status, pageRequest);
return RoomResponses.from(roomsWithPage);
}
Page<Room> roomsWithPage = roomRepository.findAllByMemberAndClassificationAndStatus(memberId, classification, status, pageRequest);
Page<Room> roomsWithPage = roomRepository.findAllByClassificationAndStatus(classification, status, pageRequest);
return RoomResponses.from(roomsWithPage);
}

public RoomResponses findAll() {
final List<Room> rooms = roomRepository.findAll();
return RoomResponses.from(rooms);
}

private Room getRoom(long roomId) {
return roomRepository.findById(roomId)
.orElseThrow(() -> new CoreaException(ExceptionType.ROOM_NOT_FOUND, String.format("해당 Id의 방이 없습니다. 입력된 Id=%d", roomId)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void findReviewers() {
@DisplayName("리뷰어 결과를 가져올 때 존재하지 않는 방이나 사용자의 정보를 요청하는 경우 예외를 발생한다.")
void findReviewersInvalidException() {
long memberId = 1;
long roomId = 8;
long roomId = 0;

int matchingSize = 3;
List<Participation> participations = new ArrayList<>();
Expand Down
Loading

0 comments on commit cf818d7

Please sign in to comment.