Files
CartriX/CartriX.Api/Controllers/PrinterController.cs
T
2026-05-22 01:21:04 +09:00

46 lines
1.7 KiB
C#

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<IActionResult> 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<IActionResult> Models() => Ok(await _printerService.GetModelsAsync());
[HttpGet(nameof(PrintTypes))] public async Task<IActionResult> PrintTypes() => Ok((await _printerService.GetPrintTypes()).Select(t => t.ToPrintType()));
[HttpGet(nameof(ConnectionTypes))] public async Task<IActionResult> ConnectionTypes() => Ok(await _printerService.GetConnectionTypes());
#endregion
#region Post methods
[HttpPost(nameof(Add))]
public async Task<IActionResult> 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
}
}