Skip to content

Commit

Permalink
feat: 방 수정 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
ymkim97 committed Oct 31, 2023
1 parent 25d0044 commit 2dd7c1a
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/main/java/com/moabam/api/application/RoomService.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -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());
}
}
16 changes: 16 additions & 0 deletions src/main/java/com/moabam/api/domain/entity/Room.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Participant, Long> {

Optional<Participant> findParticipantByRoomIdAndMemberId(Long roomId, Long MemberId);
}
15 changes: 15 additions & 0 deletions src/main/java/com/moabam/api/dto/ModifyRoomRequest.java
Original file line number Diff line number Diff line change
@@ -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
) {

}
10 changes: 10 additions & 0 deletions src/main/java/com/moabam/api/presentation/RoomController.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
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;
import org.springframework.web.bind.annotation.RestController;

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;
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 2dd7c1a

Please sign in to comment.