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

68 lines
2.3 KiB
C#

using Asp.Versioning;
using Core.Blueprint.DAL.Mongo.Contracts;
using Core.Blueprint.DAL.Mongo.Entities.Collections;
using Core.Blueprint.DAL.Mongo.Entities.Requests;
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 MongoSampleController(IMongoSampleService service) : ControllerBase
{
[HttpPost("Create")]
public async Task<IActionResult> CreateSample([FromBody] SampleRequest entity, CancellationToken cancellationToken)
{
var result = await service.CreateSample(entity, cancellationToken).ConfigureAwait(false);
return Created("CreatedWithIdAsync", result);
}
[HttpGet("GetAll")]
public async Task<IActionResult> GetEntities(CancellationToken cancellationToken)
{
var result = await service.GetAllSamples(cancellationToken).ConfigureAwait(false);
return Ok(result);
}
[HttpGet("{_id}/GetBy_Id")]
public async Task<IActionResult> GetSample([FromRoute] string _id, CancellationToken cancellationToken)
{
var result = await service.GetSampleById(_id, cancellationToken).ConfigureAwait(false);
if (result == null)
{
return NotFound("Entity not found");
}
return Ok(result);
}
[HttpPut("{_id}/Update")]
public async Task<IActionResult> UpdateSample([FromRoute] string _id, [FromBody] SampleCollection entity, CancellationToken cancellationToken)
{
if (_id != entity._Id?.ToString())
{
return BadRequest("Sample ID mismatch");
}
var result = await service.UpdateSample(_id, entity, cancellationToken).ConfigureAwait(false);
return Ok(result);
}
[HttpDelete("{_id}/Delete")]
public async Task<IActionResult> DeleteSample([FromRoute] string _id, CancellationToken cancellationToken)
{
var result = await service.DeleteSample(_id, cancellationToken).ConfigureAwait(false);
if (result is null) return NotFound();
return Ok(result);
}
}
}