Skip to content

Commit

Permalink
Section 12 - Session in Action
Browse files Browse the repository at this point in the history
  • Loading branch information
bhrugen committed Sep 27, 2021
1 parent 9565c1b commit db2f275
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
2 changes: 2 additions & 0 deletions BulkyBook.Utility/SD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ public static class SD
public const string PaymentStatusApproved = "Approved";
public const string PaymentStatusDelayedPayment = "ApprovedForDelayedPayment";
public const string PaymentStatusRejected = "Rejected";

public const string SessionCart = "SessionShoppingCart";
}
}
9 changes: 8 additions & 1 deletion BulkyBookWeb/Areas/Customer/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using BulkyBook.DataAccess.Repository.IRepository;
using BulkyBook.Models;
using BulkyBook.Models.ViewModels;
using BulkyBook.Utility;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Claims;

namespace BulkyBookWeb.Controllers;
Expand Down Expand Up @@ -56,12 +59,16 @@ public IActionResult Details(ShoppingCart shoppingCart)
if (cartFromDb == null) {

_unitOfWork.ShoppingCart.Add(shoppingCart);
_unitOfWork.Save();
HttpContext.Session.SetInt32(SD.SessionCart,
_unitOfWork.ShoppingCart.GetAll(u => u.ApplicationUserId == claim.Value).ToList().Count);
}
else
{
_unitOfWork.ShoppingCart.IncrementCount(cartFromDb, shoppingCart.Count);
_unitOfWork.Save();
}
_unitOfWork.Save();


return RedirectToAction(nameof(Index));
}
Expand Down
10 changes: 9 additions & 1 deletion BulkyBookWeb/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.AspNetCore.Identity.UI.Services;
using BulkyBook.Utility;
using Stripe;
using System;

var builder = WebApplication.CreateBuilder(args);

Expand All @@ -31,7 +32,13 @@
options.LogoutPath = $"/Identity/Account/Logout";
options.AccessDeniedPath = $"/Identity/Account/AccessDenied";
});

builder.Services.AddDistributedMemoryCache();
builder.Services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(100);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
var app = builder.Build();

// Configure the HTTP request pipeline.
Expand All @@ -56,6 +63,7 @@
app.UseAuthentication();

app.UseAuthorization();
app.UseSession();
app.MapRazorPages();
app.MapControllerRoute(
name: "default",
Expand Down
18 changes: 16 additions & 2 deletions BulkyBookWeb/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
@using BulkyBook.Utility
@using Microsoft.AspNetCore.Http
@using BulkyBook.Utility
@inject IHttpContextAccessor HttpContextAccessor
<!DOCTYPE html>
<html lang="en">
<head>
Expand Down Expand Up @@ -43,11 +45,23 @@
Manage Order
</a>
</li>

@if (HttpContextAccessor.HttpContext.Session.GetInt32(SD.SessionCart) != null)
{
<li class="nav-item">
<a class=nav-link asp-area="Customer" asp-controller="Cart" asp-action="Index">
<i class="bi bi-cart"></i> &nbsp;
(@HttpContextAccessor.HttpContext.Session.GetInt32(SD.SessionCart))
</a>
</li>
}
else{
<li class="nav-item">
<a class=nav-link asp-area="Customer" asp-controller="Cart" asp-action="Index">
<i class="bi bi-cart"></i>
<i class="bi bi-cart"></i> &nbsp; (0)
</a>
</li>
}
</ul>
<partial name="_LoginPartial" />
@* <form class="d-flex">
Expand Down

0 comments on commit db2f275

Please sign in to comment.