2024-04-22 21:26:34 +08:00
|
|
|
|
using Canon.Server.DataTransferObjects;
|
|
|
|
|
using Canon.Server.Entities;
|
2024-04-19 14:59:45 +08:00
|
|
|
|
using Canon.Server.Services;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-05-13 22:29:32 +08:00
|
|
|
|
using MongoDB.Bson;
|
2024-04-19 14:59:45 +08:00
|
|
|
|
|
|
|
|
|
namespace Canon.Server.Controllers;
|
|
|
|
|
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("api/[controller]")]
|
|
|
|
|
public class CompilerController(CompileDbContext dbContext, CompilerService compilerService) : ControllerBase
|
|
|
|
|
{
|
2024-04-22 21:26:34 +08:00
|
|
|
|
[HttpGet]
|
|
|
|
|
public async Task<ActionResult<IEnumerable<CompileResponse>>> ListResponses([FromQuery] int start = 1, [FromQuery] int end = 20)
|
|
|
|
|
{
|
|
|
|
|
if (end <= start || start < 1)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IQueryable<CompileResult> results = from item in dbContext.CompileResults.AsNoTracking()
|
|
|
|
|
orderby item.CompileTime descending
|
|
|
|
|
select item;
|
|
|
|
|
|
|
|
|
|
IEnumerable<CompileResult> cachedResults = await results.ToListAsync();
|
|
|
|
|
|
|
|
|
|
IEnumerable<CompileResponse> responses = cachedResults.Skip(start - 1)
|
|
|
|
|
.Take(end - start + 1)
|
|
|
|
|
.Select(result => new CompileResponse(result));
|
|
|
|
|
|
|
|
|
|
return Ok(responses);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-19 14:59:45 +08:00
|
|
|
|
[HttpGet("{compileId}")]
|
|
|
|
|
public async Task<ActionResult<CompileResponse>> GetResponse(string compileId)
|
|
|
|
|
{
|
2024-04-22 21:26:34 +08:00
|
|
|
|
CompileResult? result = await (from item in dbContext.CompileResults.AsNoTracking()
|
2024-05-13 22:29:32 +08:00
|
|
|
|
where item.Id == new ObjectId(compileId)
|
2024-04-22 21:26:34 +08:00
|
|
|
|
select item).FirstOrDefaultAsync();
|
2024-04-19 14:59:45 +08:00
|
|
|
|
|
|
|
|
|
if (result is null)
|
|
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-22 21:26:34 +08:00
|
|
|
|
return Ok(new CompileResponse(result));
|
2024-04-19 14:59:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public async Task<ActionResult<CompileResponse>> Compile(SourceCode sourceCode)
|
|
|
|
|
{
|
|
|
|
|
CompileResponse response = await compilerService.Compile(sourceCode);
|
|
|
|
|
return Ok(response);
|
|
|
|
|
}
|
2024-04-22 21:26:34 +08:00
|
|
|
|
|
|
|
|
|
[HttpDelete("{compileId}")]
|
|
|
|
|
[ProducesResponseType(204)]
|
|
|
|
|
[ProducesResponseType(404)]
|
|
|
|
|
public async Task<IActionResult> DeleteCompileResult(string compileId)
|
|
|
|
|
{
|
|
|
|
|
CompileResult? result = await (from item in dbContext.CompileResults
|
2024-05-13 22:29:32 +08:00
|
|
|
|
where item.Id == new ObjectId(compileId)
|
2024-04-22 21:26:34 +08:00
|
|
|
|
select item).FirstOrDefaultAsync();
|
|
|
|
|
|
|
|
|
|
if (result is null)
|
|
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbContext.CompileResults.Remove(result);
|
|
|
|
|
await dbContext.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpDelete]
|
|
|
|
|
[ProducesResponseType(204)]
|
|
|
|
|
public async Task<IActionResult> DeleteAllCompileResult()
|
|
|
|
|
{
|
|
|
|
|
dbContext.CompileResults.RemoveRange(dbContext.CompileResults);
|
|
|
|
|
await dbContext.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
2024-04-19 14:59:45 +08:00
|
|
|
|
}
|