feat: add swagger ui to create API documentation automatically

This commit is contained in:
2025-05-06 10:05:38 +02:00
parent 0e14821cec
commit 33f8e80f62
3 changed files with 77 additions and 68 deletions

View File

@@ -1,89 +1,86 @@
using Microsoft.AspNetCore.Mvc;
using System.Linq.Expressions;
using System.Reflection.Metadata;
using WebApp.Data;
using WebApp.Entities;
namespace WebApp.Controllers.Api
namespace WebApp.Controllers.Api;
[ApiController]
[Route("api/events")]
public class EventsApiController : ControllerBase
{
[ApiController]
[Route("api/events")]
public class EventsApiController : ControllerBase
private readonly ApplicationDbContext _context;
public EventsApiController(ApplicationDbContext context)
{
private readonly ApplicationDbContext _context;
_context = context;
}
public EventsApiController(ApplicationDbContext context)
{
_context = context;
}
// GET: /api/events
[HttpGet]
public IActionResult GetAll()
{
var events = _context.Events.ToList();
return Ok(events);
}
// GET: /api/events
[HttpGet]
public IActionResult GetAll()
{
var events = _context.Events.ToList();
return Ok(events);
}
// GET: /api/events/5
[HttpGet("{id}")]
public IActionResult GetById(int id)
{
var ev = _context.Events.Find(id);
if (ev == null)
return NotFound();
// GET: /api/events/5
[HttpGet("{id}")]
public IActionResult GetById(int id)
{
var ev = _context.Events.Find(id);
if (ev == null)
return NotFound();
return Ok(ev);
}
return Ok(ev);
}
// POST: /api/events
[HttpPost]
public IActionResult Create([FromBody] Event ev)
{
if (!ModelState.IsValid)
return BadRequest(ModelState);
// POST: /api/events
[HttpPost]
public IActionResult Create([FromBody] Event ev)
{
if (!ModelState.IsValid)
return BadRequest(ModelState);
ev.EventDate = DateTime.SpecifyKind(ev.EventDate, DateTimeKind.Utc);
ev.EventDate = DateTime.SpecifyKind(ev.EventDate, DateTimeKind.Utc);
_context.Events.Add(ev);
_context.SaveChanges();
_context.Events.Add(ev);
_context.SaveChanges();
return CreatedAtAction(nameof(GetById), new { id = ev.EventId }, ev);
}
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
[HttpPut("{id}")]
public IActionResult Update(int id, [FromBody] Event updated)
{
if (id != updated.EventId)
return BadRequest("ID w URL nie zgadza się z obiektem.");
var ev = _context.Events.Find(id);
if (ev == null)
return NotFound();
var ev = _context.Events.Find(id);
if (ev == null)
return NotFound();
ev.Title = updated.Title;
ev.Description = updated.Description;
ev.Location = updated.Location;
ev.EventDate = updated.EventDate;
ev.OrganisationId = updated.OrganisationId;
ev.Title = updated.Title;
ev.Description = updated.Description;
ev.Location = updated.Location;
ev.EventDate = updated.EventDate;
ev.OrganisationId = updated.OrganisationId;
_context.SaveChanges();
return NoContent();
}
_context.SaveChanges();
return NoContent();
}
// DELETE: /api/events/5
[HttpDelete("{id}")]
public IActionResult Delete(int id)
{
var ev = _context.Events.Find(id);
if (ev == null)
return NotFound();
// DELETE: /api/events/5
[HttpDelete("{id}")]
public IActionResult Delete(int id)
{
var ev = _context.Events.Find(id);
if (ev == null)
return NotFound();
_context.Events.Remove(ev);
_context.SaveChanges();
_context.Events.Remove(ev);
_context.SaveChanges();
return NoContent();
}
return NoContent();
}
}