-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: CCTV 삭제 API 구현 (#63) (KAN-114)
- Loading branch information
Showing
5 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
src/main/java/org/ioteatime/meonghanyangserver/cctv/controller/CctvApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/org/ioteatime/meonghanyangserver/cctv/controller/CctvController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/org/ioteatime/meonghanyangserver/cctv/service/CctvService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 인 경우와 동일하므로 처리하지 않음 | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/org/ioteatime/meonghanyangserver/common/type/CctvErrorType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/org/ioteatime/meonghanyangserver/common/type/CctvSuccessType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |