Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/Sielth/eShopOnWeb
Browse files Browse the repository at this point in the history
  • Loading branch information
HanaaChamma committed May 17, 2023
2 parents faa0171 + 01966c6 commit 20e4fff
Show file tree
Hide file tree
Showing 16 changed files with 210 additions and 87 deletions.
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<PackageVersion Include="BlazorInputFile" Version="0.2.0" />
<PackageVersion Include="Blazored.LocalStorage" Version="4.3.0" />
<PackageVersion Include="BuildBundlerMinifier" Version="3.2.449" PrivateAssets="All" />
<PackageVersion Include="FluentAssertions" Version="6.11.0" />
<PackageVersion Include="FluentValidation" Version="11.5.1" />
<PackageVersion Include="MediatR" Version="12.0.1" />
<PackageVersion Include="Microsoft.AspNetCore.Components.Authorization" Version="7.0.3" />
Expand Down
10 changes: 0 additions & 10 deletions src/Infrastructure/Identity/ApplicationUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,3 @@ public class ApplicationUser : IdentityUser
{

}

// Liskov
// Open Closed
// Single Responsibility
public class MemberUser : ApplicationUser
{
public int MemberPoints { get; set; }
public string CreditCardInfo { get; set; } // TODO: Define

}
11 changes: 11 additions & 0 deletions src/Infrastructure/Identity/MemberUser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Microsoft.eShopWeb.Infrastructure.Identity;

// Liskov
// Open Closed
// Single Responsibility
public class MemberUser : ApplicationUser
{
public int MemberPoints { get; set; }
public string? OtherInfos { get; set; } // TODO: Define

}
16 changes: 3 additions & 13 deletions src/Web/Areas/Identity/Pages/Account/Register.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,9 @@
</div>
<div class="modal-body">
<div class="form-group">
<label asp-for="Input.Email"></label>
<input asp-for="Input.Email" class="form-control"/>
<span asp-validation-for="Input.Email" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Input.Email"></label>
<input asp-for="Input.Email" class="form-control"/>
<span asp-validation-for="Input.Email" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Input.Email"></label>
<input asp-for="Input.Email" class="form-control"/>
<span asp-validation-for="Input.Email" class="text-danger"></span>
<label asp-for="Input.OtherInfos"></label>
<input asp-for="Input.OtherInfos" class="form-control"/>
<span asp-validation-for="Input.OtherInfos" class="text-danger"></span>
</div>
</div>
<div class="modal-footer">
Expand Down
35 changes: 15 additions & 20 deletions src/Web/Areas/Identity/Pages/Account/Register.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Security.Claims;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Ardalis.GuardClauses;
using AutoMapper.Execution;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI.Services;
Expand Down Expand Up @@ -34,8 +36,7 @@ public RegisterModel(
_emailSender = emailSender;
}

[BindProperty]
public InputModel? Input { get; set; }
[BindProperty] public InputModel? Input { get; set; }

public string? ReturnUrl { get; set; }

Expand All @@ -47,7 +48,8 @@ public class InputModel
public string? Email { get; set; }

[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.",
MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string? Password { get; set; }
Expand All @@ -56,14 +58,11 @@ public class InputModel
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string? ConfirmPassword { get; set; }


[Display(Name = "Membership Plus")]
public bool? MembershipPlus { get; set; }

[DataType(DataType.CreditCard)]
[Display(Name = "Credit Card Number")]
public string? CreditCardNumber { get; set; }


[Display(Name = "Membership Plus")] public bool? MembershipPlus { get; set; }

[Display(Name = "Other Infos")] public string? OtherInfos { get; set; }
}

public void OnGet(string? returnUrl = null)
Expand All @@ -76,19 +75,14 @@ public async Task<IActionResult> OnPostAsync(string? returnUrl = null)
returnUrl = returnUrl ?? Url.Content("~/");
if (ModelState.IsValid)
{
ApplicationUser user;
if (Input?.MembershipPlus is true)
{
user = new MemberUser { UserName = Input?.Email, Email = Input?.Email, CreditCardInfo = Input.CreditCardNumber};
}
else
{
user = new ApplicationUser { UserName = Input?.Email, Email = Input?.Email };
}
var user = Input?.MembershipPlus is true
? new MemberUser { UserName = Input?.Email, Email = Input?.Email, OtherInfos = Input?.OtherInfos }
: new ApplicationUser { UserName = Input?.Email, Email = Input?.Email };

var result = await _userManager.CreateAsync(user, Input?.Password);
if (result.Succeeded)
{
if (user is MemberUser) await _userManager.AddClaimAsync(user, new Claim("Member", "True"));
_logger.LogInformation("User created a new account with password.");

var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
Expand All @@ -105,6 +99,7 @@ await _emailSender.SendEmailAsync(Input?.Email, "Confirm your email",
await _signInManager.SignInAsync(user, isPersistent: false);
return LocalRedirect(returnUrl);
}

foreach (var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
Expand Down
9 changes: 9 additions & 0 deletions src/Web/Pages/Basket/BasketViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,13 @@ public int AddPoints()

return 0;
}

public decimal GetTotalAfterDiscount(decimal points)
{
decimal total = CalculateItemsTotalPrice();
decimal percentDiscount = points / 10;
decimal multiplier = (100 - percentDiscount) / 100;
decimal priceAfterDiscount = total * multiplier;
return priceAfterDiscount + CalculateDelivery();
}
}
12 changes: 6 additions & 6 deletions src/Web/Pages/Basket/Checkout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
}
<section class="esh-catalog-hero">
<div class="container">
<img class="esh-catalog-title" src="~/images/main_banner_text.png" />
<img class="esh-catalog-title" src="~/images/main_banner_text.png"/>
</div>
</section>

Expand All @@ -15,7 +15,7 @@
{
<form asp-page="Checkout" method="post">
<article class="esh-basket-titles row">
<br />
<br/>
<section class="esh-basket-title col-xs-3">Product</section>
<section class="esh-basket-title col-xs-3 hidden-lg-down"></section>
<section class="esh-basket-title col-xs-2">Price</section>
Expand All @@ -30,13 +30,13 @@
<article class="esh-basket-items row">
<div>
<section class="esh-basket-item esh-basket-item--middle col-lg-3 hidden-lg-down">
<img class="esh-basket-image" src="@item.PictureUrl" />
<img class="esh-basket-image" src="@item.PictureUrl"/>
</section>
<section class="esh-basket-item esh-basket-item--middle col-xs-3">@item.ProductName</section>
<section class="esh-basket-item esh-basket-item--middle col-xs-2">$ @item.UnitPrice.ToString("N2")</section>
<section class="esh-basket-item esh-basket-item--middle col-xs-2">
<input type="hidden" name="@("Items[" + i + "].Id")" value="@item.Id" />
<input type="hidden" name="@("Items[" + i + "].Quantity")" value="@item.Quantity" />
<input type="hidden" name="@("Items[" + i + "].Id")" value="@item.Id"/>
<input type="hidden" name="@("Items[" + i + "].Quantity")" value="@item.Quantity"/>
@item.Quantity
</section>
<section class="esh-basket-item esh-basket-item--middle esh-basket-item--mark col-xs-2">$ @Math.Round(item.Quantity * item.UnitPrice, 2).ToString("N2")</section>
Expand Down Expand Up @@ -69,7 +69,7 @@
<a asp-page="Index" class="btn esh-basket-checkout text-white">[ Back ]</a>
</section>
<section class="esh-basket-item col-xs-push-7 col-xs-4 text-right">
<input type="submit" class="btn esh-basket-checkout" value="[ Pay Now ]" />
<input type="submit" class="btn esh-basket-checkout" value="[ Pay Now ]"/>
</section>
</div>
</div>
Expand Down
44 changes: 29 additions & 15 deletions src/Web/Pages/Basket/Checkout.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Ardalis.GuardClauses;
using System.ComponentModel;
using Ardalis.GuardClauses;
using AutoMapper.Execution;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
Expand Down Expand Up @@ -34,30 +36,42 @@ public CheckoutModel(IBasketService basketService,
_logger = logger;
}

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

// TODO: Find a way how to get the ApplicationUser (or MemberUser)
// public ApplicationUser AppUser => GetUser();

// private ApplicationUser GetUser()
// {
// return _signInManager.UserManager.Users.FirstOrDefault(user =>
// User.Identity != null && user.UserName == User.Identity.Name &&
// user.GetType().IsAssignableTo(typeof(MemberUser)));
//
// }

// public decimal GetPoints()
// {
// if (User.HasClaim("Member", "True"))
// {
// var user = _signInManager.UserManager.Users.FirstOrDefault(user =>
// User.Identity != null && user.UserName == User.Identity.Name && user.GetType().IsAssignableTo(typeof(MemberUser)));
// if (user is not null) return ((user as MemberUser)!).MemberPoints;
// }
//
// return 0;
// }

public async Task OnGet()
{
await SetBasketModelAsync();
}

public decimal GetPriceAfterDiscount()
{
decimal points = ((MemberUser)User.Identity).MemberPoints; // TODO: find how to get applicationUser
decimal total = 0; // TODO: Get Total from Basket
decimal procentDiscount = points / 10;
decimal multiplier = (100 - procentDiscount) / 100;
decimal priceAfterDiscount = total * multiplier;
return priceAfterDiscount;
}


public async Task<IActionResult> OnPost(IEnumerable<BasketItemViewModel> items)
{
try
{
await SetBasketModelAsync();
if (User is MemberUser) GetPriceAfterDiscount(); //TODO: How to change total


if (!ModelState.IsValid)
{
return BadRequest();
Expand Down
22 changes: 12 additions & 10 deletions src/Web/Pages/Shared/Components/FavouriteComponent/Favourite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,16 @@ public class Favourite : ViewComponent
{
private readonly IFavouritesViewModelService _favouriteService;
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly IFavouritItemseHelper _favouritItemseHelper;
private readonly IFavouritItemseHelper _favouriteItemseHelper;

public Favourite(IFavouritesViewModelService basketService,
SignInManager<ApplicationUser> signInManager, IFavouritItemseHelper favouriteItemseHelper)
{
_favouriteService = basketService;
_signInManager = signInManager;
_favouriteItemseHelper = favouriteItemseHelper;
}

public async Task<IViewComponentResult> InvokeAsync()
{
var vm = new FavouriteComponentViewModel
Expand All @@ -28,26 +36,20 @@ public async Task<IViewComponentResult> InvokeAsync()
return View(vm);
}

public Favourite(IFavouritesViewModelService basketService,
SignInManager<ApplicationUser> signInManager)
{
_favouriteService = basketService;
_signInManager = signInManager;
}

private async Task<int> CountTotalFavouriteItems()
private async Task<int>? CountTotalFavouriteItems()
{
if (_signInManager.IsSignedIn(HttpContext.User))
{
Guard.Against.Null(User?.Identity?.Name, nameof(User.Identity.Name));
return await _favouritItemseHelper.CountTotalFavouriteItems(User.Identity.Name);
return await _favouriteItemseHelper.CountTotalFavouriteItems(User.Identity.Name);
}

string? anonymousId = GetAnnonymousIdFromCookie();
if (anonymousId == null)
return 0;

return await _favouritItemseHelper.CountTotalFavouriteItems(anonymousId);
return await _favouriteItemseHelper.CountTotalFavouriteItems(anonymousId);
}
private string? GetAnnonymousIdFromCookie()
{
Expand Down
5 changes: 5 additions & 0 deletions src/Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
using Microsoft.eShopWeb.Web.Configuration;
using Microsoft.eShopWeb.Web.HealthChecks;
using Microsoft.eShopWeb.Web.Interfaces;
using Microsoft.eShopWeb.Web.Mapping;
using Microsoft.eShopWeb.Web.Mapping.Favourites;
using Microsoft.eShopWeb.Web.Pages.Shared.Username;
using Microsoft.eShopWeb.Web.Services;
using Microsoft.eShopWeb.Web.Services.FavouriteHeplers;
using Microsoft.Extensions.Diagnostics.HealthChecks;

var builder = WebApplication.CreateBuilder(args);
Expand Down Expand Up @@ -48,6 +51,8 @@
builder.Services.AddScoped<IFavouriteService, FavouriteService>();
builder.Services.AddScoped<IFavouritesViewModelService, FavouriteViewModelService>();
builder.Services.AddScoped<IFavouriteQueryService, FavouriteQueryService>();
builder.Services.AddScoped<IFavouritItemseHelper, FavouriteItemsHelper>();
builder.Services.AddScoped(typeof(IFavouriteMapper), typeof(FavouriteMapper));
builder.Configuration.AddEnvironmentVariables();
builder.Services.AddCoreServices(builder.Configuration);
builder.Services.AddWebServices(builder.Configuration);
Expand Down
5 changes: 1 addition & 4 deletions src/Web/ViewModels/FavouriteComponentViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,5 @@

public class FavouriteComponentViewModel
{
//public int ItemsCount { get; set; }


public int FavourieItemCount { get; set; }
public int? FavourieItemCount { get; set; }
}
1 change: 1 addition & 0 deletions tests/UnitTests/UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Moq" />
Expand Down
Loading

0 comments on commit 20e4fff

Please sign in to comment.