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

This commit is contained in:
Bangara Raju Kottedi 2026-01-19 16:52:54 +05:30
parent 703c08022b
commit 91e55f925b
6 changed files with 78 additions and 21 deletions

View File

@ -2,6 +2,7 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using SampleApplication.Entities; using SampleApplication.Entities;
using System.Threading.Tasks;
namespace SampleApplication.Controllers namespace SampleApplication.Controllers
{ {
@ -29,7 +30,7 @@ namespace SampleApplication.Controllers
[HttpPost] [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 // - Ignore duplicate IDs
// - Save via EF Core // - Save via EF Core
if (!shapes.Any())
{
throw new ArgumentNullException();
}
this._calculator.AddShapes(shapes); 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}")] [HttpGet("{id}")]
@ -58,19 +66,33 @@ namespace SampleApplication.Controllers
var shape = await this._db.Shapes.Where(s => s.Id == id).FirstOrDefaultAsync(); var shape = await this._db.Shapes.Where(s => s.Id == id).FirstOrDefaultAsync();
if(shape is null)
{
return NotFound();
}
return Ok(shape); return Ok(shape);
} }
[HttpGet("stats")] [HttpGet("stats")]
public IActionResult Stats() public async Task<IActionResult> Stats()
{ {
// TODO: // 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});
} }
} }

View File

@ -8,6 +8,6 @@ namespace SampleApplication.Entities
public ShapeType Type { get; set; } public ShapeType Type { get; set; }
public double[] Dimensions { get; set; } public required double[] Dimensions { get; set; }
} }
} }

View File

@ -1,5 +1,8 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi;
using SampleApplication; using SampleApplication;
using System.Reflection;
using System.IO;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
@ -8,12 +11,24 @@ var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<SampleDbContext>(options => builder.Services.AddDbContext<SampleDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"))); options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddSingleton<ShapeProcessor>(); builder.Services.AddScoped<ShapeProcessor>();
builder.Services.AddControllers(); builder.Services.AddControllers();
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi // Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi(); 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(); var app = builder.Build();
@ -21,6 +36,9 @@ var app = builder.Build();
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment())
{ {
app.MapOpenApi(); app.MapOpenApi();
app.UseSwagger();
app.UseSwaggerUI();
} }
app.UseHttpsRedirection(); app.UseHttpsRedirection();

View File

@ -1,23 +1,22 @@
{ {
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": { "profiles": {
"http": { "http": {
"commandName": "Project", "commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "http://localhost:5078",
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development"
} },
"dotnetRunMessages": true,
"applicationUrl": "http://localhost:5078"
}, },
"https": { "https": {
"commandName": "Project", "commandName": "Project",
"dotnetRunMessages": true, "launchBrowser": true,
"launchBrowser": false,
"applicationUrl": "https://localhost:7203;http://localhost:5078",
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development"
} },
"dotnetRunMessages": true,
"applicationUrl": "https://localhost:7203;http://localhost:5078"
} }
} },
} "$schema": "https://json.schemastore.org/launchsettings.json"
}

View File

@ -15,6 +15,7 @@
</PackageReference> </PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="10.0.2" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="10.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="10.0.2" /> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="10.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.1.0" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,4 +1,6 @@
using Microsoft.EntityFrameworkCore; using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;
namespace SampleApplication namespace SampleApplication
{ {
@ -9,5 +11,20 @@ namespace SampleApplication
} }
public DbSet<Entities.ShapeInput> Shapes { get; set; } 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());
});
}
} }
} }