Skip to content

Commit

Permalink
Merge pull request #14 from Linkode2024/LINKODE-16-BE-API-스터디룸-삭제-API
Browse files Browse the repository at this point in the history
Feat : 스터디룸 삭제 API
  • Loading branch information
Mouon authored Jul 5, 2024
2 parents 0b6cf5c + d2a86c2 commit e2375f8
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(), "이미 존재하는 회원입니다.");

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -21,6 +23,18 @@ public class StudyroomController {
@Autowired
JwtProvider jwtProvider;

@PatchMapping("/removal")
public BaseResponse<BaseExceptionResponseStatus> 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");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.linkode.api_server.dto;
package com.linkode.api_server.dto.studyroom;

import jakarta.persistence.Column;
import lombok.AllArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.linkode.api_server.dto;
package com.linkode.api_server.dto.studyroom;

import lombok.AllArgsConstructor;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<MemberStudyroom, Long> {


}
@Query("SELECT ms.role FROM MemberStudyroom ms WHERE ms.studyroom.studyroomId = :studyroomId AND ms.member.memberId = :memberId")
Optional<MemberRole> findRoleByMemberIdAndStudyroomId(long studyroomId, long memberId);
@Transactional
@Modifying
@Query("UPDATE MemberStudyroom ms SET ms.status = 'DELETE' WHERE ms.studyroom.studyroomId = :studyroomId")
int deleteMemberStudyroom(long studyroomId);
}
Original file line number Diff line number Diff line change
@@ -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<Studyroom, Long> {
@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<Studyroom> findById(long studyroomId);
}
51 changes: 40 additions & 11 deletions src/main/java/com/linkode/api_server/service/StudyroomService.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<MemberRole> 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");
Expand Down Expand Up @@ -70,8 +103,4 @@ public void joinStudyroom(JoinStudyroomRequest request){
memberstudyroomRepository.save(memberStudyroom);

}




}

0 comments on commit e2375f8

Please sign in to comment.