From df0737e55afeb5c01eb39909605970d1d3a73ffc Mon Sep 17 00:00:00 2001 From: Melodi <62381002+Melodi17@users.noreply.github.com> Date: Sun, 13 Sep 2026 13:22:20 +1000 Subject: refactor: cleanup; feat: connection to agent --- src/marlin.web/Controllers/AgentsController.cs | 89 ++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 src/marlin.web/Controllers/AgentsController.cs (limited to 'src/marlin.web/Controllers/AgentsController.cs') diff --git a/src/marlin.web/Controllers/AgentsController.cs b/src/marlin.web/Controllers/AgentsController.cs new file mode 100644 index 0000000..199acf9 --- /dev/null +++ b/src/marlin.web/Controllers/AgentsController.cs @@ -0,0 +1,89 @@ +namespace marlin.web.Controllers; + +using core.models; +using core.util; +using Data; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using Models; + +[Route("agents")] +public class AgentsController(ApplicationDbContext dbContext) : Controller +{ + [HttpGet("list")] + public async Task List(CancellationToken ct) + { + var items = await dbContext + .Agents + .Include(x => x.Jobs.Where(j => j.Status == JobStatus.Active)) + .ToListAsync(ct); + + return this.View(items); + } + + [HttpGet("details/{id}")] + public async Task Details(Guid id, CancellationToken ct) + { + var item = await dbContext.Agents + .Include(x => x.Jobs.Where(j => j.Status == JobStatus.Active)) + .FirstOrDefaultAsync(x => x.Id == id, ct); + + if (item == null) + return this.NotFound(); + + return this.View(item); + } + + [HttpPost("edit/{id}")] + public async Task Edit(Guid id, Agent agent, CancellationToken ct) + { + var item = await dbContext.Agents + .FirstOrDefaultAsync(x => x.Id == id, ct); + + if (item == null) + return this.NotFound(); + + item.Name = agent.Name; + item.MaxConcurrentJobs = agent.MaxConcurrentJobs; + + await dbContext.SaveChangesAsync(ct); + + return this.RedirectToAction("Details", new { id }); + } + + [HttpPost("details/{id}/regenerate-token")] + public async Task RegenerateToken(Guid id, CancellationToken ct) + { + var item = await dbContext.Agents + .FirstOrDefaultAsync(x => x.Id == id, ct); + + if (item == null) + return this.NotFound(); + + item.AuthToken = SecureTokenGenerator.CreateAgentToken(); + + await dbContext.SaveChangesAsync(ct); + + return this.RedirectToAction("Details", new { id }); + } + + [HttpGet("create")] + public IActionResult Create() + { + return this.View(); + } + + [HttpPost("create")] + public async Task CreatePost(Agent agent, CancellationToken ct) + { + agent.Id = Guid.Empty; + agent.AuthToken = SecureTokenGenerator.CreateAgentToken(); + agent.LastSeen = DateTime.UtcNow; + agent.Capabilities = new CapabilitySet(); + + await dbContext.Agents.AddAsync(agent, ct); + await dbContext.SaveChangesAsync(ct); + + return this.RedirectToAction("Details", new { id = agent.Id }); + } +} \ No newline at end of file -- cgit v1.3.1