diff --git a/src/main/java/com/linkode/api_server/common/response/status/BaseExceptionResponseStatus.java b/src/main/java/com/linkode/api_server/common/response/status/BaseExceptionResponseStatus.java index ae592c0..cbce355 100644 --- a/src/main/java/com/linkode/api_server/common/response/status/BaseExceptionResponseStatus.java +++ b/src/main/java/com/linkode/api_server/common/response/status/BaseExceptionResponseStatus.java @@ -10,7 +10,7 @@ public enum BaseExceptionResponseStatus implements ResponseStatus{ * 1000: 요청 성공 (OK) */ SUCCESS(1000,HttpStatus.OK.value(), "요청에 성공하였습니다."), - + FAILURE(2000, HttpStatus.BAD_REQUEST.value(), "요청에 실패하였습니다."), INVALID_TOKEN(2000, HttpStatus.OK.value(), "유효하지 않은 토큰입니다."), ALREADY_EXIST_MEMBER(3000, HttpStatus.OK.value(), "이미 존재하는 회원입니다."); diff --git a/src/main/java/com/linkode/api_server/controller/StudyroomController.java b/src/main/java/com/linkode/api_server/controller/StudyroomController.java index 3fae483..d7a6052 100644 --- a/src/main/java/com/linkode/api_server/controller/StudyroomController.java +++ b/src/main/java/com/linkode/api_server/controller/StudyroomController.java @@ -1,7 +1,9 @@ package com.linkode.api_server.controller; -import com.linkode.api_server.dto.CreateStudyroomRequest; -import com.linkode.api_server.dto.CreateStudyroomResponse; +import com.linkode.api_server.common.response.BaseResponse; +import com.linkode.api_server.common.response.status.BaseExceptionResponseStatus; +import com.linkode.api_server.dto.studyroom.CreateStudyroomRequest; +import com.linkode.api_server.dto.studyroom.CreateStudyroomResponse; import com.linkode.api_server.service.StudyroomService; import com.linkode.api_server.util.JwtProvider; import lombok.RequiredArgsConstructor; @@ -21,6 +23,18 @@ public class StudyroomController { @Autowired JwtProvider jwtProvider; + @PatchMapping("/removal") + public BaseResponse deleteStudyroom(@RequestHeader("Authorization") String authorization, @RequestParam long studyroomId){ + + long memberId = jwtProvider.extractIdFromHeader(authorization); + BaseExceptionResponseStatus responseStatus = studyroomService.deleteStudyroom(studyroomId,memberId); + log.info("Run Delete Studyroom API "); + if (responseStatus == BaseExceptionResponseStatus.SUCCESS) { + return new BaseResponse<>(responseStatus); + } else { + return new BaseResponse<>(responseStatus, responseStatus); + } } + @PostMapping("/generation") public CreateStudyroomResponse createStudyroom(@RequestHeader("Authorization") String authorization, @RequestBody CreateStudyroomRequest request){ log.info("Success createStudyroom API"); diff --git a/src/main/java/com/linkode/api_server/dto/CreateStudyroomRequest.java b/src/main/java/com/linkode/api_server/dto/studyroom/CreateStudyroomRequest.java similarity index 87% rename from src/main/java/com/linkode/api_server/dto/CreateStudyroomRequest.java rename to src/main/java/com/linkode/api_server/dto/studyroom/CreateStudyroomRequest.java index bbcc377..2eae378 100644 --- a/src/main/java/com/linkode/api_server/dto/CreateStudyroomRequest.java +++ b/src/main/java/com/linkode/api_server/dto/studyroom/CreateStudyroomRequest.java @@ -1,4 +1,4 @@ -package com.linkode.api_server.dto; +package com.linkode.api_server.dto.studyroom; import jakarta.persistence.Column; import lombok.AllArgsConstructor; diff --git a/src/main/java/com/linkode/api_server/dto/CreateStudyroomResponse.java b/src/main/java/com/linkode/api_server/dto/studyroom/CreateStudyroomResponse.java similarity index 87% rename from src/main/java/com/linkode/api_server/dto/CreateStudyroomResponse.java rename to src/main/java/com/linkode/api_server/dto/studyroom/CreateStudyroomResponse.java index 1fd2824..3ddb224 100644 --- a/src/main/java/com/linkode/api_server/dto/CreateStudyroomResponse.java +++ b/src/main/java/com/linkode/api_server/dto/studyroom/CreateStudyroomResponse.java @@ -1,4 +1,4 @@ -package com.linkode.api_server.dto; +package com.linkode.api_server.dto.studyroom; import lombok.AllArgsConstructor; import lombok.Getter; diff --git a/src/main/java/com/linkode/api_server/dto/studyroom/DeleteStudyroomRequest.java b/src/main/java/com/linkode/api_server/dto/studyroom/DeleteStudyroomRequest.java new file mode 100644 index 0000000..b6f377e --- /dev/null +++ b/src/main/java/com/linkode/api_server/dto/studyroom/DeleteStudyroomRequest.java @@ -0,0 +1,15 @@ +package com.linkode.api_server.dto.studyroom; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter @Setter +@AllArgsConstructor +@NoArgsConstructor +public class DeleteStudyroomRequest { + + private long studyroomId; + +} diff --git a/src/main/java/com/linkode/api_server/dto/JoinStudyroomRequest.java b/src/main/java/com/linkode/api_server/dto/studyroom/JoinStudyroomRequest.java similarity index 88% rename from src/main/java/com/linkode/api_server/dto/JoinStudyroomRequest.java rename to src/main/java/com/linkode/api_server/dto/studyroom/JoinStudyroomRequest.java index fa1ea13..b2fbc3a 100644 --- a/src/main/java/com/linkode/api_server/dto/JoinStudyroomRequest.java +++ b/src/main/java/com/linkode/api_server/dto/studyroom/JoinStudyroomRequest.java @@ -1,4 +1,4 @@ -package com.linkode.api_server.dto; +package com.linkode.api_server.dto.studyroom; import com.linkode.api_server.domain.memberstudyroom.MemberRole; import lombok.AllArgsConstructor; diff --git a/src/main/java/com/linkode/api_server/repository/MemberstudyroomRepository.java b/src/main/java/com/linkode/api_server/repository/MemberstudyroomRepository.java index 5aeb3d5..36e3d80 100644 --- a/src/main/java/com/linkode/api_server/repository/MemberstudyroomRepository.java +++ b/src/main/java/com/linkode/api_server/repository/MemberstudyroomRepository.java @@ -1,12 +1,23 @@ package com.linkode.api_server.repository; -import com.linkode.api_server.domain.Studyroom; +import com.linkode.api_server.domain.memberstudyroom.MemberRole; import com.linkode.api_server.domain.memberstudyroom.MemberStudyroom; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; + +import java.util.Optional; @Repository +@Transactional(readOnly = true) public interface MemberstudyroomRepository extends JpaRepository { - -} + @Query("SELECT ms.role FROM MemberStudyroom ms WHERE ms.studyroom.studyroomId = :studyroomId AND ms.member.memberId = :memberId") + Optional findRoleByMemberIdAndStudyroomId(long studyroomId, long memberId); + @Transactional + @Modifying + @Query("UPDATE MemberStudyroom ms SET ms.status = 'DELETE' WHERE ms.studyroom.studyroomId = :studyroomId") + int deleteMemberStudyroom(long studyroomId); +} \ No newline at end of file diff --git a/src/main/java/com/linkode/api_server/repository/StudyroomRepository.java b/src/main/java/com/linkode/api_server/repository/StudyroomRepository.java index ba460d5..4aa27f2 100644 --- a/src/main/java/com/linkode/api_server/repository/StudyroomRepository.java +++ b/src/main/java/com/linkode/api_server/repository/StudyroomRepository.java @@ -1,11 +1,22 @@ package com.linkode.api_server.repository; import com.linkode.api_server.domain.Studyroom; -import com.linkode.api_server.dto.CreateStudyroomResponse; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; + +import java.util.Optional; @Repository +@Transactional(readOnly = true) public interface StudyroomRepository extends JpaRepository { + @Transactional + @Modifying + @Query("UPDATE Studyroom sr SET sr.status = 'DELETE' WHERE sr.studyroomId = :studyroomId") + int deleteStudyroom(long studyroomId); -} + @Query("SELECT sr From Studyroom sr WHERE sr.studyroomId = :studyroomId AND sr.status = 'ACTIVE'") + Optional findById(long studyroomId); +} \ No newline at end of file diff --git a/src/main/java/com/linkode/api_server/service/StudyroomService.java b/src/main/java/com/linkode/api_server/service/StudyroomService.java index 4aa8899..20a6aac 100644 --- a/src/main/java/com/linkode/api_server/service/StudyroomService.java +++ b/src/main/java/com/linkode/api_server/service/StudyroomService.java @@ -1,19 +1,21 @@ package com.linkode.api_server.service; -import com.linkode.api_server.domain.Member; -import com.linkode.api_server.domain.Studyroom; -import com.linkode.api_server.domain.base.BaseStatus; +import com.linkode.api_server.common.response.status.BaseExceptionResponseStatus; import com.linkode.api_server.domain.memberstudyroom.MemberRole; import com.linkode.api_server.domain.memberstudyroom.MemberStudyroom; -import com.linkode.api_server.dto.CreateStudyroomRequest; -import com.linkode.api_server.dto.CreateStudyroomResponse; -import com.linkode.api_server.dto.JoinStudyroomRequest; -import com.linkode.api_server.repository.MemberRepository; import com.linkode.api_server.repository.MemberstudyroomRepository; import com.linkode.api_server.repository.StudyroomRepository; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.Optional; +import com.linkode.api_server.domain.Member; +import com.linkode.api_server.domain.Studyroom; +import com.linkode.api_server.domain.base.BaseStatus; +import com.linkode.api_server.dto.studyroom.CreateStudyroomRequest; +import com.linkode.api_server.dto.studyroom.CreateStudyroomResponse; +import com.linkode.api_server.dto.studyroom.JoinStudyroomRequest; +import com.linkode.api_server.repository.MemberRepository; import org.springframework.transaction.annotation.Transactional; @Service @@ -27,6 +29,37 @@ public class StudyroomService { @Autowired private MemberRepository memberRepository; + public BaseExceptionResponseStatus deleteStudyroom(long studyroomId, long memberId) { + + + if(!studyroomRepository.findById(studyroomId).isPresent()){ + log.info("StudyRoom Id is Invalid"); + return BaseExceptionResponseStatus.FAILURE; + } + + Optional optionalMemberRole = memberstudyroomRepository.findRoleByMemberIdAndStudyroomId(studyroomId, memberId); + if (optionalMemberRole.isEmpty()) { + log.info("Member Role not found for memberId: " + memberId + " and studyroomId: " + studyroomId); + return BaseExceptionResponseStatus.FAILURE; + } + MemberRole memberRole = optionalMemberRole.orElseThrow(() -> new IllegalArgumentException("Error because of Invalid Member Id or Invalid StudyRoom Id")); + + if (memberRole .equals(MemberRole.CAPTAIN)) { + if(studyroomRepository.deleteStudyroom(studyroomId)==1){ + memberstudyroomRepository.deleteMemberStudyroom(studyroomId); + log.info("Success delete studyRoom in Service layer"); + return BaseExceptionResponseStatus.SUCCESS; + }else { + log.info("Failure delete studyRoom"); + return BaseExceptionResponseStatus.FAILURE; + } + } else { + log.info("Crew Member can't delete studyRoom"); + return BaseExceptionResponseStatus.FAILURE; + } + + } + @Transactional public CreateStudyroomResponse createStudyroom(CreateStudyroomRequest request, long memberId) { log.info("Start createStudyroom method of StudyroomService Class"); @@ -70,8 +103,4 @@ public void joinStudyroom(JoinStudyroomRequest request){ memberstudyroomRepository.save(memberStudyroom); } - - - - }