Files
no-copy/src/main/java/ru/soune/nocopy/handler/ViolationNotionHandler.java
T
backdev 184f3a730c
Test Workflow / test (push) Has been cancelled
NCBACK-162
2026-06-01 14:49:36 +07:00

258 lines
11 KiB
Java

package ru.soune.nocopy.handler;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import ru.soune.nocopy.dto.BaseRequest;
import ru.soune.nocopy.dto.BaseResponse;
import ru.soune.nocopy.dto.MessageCode;
import ru.soune.nocopy.dto.violation.*;
import ru.soune.nocopy.service.violation.ViolationNotionService;
import java.util.List;
@Slf4j
@Component
@RequiredArgsConstructor
public class ViolationNotionHandler implements RequestHandler {
private final ObjectMapper objectMapper;
private final ViolationNotionService notionService;
private final HttpServletRequest httpServletRequest;
@Override
public BaseResponse handle(BaseRequest request) {
try {
ViolationNotionRequest notionRequest = objectMapper.convertValue(
request.getMessageBody(), ViolationNotionRequest.class);
Long userId = (Long) httpServletRequest.getAttribute("userId");
String action = notionRequest.getAction();
if (action == null || action.trim().isEmpty()) {
return new BaseResponse(
request.getMsgId(),
MessageCode.INVALID_ACTION.getCode(),
"Action is required",
List.of("get_notions", "get_notion",
"add_notion", "update_notion",
"delete_notion"));
}
switch (action) {
case "get_notions":
return handleGetNotions(request, notionRequest, userId);
case "get_notion":
return handleGetNotion(request, notionRequest, userId);
case "add_notion":
return handleAddNotion(request, notionRequest, userId);
case "update_notion":
return handleUpdateNotion(request, notionRequest, userId);
case "delete_notion":
return handleDeleteNotion(request, notionRequest, userId);
default:
return new BaseResponse(
request.getMsgId(),
MessageCode.INVALID_ACTION.getCode(),
"Invalid action: " + action,
List.of("get_notions", "get_notion",
"add_notion", "update_notion",
"delete_notion"));
}
} catch (IllegalArgumentException e) {
log.error("Validation error: {}", e.getMessage());
return new BaseResponse(
request.getMsgId(),
MessageCode.VALIDATION_ERROR.getCode(),
e.getMessage(),
"Validation error: " + e.getMessage());
} catch (SecurityException e) {
log.error("Security error: {}", e.getMessage());
return new BaseResponse(
request.getMsgId(),
MessageCode.ACCESS_DENIED.getCode(),
e.getMessage(),
"Security error: " + e.getMessage());
} catch (Exception e) {
log.error("Unexpected error", e);
return new BaseResponse(
request.getMsgId(),
MessageCode.INTERNAL_ERROR.getCode(),
"Internal server error",
"Unexpected error: " + e);
}
}
private BaseResponse handleGetNotions(BaseRequest request, ViolationNotionRequest notionRequest, Long userId) {
if (notionRequest.getViolationId() == null) {
return new BaseResponse(
request.getMsgId(),
MessageCode.INVALID_JSON_BODY.getCode(),
"ViolationId is required for get_notions action",
"field: violation_id");
}
try {
ViolationNotionsListResponse notions = notionService.getNotionsByViolationId(
notionRequest.getViolationId(), userId);
return BaseResponse.builder()
.msgId(request.getMsgId())
.messageBody(notions)
.messageDesc(MessageCode.SUCCESS.getDescription())
.messageCode(MessageCode.SUCCESS.getCode())
.build();
} catch (IllegalArgumentException e) {
return new BaseResponse(
request.getMsgId(),
MessageCode.NOTION_NOT_FOUND.getCode(),
e.getMessage(),
"Unexpected error: " + e);
}
}
private BaseResponse handleGetNotion(BaseRequest request, ViolationNotionRequest notionRequest, Long userId) {
if (notionRequest.getNotionId() == null) {
return new BaseResponse(
request.getMsgId(),
MessageCode.INVALID_JSON_BODY.getCode(),
"NotionId is required for get_notion action",
"field: notion_id");
}
try {
ViolationNotionResponse notion = notionService.getNotionById(
notionRequest.getNotionId(), userId);
return BaseResponse.builder()
.msgId(request.getMsgId())
.messageBody(notion)
.messageDesc(MessageCode.SUCCESS.getDescription())
.messageCode(MessageCode.SUCCESS.getCode())
.build();
} catch (IllegalArgumentException e) {
return new BaseResponse(
request.getMsgId(),
MessageCode.NOTION_NOT_FOUND.getCode(),
e.getMessage(),
"Unexpected error: " + e);
}
}
private BaseResponse handleAddNotion(BaseRequest request, ViolationNotionRequest notionRequest, Long userId) {
if (notionRequest.getViolationId() == null) {
return new BaseResponse(
request.getMsgId(),
MessageCode.INVALID_JSON_BODY.getCode(),
"ViolationId is required for add_notion action",
"field: notion_id");
}
if (notionRequest.getMessage() == null || notionRequest.getMessage().trim().isEmpty()) {
return new BaseResponse(
request.getMsgId(),
MessageCode.INVALID_JSON_BODY.getCode(),
"Message is required for add_notion action",
"field: message");
}
try {
ViolationNotionResponse created = notionService.createNotion(
notionRequest.getViolationId(), userId, notionRequest.getMessage());
return BaseResponse.builder()
.msgId(request.getMsgId())
.messageBody(created)
.messageDesc(MessageCode.SUCCESS.getDescription())
.messageCode(MessageCode.SUCCESS.getCode())
.build();
} catch (IllegalArgumentException e) {
return new BaseResponse(
request.getMsgId(),
MessageCode.NOTION_NOT_FOUND.getCode(),
e.getMessage(),
"Unexpected error: " + e);
}
}
private BaseResponse handleUpdateNotion(BaseRequest request, ViolationNotionRequest notionRequest, Long userId) {
if (notionRequest.getNotionId() == null) {
return new BaseResponse(
request.getMsgId(),
MessageCode.INVALID_JSON_BODY.getCode(),
"NotionId is required for update_notion action",
"field: notion_id");
}
if (notionRequest.getMessage() == null || notionRequest.getMessage().trim().isEmpty()) {
return new BaseResponse(
request.getMsgId(),
MessageCode.INVALID_JSON_BODY.getCode(),
"Message is required for update_notion action",
"field: message");
}
try {
ViolationNotionResponse updated = notionService.updateNotion(
notionRequest.getNotionId(), userId, notionRequest.getMessage());
return BaseResponse.builder()
.msgId(request.getMsgId())
.messageBody(updated)
.messageDesc(MessageCode.SUCCESS.getDescription())
.messageCode(MessageCode.SUCCESS.getCode())
.build();
} catch (IllegalArgumentException e) {
return new BaseResponse(
request.getMsgId(),
MessageCode.NOTION_NOT_FOUND.getCode(),
e.getMessage(),
"IllegalArgumentException error: " + e);
} catch (SecurityException e) {
return new BaseResponse(
request.getMsgId(),
MessageCode.ACCESS_DENIED.getCode(),
e.getMessage(),
"SecurityException error: " + e);
}
}
private BaseResponse handleDeleteNotion(BaseRequest request, ViolationNotionRequest notionRequest, Long userId) {
if (notionRequest.getNotionId() == null) {
return new BaseResponse(
request.getMsgId(),
MessageCode.INVALID_JSON_BODY.getCode(),
"NotionId is required for delete_notion action",
"field: notion_id");
}
try {
notionService.deleteNotion(notionRequest.getNotionId(), userId);
DeleteNotionResponse deleteResponse = DeleteNotionResponse.builder()
.notionId(notionRequest.getNotionId())
.message("Notion deleted successfully")
.deleted(true)
.build();
return BaseResponse.builder()
.msgId(request.getMsgId())
.messageBody(deleteResponse)
.messageDesc(MessageCode.SUCCESS.getDescription())
.messageCode(MessageCode.SUCCESS.getCode())
.build();
} catch (IllegalArgumentException e) {
return new BaseResponse(
request.getMsgId(),
MessageCode.NOTION_NOT_FOUND.getCode(),
e.getMessage(),
null);
} catch (SecurityException e) {
return new BaseResponse(
request.getMsgId(),
MessageCode.ACCESS_DENIED.getCode(),
e.getMessage(),
null);
}
}
}