added swagger ui and implemented save, get, and stats api.

This commit is contained in:
2026-01-19 16:52:54 +05:30
parent 703c08022b
commit 91e55f925b
6 changed files with 78 additions and 21 deletions
@@ -2,6 +2,7 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SampleApplication.Entities;
using System.Threading.Tasks;
namespace SampleApplication.Controllers
{
@@ -29,7 +30,7 @@ namespace SampleApplication.Controllers
[HttpPost]
public IActionResult AddShapes(List<ShapeInput> shapes)
public async Task<IActionResult> AddShapes([FromBody] List<ShapeInput> shapes)
{
@@ -40,12 +41,19 @@ namespace SampleApplication.Controllers
// - Ignore duplicate IDs
// - Save via EF Core
if (!shapes.Any())
{
throw new ArgumentNullException();
}
this._calculator.AddShapes(shapes);
this._db.Shapes.AddRange(shapes);
this._db.Shapes.AddRange(shapes);
return Ok(shapes);
await this._db.SaveChangesAsync();
return Ok();
}
[HttpGet("{id}")]
@@ -58,19 +66,33 @@ namespace SampleApplication.Controllers
var shape = await this._db.Shapes.Where(s => s.Id == id).FirstOrDefaultAsync();
if(shape is null)
{
return NotFound();
}
return Ok(shape);
}
[HttpGet("stats")]
public IActionResult Stats()
public async Task<IActionResult> Stats()
{
// TODO:
throw new NotImplementedException();
var shapes = await this._db.Shapes.ToListAsync();
if (!shapes.Any())
{
return NoContent();
}
this._calculator.AddShapes(shapes);
var stats = this._calculator.GetStatistics();
return Ok(new {stats.TotalShapes, stats.TotalArea, stats.MaxPerimeterId});
}
}