From 8c2fe1ff9597722fde00d00411455a1dc9fd26cc Mon Sep 17 00:00:00 2001 From: Josh Creek <8179928+jcreek@users.noreply.github.com> Date: Sat, 16 Sep 2023 12:12:00 +0100 Subject: [PATCH] feat(#10): Add user creation and editing --- WhoAtX/Controllers/UserController.cs | 107 +++++++++++++++--- ... 20230915100710_InitialCreate.Designer.cs} | 3 +- ...ate.cs => 20230915100710_InitialCreate.cs} | 2 +- .../ApplicationDbContextModelSnapshot.cs | 1 - WhoAtX/Models/UserProfile.cs | 2 +- WhoAtX/Models/UserProfileViewModel.cs | 17 +-- WhoAtX/Views/Shared/_Layout.cshtml | 6 + WhoAtX/Views/User/Edit.cshtml | 63 +++++++++++ WhoAtX/Views/User/New.cshtml | 62 ++++++++++ 9 files changed, 235 insertions(+), 28 deletions(-) rename WhoAtX/Migrations/{20230914205826_InitialCreate.Designer.cs => 20230915100710_InitialCreate.Designer.cs} (95%) rename WhoAtX/Migrations/{20230914205826_InitialCreate.cs => 20230915100710_InitialCreate.cs} (97%) create mode 100644 WhoAtX/Views/User/Edit.cshtml create mode 100644 WhoAtX/Views/User/New.cshtml diff --git a/WhoAtX/Controllers/UserController.cs b/WhoAtX/Controllers/UserController.cs index d452cae..9cc9ffe 100644 --- a/WhoAtX/Controllers/UserController.cs +++ b/WhoAtX/Controllers/UserController.cs @@ -13,10 +13,45 @@ namespace WhoAtX.Controllers _context = context; } - public IActionResult Profile() + private UserProfileViewModel MapUserProfileToViewModel(UserProfile userProfile) { - // Retrieve user profile data from the database - var userProfile = _context.UserProfiles.FirstOrDefault(); + var userProfileViewModel = new UserProfileViewModel + { + Id = userProfile.Id, + Name = userProfile.Name, + Pronouns = userProfile.Pronouns, + NamePronunciationPath = userProfile.NamePronunciationPath, + AreasOfKnowledge = userProfile.AreasOfKnowledge, + Projects = userProfile.Projects, + Team = userProfile.Team, + WorkingHours = userProfile.WorkingHours, + TimeZone = userProfile.TimeZone, + }; + + return userProfileViewModel; + } + + private UserProfile MapViewModelToUserProfile(UserProfileViewModel userProfileViewModel) + { + var userProfile = new UserProfile + { + Id = userProfileViewModel.Id, + Name = userProfileViewModel.Name, + Pronouns = userProfileViewModel.Pronouns, + NamePronunciationPath = userProfileViewModel.NamePronunciationPath, + AreasOfKnowledge = userProfileViewModel.AreasOfKnowledge, + Projects = userProfileViewModel.Projects, + Team = userProfileViewModel.Team, + WorkingHours = userProfileViewModel.WorkingHours, + TimeZone = userProfileViewModel.TimeZone, + }; + + return userProfile; + } + + public IActionResult Profile(int id) + { + var userProfile = _context.UserProfiles.Find(id); if (userProfile is null) { @@ -39,21 +74,63 @@ namespace WhoAtX.Controllers }; } - // Map UserProfile to UserProfileViewModel - var userProfileViewModel = new UserProfileViewModel - { - Name = userProfile.Name, - Pronouns = userProfile.Pronouns, - NamePronunciationPath = userProfile.NamePronunciationPath, - AreasOfKnowledge = userProfile.AreasOfKnowledge, - Projects = userProfile.Projects, - Team = userProfile.Team, - WorkingHours = userProfile.WorkingHours, - TimeZone = userProfile.TimeZone, - }; + var userProfileViewModel = MapUserProfileToViewModel(userProfile); // Pass the user profile data to the view return View(userProfileViewModel); } + + public IActionResult New() + { + return View(new UserProfileViewModel()); + } + + [HttpPost] + public IActionResult CreateUserProfile(UserProfileViewModel userProfileViewModel) + { + if (ModelState.IsValid) + { + var userProfile = MapViewModelToUserProfile(userProfileViewModel); + + _context.UserProfiles.Add(userProfile); + _context.SaveChanges(); + + return RedirectToAction("Profile"); + } + + // If ModelState is not valid, return to the create page with validation errors + return View("New", userProfileViewModel); + } + + public IActionResult Edit(int id) + { + var userProfile = _context.UserProfiles.Find(id); + + if (userProfile == null) + { + return NotFound(); + } + + var userProfileViewModel = MapUserProfileToViewModel(userProfile); + + return View(userProfileViewModel); + } + + [HttpPost] + public IActionResult UpdateUserProfile(UserProfileViewModel userProfileViewModel) + { + if (ModelState.IsValid) + { + var userProfile = MapViewModelToUserProfile(userProfileViewModel); + + _context.UserProfiles.Update(userProfile); + _context.SaveChanges(); + + return RedirectToAction("Profile"); + } + + // If ModelState is not valid, return to the edit page with validation errors + return View("Edit", userProfileViewModel); + } } } diff --git a/WhoAtX/Migrations/20230914205826_InitialCreate.Designer.cs b/WhoAtX/Migrations/20230915100710_InitialCreate.Designer.cs similarity index 95% rename from WhoAtX/Migrations/20230914205826_InitialCreate.Designer.cs rename to WhoAtX/Migrations/20230915100710_InitialCreate.Designer.cs index 47a6f03..c0c9af0 100644 --- a/WhoAtX/Migrations/20230914205826_InitialCreate.Designer.cs +++ b/WhoAtX/Migrations/20230915100710_InitialCreate.Designer.cs @@ -10,7 +10,7 @@ using WhoAtX.Data; namespace WhoAtX.Migrations { [DbContext(typeof(ApplicationDbContext))] - [Migration("20230914205826_InitialCreate")] + [Migration("20230915100710_InitialCreate")] partial class InitialCreate { /// @@ -34,7 +34,6 @@ namespace WhoAtX.Migrations .HasColumnType("TEXT"); b.Property("NamePronunciationPath") - .IsRequired() .HasColumnType("TEXT"); b.Property("Projects") diff --git a/WhoAtX/Migrations/20230914205826_InitialCreate.cs b/WhoAtX/Migrations/20230915100710_InitialCreate.cs similarity index 97% rename from WhoAtX/Migrations/20230914205826_InitialCreate.cs rename to WhoAtX/Migrations/20230915100710_InitialCreate.cs index 4891400..1d27983 100644 --- a/WhoAtX/Migrations/20230914205826_InitialCreate.cs +++ b/WhoAtX/Migrations/20230915100710_InitialCreate.cs @@ -18,7 +18,7 @@ namespace WhoAtX.Migrations .Annotation("Sqlite:Autoincrement", true), Name = table.Column(type: "TEXT", nullable: false), Pronouns = table.Column(type: "TEXT", nullable: false), - NamePronunciationPath = table.Column(type: "TEXT", nullable: false), + NamePronunciationPath = table.Column(type: "TEXT", nullable: true), AreasOfKnowledge = table.Column(type: "TEXT", nullable: false), Projects = table.Column(type: "TEXT", nullable: false), Team = table.Column(type: "TEXT", nullable: false), diff --git a/WhoAtX/Migrations/ApplicationDbContextModelSnapshot.cs b/WhoAtX/Migrations/ApplicationDbContextModelSnapshot.cs index f1aafce..0a32f49 100644 --- a/WhoAtX/Migrations/ApplicationDbContextModelSnapshot.cs +++ b/WhoAtX/Migrations/ApplicationDbContextModelSnapshot.cs @@ -31,7 +31,6 @@ namespace WhoAtX.Migrations .HasColumnType("TEXT"); b.Property("NamePronunciationPath") - .IsRequired() .HasColumnType("TEXT"); b.Property("Projects") diff --git a/WhoAtX/Models/UserProfile.cs b/WhoAtX/Models/UserProfile.cs index c7f41eb..e87055c 100644 --- a/WhoAtX/Models/UserProfile.cs +++ b/WhoAtX/Models/UserProfile.cs @@ -5,7 +5,7 @@ public class UserProfile public int Id { get; set; } public string Name { get; set; } public string Pronouns { get; set; } - public string NamePronunciationPath { get; set; } + public string? NamePronunciationPath { get; set; } public string AreasOfKnowledge { get; set; } public string Projects { get; set; } public string Team { get; set; } diff --git a/WhoAtX/Models/UserProfileViewModel.cs b/WhoAtX/Models/UserProfileViewModel.cs index b5a5fd1..a4c0db6 100644 --- a/WhoAtX/Models/UserProfileViewModel.cs +++ b/WhoAtX/Models/UserProfileViewModel.cs @@ -2,17 +2,18 @@ namespace WhoAtX.Models; public class UserProfileViewModel { - public string Name { get; set; } - public string Pronouns { get; set; } - public string NamePronunciationPath { get; set; } + public int Id { get; set; } + public string? Name { get; set; } + public string? Pronouns { get; set; } + public string? NamePronunciationPath { get; set; } // Add more personal details here - public string AreasOfKnowledge { get; set; } - public string Projects { get; set; } - public string Team { get; set; } - public string WorkingHours { get; set; } - public string TimeZone { get; set; } + public string? AreasOfKnowledge { get; set; } + public string? Projects { get; set; } + public string? Team { get; set; } + public string? WorkingHours { get; set; } + public string? TimeZone { get; set; } // Add more professional details here } diff --git a/WhoAtX/Views/Shared/_Layout.cshtml b/WhoAtX/Views/Shared/_Layout.cshtml index afa6713..b0808fa 100644 --- a/WhoAtX/Views/Shared/_Layout.cshtml +++ b/WhoAtX/Views/Shared/_Layout.cshtml @@ -25,6 +25,12 @@ + + diff --git a/WhoAtX/Views/User/Edit.cshtml b/WhoAtX/Views/User/Edit.cshtml new file mode 100644 index 0000000..74dc728 --- /dev/null +++ b/WhoAtX/Views/User/Edit.cshtml @@ -0,0 +1,63 @@ +@model UserProfileViewModel + +@{ + ViewData["Title"] = "Edit User Profile"; +} + +
+

Edit User Profile

+ + @using (Html.BeginForm("UpdateUserProfile", "User", FormMethod.Post)) + { + + +
+ + +
+ + +
+ + +
+ + @* *@ + @*
*@ + @* *@ + @* *@ + @*
*@ + + +
+ + +
+ + +
+ + +
+ + +
+ + +
+ + +
+ + +
+ + +
+ + +
+ + + } +
diff --git a/WhoAtX/Views/User/New.cshtml b/WhoAtX/Views/User/New.cshtml new file mode 100644 index 0000000..1f0a711 --- /dev/null +++ b/WhoAtX/Views/User/New.cshtml @@ -0,0 +1,62 @@ +@model UserProfileViewModel + +@{ + ViewData["Title"] = "Create New User Profile"; +} + +
+

Create New User Profile

+ + @using (Html.BeginForm("CreateUserProfile", "User", FormMethod.Post)) + { + +
+ + +
+ + +
+ + +
+ + @* *@ + @*
*@ + @* *@ + @* *@ + @*
*@ + + +
+ + +
+ + +
+ + +
+ + +
+ + +
+ + +
+ + +
+ + +
+ + +
+ + + } +