From ee22a4de5005169e07479b1f11c93d6e789cf2ad Mon Sep 17 00:00:00 2001 From: syneffort Date: Mon, 26 Feb 2024 11:30:46 +0900 Subject: [PATCH] REST CRUD --- MyFirstMauiApp/UseRestService/AppShell.xaml | 7 +- .../UseRestService/AppShell.xaml.cs | 6 +- .../UseRestService/Data/PartManager.cs | 79 ++++++++++++++-- .../UseRestService/Pages/AddPartPage.xaml | 26 ++++++ .../UseRestService/Pages/AddPartPage.xaml.cs | 35 ++++++++ .../UseRestService/Pages/PartsPage.xaml | 36 ++++++-- .../UseRestService/Pages/PartsPage.xaml.cs | 3 + .../UseRestService/UseRestService.csproj | 3 + .../ViewModels/AddPartViewModel.cs | 89 +++++++++++++++++++ .../ViewModels/PartsViewModel.cs | 81 ++++++++++++++++- 10 files changed, 346 insertions(+), 19 deletions(-) create mode 100644 MyFirstMauiApp/UseRestService/Pages/AddPartPage.xaml create mode 100644 MyFirstMauiApp/UseRestService/Pages/AddPartPage.xaml.cs create mode 100644 MyFirstMauiApp/UseRestService/ViewModels/AddPartViewModel.cs diff --git a/MyFirstMauiApp/UseRestService/AppShell.xaml b/MyFirstMauiApp/UseRestService/AppShell.xaml index 2ab84d9..4251948 100644 --- a/MyFirstMauiApp/UseRestService/AppShell.xaml +++ b/MyFirstMauiApp/UseRestService/AppShell.xaml @@ -3,12 +3,11 @@ x:Class="UseRestService.AppShell" xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" - xmlns:local="clr-namespace:UseRestService" + xmlns:local="clr-namespace:UseRestService.Pages" Shell.FlyoutBehavior="Disabled"> + ContentTemplate="{DataTemplate local:PartsPage}" + Route="listparts" /> diff --git a/MyFirstMauiApp/UseRestService/AppShell.xaml.cs b/MyFirstMauiApp/UseRestService/AppShell.xaml.cs index e6fb281..399c4f2 100644 --- a/MyFirstMauiApp/UseRestService/AppShell.xaml.cs +++ b/MyFirstMauiApp/UseRestService/AppShell.xaml.cs @@ -1,10 +1,14 @@ -namespace UseRestService +using UseRestService.Pages; + +namespace UseRestService { public partial class AppShell : Shell { public AppShell() { InitializeComponent(); + + Routing.RegisterRoute("addpart", typeof(AddPartPage)); } } } diff --git a/MyFirstMauiApp/UseRestService/Data/PartManager.cs b/MyFirstMauiApp/UseRestService/Data/PartManager.cs index d582cd1..47a4ae2 100644 --- a/MyFirstMauiApp/UseRestService/Data/PartManager.cs +++ b/MyFirstMauiApp/UseRestService/Data/PartManager.cs @@ -1,7 +1,9 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net.Http.Json; using System.Text; +using System.Text.Json; using System.Threading.Tasks; namespace UseRestService.Data @@ -9,34 +11,99 @@ namespace UseRestService.Data public static class PartsManager { // TODO: Add fields for BaseAddress, Url, and authorizationKey - static readonly string BaseAddress = "URL GOES HERE"; + static readonly string BaseAddress = "https://mslearnpartsserver425916999.azurewebsites.net"; static readonly string Url = $"{BaseAddress}/api/"; static HttpClient client; + static string authorizationKey; + private static async Task GetClient() { - throw new NotImplementedException(); + if (client != null) + return client; + + client = new HttpClient(); + + if (string.IsNullOrEmpty(authorizationKey)) + { + authorizationKey = await client.GetStringAsync($"{Url}login"); + authorizationKey = JsonSerializer.Deserialize(authorizationKey); + } + + client.DefaultRequestHeaders.Add("Authorization", authorizationKey); + client.DefaultRequestHeaders.Add("Accept", "application/json"); + + return client; } public static async Task> GetAll() { - throw new NotImplementedException(); + if (Connectivity.Current.NetworkAccess != NetworkAccess.Internet) + return new List(); + + var client = await GetClient(); + string result = await client.GetStringAsync($"{Url}parts"); + + return JsonSerializer.Deserialize>(result, new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true, + }); } public static async Task Add(string partName, string supplier, string partType) { - throw new NotImplementedException(); + if (Connectivity.Current.NetworkAccess != NetworkAccess.Internet) + return new Part(); + + var part = new Part() + { + PartName = partName, + Suppliers = new List(new[] { supplier }), + PartID = string.Empty, + PartType = partType, + PartAvailableDate = DateTime.Now.Date + }; + + var msg = new HttpRequestMessage(HttpMethod.Post, $"{Url}parts"); + msg.Content = JsonContent.Create(part); + + var response = await client.SendAsync(msg); + response.EnsureSuccessStatusCode(); + + var returnedJson = await response.Content.ReadAsStringAsync(); + + var insertedPart = JsonSerializer.Deserialize(returnedJson, new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true, + }); + + return insertedPart; } public static async Task Update(Part part) { - throw new NotImplementedException(); + if (Connectivity.Current.NetworkAccess != NetworkAccess.Internet) + return; + + HttpRequestMessage msg = new(HttpMethod.Put, $"{Url}parts/{part.PartID}"); + msg.Content = JsonContent.Create(part); + + var client = await GetClient(); + var response = await client.SendAsync(msg); + response.EnsureSuccessStatusCode(); } public static async Task Delete(string partID) { - throw new NotImplementedException(); + if (Connectivity.Current.NetworkAccess != NetworkAccess.Internet) + return; + + HttpRequestMessage msg = new(HttpMethod.Delete, $"{Url}parts/{partID}"); + + var client = await GetClient(); + var response = await client.SendAsync(msg); + response.EnsureSuccessStatusCode(); } } } diff --git a/MyFirstMauiApp/UseRestService/Pages/AddPartPage.xaml b/MyFirstMauiApp/UseRestService/Pages/AddPartPage.xaml new file mode 100644 index 0000000..7dad2f5 --- /dev/null +++ b/MyFirstMauiApp/UseRestService/Pages/AddPartPage.xaml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + +