From 1acdefcb61ed57221d5046bc5570c0cee9bdbb15 Mon Sep 17 00:00:00 2001 From: rostech Date: Wed, 14 Sep 2022 23:40:40 +0300 Subject: [PATCH] New form for shipping address is added to profile page --- src/DevBetterWeb.Core/ValueObjects/Address.cs | 5 ++ src/DevBetterWeb.Web/DevBetterWeb.Web.csproj | 1 + .../Pages/User/MyProfile/Index.cshtml | 47 +++++++++--- .../Pages/User/MyProfile/Personal.cshtml | 8 -- .../Pages/User/MyProfile/Personal.cshtml.cs | 1 - .../User/MyProfile/ShippingAddress.cshtml | 66 +++++++++++++++++ .../User/MyProfile/ShippingAddress.cshtml.cs | 73 +++++++++++++++++++ .../User/UserShippingAddressUpdateModel.cs | 32 ++++++++ 8 files changed, 215 insertions(+), 18 deletions(-) create mode 100644 src/DevBetterWeb.Web/Pages/User/MyProfile/ShippingAddress.cshtml create mode 100644 src/DevBetterWeb.Web/Pages/User/MyProfile/ShippingAddress.cshtml.cs create mode 100644 src/DevBetterWeb.Web/Pages/User/UserShippingAddressUpdateModel.cs diff --git a/src/DevBetterWeb.Core/ValueObjects/Address.cs b/src/DevBetterWeb.Core/ValueObjects/Address.cs index 3d6305a4c..12e8cbd47 100644 --- a/src/DevBetterWeb.Core/ValueObjects/Address.cs +++ b/src/DevBetterWeb.Core/ValueObjects/Address.cs @@ -47,4 +47,9 @@ public string ToCityStateCountryString() return string.Join(',', tokens); } + + public override string ToString() + { + return $"{this.Street}, {this.City}, {this.State} {this.PostalCode}, {this.Country}"; + } } diff --git a/src/DevBetterWeb.Web/DevBetterWeb.Web.csproj b/src/DevBetterWeb.Web/DevBetterWeb.Web.csproj index 3407d05f7..4b4bdcb6b 100644 --- a/src/DevBetterWeb.Web/DevBetterWeb.Web.csproj +++ b/src/DevBetterWeb.Web/DevBetterWeb.Web.csproj @@ -30,6 +30,7 @@ + diff --git a/src/DevBetterWeb.Web/Pages/User/MyProfile/Index.cshtml b/src/DevBetterWeb.Web/Pages/User/MyProfile/Index.cshtml index dbe78d654..9f688436e 100644 --- a/src/DevBetterWeb.Web/Pages/User/MyProfile/Index.cshtml +++ b/src/DevBetterWeb.Web/Pages/User/MyProfile/Index.cshtml @@ -50,6 +50,33 @@
+
+
+ Shipping Address +
+ +
+ @if (@Model.UserProfileViewModel.Address != "none") + { +
+ + @Model.UserProfileViewModel.Address + +
+ } + + + City, State (optional), and Country are used for map location. +
+ Full shipping address is only visible to admins and is used for shipping books and other packages to members from time to time. +
+
+
+ Edit +
+
+
+
Books You've Read @@ -95,8 +122,10 @@ } else { -
You haven't added any books yet. Alumni members can add one book to the devBetter reading list. - Add one below!
+
+ You haven't added any books yet. Alumni members can add one book to the devBetter reading list. + Add one below! +
Add
@@ -153,13 +182,13 @@

-
-
- Billing Information -
-
- View -
+
+
+ Billing Information
+
+ View +
+
\ No newline at end of file diff --git a/src/DevBetterWeb.Web/Pages/User/MyProfile/Personal.cshtml b/src/DevBetterWeb.Web/Pages/User/MyProfile/Personal.cshtml index e4299306e..9b0cfbfb9 100644 --- a/src/DevBetterWeb.Web/Pages/User/MyProfile/Personal.cshtml +++ b/src/DevBetterWeb.Web/Pages/User/MyProfile/Personal.cshtml @@ -49,14 +49,6 @@ -
- - Only administrators can view this information - include country if - non-US - -
-

If set, birthday will be visible to other devBetter members and may be used to celebrate your day via Discord, coaching sessions, etc.

diff --git a/src/DevBetterWeb.Web/Pages/User/MyProfile/Personal.cshtml.cs b/src/DevBetterWeb.Web/Pages/User/MyProfile/Personal.cshtml.cs index d6c9a3a3d..e9c2dc478 100644 --- a/src/DevBetterWeb.Web/Pages/User/MyProfile/Personal.cshtml.cs +++ b/src/DevBetterWeb.Web/Pages/User/MyProfile/Personal.cshtml.cs @@ -80,7 +80,6 @@ public async Task OnPost() member.UpdateName(UserPersonalUpdateModel.FirstName, UserPersonalUpdateModel.LastName); member.UpdatePEInfo(UserPersonalUpdateModel.PEFriendCode, UserPersonalUpdateModel.PEUsername); member.UpdateAboutInfo(UserPersonalUpdateModel.AboutInfo); - member.UpdateAddress(UserPersonalUpdateModel.Address); member.UpdateBirthday(UserPersonalUpdateModel.BirthdayDay, UserPersonalUpdateModel.BirthdayMonth); member.UpdateDiscord(UserPersonalUpdateModel.DiscordUsername); member.UpdateEmail(UserPersonalUpdateModel.Email); diff --git a/src/DevBetterWeb.Web/Pages/User/MyProfile/ShippingAddress.cshtml b/src/DevBetterWeb.Web/Pages/User/MyProfile/ShippingAddress.cshtml new file mode 100644 index 000000000..839aad75f --- /dev/null +++ b/src/DevBetterWeb.Web/Pages/User/MyProfile/ShippingAddress.cshtml @@ -0,0 +1,66 @@ +@page +@using System.Linq; +@model DevBetterWeb.Web.Pages.User.ShippingAddressModel +@{ + ViewData["Title"] = "My Profile: Shipping Address"; +} + +

@ViewData["Title"]

+ +
+
+
+
+
+ Shipping Address +
+
+ +
+ + + +
+ +
+ + + +
+ +
+ + + +
+ +
+ + + +
+ +
+ + + +
+
+
+
+
+ +
+ +
+
+ + Back to My Profile + + +
diff --git a/src/DevBetterWeb.Web/Pages/User/MyProfile/ShippingAddress.cshtml.cs b/src/DevBetterWeb.Web/Pages/User/MyProfile/ShippingAddress.cshtml.cs new file mode 100644 index 000000000..5d6958ec1 --- /dev/null +++ b/src/DevBetterWeb.Web/Pages/User/MyProfile/ShippingAddress.cshtml.cs @@ -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 _userManager; + private readonly IMemberRegistrationService _memberRegistrationService; + private readonly IRepository _memberRepository; + + public ShippingAddressModel(UserManager userManager, + IMemberRegistrationService memberRegistrationService, + IRepository 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); + } + } +} diff --git a/src/DevBetterWeb.Web/Pages/User/UserShippingAddressUpdateModel.cs b/src/DevBetterWeb.Web/Pages/User/UserShippingAddressUpdateModel.cs new file mode 100644 index 000000000..15dbfce2c --- /dev/null +++ b/src/DevBetterWeb.Web/Pages/User/UserShippingAddressUpdateModel.cs @@ -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; } + +}