feat(CompaniesController): OData implementiert
This commit is contained in:
parent
9473ad7619
commit
6ba7fe230a
@ -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<Company> Get()
|
||||
{
|
||||
return _repo.GetAll();
|
||||
}
|
||||
|
||||
[EnableQuery]
|
||||
[HttpGet("{id}")]
|
||||
public SingleResult<Company> 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();
|
||||
}
|
||||
}
|
||||
@ -11,8 +11,4 @@
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Controllers\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user