diff --git a/Controllers/AdminController.cs b/Controllers/AdminController.cs index 998344c..e3dd626 100644 --- a/Controllers/AdminController.cs +++ b/Controllers/AdminController.cs @@ -9,7 +9,7 @@ namespace ERPSystemTimologio.Controllers { - //[LoggedIn, IsAdmin] + [LoggedIn, IsAdmin] public class AdminController : Controller { private readonly TimologioEntities db = new TimologioEntities(); @@ -539,5 +539,155 @@ public ActionResult CreateUser(UserCreateAdminModel _user) return RedirectToAction("CreateUser", "Admin"); } + + [HttpGet] + public ActionResult EditUser(int? id) + { + if (id == null) + { + TempData["error_message"] = "Invalid User Id get"; + return RedirectToAction("ViewVerifiedUsers", "Admin"); + } + + var user = this.db.Users.Where(u => u.Id == id).SingleOrDefault(); + + if(user == null) + { + TempData["error_message"] = "User not found"; + return RedirectToAction("ViewVerifiedUsers", "Admin"); + } + + ViewBag.Branches = this.db.Branches.ToList(); + ViewBag.Regions = this.db.Regions.ToList(); + ViewBag.Permissions = this.db.Permissions.ToList(); + + List _permissionIds = new List { }; + + foreach(var permission in user.Permissions.ToList()) + { + _permissionIds.Add(permission.Id); + } + + var _user = new UserEditAdminModel + { + Id = user.Id, + Verified = user.Verified, + Name = user.Name, + Username = user.Username, + Email = user.Email, + Salary = user.Salary, + HireDate = user.HireDate, + Type = user.Type, + RegionId = user.RegionId, + BranchId = user.BranchId, + LocalAddress = user.Address.LocalAddress, + PoliceStation = user.Address.PoliceStation, + City = user.Address.City, + Country = user.Address.Country, + ZipCode = user.Address.ZipCode, + PermissionIds = _permissionIds ?? new List { }, + }; + + return View(_user); + } + + [HttpPost] + public ActionResult EditUser(UserEditAdminModel editedUser, int? id) + { + if (id == null) + { + TempData["error_message"] = "Invalid User Id post"; + return RedirectToAction("ViewVerifiedUsers", "Admin"); + } + + var dbUser = this.db.Users.Where(u => u.Id == id).SingleOrDefault(); + + if (dbUser == null) + { + TempData["error_message"] = "User not found"; + return RedirectToAction("ViewVerifiedUsers", "Admin"); + } + + ViewBag.Branches = this.db.Branches.ToList(); + ViewBag.Regions = this.db.Regions.ToList(); + ViewBag.Permissions = this.db.Permissions.ToList(); + + if (!ModelState.IsValid) + { + return View(editedUser); + } + + if (editedUser.PermissionIds.Count() > 0) + { + editedUser.PermissionIds.RemoveAll(n => n == 0); + editedUser.PermissionIds = editedUser.PermissionIds.Distinct().ToList(); + } + + dbUser.Verified = editedUser.Verified; + dbUser.Name = editedUser.Name; + dbUser.Salary = editedUser.Salary; + dbUser.Type = editedUser.Type; + dbUser.HireDate = editedUser.HireDate; + dbUser.RegionId = null; + dbUser.BranchId = null; + + db.SaveChanges(); + + if (editedUser.RegionId != null && editedUser.BranchId == null) + { + if (db.Regions.Where(r => r.Id == editedUser.RegionId).Count() == 0) + { + TempData["error_message"] = "Region not found"; + return RedirectToAction("EditUser", "Admin", new { id }); + } + + dbUser.RegionId = editedUser.RegionId; + db.SaveChanges(); + } + else if (editedUser.BranchId != null) + { + if (db.Branches.Where(b => b.Id == editedUser.BranchId).Count() == 0) + { + TempData["error_message"] = "Branch not found"; + return RedirectToAction("EditUser", "Admin", new { id }); + } + + dbUser.BranchId = editedUser.BranchId; + db.SaveChanges(); + } + + editedUser.RegionId = dbUser.RegionId; + editedUser.BranchId = dbUser.BranchId; + + db.SaveChanges(); + + dbUser.Address.LocalAddress = editedUser.LocalAddress; + dbUser.Address.PoliceStation = editedUser.PoliceStation; + dbUser.Address.City = editedUser.City; + dbUser.Address.Country = editedUser.Country; + dbUser.Address.ZipCode = editedUser.ZipCode; + + db.SaveChanges(); + + dbUser.Permissions.Clear(); + + db.SaveChanges(); + + for (var i = 0; i < editedUser.PermissionIds.Count(); ++i) + { + var pid = editedUser.PermissionIds[i]; + var permission = this.db.Permissions.Where(p => p.Id == pid).SingleOrDefault(); + + this.db.Permissions.Attach(permission); + dbUser.Permissions.Add(permission); + db.SaveChanges(); + } + + db.SaveChanges(); + + TempData["success_message"] = "Successfully updated user"; + + return View(editedUser); + } } } \ No newline at end of file diff --git a/ERPSystemTimologio.csproj b/ERPSystemTimologio.csproj index 923d96d..fffe633 100644 --- a/ERPSystemTimologio.csproj +++ b/ERPSystemTimologio.csproj @@ -200,6 +200,7 @@ + @@ -288,6 +289,7 @@ + diff --git a/Models/UserEditAdminModel.cs b/Models/UserEditAdminModel.cs new file mode 100644 index 0000000..a5fd4ce --- /dev/null +++ b/Models/UserEditAdminModel.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Web; +using ERPSystemTimologio.EF; + +namespace ERPSystemTimologio.Models +{ + public class UserEditAdminModel : User + { + [Required] + [RegularExpression(@"^[10]+$", ErrorMessage = "The field must 0 or 1")] + public new int Verified { get; set; } + [Required] + [MinLength(3)] + public new string Name { get; set; } + + //[Required] + //[MinLength(3)] + //[UniqueUsernameValidation] + //public new string Username { get; set; } + + //[Required] + //[EmailAddress] + //[UniqueEmailValidation] + //public new string Email { get; set; } + + [Required] + [Range(0, 500000)] + public new double? Salary { get; set; } + + [Required] + [DataType(DataType.Date)] + [DisplayFormat(DataFormatString = "yyyy-MM-dd")] + public new DateTime? HireDate { get; set; } + + [Required] + [Range(1, 4)] + public new int Type { get; set; } + + [Range(0, int.MaxValue)] + public new int? RegionId { get; set; } + + [Range(0, int.MaxValue)] + public new int? BranchId { get; set; } + + public string LocalAddress { get; set; } + + public string PoliceStation { get; set; } + + public string City { get; set; } + + public string Country { get; set; } + + public string ZipCode { get; set; } + + public List PermissionIds { get; set; } + } +} \ No newline at end of file diff --git a/Views/Admin/EditUser.cshtml b/Views/Admin/EditUser.cshtml new file mode 100644 index 0000000..59a074c --- /dev/null +++ b/Views/Admin/EditUser.cshtml @@ -0,0 +1,380 @@ +@model ERPSystemTimologio.Models.UserEditAdminModel +@{ + ViewBag.Title = "Edit User"; +} + +
+
+ +
+ + + @if (!ViewData.ModelState.IsValidField("Verified")) + { + + @Html.ValidationMessage("Verified") + + } +
+
+ +
+ +
+ + + @if (!ViewData.ModelState.IsValidField("Name")) + { + + @Html.ValidationMessage("Name") + + } +
+
+ +
+ +
+ + + @if (!ViewData.ModelState.IsValidField("Username")) + { + + @Html.ValidationMessage("Username") + + } +
+
+ +
+ +
+ + + @if (!ViewData.ModelState.IsValidField("Email")) + { + + @Html.ValidationMessage("Email") + + } +
+
+ +
+ +
+ + + @if (!ViewData.ModelState.IsValidField("Salary")) + { + + @Html.ValidationMessage("Salary") + + } +
+
+ +
+ +
+ + + @if (!ViewData.ModelState.IsValidField("Type")) + { + + @Html.ValidationMessage("Type") + + } +
+
+ +
+ +
+ + + @if (!ViewData.ModelState.IsValidField("HireDate")) + { + + @Html.ValidationMessage("HireDate") + + } +
+
+ +
+ +
+ + @if (!ViewData.ModelState.IsValidField("RegionId")) + { + + @Html.ValidationMessage("RegionId") + + } +
+
+ +
+ +
+ + @if (!ViewData.ModelState.IsValidField("BranchId")) + { + + @Html.ValidationMessage("BranchId") + + } +
+
+ + +
+
+
+ +
+ +
+ + +
+
+ @{ + int count = 0; + } + + @if (Model != null && Model.PermissionIds.Count() > 0) + { + foreach (int permissionId in Model.PermissionIds) + { + { count++; } +
+ +
+ + if (count > 1) + { + + } + } + } + else + { +
+ +
+ } +
+ + @if (!ViewData.ModelState.IsValidField("PermissionIds")) + { + + @Html.ValidationMessage("PermissionIds") + + } +
+ +
+ + + +
+ +
+ + + @if (!ViewData.ModelState.IsValidField("LocalAddress")) + { + + @Html.ValidationMessage("LocalAddress") + + } +
+
+ +
+ +
+ + + @if (!ViewData.ModelState.IsValidField("PoliceStation")) + { + + @Html.ValidationMessage("PoliceStation") + + } +
+
+ +
+ +
+ + + @if (!ViewData.ModelState.IsValidField("City")) + { + + @Html.ValidationMessage("City") + + } +
+
+ +
+ +
+ + + @if (!ViewData.ModelState.IsValidField("Country")) + { + + @Html.ValidationMessage("Country") + + } +
+
+ +
+ +
+ + + @if (!ViewData.ModelState.IsValidField("ZipCode")) + { + + @Html.ValidationMessage("ZipCode") + + } +
+
+ +
+
+ +
+
+
\ No newline at end of file