feat(任务中心): 任务中心模块-定时任务-删除

This commit is contained in:
lan-yonghui 2024-01-25 21:16:39 +08:00 committed by Craftsman
parent 6afb32e22e
commit c7432a3d23
3 changed files with 64 additions and 9 deletions

View File

@ -12,10 +12,7 @@ import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.apache.shiro.authz.annotation.RequiresPermissions; import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List; import java.util.List;
@ -56,7 +53,12 @@ public class TaskCenterController {
return taskCenterService.getSystemSchedulePage(request); return taskCenterService.getSystemSchedulePage(request);
} }
@GetMapping("/scheduled/delete/{id}")
@Operation(summary = "系统-任务中心-删除定时任务")
@CheckOwner(resourceId = "#id", resourceType = "scheduled")
public void delete(@PathVariable String id) {
taskCenterService.delete(id);
}
} }

View File

@ -3,20 +3,20 @@ package io.metersphere.system.service;
import com.github.pagehelper.Page; import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod; import com.github.pagehelper.page.PageMethod;
import io.metersphere.api.domain.ApiScenario; import io.metersphere.api.domain.ApiScenario;
import io.metersphere.api.mapper.ApiDefinitionSwaggerMapper;
import io.metersphere.project.domain.Project; import io.metersphere.project.domain.Project;
import io.metersphere.project.mapper.ProjectMapper; import io.metersphere.project.mapper.ProjectMapper;
import io.metersphere.sdk.constants.ScheduleResourceType; import io.metersphere.sdk.constants.ScheduleResourceType;
import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.util.Translator; import io.metersphere.sdk.util.Translator;
import io.metersphere.system.domain.Organization; import io.metersphere.system.domain.Organization;
import io.metersphere.system.domain.Schedule;
import io.metersphere.system.dto.sdk.OptionDTO; import io.metersphere.system.dto.sdk.OptionDTO;
import io.metersphere.system.dto.taskcenter.TaskCenterScheduleDTO; import io.metersphere.system.dto.taskcenter.TaskCenterScheduleDTO;
import io.metersphere.system.dto.taskcenter.enums.ScheduleTagType; import io.metersphere.system.dto.taskcenter.enums.ScheduleTagType;
import io.metersphere.system.dto.taskcenter.request.TaskCenterSchedulePageRequest; import io.metersphere.system.dto.taskcenter.request.TaskCenterSchedulePageRequest;
import io.metersphere.system.mapper.BaseProjectMapper; import io.metersphere.system.mapper.*;
import io.metersphere.system.mapper.ExtOrganizationMapper; import io.metersphere.system.sechedule.ScheduleService;
import io.metersphere.system.mapper.ExtScheduleMapper;
import io.metersphere.system.mapper.OrganizationMapper;
import io.metersphere.system.utils.PageUtils; import io.metersphere.system.utils.PageUtils;
import io.metersphere.system.utils.Pager; import io.metersphere.system.utils.Pager;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
@ -58,6 +58,15 @@ public class TaskCenterService {
@Resource @Resource
OrganizationMapper organizationMapper; OrganizationMapper organizationMapper;
@Resource
ApiDefinitionSwaggerMapper apiDefinitionSwaggerMapper;
@Resource
ScheduleMapper scheduleMapper;
@Resource
ScheduleService scheduleService;
private static final String CREATE_TIME_SORT = "create_time desc"; private static final String CREATE_TIME_SORT = "create_time desc";
@ -212,4 +221,20 @@ public class TaskCenterService {
return organization; return organization;
} }
public void delete(String id) {
Schedule schedule = checkScheduleExit(id);
if (ScheduleTagType.API_IMPORT.getNames().contains(schedule.getResourceType())) {
apiDefinitionSwaggerMapper.deleteByPrimaryKey(schedule.getResourceId());
}
scheduleService.deleteByResourceId(schedule.getResourceId(), schedule.getJob());
}
private Schedule checkScheduleExit(String id) {
Schedule schedule = scheduleMapper.selectByPrimaryKey(id);
if (schedule == null) {
throw new MSException(Translator.get("schedule_not_exist"));
}
return schedule;
}
} }

View File

@ -1,12 +1,18 @@
package io.metersphere.system.controller; package io.metersphere.system.controller;
import io.metersphere.api.domain.ApiDefinitionSwagger;
import io.metersphere.api.mapper.ApiDefinitionSwaggerMapper;
import io.metersphere.sdk.constants.SessionConstants; import io.metersphere.sdk.constants.SessionConstants;
import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.LogUtils;
import io.metersphere.system.base.BaseTest; import io.metersphere.system.base.BaseTest;
import io.metersphere.system.controller.handler.ResultHolder; import io.metersphere.system.controller.handler.ResultHolder;
import io.metersphere.system.domain.Schedule;
import io.metersphere.system.dto.taskcenter.enums.ScheduleTagType; import io.metersphere.system.dto.taskcenter.enums.ScheduleTagType;
import io.metersphere.system.dto.taskcenter.request.TaskCenterSchedulePageRequest; import io.metersphere.system.dto.taskcenter.request.TaskCenterSchedulePageRequest;
import io.metersphere.system.mapper.ScheduleMapper;
import io.metersphere.system.utils.Pager; import io.metersphere.system.utils.Pager;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.*; import org.junit.jupiter.api.*;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
@ -35,10 +41,15 @@ class TaskCenterScheduleControllerTests extends BaseTest {
private final static String SCHEDULED_PROJECT_PAGE = BASE_PATH + "project/schedule/page"; private final static String SCHEDULED_PROJECT_PAGE = BASE_PATH + "project/schedule/page";
private final static String SCHEDULED_ORG_PAGE = BASE_PATH + "org/schedule/page"; private final static String SCHEDULED_ORG_PAGE = BASE_PATH + "org/schedule/page";
private final static String SCHEDULED_SYSTEM_PAGE = BASE_PATH + "system/schedule/page"; private final static String SCHEDULED_SYSTEM_PAGE = BASE_PATH + "system/schedule/page";
private final static String SCHEDULED_DELETE = BASE_PATH + "scheduled/delete/";
private static final ResultMatcher ERROR_REQUEST_MATCHER = status().is5xxServerError(); private static final ResultMatcher ERROR_REQUEST_MATCHER = status().is5xxServerError();
@Resource
ScheduleMapper scheduleMapper;
@Resource
ApiDefinitionSwaggerMapper apiDefinitionSwaggerMapper;
@ -138,4 +149,21 @@ class TaskCenterScheduleControllerTests extends BaseTest {
doTaskCenterSchedulePageError(SCHEDULED_ORG_PAGE, ScheduleTagType.API_IMPORT.toString()); doTaskCenterSchedulePageError(SCHEDULED_ORG_PAGE, ScheduleTagType.API_IMPORT.toString());
} }
@Test
@Order(12)
void testDel() throws Exception {
LogUtils.info("delete Schedule test");
String scheduleId = "1";
Schedule oldSchedule = scheduleMapper.selectByPrimaryKey(scheduleId);
// @@请求成功
this.requestGet(SCHEDULED_DELETE + scheduleId);
Schedule schedule = scheduleMapper.selectByPrimaryKey(scheduleId);
Assertions.assertNull(schedule);
if (ScheduleTagType.API_IMPORT.getNames().contains(oldSchedule.getType())) {
ApiDefinitionSwagger apiDefinitionSwagger = apiDefinitionSwaggerMapper.selectByPrimaryKey(oldSchedule.getResourceId());
Assertions.assertNull(apiDefinitionSwagger);
}
this.requestGet(SCHEDULED_DELETE + "schedule-121", ERROR_REQUEST_MATCHER);
}
} }