Files
KART-SERVER-JAVA/src/main/java/com/kri/kart/server/controller/LogController.java

53 lines
2.4 KiB
Java

package com.kri.kart.server.controller;
import com.kri.common.feature.web.BusinessException;
import com.kri.common.feature.web.RequestUserContext;
import com.kri.kart.server.common.consts.KartConstant;
import com.kri.kart.server.dto.log.VisitLogRequestDto;
import com.kri.kart.server.dto.project.ProjectRefDto;
import com.kri.kart.server.dto.share.ShareLinkDto;
import com.kri.kart.server.service.LogService;
import com.kri.kart.server.service.ProjectService;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/v1/log")
@RequiredArgsConstructor
@ConditionalOnProperty(prefix = "kri.kart.database", name = "enabled", havingValue = "true")
public class LogController {
private final LogService logService;
private final ProjectService projectService;
@PostMapping(value = "/visit", produces = MediaType.TEXT_PLAIN_VALUE)
public String visit(
@Valid @RequestBody VisitLogRequestDto request,
@Parameter(hidden = true) RequestUserContext ctx,
HttpServletRequest servletRequest) {
ShareLinkDto link =
(ShareLinkDto) servletRequest.getAttribute(KartConstant.REQUEST_LINK_ATTRIBUTE);
if (link != null
&& (link.projectType() != request.projectType()
|| !link.pkey().equals(request.pkey()))) {
throw BusinessException.forbidden("EXTERNAL_SCOPE_DENIED", "공유 프로젝트의 방문 로그만 기록할 수 있습니다.");
}
if (link == null && (request.projectType() != null || request.pkey() != null)) {
if (request.projectType() == null || request.pkey() == null) {
throw BusinessException.badRequest("projectType과 pkey를 함께 입력해 주세요.");
}
projectService.requireAccess(new ProjectRefDto(request.projectType(), request.pkey()), ctx);
}
logService.writeVisitLog(
request.projectType(), request.pkey(), request.logContent(), ctx);
return "PASS";
}
}