diff --git a/src/main/java/com/moabam/api/application/RoomService.java b/src/main/java/com/moabam/api/application/RoomService.java index a855f1c9..94168e55 100644 --- a/src/main/java/com/moabam/api/application/RoomService.java +++ b/src/main/java/com/moabam/api/application/RoomService.java @@ -1,5 +1,7 @@ package com.moabam.api.application; +import static com.moabam.global.error.model.ErrorMessage.*; + import java.util.List; import org.springframework.stereotype.Service; @@ -12,7 +14,10 @@ import com.moabam.api.domain.repository.RoomRepository; import com.moabam.api.domain.repository.RoutineRepository; import com.moabam.api.dto.CreateRoomRequest; +import com.moabam.api.dto.ModifyRoomRequest; import com.moabam.api.dto.RoomMapper; +import com.moabam.global.error.exception.ForbiddenException; +import com.moabam.global.error.exception.NotFoundException; import lombok.RequiredArgsConstructor; @@ -39,4 +44,21 @@ public void createRoom(Long memberId, CreateRoomRequest createRoomRequest) { routineRepository.saveAll(routines); participantRepository.save(participant); } + + @Transactional + public void modifyRoom(Long memberId, Long roomId, ModifyRoomRequest modifyRoomRequest) { + // TODO: 추후에 별도 메서드로 뺄듯 + Participant participant = participantRepository.findParticipantByRoomIdAndMemberId(roomId, memberId) + .orElseThrow(() -> new NotFoundException(PARTICIPANT_NOT_FOUND)); + + if (!participant.isManager()) { + throw new ForbiddenException(ROOM_MODIFY_UNAUTHORIZED_REQUEST); + } + + Room room = roomRepository.findById(roomId).orElseThrow(() -> new NotFoundException(ROOM_NOT_FOUND)); + room.changeTitle(modifyRoomRequest.title()); + room.changePassword(modifyRoomRequest.password()); + room.changeCertifyTime(modifyRoomRequest.certifyTime()); + room.changeMaxCount(modifyRoomRequest.maxUserCount()); + } } diff --git a/src/main/java/com/moabam/api/domain/entity/Room.java b/src/main/java/com/moabam/api/domain/entity/Room.java index 5148c464..5e150acf 100644 --- a/src/main/java/com/moabam/api/domain/entity/Room.java +++ b/src/main/java/com/moabam/api/domain/entity/Room.java @@ -95,6 +95,22 @@ public void changeAnnouncement(String announcement) { this.announcement = announcement; } + public void changeTitle(String title) { + this.title = title; + } + + public void changePassword(String password) { + this.password = password; + } + + public void changeMaxCount(int maxUserCount) { + if (maxUserCount < this.currentUserCount) { + throw new BadRequestException(ROOM_MAX_USER_COUNT_MODIFY_FAIL); + } + + this.maxUserCount = maxUserCount; + } + public void upgradeRoomImage(String roomImage) { this.roomImage = roomImage; } diff --git a/src/main/java/com/moabam/api/domain/repository/ParticipantRepository.java b/src/main/java/com/moabam/api/domain/repository/ParticipantRepository.java index 3f0a7bfc..be814a44 100644 --- a/src/main/java/com/moabam/api/domain/repository/ParticipantRepository.java +++ b/src/main/java/com/moabam/api/domain/repository/ParticipantRepository.java @@ -1,9 +1,12 @@ package com.moabam.api.domain.repository; +import java.util.Optional; + import org.springframework.data.jpa.repository.JpaRepository; import com.moabam.api.domain.entity.Participant; public interface ParticipantRepository extends JpaRepository { + Optional findParticipantByRoomIdAndMemberId(Long roomId, Long MemberId); } diff --git a/src/main/java/com/moabam/api/dto/ModifyRoomRequest.java b/src/main/java/com/moabam/api/dto/ModifyRoomRequest.java new file mode 100644 index 00000000..4e24341a --- /dev/null +++ b/src/main/java/com/moabam/api/dto/ModifyRoomRequest.java @@ -0,0 +1,15 @@ +package com.moabam.api.dto; + +import org.hibernate.validator.constraints.Range; + +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Pattern; + +public record ModifyRoomRequest( + @NotBlank String title, + @Pattern(regexp = "^(|[0-9]{4,8})$") String password, + @Range(min = 0, max = 23) int certifyTime, + @Range(min = 0, max = 10) int maxUserCount +) { + +} diff --git a/src/main/java/com/moabam/api/presentation/RoomController.java b/src/main/java/com/moabam/api/presentation/RoomController.java index 9f01944e..c0edc651 100644 --- a/src/main/java/com/moabam/api/presentation/RoomController.java +++ b/src/main/java/com/moabam/api/presentation/RoomController.java @@ -1,7 +1,9 @@ package com.moabam.api.presentation; import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseStatus; @@ -9,6 +11,7 @@ import com.moabam.api.application.RoomService; import com.moabam.api.dto.CreateRoomRequest; +import com.moabam.api.dto.ModifyRoomRequest; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; @@ -25,4 +28,11 @@ public class RoomController { public void createRoom(@Valid @RequestBody CreateRoomRequest createRoomRequest) { roomService.createRoom(1L, createRoomRequest); } + + @PutMapping("/{roomId}") + @ResponseStatus(HttpStatus.OK) + public void modifyRoom(@Valid @RequestBody ModifyRoomRequest modifyRoomRequest, + @PathVariable("roomId") Long roomId) { + roomService.modifyRoom(1L, roomId, modifyRoomRequest); + } } diff --git a/src/main/java/com/moabam/global/error/model/ErrorMessage.java b/src/main/java/com/moabam/global/error/model/ErrorMessage.java index 662874f6..dd05b455 100644 --- a/src/main/java/com/moabam/global/error/model/ErrorMessage.java +++ b/src/main/java/com/moabam/global/error/model/ErrorMessage.java @@ -7,7 +7,11 @@ @RequiredArgsConstructor public enum ErrorMessage { - INVALID_REQUEST_FIELD("올바른 요청 정보가 아닙니다."); + INVALID_REQUEST_FIELD("올바른 요청 정보가 아닙니다."), + ROOM_NOT_FOUND("존재하지 않는 방 입니다."), + ROOM_MAX_USER_COUNT_MODIFY_FAIL("잘못된 최대 인원수 설정입니다."), + ROOM_MODIFY_UNAUTHORIZED_REQUEST("방장이 아닌 사용자는 방을 수정할 수 없습니다."), + PARTICIPANT_NOT_FOUND("방에 대한 참여자의 정보가 없습니다."); private final String message; }