lti-provider/src/main/java/ru/oa2/lti/application/controller/TaskController.java

150 lines
5.6 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package ru.oa2.lti.application.controller;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import ru.oa2.lti.application.service.history.HistoryService;
import ru.oa2.lti.application.service.task.TaskService;
import ru.oa2.lti.domain.model.ResultRequest;
import ru.oa2.lti.domain.model.auth.LtiLogin;
import ru.oa2.lti.domain.model.auth.Payload;
import ru.oa2.lti.domain.model.results.ResultResponse;
import ru.oa2.lti.domain.model.task.RequestUpdateTask;
import ru.oa2.lti.domain.model.task.TaskData;
import java.util.Objects;
/*
Контроллер работы с задачами(лабораторными работами)
GET - возвращает контекст лабораторной работы
POST - обновление/создание лабораторной работы (только для преподавателей/администраторов)
POST /submit - отправка на автоматическую проверку
*/
@Controller
@RequestMapping("/tool/lti/task")
public class TaskController {
private final TaskService taskService;
private final HistoryService historyService;
public TaskController(TaskService taskService, HistoryService historyService) {
this.taskService = taskService;
this.historyService = historyService;
}
@GetMapping
public String showDockerTas(Model model,
HttpServletRequest request) {
var session = request.getSession();
var payload = (Payload) session.getAttribute("payload");
if (payload != null) {
var data = taskService.getTask(payload.getContextId());
if (data == null) {
historyService.logAction(
payload.getDeploymentId(),
payload.getContextId(),
"TASK_ERROR",
"Task not found"
);
return "redirect:/error";
}
model.addAttribute("name", data.getName());
model.addAttribute("description", data.getDescription());
// Логирование просмотра задачи
historyService.logAction(
payload.getDeploymentId(),
payload.getContextId(),
"TASK_VIEW",
String.format("Task viewed: %s, isCoach=%s", data.getName(), payload.getCoach())
);
if (Objects.requireNonNull(payload).getCoach()) {
model.addAttribute("initScript", data.getInitScript());
model.addAttribute("checkScript", data.getVerificationScript());
model.addAttribute("deleteScript", data.getDeleteScript());
return "task-editor";
}
}
return "task";
}
@PostMapping
public ResponseEntity updateTask(@RequestBody TaskData data,
HttpServletRequest request) {
var session = request.getSession();
var payload = (Payload) session.getAttribute("payload");
if (payload != null && payload.getCoach()) {
// Логирование обновления задачи
historyService.logAction(
payload.getDeploymentId(),
payload.getContextId(),
"TASK_UPDATE",
String.format("Task updated by coach: %s", data.getName())
);
return ResponseEntity.accepted().body(
taskService.saveTask(RequestUpdateTask.builder()
.contextId(payload.getContextId())
.data(data)
.build())
);
}
// Логирование попытки несанкционированного обновления
if (payload != null) {
historyService.logAction(
payload.getDeploymentId(),
payload.getContextId(),
"TASK_UPDATE_DENIED",
"Unauthorized task update attempt"
);
}
return ResponseEntity.status(HttpStatusCode.valueOf(403))
.body(new ResultResponse("forbidden"));
}
@PostMapping("/submit")
public ResponseEntity result(HttpServletRequest req) {
HttpSession session = req.getSession(false);
if (session == null) return ResponseEntity.status(401).build();
var ltiLogin = (LtiLogin) session.getAttribute("lti_login");
var idToken = (String) session.getAttribute("id_token");
var payload = (Payload) session.getAttribute("payload");
// Логирование отправки задачи на проверку
if (payload != null) {
historyService.logAction(
payload.getDeploymentId(),
payload.getContextId(),
"TASK_SUBMIT",
String.format("Task submitted for checking, clientId=%s", ltiLogin.getClientId())
);
}
return ResponseEntity
.accepted()
.body(taskService.checkTask(new ResultRequest(ltiLogin.getClientId(), idToken)));
}
}