From 6ba7fe230adf60a9a678faf1477a8c12b2e40749 Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Wed, 29 Jan 2025 23:58:04 +0100 Subject: [PATCH] feat(CompaniesController): OData implementiert --- .../Controllers/CompaniesController.cs | 73 +++++++++++++++++++ .../DigitalData.Swagger.MockAPI.csproj | 4 - 2 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 src/DigitalData.Swagger.MockAPI/Controllers/CompaniesController.cs diff --git a/src/DigitalData.Swagger.MockAPI/Controllers/CompaniesController.cs b/src/DigitalData.Swagger.MockAPI/Controllers/CompaniesController.cs new file mode 100644 index 0000000..bcb2202 --- /dev/null +++ b/src/DigitalData.Swagger.MockAPI/Controllers/CompaniesController.cs @@ -0,0 +1,73 @@ +using DigitalData.Swagger.MockAPI.Dtos; +using DigitalData.Swagger.MockAPI.Repos; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.OData.Formatter; +using Microsoft.AspNetCore.OData.Query; +using Microsoft.AspNetCore.OData.Results; + +namespace DigitalData.Swagger.MockAPI.Controllers; + +[Route("api/[controller]")] +[ApiController] +public class CompaniesController(ICompanyRepo repo) : ControllerBase +{ + private readonly ICompanyRepo _repo = repo; + + [EnableQuery(PageSize = 3)] + [HttpGet] + public IQueryable Get() + { + return _repo.GetAll(); + } + + [EnableQuery] + [HttpGet("{id}")] + public SingleResult Get([FromODataUri] int key) + { + return SingleResult.Create(_repo.GetById(key)); + } + + [HttpPost] + public IActionResult Post([FromBody] Company company) + { + if (!ModelState.IsValid) + { + return BadRequest(ModelState); + } + _repo.Create(company); + + return Created("companies", company); + } + + [HttpPut] + public IActionResult Put([FromODataUri] int key, [FromBody] Company company) + { + if (!ModelState.IsValid) + { + return BadRequest(ModelState); + } + + if (key != company.ID) + { + return BadRequest(); + } + + _repo.Update(company); + + return NoContent(); + } + + [HttpDelete] + public IActionResult Delete([FromODataUri] int key) + { + var company = _repo.GetById(key); + if (company is null) + { + return BadRequest(); + } + + _repo.Delete(company.First()); + + return NoContent(); + } +} \ No newline at end of file diff --git a/src/DigitalData.Swagger.MockAPI/DigitalData.Swagger.MockAPI.csproj b/src/DigitalData.Swagger.MockAPI/DigitalData.Swagger.MockAPI.csproj index 898be1c..f271bc1 100644 --- a/src/DigitalData.Swagger.MockAPI/DigitalData.Swagger.MockAPI.csproj +++ b/src/DigitalData.Swagger.MockAPI/DigitalData.Swagger.MockAPI.csproj @@ -11,8 +11,4 @@ - - - -