mirror of
https://github.com/GCMatters/hermes.git
synced 2026-02-04 05:30:13 +01:00
feat: add swagger ui to create API documentation automatically
This commit is contained in:
@@ -1,89 +1,86 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using System.Linq.Expressions;
|
|
||||||
using System.Reflection.Metadata;
|
|
||||||
using WebApp.Data;
|
using WebApp.Data;
|
||||||
using WebApp.Entities;
|
using WebApp.Entities;
|
||||||
|
|
||||||
namespace WebApp.Controllers.Api
|
namespace WebApp.Controllers.Api;
|
||||||
|
|
||||||
|
[ApiController]
|
||||||
|
[Route("api/events")]
|
||||||
|
public class EventsApiController : ControllerBase
|
||||||
{
|
{
|
||||||
[ApiController]
|
private readonly ApplicationDbContext _context;
|
||||||
[Route("api/events")]
|
|
||||||
public class EventsApiController : ControllerBase
|
public EventsApiController(ApplicationDbContext context)
|
||||||
{
|
{
|
||||||
private readonly ApplicationDbContext _context;
|
_context = context;
|
||||||
|
}
|
||||||
|
|
||||||
public EventsApiController(ApplicationDbContext context)
|
// GET: /api/events
|
||||||
{
|
[HttpGet]
|
||||||
_context = context;
|
public IActionResult GetAll()
|
||||||
}
|
{
|
||||||
|
var events = _context.Events.ToList();
|
||||||
|
return Ok(events);
|
||||||
|
}
|
||||||
|
|
||||||
// GET: /api/events
|
// GET: /api/events/5
|
||||||
[HttpGet]
|
[HttpGet("{id}")]
|
||||||
public IActionResult GetAll()
|
public IActionResult GetById(int id)
|
||||||
{
|
{
|
||||||
var events = _context.Events.ToList();
|
var ev = _context.Events.Find(id);
|
||||||
return Ok(events);
|
if (ev == null)
|
||||||
}
|
return NotFound();
|
||||||
|
|
||||||
// GET: /api/events/5
|
return Ok(ev);
|
||||||
[HttpGet("{id}")]
|
}
|
||||||
public IActionResult GetById(int id)
|
|
||||||
{
|
|
||||||
var ev = _context.Events.Find(id);
|
|
||||||
if (ev == null)
|
|
||||||
return NotFound();
|
|
||||||
|
|
||||||
return Ok(ev);
|
// POST: /api/events
|
||||||
}
|
[HttpPost]
|
||||||
|
public IActionResult Create([FromBody] Event ev)
|
||||||
|
{
|
||||||
|
if (!ModelState.IsValid)
|
||||||
|
return BadRequest(ModelState);
|
||||||
|
|
||||||
// POST: /api/events
|
ev.EventDate = DateTime.SpecifyKind(ev.EventDate, DateTimeKind.Utc);
|
||||||
[HttpPost]
|
|
||||||
public IActionResult Create([FromBody] Event ev)
|
|
||||||
{
|
|
||||||
if (!ModelState.IsValid)
|
|
||||||
return BadRequest(ModelState);
|
|
||||||
|
|
||||||
ev.EventDate = DateTime.SpecifyKind(ev.EventDate, DateTimeKind.Utc);
|
_context.Events.Add(ev);
|
||||||
|
_context.SaveChanges();
|
||||||
|
|
||||||
_context.Events.Add(ev);
|
return CreatedAtAction(nameof(GetById), new { id = ev.EventId }, ev);
|
||||||
_context.SaveChanges();
|
}
|
||||||
|
|
||||||
return CreatedAtAction(nameof(GetById), new { id = ev.EventId }, ev);
|
// PUT: /api/events/5
|
||||||
}
|
[HttpPut("{id}")]
|
||||||
|
public IActionResult Update(int id, [FromBody] Event updated)
|
||||||
|
{
|
||||||
|
if (id != updated.EventId)
|
||||||
|
return BadRequest("ID w URL nie zgadza się z obiektem.");
|
||||||
|
|
||||||
// PUT: /api/events/5
|
var ev = _context.Events.Find(id);
|
||||||
[HttpPut("{id}")]
|
if (ev == null)
|
||||||
public IActionResult Update(int id, [FromBody] Event updated)
|
return NotFound();
|
||||||
{
|
|
||||||
if (id != updated.EventId)
|
|
||||||
return BadRequest("ID w URL nie zgadza się z obiektem.");
|
|
||||||
|
|
||||||
var ev = _context.Events.Find(id);
|
ev.Title = updated.Title;
|
||||||
if (ev == null)
|
ev.Description = updated.Description;
|
||||||
return NotFound();
|
ev.Location = updated.Location;
|
||||||
|
ev.EventDate = updated.EventDate;
|
||||||
|
ev.OrganisationId = updated.OrganisationId;
|
||||||
|
|
||||||
ev.Title = updated.Title;
|
_context.SaveChanges();
|
||||||
ev.Description = updated.Description;
|
return NoContent();
|
||||||
ev.Location = updated.Location;
|
}
|
||||||
ev.EventDate = updated.EventDate;
|
|
||||||
ev.OrganisationId = updated.OrganisationId;
|
|
||||||
|
|
||||||
_context.SaveChanges();
|
// DELETE: /api/events/5
|
||||||
return NoContent();
|
[HttpDelete("{id}")]
|
||||||
}
|
public IActionResult Delete(int id)
|
||||||
|
{
|
||||||
|
var ev = _context.Events.Find(id);
|
||||||
|
if (ev == null)
|
||||||
|
return NotFound();
|
||||||
|
|
||||||
// DELETE: /api/events/5
|
_context.Events.Remove(ev);
|
||||||
[HttpDelete("{id}")]
|
_context.SaveChanges();
|
||||||
public IActionResult Delete(int id)
|
|
||||||
{
|
|
||||||
var ev = _context.Events.Find(id);
|
|
||||||
if (ev == null)
|
|
||||||
return NotFound();
|
|
||||||
|
|
||||||
_context.Events.Remove(ev);
|
return NoContent();
|
||||||
_context.SaveChanges();
|
|
||||||
|
|
||||||
return NoContent();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
using Microsoft.AspNetCore.Identity;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using WebApp.Data;
|
using WebApp.Data;
|
||||||
using WebApp.Entities;
|
using WebApp.Entities;
|
||||||
@@ -15,12 +14,20 @@ builder.Services.AddDefaultIdentity<User>(options => options.SignIn.RequireConfi
|
|||||||
.AddEntityFrameworkStores<ApplicationDbContext>();
|
.AddEntityFrameworkStores<ApplicationDbContext>();
|
||||||
builder.Services.AddControllersWithViews();
|
builder.Services.AddControllersWithViews();
|
||||||
|
|
||||||
|
builder.Services.AddEndpointsApiExplorer();
|
||||||
|
builder.Services.AddSwaggerGen(c =>
|
||||||
|
{
|
||||||
|
c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo { Title = "hermes", Version = "v1" });
|
||||||
|
});
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
// Configure the HTTP request pipeline.
|
// Configure the HTTP request pipeline.
|
||||||
if (app.Environment.IsDevelopment())
|
if (app.Environment.IsDevelopment())
|
||||||
{
|
{
|
||||||
app.UseMigrationsEndPoint();
|
app.UseMigrationsEndPoint();
|
||||||
|
app.UseSwagger();
|
||||||
|
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "hermes v1"));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -21,8 +21,13 @@
|
|||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
|
<PackageReference Include="Microsoft.OpenApi" Version="1.6.24" />
|
||||||
<PackageReference Include="Npgsql" Version="9.0.3" />
|
<PackageReference Include="Npgsql" Version="9.0.3" />
|
||||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
|
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
|
||||||
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="8.1.1" />
|
||||||
|
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="8.1.1" />
|
||||||
|
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="8.1.1" />
|
||||||
|
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="8.1.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
Reference in New Issue
Block a user