Перевел некоторые методы на асинхрон

This commit is contained in:
Hukuta committed 2026-05-06 00:36:49 +09:00
1 parent 02160f451f
commit ae561f8311
2 files changed
+9 -15

No files matched your search

+4 -8
View File
@@ -2,7 +2,6 @@
using CartriX.Shared.Classes; using CartriX.Shared.Classes;
using CartriX.Shared.DTOs.Printer; using CartriX.Shared.DTOs.Printer;
using CartriX.Shared.Mappers; using CartriX.Shared.Mappers;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
namespace CartriX.Api.Controllers namespace CartriX.Api.Controllers
@@ -18,22 +17,19 @@ namespace CartriX.Api.Controllers
_context = context; _context = context;
_printerNetworkService = printerNetworkService; _printerNetworkService = printerNetworkService;
} }
[HttpGet("{IpAddressOrHost}")] [HttpGet("{IpAddressOrHost}")]
public async Task<ActionResult> GetInformationFromSnmp(string IpAddressOrHost) public async Task<ActionResult> GetInformationFromSnmp(string IpAddressOrHost)
{ {
var value = await _printerNetworkService.GetPrinterListParametersAsync(IpAddressOrHost); var value = await _printerNetworkService.GetPrinterListParametersAsync(IpAddressOrHost);
return Ok(value); return Ok(value);
} }
[HttpPost] [HttpPost]
public IActionResult AddPrinter([FromBody] CreatePrinterRequestDto requestDto) public async Task<IActionResult> AddPrinter([FromBody] CreatePrinterRequestDto requestDto)
{ {
var printerModel = requestDto.ToPrinterModel(_context); var printerModel = requestDto.ToPrinterModelAsync(_context);
_context.Printers.Add(printerModel); await _context.Printers.AddAsync(printerModel);
_context.SaveChanges(); await _context.SaveChangesAsync();
return Ok(printerModel.ToDto()); return Ok(printerModel.ToDto());
} }
+5 -7
View File
@@ -1,9 +1,7 @@
using CartriX.Shared.Classes; using CartriX.Shared.Classes;
using CartriX.Shared.DTOs.Printer; using CartriX.Shared.DTOs.Printer;
using CartriX.Shared.Models; using CartriX.Shared.Models;
using System; using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Text;
namespace CartriX.Shared.Mappers namespace CartriX.Shared.Mappers
{ {
@@ -16,7 +14,7 @@ namespace CartriX.Shared.Mappers
Name = printer.hostname Name = printer.hostname
}; };
} }
public static PrinterDto ToDto(this Printer printer) public static async Task<PrinterDto> ToDtoAsync(this Printer printer)
{ {
return new() return new()
{ {
@@ -26,10 +24,10 @@ namespace CartriX.Shared.Mappers
modelName = printer.model.name modelName = printer.model.name
}; };
} }
public static Printer ToPrinterModel(this CreatePrinterRequestDto requestDto, AppDbContext context) public static async Task<Printer> ToPrinterModelAsync(this CreatePrinterRequestDto requestDto, AppDbContext context)
{ {
Location location = (context.Locations.Any(l => l.name == requestDto.locationValue)) ? context.Locations.First(l => l.name == requestDto.locationValue) : new() { name = requestDto.locationValue }; Location location = (await context.Locations.AnyAsync(l => l.name == requestDto.locationValue)) ? await context.Locations.FirstAsync(l => l.name == requestDto.locationValue) : new() { name = requestDto.locationValue };
PrinterModel model = (context.PrinterModels.Any(m => m.name == requestDto.modelName)) ? context.PrinterModels.First(p => p.name == requestDto.modelName) : new() { name = requestDto.modelName }; PrinterModel model = (await context.PrinterModels.AnyAsync(m => m.name == requestDto.modelName)) ? await context.PrinterModels.FirstAsync(p => p.name == requestDto.modelName) : new() { name = requestDto.modelName };
return new() return new()
{ {