@using CartriX.Shared.DTOs.Printer @using Newtonsoft.Json @inject HttpClient Http @inject Radzen.NotificationService NotificationService @page "/PrinterAdd" @switch (variants) { case VariantsToAdd.IpOrHostname: @if (isLoading) {

Загрузка...

} @if (errorMessage != null) { @errorMessage } @if (queryPrinterToAdd != null) { Результат запроса от API: }
break; }
@code { private enum VariantsToAdd { IpOrHostname, Custom } private VariantsToAdd variants = VariantsToAdd.IpOrHostname; private QueryPrinterToAdd? queryPrinterToAdd = null; private string ipAddressInput = string.Empty; private string? errorMessage = null; private bool isLoading = false; private bool isAdding = false; private async Task OnQueryPrinter() { if (string.IsNullOrWhiteSpace(ipAddressInput)) { errorMessage = "Пожалуйста, введите IP адрес или имя хоста"; return; } isLoading = true; errorMessage = null; queryPrinterToAdd = null; try { var response = await Http.GetAsync($"http://localhost:5197/api/Printer/GetSNMP/{Uri.EscapeDataString(ipAddressInput)}"); var responseContent = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { var settings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver() }; queryPrinterToAdd = JsonConvert.DeserializeObject(responseContent, settings); } else { errorMessage = $"Ошибка {response.StatusCode}: {responseContent}"; } } catch (Exception ex) { errorMessage = $"Ошибка при запросе: {ex.Message}"; } finally { isLoading = false; } } /// /// Проверяет, заполнены ли обязательные поля /// private bool CanAddPrinter() { return queryPrinterToAdd != null && !string.IsNullOrWhiteSpace(queryPrinterToAdd.hostname) && !string.IsNullOrWhiteSpace(queryPrinterToAdd.ipAddress); } /// /// Добавляет принтер в БД /// private async Task OnAddPrinter() { if (!CanAddPrinter()) return; isAdding = true; try { var response = await Http.PostAsJsonAsync("http://localhost:5197/api/Printer", queryPrinterToAdd); if (response.IsSuccessStatusCode) { NotificationService.Notify( new NotificationMessage { Severity = NotificationSeverity.Success, Summary = "Успешно", Detail = "Принтер успешно добавлен", Duration = 4000 }); // Очищаем форму queryPrinterToAdd = null; ipAddressInput = string.Empty; } else { var errorContent = await response.Content.ReadAsStringAsync(); NotificationService.Notify( new NotificationMessage { Severity = NotificationSeverity.Error, Summary = "Ошибка", Detail = $"Ошибка {response.StatusCode}: {errorContent}", Duration = 4000 }); } } catch (Exception ex) { NotificationService.Notify( new NotificationMessage { Severity = NotificationSeverity.Error, Summary = "Ошибка", Detail = $"Ошибка при добавлении: {ex.Message}", Duration = 4000 }); } finally { isAdding = false; } } }