From fcc9084c4fd39c2645958f9390c677fcf1d21878 Mon Sep 17 00:00:00 2001 From: Jon Hilton Date: Mon, 13 Jul 2020 20:00:32 +0100 Subject: [PATCH] Added shopping cart example --- .vscode/launch.json | 17 +++++++++++ .vscode/tasks.json | 42 +++++++++++++++++++++++++++ Client/Pages/Cart/Cart.razor | 11 +++++++ Client/Pages/Cart/Cart.razor.cs | 30 +++++++++++++++++++ Client/Pages/Cart/Item.razor | 20 +++++++++++++ Client/Pages/Cart/Item.razor.cs | 42 +++++++++++++++++++++++++++ Client/Pages/Cart/Quantity.razor | 9 ++++++ Client/Pages/Cart/Quantity.razor.cs | 19 ++++++++++++ Server/Data/Database.cs | 4 +-- Server/Properties/launchSettings.json | 2 +- 10 files changed, 193 insertions(+), 3 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 .vscode/tasks.json create mode 100644 Client/Pages/Cart/Cart.razor create mode 100644 Client/Pages/Cart/Cart.razor.cs create mode 100644 Client/Pages/Cart/Item.razor create mode 100644 Client/Pages/Cart/Item.razor.cs create mode 100644 Client/Pages/Cart/Quantity.razor create mode 100644 Client/Pages/Cart/Quantity.razor.cs diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..a7b60b5 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,17 @@ +{ + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md + "version": "0.2.0", + "configurations": [ + { + "name": "Launch and Debug Hosted Blazor WebAssembly App", + "type": "blazorwasm", + "request": "launch", + "hosted": true, + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/Server/bin/Debug/netcoreapp3.1/ShoppingCartStarter.Server.dll", + "cwd": "${workspaceFolder}/Server" + } + ] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..ed120a7 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,42 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/Server/ShoppingCartStarter.Server.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "publish", + "command": "dotnet", + "type": "process", + "args": [ + "publish", + "${workspaceFolder}/Server/ShoppingCartStarter.Server.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "watch", + "command": "dotnet", + "type": "process", + "args": [ + "watch", + "run", + "${workspaceFolder}/Server/ShoppingCartStarter.Server.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/Client/Pages/Cart/Cart.razor b/Client/Pages/Cart/Cart.razor new file mode 100644 index 0000000..e0b21d4 --- /dev/null +++ b/Client/Pages/Cart/Cart.razor @@ -0,0 +1,11 @@ +@inherits CartBase +@page "/cart" + +

Your Shopping Cart

+ +@if(Model != null){ + @foreach (var line in Model.Items) + { + + } +} \ No newline at end of file diff --git a/Client/Pages/Cart/Cart.razor.cs b/Client/Pages/Cart/Cart.razor.cs new file mode 100644 index 0000000..650deef --- /dev/null +++ b/Client/Pages/Cart/Cart.razor.cs @@ -0,0 +1,30 @@ +using System.Net.Http; +using System.Net.Http.Json; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Components; +using ShoppingCartStarter.Shared.Cart; + +namespace ShoppingCartStarter.Client.Pages.Cart +{ + public class CartBase : ComponentBase + { + [Inject] private HttpClient Http { get; set; } + + protected Details.Model Model { get; set; } + + protected override async Task OnInitializedAsync() + { + await ReloadCart(); + } + + protected void RemoveItem(Details.Model.LineItem item) + { + Model.Items.Remove(item); + } + + protected async Task ReloadCart() + { + Model = await Http.GetFromJsonAsync("api/cart"); + } + } +} \ No newline at end of file diff --git a/Client/Pages/Cart/Item.razor b/Client/Pages/Cart/Item.razor new file mode 100644 index 0000000..8b363c1 --- /dev/null +++ b/Client/Pages/Cart/Item.razor @@ -0,0 +1,20 @@ +@inherits ItemBase + +
+
+
+ +
+

@Details.Name

+
+ + + +
+
+
+
+
+ £@Details.Price +
+
\ No newline at end of file diff --git a/Client/Pages/Cart/Item.razor.cs b/Client/Pages/Cart/Item.razor.cs new file mode 100644 index 0000000..5d32688 --- /dev/null +++ b/Client/Pages/Cart/Item.razor.cs @@ -0,0 +1,42 @@ +using Microsoft.AspNetCore.Components; +using ShoppingCartStarter.Shared.Cart; +using ShoppingCartStarter.Shared.Cart.LineItem; +using System; +using System.Net.Http; +using System.Net.Http.Json; +using System.Threading.Tasks; + +namespace ShoppingCartStarter.Client.Pages.Cart +{ + public class ItemBase : ComponentBase + { + [Parameter] + public Details.Model.LineItem Details { get; set; } + + [Inject] + public HttpClient Http { get; set; } + + [Parameter] + public EventCallback OnDeleted { get; set; } + + [Parameter] + public EventCallback OnQuantityChanged { get; set; } + + protected async Task OnDeleteClicked() + { + await Http.DeleteAsync($"api/cart/lines/{Details.Id}"); + await OnDeleted.InvokeAsync(Details); + } + + protected async Task QuantityChanged(int value) + { + await Http.PutAsJsonAsync("api/cart/lines", new Update.Command + { + Id = Details.Id, + Quantity = value + }); + + await OnQuantityChanged.InvokeAsync(EventArgs.Empty); + } + } +} \ No newline at end of file diff --git a/Client/Pages/Cart/Quantity.razor b/Client/Pages/Cart/Quantity.razor new file mode 100644 index 0000000..be13714 --- /dev/null +++ b/Client/Pages/Cart/Quantity.razor @@ -0,0 +1,9 @@ +@inherits QuantityBase + + \ No newline at end of file diff --git a/Client/Pages/Cart/Quantity.razor.cs b/Client/Pages/Cart/Quantity.razor.cs new file mode 100644 index 0000000..9051736 --- /dev/null +++ b/Client/Pages/Cart/Quantity.razor.cs @@ -0,0 +1,19 @@ +using Microsoft.AspNetCore.Components; +using System; +using System.Threading.Tasks; + +namespace ShoppingCartStarter.Client.Pages.Cart +{ + public class QuantityBase : ComponentBase + { + [Parameter] public EventCallback ValueChanged { get; set; } + + [Parameter] public int Value { get; set; } + + protected Task OnChange(ChangeEventArgs e) + { + Value = Convert.ToInt32(e.Value); + return ValueChanged.InvokeAsync(Value); + } + } +} \ No newline at end of file diff --git a/Server/Data/Database.cs b/Server/Data/Database.cs index ec78c10..47c1505 100644 --- a/Server/Data/Database.cs +++ b/Server/Data/Database.cs @@ -17,14 +17,14 @@ namespace ShoppingCartStarter.Server.Data { using var serviceScope = app.ApplicationServices.GetService().CreateScope(); using var context = serviceScope.ServiceProvider.GetRequiredService(); - + context.Database.Migrate(); ShoppingCart cart; if (context.Carts.Any()) { cart = context.Carts - .Include(x=>x.LineItems) + .Include(x => x.LineItems) .FirstOrDefault(); } else diff --git a/Server/Properties/launchSettings.json b/Server/Properties/launchSettings.json index d591409..6f5995f 100644 --- a/Server/Properties/launchSettings.json +++ b/Server/Properties/launchSettings.json @@ -20,7 +20,7 @@ "commandName": "Project", "launchBrowser": true, "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", - "applicationUrl": "https://localhost:5001;http://localhost:5000", + "applicationUrl": "https://localhost:5004;http://localhost:5005", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" }