first commit -push
This commit is contained in:
154
RobotNet.ScriptManager/Controllers/ScriptMissionsController.cs
Normal file
154
RobotNet.ScriptManager/Controllers/ScriptMissionsController.cs
Normal file
@@ -0,0 +1,154 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using RobotNet.Script.Shares;
|
||||
using RobotNet.ScriptManager.Data;
|
||||
using RobotNet.ScriptManager.Services;
|
||||
using RobotNet.Shares;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace RobotNet.ScriptManager.Controllers;
|
||||
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
public class ScriptMissionsController(ScriptMissionManager missionManager, ScriptManagerDbContext scriptManagerDb, ScriptMissionCreator missionCreator) : ControllerBase
|
||||
{
|
||||
[HttpGet]
|
||||
[Route("")]
|
||||
public IEnumerable<ScriptMissionDto> GetAlls() => missionManager.GetMissionDatas();
|
||||
|
||||
[HttpGet]
|
||||
[Route("Runner")]
|
||||
public async Task<SearchResult<InstanceMissionDto>> Search(
|
||||
[FromQuery(Name = "txtSearch")] string? txtSearch,
|
||||
[FromQuery(Name = "page")] int page,
|
||||
[FromQuery(Name = "size")] int size)
|
||||
{
|
||||
IOrderedQueryable<InstanceMission> query;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(txtSearch))
|
||||
{
|
||||
query = scriptManagerDb.InstanceMissions.AsQueryable().OrderByDescending(x => x.CreatedAt);
|
||||
}
|
||||
else
|
||||
{
|
||||
query = scriptManagerDb.InstanceMissions.AsQueryable().Where(x => x.MissionName.Contains(txtSearch)).OrderByDescending(x => x.CreatedAt);
|
||||
}
|
||||
|
||||
var total = await query.CountAsync();
|
||||
|
||||
// Đảm bảo size hợp lệ
|
||||
if (size <= 0) size = 10;
|
||||
|
||||
// Tính tổng số trang
|
||||
var totalPages = (int)Math.Ceiling(total / (double)size);
|
||||
|
||||
// Đảm bảo page hợp lệ
|
||||
if (page <= 0) page = 1;
|
||||
if (page > totalPages && totalPages > 0) page = totalPages;
|
||||
|
||||
// Nếu không có dữ liệu, trả về rỗng
|
||||
var items = Enumerable.Empty<InstanceMissionDto>();
|
||||
if (total > 0)
|
||||
{
|
||||
items = [.. await query
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.Skip((page - 1) * size)
|
||||
.Take(size)
|
||||
.Select(x => new InstanceMissionDto
|
||||
{
|
||||
Id = x.Id,
|
||||
MissionName = x.MissionName,
|
||||
Parameters = x.Parameters,
|
||||
CreatedAt = x.CreatedAt,
|
||||
Status = x.Status,
|
||||
Log = x.Log
|
||||
})
|
||||
.ToListAsync()];
|
||||
}
|
||||
|
||||
return new SearchResult<InstanceMissionDto>
|
||||
{
|
||||
Total = total,
|
||||
Page = page,
|
||||
Size = size,
|
||||
Items = items
|
||||
};
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("Runner")]
|
||||
public async Task<MessageResult<Guid>> Create(InstanceMissionCreateModel model)
|
||||
{
|
||||
try
|
||||
{
|
||||
var id = await missionCreator.CreateMissionAsync(model.Name, model.Parameters);
|
||||
return new(true, "Tạo nhiệm vụ thành công") { Data = id };
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return new MessageResult<Guid>(false, $"Lỗi khi tạo nhiệm vụ: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpDelete]
|
||||
[Route("Runner/{id:guid}")]
|
||||
public async Task<MessageResult> Cancel(Guid id, [FromQuery] string? reason)
|
||||
{
|
||||
if (missionManager.Cancel(id, $"Cancel by user '{HttpContext.User.FindFirst(ClaimTypes.Name)?.Value??HttpContext.User.Identity?.Name}' with reason '{reason}'"))
|
||||
{
|
||||
var mission = await scriptManagerDb.InstanceMissions.FindAsync(id);
|
||||
if (mission != null)
|
||||
{
|
||||
mission.Status = MissionStatus.Canceling;
|
||||
await scriptManagerDb.SaveChangesAsync();
|
||||
}
|
||||
return new MessageResult(true, "Gửi yêu cầu hủy nhiệm vụ thành công");
|
||||
}
|
||||
else
|
||||
{
|
||||
return new MessageResult(false, "Không thể hủy nhiệm vụ này hoặc nhiệm vụ không tồn tại");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPut]
|
||||
[Route("Runner/{id:guid}/pause")]
|
||||
public async Task<MessageResult> Pause(Guid id)
|
||||
{
|
||||
if (missionManager.Pause(id))
|
||||
{
|
||||
var mission = await scriptManagerDb.InstanceMissions.FindAsync(id);
|
||||
if (mission != null)
|
||||
{
|
||||
mission.Status = MissionStatus.Pausing;
|
||||
await scriptManagerDb.SaveChangesAsync();
|
||||
}
|
||||
return new MessageResult(true, "Gửi yêu cầu tạm dừng nhiệm vụ thành công");
|
||||
}
|
||||
else
|
||||
{
|
||||
return new MessageResult(false, "Không thể tạm dừng nhiệm vụ này hoặc nhiệm vụ không tồn tại");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPut]
|
||||
[Route("Runner/{id:guid}/resume")]
|
||||
public async Task<MessageResult> Resume(Guid id)
|
||||
{
|
||||
if (missionManager.Resume(id))
|
||||
{
|
||||
var mission = await scriptManagerDb.InstanceMissions.FindAsync(id);
|
||||
if (mission != null)
|
||||
{
|
||||
mission.Status = MissionStatus.Resuming;
|
||||
await scriptManagerDb.SaveChangesAsync();
|
||||
}
|
||||
return new MessageResult(true, "Gửi yêu cầu tiếp tục nhiệm vụ thành công");
|
||||
}
|
||||
else
|
||||
{
|
||||
return new MessageResult(false, "Không thể tiếp tục nhiệm vụ này hoặc nhiệm vụ không tồn tại");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user