mirror of
https://gitlab.com/Hukuta47/cartrix.git
synced 2026-12-05 04:20:55 +00:00
51 lines
2.2 KiB
C#
51 lines
2.2 KiB
C#
using CartriX.Api.Services;
|
|
using CartriX.Shared.DTOs.Printer;
|
|
using CartriX.Shared.Mappers;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace CartriX.Api.Controllers
|
|
{
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
public class PrinterController : ControllerBase
|
|
{
|
|
private readonly PrinterService _printerService;
|
|
public PrinterController(PrinterService printerService) => _printerService = printerService;
|
|
|
|
[HttpGet("GetAll")]
|
|
public async Task<IActionResult> GetAll()
|
|
{
|
|
Console.WriteLine("Поступил запрос на вывод всего списка принтеров.");
|
|
var result = await _printerService.GetAll();
|
|
|
|
Console.WriteLine($"Конверация в {nameof(PrinterDto)}");
|
|
var coverted = result.Select(p => p.ToDto());
|
|
|
|
Console.WriteLine("Запрос был сделан успешно, вывод.");
|
|
return Ok(coverted);
|
|
}
|
|
[HttpGet("GetById/{id}")]
|
|
public async Task<IActionResult> GetById([FromRoute]int id)
|
|
{
|
|
Console.WriteLine("Поступил запрос на вывод всего списка принтеров.");
|
|
var result = await _printerService.GetById(id);
|
|
Console.WriteLine("Проверка, есть ли объект в базе данных.");
|
|
if (result == null)
|
|
{
|
|
Console.WriteLine("Проверка пройдена, объект не найден.");
|
|
return NotFound();
|
|
}
|
|
Console.WriteLine($"Проверка пройдена, объект найден, конвертация в {nameof(PrinterDto)}");
|
|
var convert = result.ToDto();
|
|
Console.WriteLine("Объект конвертирован для вывода информации");
|
|
return Ok(convert);
|
|
}
|
|
[HttpPost("AddPrinter")]
|
|
public async Task<IActionResult> AddPrinter([FromBody] CreatePrinterRequestDto requestDto)
|
|
{
|
|
await _printerService.AddAsync(requestDto);
|
|
return Ok($"Принтер с названием хоста \"{requestDto.hostname}\" был добавлен.");
|
|
}
|
|
}
|
|
}
|