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, 방 이름 구현 #873

Open
wants to merge 5 commits into
base: BE/dev
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
import site.coduo.pairroom.service.PairRoomService;
import site.coduo.pairroom.service.dto.PairRoomCreateRequest;
import site.coduo.pairroom.service.dto.PairRoomCreateResponse;
import site.coduo.pairroom.service.dto.PairRoomExistByEasyAccessCodeResponse;
import site.coduo.pairroom.service.dto.PairRoomExistResponse;
import site.coduo.pairroom.service.dto.PairRoomMemberResponse;
import site.coduo.pairroom.service.dto.PairRoomNameUpdateRequest;
import site.coduo.pairroom.service.dto.PairRoomReadRequest;
import site.coduo.pairroom.service.dto.PairRoomReadResponse;
import site.coduo.pairroom.service.dto.PairRoomStatusUpdateRequest;
Expand Down Expand Up @@ -95,6 +97,14 @@ public ResponseEntity<PairRoomExistResponse> pairRoomExists(@RequestParam("acces
return ResponseEntity.ok(response);
}

@GetMapping("/pair-room/exists/easy")
public ResponseEntity<PairRoomExistByEasyAccessCodeResponse> pairRoomExistsByEasyAccessCode(
@RequestParam("easy_access_code") final String easyAccessCode) {
final PairRoomExistByEasyAccessCodeResponse response = pairRoomService.existsByEasyAccessCode(easyAccessCode);

return ResponseEntity.ok(response);
}

@DeleteMapping("/pair-room/{accessCode}")
public ResponseEntity<Void> deletePairRoom(@PathVariable("accessCode") final String accessCode) {
pairRoomService.deletePairRoom(accessCode);
Expand All @@ -111,4 +121,14 @@ public ResponseEntity<ExistMemberInPairRoomResponse> existMemberInPairRoom(
final ExistMemberInPairRoomResponse response = new ExistMemberInPairRoomResponse(existMemberInPairRoom);
return ResponseEntity.ok(response);
}

@PatchMapping("/pair-room/{accessCode}/room-name")
public ResponseEntity<Void> updatePairRoomName(
@PathVariable("accessCode") final String accessCode,
@Valid @RequestBody PairRoomNameUpdateRequest pairRoomNameUpdateRequest) {
pairRoomService.updatePairRoomName(accessCode, pairRoomNameUpdateRequest.roomName());

return ResponseEntity.noContent()
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package site.coduo.pairroom.controller;

import java.util.InvalidPropertiesFormatException;

import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.ResponseEntity;
Expand All @@ -14,9 +12,10 @@
import site.coduo.pairroom.exception.DuplicatePairNameException;
import site.coduo.pairroom.exception.InvalidAccessCodeException;
import site.coduo.pairroom.exception.InvalidNameFormatException;
import site.coduo.pairroom.exception.InvalidPairRoomStatusException;
import site.coduo.pairroom.exception.InvalidRoomNameFormatException;
import site.coduo.pairroom.exception.PairRoomException;
import site.coduo.pairroom.exception.PairRoomNotFoundException;
import site.coduo.pairroom.exception.InvalidPairRoomStatusException;

@Slf4j
@RestControllerAdvice
Expand All @@ -40,16 +39,26 @@ public ResponseEntity<ApiErrorResponse> handleInvalidAccessCodeException(final I
}

@ExceptionHandler(InvalidNameFormatException.class)
public ResponseEntity<ApiErrorResponse> handleInvalidPropertiesFormatException(
final InvalidPropertiesFormatException e) {
public ResponseEntity<ApiErrorResponse> handleInvalidNameFormatException(
final InvalidNameFormatException e) {
log.warn(e.getMessage());

return ResponseEntity.status(PairRoomApiError.INVALID_PROPERTIES_FORMAT.getHttpStatus())
.body(new ApiErrorResponse(PairRoomApiError.INVALID_PROPERTIES_FORMAT.getMessage()));
return ResponseEntity.status(PairRoomApiError.INVALID_PAIR_NAME.getHttpStatus())
.body(new ApiErrorResponse(PairRoomApiError.INVALID_PAIR_NAME.getMessage()));
}

@ExceptionHandler(InvalidRoomNameFormatException.class)
public ResponseEntity<ApiErrorResponse> handleInvalidRoomNameFormatException(
final InvalidRoomNameFormatException e) {
log.warn(e.getMessage());

return ResponseEntity.status(PairRoomApiError.INVALID_ROOM_NAME.getHttpStatus())
.body(new ApiErrorResponse(PairRoomApiError.INVALID_ROOM_NAME.getMessage()));
}

@ExceptionHandler(InvalidPairRoomStatusException.class)
public ResponseEntity<ApiErrorResponse> handlePairRoomStatusNotFoundException(final InvalidPairRoomStatusException e) {
public ResponseEntity<ApiErrorResponse> handlePairRoomStatusNotFoundException(
final InvalidPairRoomStatusException e) {
log.warn(e.getMessage());

return ResponseEntity.status(PairRoomApiError.INVALID_PROPERTIES_FORMAT.getHttpStatus())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
import site.coduo.pairroom.controller.dto.request.ExistMemberInPairRoomResponse;
import site.coduo.pairroom.service.dto.PairRoomCreateRequest;
import site.coduo.pairroom.service.dto.PairRoomCreateResponse;
import site.coduo.pairroom.service.dto.PairRoomExistByEasyAccessCodeResponse;
import site.coduo.pairroom.service.dto.PairRoomExistResponse;
import site.coduo.pairroom.service.dto.PairRoomMemberResponse;
import site.coduo.pairroom.service.dto.PairRoomNameUpdateRequest;
import site.coduo.pairroom.service.dto.PairRoomReadRequest;
import site.coduo.pairroom.service.dto.PairRoomReadResponse;
import site.coduo.pairroom.service.dto.PairRoomStatusUpdateRequest;
Expand Down Expand Up @@ -76,6 +78,11 @@ ResponseEntity<List<PairRoomMemberResponse>> getPairRooms(
schema = @Schema(implementation = PairRoomExistResponse.class)))
ResponseEntity<PairRoomExistResponse> pairRoomExists(String accessCode);

@Operation(summary = "페어 이름 액세스 코드로 페어룸이 존재하는지 조회한다.")
@ApiResponse(responseCode = "200", description = "페어룸 존재 여부, 페어룸 접근 코드", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = PairRoomExistByEasyAccessCodeResponse.class)))
ResponseEntity<PairRoomExistByEasyAccessCodeResponse> pairRoomExistsByEasyAccessCode(String easyAccessCode);

Comment on lines +81 to +85
Copy link
Member

Choose a reason for hiding this comment

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

easyAccessCode 가 리퀘스트 파라미터로 넘어오는데 이부분도 이렇게 문서화 가능합니다!
변경 부탁드려요

@Operation(summary = "페어룸을 삭제한다.")
@ApiResponse(responseCode = "204", description = "페어룸 삭제 성공")
ResponseEntity<Void> deletePairRoom(
Expand All @@ -92,4 +99,13 @@ ResponseEntity<ExistMemberInPairRoomResponse> existMemberInPairRoom(
@Parameter(description = "페어룸 접근 코드", required = true)
String pairRoomAccessCode
);

@Operation(summary = "페어룸 이름을 변경한다.")
@ApiResponse(responseCode = "204", description = "페어룸 이름 변경 성공")
ResponseEntity<Void> updatePairRoomName(
@Parameter(description = "페어룸 접근 코드", required = true)
String accessCode,
@Parameter(description = "변경할 페어룸 이름", required = true)
PairRoomNameUpdateRequest request
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public enum PairRoomApiError {

INVALID_REQUEST(HttpStatus.BAD_REQUEST, "유효하지 않은 요청입니다."),
INVALID_PAIR_NAME(HttpStatus.BAD_REQUEST, "올바르지 않은 페어 이름입니다."),
INVALID_ROOM_NAME(HttpStatus.BAD_REQUEST, "올바르지 않은 방 이름입니다."),
INVALID_ACCESS_CODE(HttpStatus.BAD_REQUEST, "올바르지 않은 접근 코드입니다."),
PAIR_ROOM_NOT_FOUND(HttpStatus.NOT_FOUND, "페어룸이 존재하지 않습니다."),
INVALID_PROPERTIES_FORMAT(HttpStatus.BAD_REQUEST, "올바르지 않은 데이터 형식입니다.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class PairRoom {
private final MissionUrl missionUrl;
private final AccessCode accessCode;
private final AccessCode easyAccessCode;
private final RoomName roomName;

public String getAccessCodeText() {
return accessCode.getValue();
Expand All @@ -36,6 +37,10 @@ public String getEasyAccessCodeText() {
return easyAccessCode.getValue();
}

public String getRoomName() {
return roomName.getValue();
}

@Override
public boolean equals(final Object o) {
if (this == o) {
Expand All @@ -61,6 +66,7 @@ public String toString() {
", missionUrl=" + missionUrl +
", accessCode=" + accessCode +
", easyAccessCode=" + easyAccessCode +
", roomName=" + roomName +
Copy link
Member

Choose a reason for hiding this comment

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

hashCode랑 equals 에서도 roomName 추가해주세요!

'}';
}
}
37 changes: 37 additions & 0 deletions backend/src/main/java/site/coduo/pairroom/domain/RoomName.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package site.coduo.pairroom.domain;

import lombok.Getter;
import site.coduo.pairroom.exception.InvalidRoomNameFormatException;

@Getter
public class RoomName {

private static final String DEFAULT_ROOM_NAME_SUFFIX = "의 페어룸";
private static final int MAX_LENGTH = 35;

private final String value;

public RoomName(final String value) {
validateEmpty(value);
final String trimmedValue = value.trim();
Copy link
Member

Choose a reason for hiding this comment

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

trim() 보다 strip() 요친구가 더 많은 공백을 제거해줍니다 !
참고

validateLength(trimmedValue);
this.value = trimmedValue;
}

public static RoomName makeDefaultRoomNameFrom(final String easyAccessCode) {
final String defaultRoomName = easyAccessCode + DEFAULT_ROOM_NAME_SUFFIX;
return new RoomName(defaultRoomName);
}

private void validateEmpty(final String value) {
if (value == null || value.isBlank()) {
throw new InvalidRoomNameFormatException("페어룸 이름이 비어있습니다.");
}
}

private void validateLength(final String value) {
if (value.length() > MAX_LENGTH) {
throw new InvalidRoomNameFormatException(String.format("페어룸 이름은 %d자 이하여야 합니다.", MAX_LENGTH));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package site.coduo.pairroom.exception;

public class InvalidRoomNameFormatException extends PairRoomException {

public InvalidRoomNameFormatException(final String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import site.coduo.pairroom.domain.PairName;
import site.coduo.pairroom.domain.PairRoom;
import site.coduo.pairroom.domain.PairRoomStatus;
import site.coduo.pairroom.domain.RoomName;
import site.coduo.pairroom.domain.accesscode.AccessCode;

@Getter
Expand Down Expand Up @@ -53,21 +54,26 @@ public class PairRoomEntity extends BaseTimeEntity {
@Column(name = "EASY_ACCESS_CODE", nullable = false, unique = true)
private String easyAccessCode;

@Column(name = "ROOM_NAME", nullable = false)
private String roomName;

@Builder
private PairRoomEntity(final Long id,
final PairRoomStatus status,
final String navigator,
final String driver,
final String missionUrl,
final String accessCode,
final String easyAccessCode) {
final String easyAccessCode,
final String roomName) {
this.id = id;
this.status = status;
this.navigator = navigator;
this.driver = driver;
this.missionUrl = missionUrl;
this.accessCode = accessCode;
this.easyAccessCode = easyAccessCode;
this.roomName = roomName;
}

public static PairRoomEntity from(final PairRoom pairRoom) {
Expand All @@ -78,7 +84,8 @@ public static PairRoomEntity from(final PairRoom pairRoom) {
pairRoom.getDriverName(),
pairRoom.getMissionUrl(),
pairRoom.getAccessCodeText(),
pairRoom.getEasyAccessCodeText()
pairRoom.getEasyAccessCodeText(),
pairRoom.getRoomName()
);
}

Expand All @@ -88,14 +95,19 @@ public PairRoom toDomain() {
new Pair(new PairName(navigator), new PairName(driver)),
new MissionUrl(missionUrl),
new AccessCode(accessCode),
new AccessCode(easyAccessCode)
new AccessCode(easyAccessCode),
new RoomName(roomName)
);
}

public void updateStatus(final PairRoomStatus status) {
this.status = status;
}

public void updateRoomName(final RoomName roomName) {
this.roomName = roomName.getValue();
}

public void swapNavigatorWithDriver() {
final String temp = this.navigator;
this.navigator = this.driver;
Expand Down Expand Up @@ -133,6 +145,7 @@ public String toString() {
", missionUrl='" + missionUrl + '\'' +
", accessCode='" + accessCode + '\'' +
", easyAccessCode='" + easyAccessCode + '\'' +
", roomName='" + roomName + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public interface PairRoomRepository extends JpaRepository<PairRoomEntity, Long>

Optional<PairRoomEntity> findByAccessCode(String accessCode);

Optional<PairRoomEntity> findByEasyAccessCode(String easyAccessCode);

default PairRoomEntity fetchByAccessCode(String accessCodeText) {
return findByAccessCode(accessCodeText)
.orElseThrow(() -> new PairRoomNotFoundException("존재하지 않는 페어룸 접근 코드입니다."));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package site.coduo.pairroom.service;

import java.util.List;
import java.util.Optional;

import jakarta.annotation.Nullable;
import jakarta.persistence.EntityNotFoundException;
Expand All @@ -17,6 +18,7 @@
import site.coduo.pairroom.domain.PairName;
import site.coduo.pairroom.domain.PairRoom;
import site.coduo.pairroom.domain.PairRoomStatus;
import site.coduo.pairroom.domain.RoomName;
import site.coduo.pairroom.domain.accesscode.AccessCode;
import site.coduo.pairroom.domain.accesscode.generator.AccessCodeGenerator;
import site.coduo.pairroom.domain.accesscode.generator.EasyAccessCodeGenerator;
Expand All @@ -27,6 +29,7 @@
import site.coduo.pairroom.repository.PairRoomMemberRepository;
import site.coduo.pairroom.repository.PairRoomRepository;
import site.coduo.pairroom.service.dto.PairRoomCreateRequest;
import site.coduo.pairroom.service.dto.PairRoomExistByEasyAccessCodeResponse;
import site.coduo.pairroom.service.dto.PairRoomMemberResponse;
import site.coduo.pairroom.service.dto.PairRoomReadResponse;
import site.coduo.timer.domain.Timer;
Expand Down Expand Up @@ -72,6 +75,13 @@ public boolean existsByAccessCode(final String accessCode) {
return pairRoomRepository.existsByAccessCodeAndStatusNot(accessCode, PairRoomStatus.DELETED);
}

public PairRoomExistByEasyAccessCodeResponse existsByEasyAccessCode(final String easyAccessCode) {
final Optional<PairRoomEntity> pairRoom = pairRoomRepository.findByEasyAccessCode(easyAccessCode);
return pairRoom.map(
pairRoomEntity -> new PairRoomExistByEasyAccessCodeResponse(true, pairRoomEntity.getAccessCode()))
.orElseGet(() -> new PairRoomExistByEasyAccessCodeResponse(false, null));
}

private PairRoom createPairRoom(final PairRoomCreateRequest request) {
final AccessCode uuidAccessCode = generateAccessCode(uuidAccessCodeGenerator);
final AccessCode easyAccessCode = generateAccessCode(
Expand All @@ -80,7 +90,8 @@ private PairRoom createPairRoom(final PairRoomCreateRequest request) {
final PairRoomStatus status = PairRoomStatus.IN_PROGRESS;
final Pair pair = new Pair(new PairName(request.navigator()), new PairName(request.driver()));
final MissionUrl missionUrl = new MissionUrl(request.missionUrl());
return new PairRoom(status, pair, missionUrl, uuidAccessCode, easyAccessCode);
final RoomName roomName = RoomName.makeDefaultRoomNameFrom(easyAccessCode.getValue());
return new PairRoom(status, pair, missionUrl, uuidAccessCode, easyAccessCode, roomName);
}

private AccessCode generateAccessCode(final AccessCodeGenerator accessCodeGenerator) {
Expand Down Expand Up @@ -163,4 +174,11 @@ private PairRoomEntity findPairRoom(final String pairRoomAccessCode) {
.orElseThrow(() -> new EntityNotFoundException(
"입력된 페어룸 접근 코드에 대응되는 페어룸이 존재하지 않습니다. - " + pairRoomAccessCode));
}

@Transactional
public void updatePairRoomName(final String accessCode, final String roomName) {
final PairRoomEntity pairRoomEntity = pairRoomRepository.fetchByAccessCode(accessCode);
final RoomName updateRoomName = new RoomName(roomName);
pairRoomEntity.updateRoomName(updateRoomName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package site.coduo.pairroom.service.dto;

import io.swagger.v3.oas.annotations.media.Schema;

@Schema(description = "페어 이름 액세스코드로 페어룸 존재 여부 확인 응답 바디")
public record PairRoomExistByEasyAccessCodeResponse(
@Schema(description = "페어룸 존재 확인")
boolean exists,

@Schema(description = "페어룸 접근 코드")
String accessCode
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ public record PairRoomMemberResponse(
String status,
String navigator,
String driver,
String accessCode
String accessCode,
String easyAccessCode,
String roomName
) {

public static PairRoomMemberResponse from(final PairRoomEntity pairRoom) {
return new PairRoomMemberResponse(pairRoom.getId(), pairRoom.getStatus().name(), pairRoom.getNavigator(),
pairRoom.getDriver(), pairRoom.getAccessCode());
pairRoom.getDriver(), pairRoom.getAccessCode(), pairRoom.getEasyAccessCode(), pairRoom.getRoomName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package site.coduo.pairroom.service.dto;

import jakarta.validation.constraints.NotBlank;

import io.swagger.v3.oas.annotations.media.Schema;

@Schema(description = "페어룸 이름 변경 요청 바디")
public record PairRoomNameUpdateRequest(
@Schema(description = "변경할 페어룸 이름", example = "파슬리와 함께한 페어룸")
@NotBlank
String roomName
) {
}
Loading
Loading