Core.Blueprint.DAL/Core.Blueprint.DAL.API/Controllers/SqlSampleController.cs
2025-05-18 14:38:54 -06:00

64 lines
2.0 KiB
C#

using Asp.Versioning;
using Core.Blueprint.DAL.SQLServer.Contracts;
using Core.Blueprint.DAL.SQLServer.Entities;
using Core.Blueprint.DAL.SQLServer.Entities.Request;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Core.Blueprint.DAL.API.Controllers
{
[ApiVersion("1.0")]
[Route("api/v{api-version:apiVersion}/[controller]")]
[Produces("application/json")]
[ApiController]
[AllowAnonymous]
public class SqlSampleController(ISqlSampleService service) : ControllerBase
{
[HttpPost("Create")]
public async Task<ActionResult<Sample>> CreateEntity([FromBody] SampleRequest request)
{
var result = await service.AddSample(request).ConfigureAwait(false);
return Created("CreatedWithIdAsync", result);
}
[HttpGet("GetAll")]
public async Task<ActionResult<IEnumerable<Sample>>> GetEntities()
{
var result = await service.GetAllSamples().ConfigureAwait(false);
return Ok(result);
}
[HttpGet("{id}/GetById")]
public async Task<ActionResult<Sample>> GetEntity(int id)
{
var result = await service.GetSampleById(id).ConfigureAwait(false);
if (result is null) return NotFound("sample not found");
return Ok(result);
}
[HttpPut("{id}/Update")]
public async Task<ActionResult<Sample>> UpdateEntity(int id, Sample entity)
{
if (id != entity.Id)
{
return BadRequest("ID mismatch");
}
var result = await service.UpdateSample(entity).ConfigureAwait(false);
return Ok(entity);
}
[HttpDelete("{id}/Delete")]
public async Task<IActionResult> DeleteEntity(int id)
{
var result = await service.DeleteSample(id).ConfigureAwait(false);
if (result is null) return NotFound();
return Ok(result);
}
}
}