From bae0b3a51cf11aecdffc3d242aefab6176095164 Mon Sep 17 00:00:00 2001 From: ywonchae1 Date: Fri, 1 Nov 2024 21:17:26 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20CCTV=20=EC=82=AD=EC=A0=9C=20API=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84=20(#63)=20(KAN-114)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cctv/controller/CctvApi.java | 13 ++++++++ .../cctv/controller/CctvController.java | 24 ++++++++++++++ .../cctv/service/CctvService.java | 32 +++++++++++++++++++ .../common/type/CctvErrorType.java | 24 ++++++++++++++ .../common/type/CctvSuccessType.java | 30 +++++++++++++++++ 5 files changed, 123 insertions(+) create mode 100644 src/main/java/org/ioteatime/meonghanyangserver/cctv/controller/CctvApi.java create mode 100644 src/main/java/org/ioteatime/meonghanyangserver/cctv/controller/CctvController.java create mode 100644 src/main/java/org/ioteatime/meonghanyangserver/common/type/CctvErrorType.java create mode 100644 src/main/java/org/ioteatime/meonghanyangserver/common/type/CctvSuccessType.java diff --git a/src/main/java/org/ioteatime/meonghanyangserver/cctv/controller/CctvApi.java b/src/main/java/org/ioteatime/meonghanyangserver/cctv/controller/CctvApi.java new file mode 100644 index 00000000..ec5bcc52 --- /dev/null +++ b/src/main/java/org/ioteatime/meonghanyangserver/cctv/controller/CctvApi.java @@ -0,0 +1,13 @@ +package org.ioteatime.meonghanyangserver.cctv.controller; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import org.ioteatime.meonghanyangserver.common.api.Api; +import org.ioteatime.meonghanyangserver.common.utils.LoginMember; +import org.springframework.web.bind.annotation.PathVariable; + +@Tag(name = "CCTV Api", description = "CCTV 관련 API 목록입니다.") +public interface CctvApi { + @Operation(summary = "CCTV 삭제(퇴출)") + Api delete(@LoginMember Long userId, @PathVariable Long cctvId); +} diff --git a/src/main/java/org/ioteatime/meonghanyangserver/cctv/controller/CctvController.java b/src/main/java/org/ioteatime/meonghanyangserver/cctv/controller/CctvController.java new file mode 100644 index 00000000..c0cfb4ad --- /dev/null +++ b/src/main/java/org/ioteatime/meonghanyangserver/cctv/controller/CctvController.java @@ -0,0 +1,24 @@ +package org.ioteatime.meonghanyangserver.cctv.controller; + +import lombok.RequiredArgsConstructor; +import org.ioteatime.meonghanyangserver.cctv.service.CctvService; +import org.ioteatime.meonghanyangserver.common.api.Api; +import org.ioteatime.meonghanyangserver.common.type.CctvSuccessType; +import org.ioteatime.meonghanyangserver.common.utils.LoginMember; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/api/cctv") +public class CctvController implements CctvApi { + private final CctvService cctvService; + + @DeleteMapping("/{cctvId}") + public Api delete(@LoginMember Long userId, @PathVariable("cctvId") Long cctvId) { + cctvService.deleteById(userId, cctvId); + return Api.success(CctvSuccessType.DELETE_CCTV); + } +} diff --git a/src/main/java/org/ioteatime/meonghanyangserver/cctv/service/CctvService.java b/src/main/java/org/ioteatime/meonghanyangserver/cctv/service/CctvService.java index 4223b86e..03005cac 100644 --- a/src/main/java/org/ioteatime/meonghanyangserver/cctv/service/CctvService.java +++ b/src/main/java/org/ioteatime/meonghanyangserver/cctv/service/CctvService.java @@ -1,11 +1,43 @@ package org.ioteatime.meonghanyangserver.cctv.service; +import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; +import org.ioteatime.meonghanyangserver.cctv.dto.db.CctvWithDeviceId; import org.ioteatime.meonghanyangserver.cctv.repository.CctvRepository; +import org.ioteatime.meonghanyangserver.clients.kvs.KvsClient; +import org.ioteatime.meonghanyangserver.common.exception.BadRequestException; +import org.ioteatime.meonghanyangserver.common.exception.NotFoundException; +import org.ioteatime.meonghanyangserver.common.type.CctvErrorType; +import org.ioteatime.meonghanyangserver.device.repository.DeviceRepository; import org.springframework.stereotype.Service; @Service @RequiredArgsConstructor public class CctvService { + private final KvsClient kvsClient; private final CctvRepository cctvRepository; + private final DeviceRepository deviceRepository; + + @Transactional + public void deleteById(Long userId, Long cctvId) { + // Device 테이블에서 userId를 조회하여 role 을 확인 + if (deviceRepository.isMasterUserId(userId)) { + // MASTER 이면 CCTV 삭제 가능 + CctvWithDeviceId cctv = + cctvRepository + .findByIdWithDeviceId(cctvId) + .orElseThrow(() -> new NotFoundException(CctvErrorType.NOT_FOUND)); + // 1. KVS 시그널링 채널 삭제 + kvsClient.deleteSignalingChannel(cctv.kvsChannelName()); + // 2. CCTV 테이블에서 삭제 + cctvRepository.deleteById(cctvId); + System.out.println("HWEHREWR"); + // 3. Device 테이블에서 삭제 + deviceRepository.deleteById(cctv.deviceId()); + } else { + // PARTICIPANT 이면 CCTV 삭제 실패 + throw new BadRequestException(CctvErrorType.ONLY_MASTER_CAN_DELETE); + } + // CCTV 인 경우는 현재 MASTER 인 경우와 동일하므로 처리하지 않음 + } } diff --git a/src/main/java/org/ioteatime/meonghanyangserver/common/type/CctvErrorType.java b/src/main/java/org/ioteatime/meonghanyangserver/common/type/CctvErrorType.java new file mode 100644 index 00000000..8b63462c --- /dev/null +++ b/src/main/java/org/ioteatime/meonghanyangserver/common/type/CctvErrorType.java @@ -0,0 +1,24 @@ +package org.ioteatime.meonghanyangserver.common.type; + +public enum CctvErrorType implements ErrorTypeCode { + ONLY_MASTER_CAN_DELETE("BAD REQUEST", "CCTV는 MASTER만 삭제할 수 있습니다."), + NOT_FOUND("NOT FOUND", "CCTV를 찾을 수 없습니다."); + + private final String message; + private final String description; + + CctvErrorType(String message, String description) { + this.message = message; + this.description = description; + } + + @Override + public String getMessage() { + return this.message; + } + + @Override + public String getDescription() { + return this.description; + } +} diff --git a/src/main/java/org/ioteatime/meonghanyangserver/common/type/CctvSuccessType.java b/src/main/java/org/ioteatime/meonghanyangserver/common/type/CctvSuccessType.java new file mode 100644 index 00000000..4b9a1f30 --- /dev/null +++ b/src/main/java/org/ioteatime/meonghanyangserver/common/type/CctvSuccessType.java @@ -0,0 +1,30 @@ +package org.ioteatime.meonghanyangserver.common.type; + +public enum CctvSuccessType implements SuccessTypeCode { + DELETE_CCTV(200, "OK", "CCTV 삭제(퇴출)에 성공하였습니다."); + + private final Integer code; + private final String message; + private final String description; + + CctvSuccessType(Integer code, String message, String description) { + this.code = code; + this.message = message; + this.description = description; + } + + @Override + public Integer getCode() { + return this.code; + } + + @Override + public String getMessage() { + return this.message; + } + + @Override + public String getDescription() { + return this.description; + } +}