-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New form for shipping address is added to profile page
- Loading branch information
Showing
8 changed files
with
215 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/DevBetterWeb.Web/Pages/User/MyProfile/ShippingAddress.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
@page | ||
@using System.Linq; | ||
@model DevBetterWeb.Web.Pages.User.ShippingAddressModel | ||
@{ | ||
ViewData["Title"] = "My Profile: Shipping Address"; | ||
} | ||
|
||
<h3>@ViewData["Title"]</h3> | ||
|
||
<form method="post"> | ||
<div class="row"> | ||
<div class="col-7"> | ||
<div class="card"> | ||
<div class="card-header"> | ||
<b>Shipping Address</b> | ||
</div> | ||
<div class="card-body"> | ||
|
||
<div class="form-group"> | ||
<label class="font-weight-bold">Country</label> | ||
<select asp-for="UserShippingAddressUpdateModel.Country" asp-items="@ISO3166.Country.List.Select(i => new SelectListItem { Text = i.Name, Value = i.Name }).ToList()" | ||
class="form-control form-control-sm"></select> | ||
<span asp-validation-for="UserShippingAddressUpdateModel.Country" class="text-danger"></span> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<label class="font-weight-bold">City</label> | ||
<input asp-for="UserShippingAddressUpdateModel.City" type="text" value="@Model.UserShippingAddressUpdateModel.City" | ||
class="form-control form-control-sm" /> | ||
<span asp-validation-for="UserShippingAddressUpdateModel.City" class="text-danger"></span> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<label class="font-weight-bold">Street</label> | ||
<input asp-for="UserShippingAddressUpdateModel.Street" type="text" value="@Model.UserShippingAddressUpdateModel.Street" | ||
class="form-control form-control-sm" /> | ||
<span asp-validation-for="UserShippingAddressUpdateModel.Street" class="text-danger"></span> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<label class="font-weight-bold">Postal code</label> | ||
<input asp-for="UserShippingAddressUpdateModel.PostalCode" type="text" value="@Model.UserShippingAddressUpdateModel.PostalCode" | ||
class="form-control form-control-sm" /> | ||
<span asp-validation-for="UserShippingAddressUpdateModel.PostalCode" class="text-danger"></span> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<label class="font-weight-bold">State</label> | ||
<input asp-for="UserShippingAddressUpdateModel.State" type="text" value="@Model.UserShippingAddressUpdateModel.State" | ||
class="form-control form-control-sm" /> | ||
<span asp-validation-for="UserShippingAddressUpdateModel.State" class="text-danger"></span> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<br /> | ||
<input type="submit" value="Save" class="btn btn-success" /> | ||
<br /> | ||
<br /> | ||
|
||
<a class="btn btn-primary nav-item" href="./">Back to My Profile</a> | ||
|
||
|
||
</form> |
73 changes: 73 additions & 0 deletions
73
src/DevBetterWeb.Web/Pages/User/MyProfile/ShippingAddress.cshtml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System.Threading.Tasks; | ||
using DevBetterWeb.Core; | ||
using DevBetterWeb.Core.Entities; | ||
using DevBetterWeb.Core.Exceptions; | ||
using DevBetterWeb.Core.Interfaces; | ||
using DevBetterWeb.Core.Specs; | ||
using DevBetterWeb.Core.ValueObjects; | ||
using DevBetterWeb.Infrastructure.Identity.Data; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
|
||
namespace DevBetterWeb.Web.Pages.User | ||
{ | ||
[Authorize(Roles = AuthConstants.Roles.ADMINISTRATORS_MEMBERS_ALUMNI)] | ||
public class ShippingAddressModel : PageModel | ||
{ | ||
private readonly UserManager<ApplicationUser> _userManager; | ||
private readonly IMemberRegistrationService _memberRegistrationService; | ||
private readonly IRepository<Member> _memberRepository; | ||
|
||
public ShippingAddressModel(UserManager<ApplicationUser> userManager, | ||
IMemberRegistrationService memberRegistrationService, | ||
IRepository<Member> memberRepository) | ||
{ | ||
_userManager = userManager; | ||
_memberRegistrationService = memberRegistrationService; | ||
_memberRepository = memberRepository; | ||
} | ||
|
||
[BindProperty] | ||
public UserShippingAddressUpdateModel UserShippingAddressUpdateModel { get; set; } | ||
|
||
public async Task OnGetAsync() | ||
{ | ||
var currentUserName = User.Identity!.Name; | ||
var applicationUser = await _userManager.FindByNameAsync(currentUserName); | ||
|
||
var spec = new MemberByUserIdSpec(applicationUser.Id); | ||
var member = await _memberRepository.FirstOrDefaultAsync(spec); | ||
|
||
if (member == null) | ||
{ | ||
member = await _memberRegistrationService.RegisterMemberAsync(applicationUser.Id); | ||
} | ||
|
||
UserShippingAddressUpdateModel = new UserShippingAddressUpdateModel(member); | ||
} | ||
|
||
public async Task OnPost() | ||
{ | ||
if (!ModelState.IsValid) return; | ||
|
||
var currentUserName = User.Identity!.Name; | ||
var applicationUser = await _userManager.FindByNameAsync(currentUserName); | ||
|
||
var spec = new MemberByUserIdSpec(applicationUser.Id); | ||
var member = await _memberRepository.FirstOrDefaultAsync(spec); | ||
if (member is null) throw new MemberNotFoundException(applicationUser.Id); | ||
|
||
member.UpdateShippingAddress(new Address(UserShippingAddressUpdateModel.Street, | ||
UserShippingAddressUpdateModel.City, | ||
UserShippingAddressUpdateModel.State, | ||
UserShippingAddressUpdateModel.PostalCode, | ||
UserShippingAddressUpdateModel.Country)); | ||
|
||
member.UpdateAddress(member.ShippingAddress.ToString()); | ||
|
||
await _memberRepository.UpdateAsync(member); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/DevBetterWeb.Web/Pages/User/UserShippingAddressUpdateModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using DevBetterWeb.Core.Entities; | ||
|
||
namespace DevBetterWeb.Web.Pages.User; | ||
|
||
public class UserShippingAddressUpdateModel | ||
{ | ||
public UserShippingAddressUpdateModel() | ||
{ | ||
} | ||
|
||
public UserShippingAddressUpdateModel(Member member) | ||
{ | ||
this.Street = member.ShippingAddress.Street; | ||
this.City = member.ShippingAddress.City; | ||
this.State = member.ShippingAddress.State; | ||
this.PostalCode = member.ShippingAddress.PostalCode; | ||
this.Country = member.ShippingAddress.Country; | ||
} | ||
|
||
[Required] | ||
public string City { get; set; } | ||
|
||
[Required] | ||
public string Country { get; set; } | ||
|
||
public string State { get; set; } | ||
public string Street { get; set; } | ||
|
||
public string PostalCode { get; set; } | ||
|
||
} |