Skip to content

Commit

Permalink
manage basket checkout after login (dotnet-architecture#371)
Browse files Browse the repository at this point in the history
  • Loading branch information
michelcedric authored May 6, 2020
1 parent fdb4869 commit 516d87a
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/ApplicationCore/Services/OrderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using Ardalis.GuardClauses;
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
using Microsoft.eShopWeb.ApplicationCore.Specifications;

namespace Microsoft.eShopWeb.ApplicationCore.Services
{
Expand All @@ -28,7 +29,9 @@ public OrderService(IAsyncRepository<Basket> basketRepository,

public async Task CreateOrderAsync(int basketId, Address shippingAddress)
{
var basket = await _basketRepository.GetByIdAsync(basketId);
var basketSpec = new BasketWithItemsSpecification(basketId);
var basket = await _basketRepository.FirstOrDefaultAsync(basketSpec);

Guard.Against.NullBasket(basketId, basket);
var items = new List<OrderItem>();
foreach (var item in basket.Items)
Expand Down
3 changes: 2 additions & 1 deletion src/Web/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
namespace Microsoft.eShopWeb.Web
 namespace Microsoft.eShopWeb.Web
{
public static class Constants
{
public const string BASKET_COOKIENAME = "eShop";
public const int ITEMS_PER_PAGE = 10;
public const string DEFAULT_USERNAME = "Guest";
public const string BASKET_ID = "BasketId";
}
}
9 changes: 8 additions & 1 deletion src/Web/Pages/Basket/Checkout.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,15 @@ public CheckoutModel(IBasketService basketService,

public BasketViewModel BasketModel { get; set; } = new BasketViewModel();

public void OnGet()
public async Task OnGet()
{
if (HttpContext.Request.Query.ContainsKey(Constants.BASKET_ID))
{
var basketId = int.Parse(HttpContext.Request.Query[Constants.BASKET_ID]);
await _basketService.TransferBasketAsync(Request.Cookies[Constants.BASKET_COOKIENAME], User.Identity.Name);
await _orderService.CreateOrderAsync(basketId, new Address("123 Main St.", "Kent", "OH", "United States", "44240"));
await _basketService.DeleteBasketAsync(basketId);
}
}

public async Task<IActionResult> OnPost(Dictionary<string, int> items)
Expand Down
7 changes: 7 additions & 0 deletions src/Web/Pages/Basket/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,15 @@
asp-page-handler="Update">
[ Update ]
</button>
@{
var data = new Dictionary<string, string>
{
{ Constants.BASKET_ID, Model.BasketModel.Id.ToString() },
};
}
<input type="submit" asp-page="Checkout"
class="btn esh-basket-checkout"
asp-all-route-data=data
value="[ Checkout ]" name="action" />
</section>
</div>
Expand Down
8 changes: 4 additions & 4 deletions src/Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,16 @@ public void ConfigureServices(IServiceCollection services)
options.Conventions.Add(new RouteTokenTransformerConvention(
new SlugifyParameterTransformer()));

});
});
services.AddRazorPages(options =>
{
options.Conventions.AuthorizePage("/Basket/Checkout");
});
services.AddControllersWithViews();

services.AddHttpContextAccessor();
services.AddSwaggerGen(c => c.SwaggerDoc("v1", new OpenApi.Models.OpenApiInfo { Title = "My API", Version = "v1"}));

services.AddSwaggerGen(c => c.SwaggerDoc("v1", new OpenApi.Models.OpenApiInfo { Title = "My API", Version = "v1" }));

services.AddHealthChecks();

Expand Down Expand Up @@ -201,7 +201,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

app.UseStaticFiles();
app.UseRouting();

app.UseHttpsRedirection();
app.UseCookiePolicy();
app.UseAuthentication();
Expand Down

0 comments on commit 516d87a

Please sign in to comment.