/
oxxsnaa
/
SoftwareDevelopmentTechnology
Обзор
Документация
Войти
/
oxxsnaa
/
SoftwareDevelopmentTechnology
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
Models/ApplicationModel.cs
125 строк
4 KB
Huawei
init
15 дек 2024, 21:17
15 дек 2024, 21:17
44ddcdf
Код
Авторство
О чём код?
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net.Http; using Newtonsoft.Json; using System.Windows; using System.ComponentModel; using System.Runtime.CompilerServices; using System.Net; using Newtonsoft.Json.Serialization; namespace TransioClientAPI.Models { public class ApplicationResponse { public int Number { get; set; } public DateTime DateCreate { get; set; } public string Sender { get; set; } public string Recipient { get; set; } public string Status { get; set; } } public class ApplicationModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private string url; public ApplicationModel(string url = "http://localhost:52525") { this.url = url; } private static void TryResponse(HttpResponseMessage responseMessage) { if (!responseMessage.IsSuccessStatusCode) { string error_string = responseMessage.Content.ReadAsStringAsync().Result; if (!string.IsNullOrEmpty(error_string)) { throw new Exception(error_string); } throw new Exception(responseMessage.StatusCode.ToString()); } } private static List<ApplicationResponse> ResponseToApplications(HttpResponseMessage responseMessage) { string response_string = responseMessage.Content.ReadAsStringAsync().Result; return JsonConvert.DeserializeObject<List<ApplicationResponse>>(response_string); } public List<ApplicationResponse> Applications { get { try { using (var client = new HttpClient()) { var response = client.GetAsync(url + "/api/Applications").Result; TryResponse(response); return ResponseToApplications(response); } } catch (Exception) { MessageBox.Show("Не удалось установить подключение. Повторите попытку позже", "АШИПКА", MessageBoxButton.OK, MessageBoxImage.Error); System.Environment.Exit(-1); return null; } } } public List<ApplicationResponse> ApplicationsByNumber(int number) { using (var client = new HttpClient()) { var response = client.GetAsync(url + $"api/Applications?number={number}").Result; TryResponse(response); return ResponseToApplications(response); } } public void DeleteApplication(int number) { using (var client = new HttpClient()) { var response = client.DeleteAsync(url + $"/api/Applications?number={number}").Result; TryResponse(response); PropertyChanged(this, new PropertyChangedEventArgs("")); } } public void ChangeApplication(ApplicationResponse application) { using (var client = new HttpClient()) { var response = client.PutAsJsonAsync(url + $"/api/Applications", application).Result; TryResponse(response); PropertyChanged(this, new PropertyChangedEventArgs("")); } } public void CreateApplication(ApplicationResponse application) { using (var client = new HttpClient()) { var response = client.PostAsJsonAsync(url + $"/api/Applications", application).Result; TryResponse(response); PropertyChanged(this, new PropertyChangedEventArgs("")); } } } }