Skip to content

Commit

Permalink
feat: CCTV 삭제 API 구현 (#63) (KAN-114)
Browse files Browse the repository at this point in the history
  • Loading branch information
ywonchae1 committed Nov 1, 2024
1 parent d0df10f commit bae0b3a
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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 인 경우와 동일하므로 처리하지 않음
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit bae0b3a

Please sign in to comment.