added swagger ui and implemented save, get, and stats api.
This commit is contained in:
parent
703c08022b
commit
91e55f925b
@ -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)
|
||||
|
||||
{
|
||||
|
||||
@ -41,11 +42,18 @@ namespace SampleApplication.Controllers
|
||||
|
||||
// - Save via EF Core
|
||||
|
||||
if (!shapes.Any())
|
||||
{
|
||||
throw new ArgumentNullException();
|
||||
}
|
||||
|
||||
this._calculator.AddShapes(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});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -8,6 +8,6 @@ namespace SampleApplication.Entities
|
||||
|
||||
public ShapeType Type { get; set; }
|
||||
|
||||
public double[] Dimensions { get; set; }
|
||||
public required double[] Dimensions { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,5 +1,8 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.OpenApi;
|
||||
using SampleApplication;
|
||||
using System.Reflection;
|
||||
using System.IO;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
@ -8,12 +11,24 @@ var builder = WebApplication.CreateBuilder(args);
|
||||
builder.Services.AddDbContext<SampleDbContext>(options =>
|
||||
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
|
||||
|
||||
builder.Services.AddSingleton<ShapeProcessor>();
|
||||
builder.Services.AddScoped<ShapeProcessor>();
|
||||
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
||||
builder.Services.AddOpenApi();
|
||||
|
||||
builder.Services.AddSwaggerGen(c =>
|
||||
{
|
||||
// XML Comments file for API Documentation
|
||||
var xmlCommentsFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
|
||||
var xmlCommentsFullPath = Path.Combine(AppContext.BaseDirectory, xmlCommentsFile);
|
||||
|
||||
if (File.Exists(xmlCommentsFullPath))
|
||||
{
|
||||
c.IncludeXmlComments(xmlCommentsFullPath);
|
||||
}
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
|
||||
@ -21,6 +36,9 @@ var app = builder.Build();
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.MapOpenApi();
|
||||
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
@ -1,23 +1,22 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/launchsettings.json",
|
||||
{
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": false,
|
||||
"applicationUrl": "http://localhost:5078",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"dotnetRunMessages": true,
|
||||
"applicationUrl": "http://localhost:5078"
|
||||
},
|
||||
"https": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": false,
|
||||
"applicationUrl": "https://localhost:7203;http://localhost:5078",
|
||||
"launchBrowser": true,
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
},
|
||||
"dotnetRunMessages": true,
|
||||
"applicationUrl": "https://localhost:7203;http://localhost:5078"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"$schema": "https://json.schemastore.org/launchsettings.json"
|
||||
}
|
||||
@ -15,6 +15,7 @@
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="10.0.2" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="10.0.2" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.1.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@ -1,4 +1,6 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace SampleApplication
|
||||
{
|
||||
@ -9,5 +11,20 @@ namespace SampleApplication
|
||||
}
|
||||
|
||||
public DbSet<Entities.ShapeInput> Shapes { get; set; }
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
modelBuilder.Entity<Entities.ShapeInput>(entity =>
|
||||
{
|
||||
entity.HasKey(e => e.Id);
|
||||
entity.Property(e => e.Id).ValueGeneratedOnAdd();
|
||||
entity.Property(e => e.Type).IsRequired();
|
||||
entity.Property(e => e.Dimensions).IsRequired();
|
||||
entity.Property(e => e.Dimensions).HasConversion(
|
||||
v => string.Join(',', v),
|
||||
v => v.Split(',', StringSplitOptions.RemoveEmptyEntries).Select(double.Parse).ToArray());
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user