From 73997c003684e756541b33690646dcd7fbd0e85b Mon Sep 17 00:00:00 2001 From: Musa Ahmed Date: Fri, 4 Oct 2024 21:06:12 -0400 Subject: [PATCH] deleting an item works --- .../Endpoints/ClothingEndpoints.cs | 4 +- .../Componenets/Clothing/ItemSummary.razor | 50 +++++++++++-------- .../Pages/Authenticated/Clothing.razor | 20 ++++++-- .../Services/Implementation/ApiService.cs | 12 ++++- .../Services/Interfaces/IApiService.cs | 13 +++-- WardrobeManager.Shared/Misc/Constants.cs | 2 +- 6 files changed, 70 insertions(+), 31 deletions(-) diff --git a/WardrobeManager.Api/Endpoints/ClothingEndpoints.cs b/WardrobeManager.Api/Endpoints/ClothingEndpoints.cs index 86d1669..742fbf6 100644 --- a/WardrobeManager.Api/Endpoints/ClothingEndpoints.cs +++ b/WardrobeManager.Api/Endpoints/ClothingEndpoints.cs @@ -70,12 +70,12 @@ public static async Task EditClothingItem( // Delete one clothing item // --------------------- public static async Task DeleteClothingItem( - int itemId, IClothingItemService clothingItemService, HttpContext context, IUserService userService, DatabaseContext _context + int id, IClothingItemService clothingItemService, HttpContext context, IUserService userService, DatabaseContext _context ){ User? user = context.Items["user"] as User; Debug.Assert(user != null, "Cannot get user"); - await clothingItemService.DeleteClothingItem(user.Id,itemId); + await clothingItemService.DeleteClothingItem(user.Id,id); return Results.Ok("Deleted"); } diff --git a/WardrobeManager.Presentation/Componenets/Clothing/ItemSummary.razor b/WardrobeManager.Presentation/Componenets/Clothing/ItemSummary.razor index bd1534f..ce68d21 100644 --- a/WardrobeManager.Presentation/Componenets/Clothing/ItemSummary.razor +++ b/WardrobeManager.Presentation/Componenets/Clothing/ItemSummary.razor @@ -11,34 +11,38 @@
@* Left section *@ -
- -
- @* Middle section *@ -
-
-
-

@Item.Name

- -
-
- - -
+
+
+
+ @* Text part *@ +
+
+
+

@Item.Name

+ +
+
+ + +
+
-

- Last updated: @Item.DateUpdated.ToString("MMMM d, yyyy") -

+

+ Last updated: @Item.DateUpdated.ToString("MMMM d, yyyy") +

+
@* Right section *@
+ @* Maybe not the best way but it works *@
- - +
- +
+ +
@@ -50,9 +54,15 @@ // This method is a callback from the parent that does stuff when the 'view' button is clicked [Parameter] public EventCallback ViewMethod { get; set; } + [Parameter] + public EventCallback DeleteMethod { get; set; } public async Task HandleView() { await ViewMethod.InvokeAsync(Item); } + public async Task HandleDelete() + { + await DeleteMethod.InvokeAsync(Item); + } } diff --git a/WardrobeManager.Presentation/Pages/Authenticated/Clothing.razor b/WardrobeManager.Presentation/Pages/Authenticated/Clothing.razor index f7258e3..dc5f7e0 100644 --- a/WardrobeManager.Presentation/Pages/Authenticated/Clothing.razor +++ b/WardrobeManager.Presentation/Pages/Authenticated/Clothing.razor @@ -12,7 +12,7 @@ @using System.Text.RegularExpressions -@inject IApiService ApiService +@inject IApiService _apiService @inject INotificationService _notificationService @* @inject HttpClient Http *@ @* @inject IHttpClientFactory ClientFactory *@ @@ -106,7 +106,7 @@ { foreach (var item in clothing) { - + } }
@@ -170,7 +170,7 @@ { try { - clothing = await ApiService.GetClothing(); + clothing = await _apiService.GetClothing(); } catch (AccessTokenNotAvailableException exception) @@ -189,6 +189,20 @@ { selectedItem = item; } + public async Task DeleteItem(ServerClothingItem item) + { + try + { + + await _apiService.Delete(item); + clothing = await _apiService.GetClothing(); + StateHasChanged(); + } + catch (HttpRequestException ex) + { + _notificationService.AddNotification($"Cannot fetch clothing: {ex.Message}", NotificationType.Error); + } + } @* Static Variables *@ public class FilterModel diff --git a/WardrobeManager.Presentation/Services/Implementation/ApiService.cs b/WardrobeManager.Presentation/Services/Implementation/ApiService.cs index 19e34f9..afd3996 100644 --- a/WardrobeManager.Presentation/Services/Implementation/ApiService.cs +++ b/WardrobeManager.Presentation/Services/Implementation/ApiService.cs @@ -40,7 +40,17 @@ public async Task Update(NewOrEditedClothingItemDTO clothing) public async Task Delete(ServerClothingItem clothing) { - var res = await _httpClient.DeleteAsync( "/clothingitem?id={clothing.Id}"); + var res = await _httpClient.DeleteAsync($"/clothing/{clothing.Id}"); + res.EnsureSuccessStatusCode(); + } + public async Task Wear(ServerClothingItem clothing) + { + var res = await _httpClient.GetAsync($"/actions/wear/{clothing.Id}"); + res.EnsureSuccessStatusCode(); + } + public async Task Wash(ServerClothingItem clothing) + { + var res = await _httpClient.GetAsync($"/actions/wear/{clothing.Id}"); res.EnsureSuccessStatusCode(); } diff --git a/WardrobeManager.Presentation/Services/Interfaces/IApiService.cs b/WardrobeManager.Presentation/Services/Interfaces/IApiService.cs index efb8177..1d6f2bc 100644 --- a/WardrobeManager.Presentation/Services/Interfaces/IApiService.cs +++ b/WardrobeManager.Presentation/Services/Interfaces/IApiService.cs @@ -4,10 +4,15 @@ namespace WardrobeManager.Presentation.Services.Interfaces; public interface IApiService { ValueTask DisposeAsync(); - Task?> GetClothing(); + + // Things to do with new/edited items Task Add(NewOrEditedClothingItemDTO clothing); - Task Delete(ServerClothingItem clothing); Task Update(NewOrEditedClothingItemDTO clothing); - // Task IsUserInitialized(); - // Task CreateUser(); + + // Things to do with existing items + Task?> GetClothing(); + Task Delete(ServerClothingItem clothing); + Task Wear(ServerClothingItem clothing); + Task Wash(ServerClothingItem clothing); + } diff --git a/WardrobeManager.Shared/Misc/Constants.cs b/WardrobeManager.Shared/Misc/Constants.cs index 0b6aea9..ebf412a 100644 --- a/WardrobeManager.Shared/Misc/Constants.cs +++ b/WardrobeManager.Shared/Misc/Constants.cs @@ -8,7 +8,7 @@ public static class ProjectConstants { public static string Name = "Wardrobe Manager"; public static string ApiUrl = "https://localhost:7026"; // should probably change this, also it should not be in the Shared project - public static string ProfileImage = "https://upload.internal.connectwithmusa.com/upload/eel-falcon-pig"; + public static string ProfileImage = "https://upload.internal.connectwithmusa.com/file/eel-falcon-pig"; public static string DefaultItemImage = "https://image.hm.com/assets/hm/a3/f5/a3f56f6e47160e931b78296bb9e479bfbcab3554.jpg?imwidth=2160"; public static string GetEmoji(ClothingCategory category)