100 lines
1.9 KiB
C#
100 lines
1.9 KiB
C#
using Microsoft.AspNetCore.Http.HttpResults;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using SampleApplication.Entities;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace SampleApplication.Controllers
|
|
{
|
|
[Route("api/shapes")]
|
|
public class ShapesController : ControllerBase
|
|
{
|
|
|
|
private readonly SampleDbContext _db;
|
|
|
|
private readonly ShapeProcessor _calculator;
|
|
|
|
public ShapesController(
|
|
|
|
SampleDbContext db,
|
|
|
|
ShapeProcessor calculator)
|
|
|
|
{
|
|
|
|
_db = db;
|
|
|
|
_calculator = calculator;
|
|
|
|
}
|
|
|
|
[HttpPost]
|
|
|
|
public async Task<IActionResult> AddShapes([FromBody] List<ShapeInput> shapes)
|
|
|
|
{
|
|
|
|
// TODO:
|
|
|
|
// - Validate
|
|
|
|
// - Ignore duplicate IDs
|
|
|
|
// - Save via EF Core
|
|
|
|
if (!shapes.Any())
|
|
{
|
|
throw new ArgumentNullException();
|
|
}
|
|
|
|
this._calculator.AddShapes(shapes);
|
|
|
|
this._db.Shapes.AddRange(shapes);
|
|
|
|
await this._db.SaveChangesAsync();
|
|
|
|
return Ok();
|
|
}
|
|
|
|
[HttpGet("{id}")]
|
|
|
|
public async Task<IActionResult> Get(int id)
|
|
|
|
{
|
|
|
|
// TODO:
|
|
|
|
var shape = await this._db.Shapes.Where(s => s.Id == id).FirstOrDefaultAsync();
|
|
|
|
if(shape is null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return Ok(shape);
|
|
}
|
|
|
|
[HttpGet("stats")]
|
|
|
|
public async Task<IActionResult> Stats()
|
|
{
|
|
|
|
// TODO:
|
|
|
|
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});
|
|
}
|
|
|
|
}
|
|
}
|