using CartriX.Api.Interfaces; using CartriX.Api.Mappers; using Microsoft.AspNetCore.Mvc; namespace CartriX.Api.Controllers { [Route("api/[controller]")] [ApiController] public class PrinterController : ControllerBase { private readonly IPrinterService _printerService; public PrinterController(IPrinterService printerService) { _printerService = printerService; } #region Get methods [HttpGet(nameof(Item))] public async Task Item([FromQuery] int id) { var Item = await _printerService.GetAsync(id); return (Item is not null) ? Ok(Item.ToItem()) : NotFound(); } [HttpGet(nameof(Models))] public async Task Models() => Ok(await _printerService.GetModelsAsync()); [HttpGet(nameof(PrintTypes))] public async Task PrintTypes() => Ok((await _printerService.GetPrintTypes()).Select(t => t.ToPrintType())); [HttpGet(nameof(ConnectionTypes))] public async Task ConnectionTypes() => Ok(await _printerService.GetConnectionTypes()); #endregion #region Post methods [HttpPost(nameof(Add))] public async Task Add([FromBody] DTOs.Printer.AddPrinter printer) { try { await _printerService.AddAsync(printer); return Ok($"Принтер {printer.name} был добавлен без ошибок."); } catch (Exception ex) { return Problem(title: $"При выполнении было получено исключение.", detail: $"{ex.Message}"); } } #endregion } }