feat: 编译历史记录和代码生成结果(#51)

地址已绑定编译结果,支持历史记录切换功能

Co-authored-by: jackfiled <xcrenchangjun@outlook.com>
Reviewed-on: PostGuard/Canon#51
Co-authored-by: Ichirinko <1621543655@qq.com>
Co-committed-by: Ichirinko <1621543655@qq.com>
This commit is contained in:
Ichirinko
2024-04-22 21:26:34 +08:00
committed by jackfiled
parent 3a584751dc
commit 366991046a
26 changed files with 1058 additions and 2002 deletions

View File

@@ -1,4 +1,5 @@
using Canon.Server.Models;
using Canon.Server.DataTransferObjects;
using Canon.Server.Entities;
using Canon.Server.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
@@ -9,19 +10,40 @@ namespace Canon.Server.Controllers;
[Route("api/[controller]")]
public class CompilerController(CompileDbContext dbContext, CompilerService compilerService) : ControllerBase
{
[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);
}
[HttpGet("{compileId}")]
public async Task<ActionResult<CompileResponse>> GetResponse(string compileId)
{
CompileResult? result = await dbContext.CompileResults
.Where(r => r.CompileId == compileId)
.FirstOrDefaultAsync();
CompileResult? result = await (from item in dbContext.CompileResults.AsNoTracking()
where item.CompileId == compileId
select item).FirstOrDefaultAsync();
if (result is null)
{
return NotFound();
}
return Ok(result);
return Ok(new CompileResponse(result));
}
[HttpPost]
@@ -30,4 +52,34 @@ public class CompilerController(CompileDbContext dbContext, CompilerService comp
CompileResponse response = await compilerService.Compile(sourceCode);
return Ok(response);
}
[HttpDelete("{compileId}")]
[ProducesResponseType(204)]
[ProducesResponseType(404)]
public async Task<IActionResult> DeleteCompileResult(string compileId)
{
CompileResult? result = await (from item in dbContext.CompileResults
where item.CompileId == compileId
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();
}
}