Set up multi-project solution with DbFirst API, Application, Domain, and Infrastructure layers. Implemented database-first EF Core for the Catalog entity, including domain, DTOs, repository, service, and controller. Configured AutoMapper, DI, Swagger, and project settings. Added .gitattributes and initial configuration files.
64 lines
1.8 KiB
C#
64 lines
1.8 KiB
C#
using DbFirst.Application.Catalogs;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace DbFirst.API.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class CatalogsController : ControllerBase
|
|
{
|
|
private readonly ICatalogService _service;
|
|
|
|
public CatalogsController(ICatalogService service)
|
|
{
|
|
_service = service;
|
|
}
|
|
|
|
[HttpGet]
|
|
public async Task<ActionResult<IEnumerable<CatalogDto>>> GetAll(CancellationToken cancellationToken)
|
|
{
|
|
var result = await _service.GetAllAsync(cancellationToken);
|
|
return Ok(result);
|
|
}
|
|
|
|
[HttpGet("{id:int}")]
|
|
public async Task<ActionResult<CatalogDto>> GetById(int id, CancellationToken cancellationToken)
|
|
{
|
|
var result = await _service.GetByIdAsync(id, cancellationToken);
|
|
if (result == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
return Ok(result);
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<ActionResult<CatalogDto>> Create(CatalogDto dto, CancellationToken cancellationToken)
|
|
{
|
|
var created = await _service.CreateAsync(dto, cancellationToken);
|
|
return CreatedAtAction(nameof(GetById), new { id = created.Guid }, created);
|
|
}
|
|
|
|
[HttpPut("{id:int}")]
|
|
public async Task<IActionResult> Update(int id, CatalogDto dto, CancellationToken cancellationToken)
|
|
{
|
|
var updated = await _service.UpdateAsync(id, dto, cancellationToken);
|
|
if (!updated)
|
|
{
|
|
return NotFound();
|
|
}
|
|
return NoContent();
|
|
}
|
|
|
|
[HttpDelete("{id:int}")]
|
|
public async Task<IActionResult> Delete(int id, CancellationToken cancellationToken)
|
|
{
|
|
var deleted = await _service.DeleteAsync(id, cancellationToken);
|
|
if (!deleted)
|
|
{
|
|
return NotFound();
|
|
}
|
|
return NoContent();
|
|
}
|
|
}
|