Skip to content

Commit

Permalink
[Added] Edit User
Browse files Browse the repository at this point in the history
  • Loading branch information
nobir committed Jul 21, 2022
1 parent d398cf2 commit 42fd074
Show file tree
Hide file tree
Showing 4 changed files with 593 additions and 1 deletion.
152 changes: 151 additions & 1 deletion Controllers/AdminController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace ERPSystemTimologio.Controllers
{
//[LoggedIn, IsAdmin]
[LoggedIn, IsAdmin]
public class AdminController : Controller
{
private readonly TimologioEntities db = new TimologioEntities();
Expand Down Expand Up @@ -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<int> _permissionIds = new List<int> { };

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<int> { },
};

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);
}
}
}
2 changes: 2 additions & 0 deletions ERPSystemTimologio.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@
<Compile Include="Models\UniqueEmailValidation.cs" />
<Compile Include="Models\UniqueUsernameValidation.cs" />
<Compile Include="Models\UserCreateAdminModel.cs" />
<Compile Include="Models\UserEditAdminModel.cs" />
<Compile Include="Models\UserLoginModel.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down Expand Up @@ -288,6 +289,7 @@
<Content Include="Views\Admin\CreatePermission.cshtml" />
<Content Include="Views\Admin\EditPermission.cshtml" />
<Content Include="Views\Admin\ViewPermissions.cshtml" />
<Content Include="Views\Admin\EditUser.cshtml" />
</ItemGroup>
<ItemGroup>
<Folder Include="App_Data\" />
Expand Down
60 changes: 60 additions & 0 deletions Models/UserEditAdminModel.cs
Original file line number Diff line number Diff line change
@@ -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<int> PermissionIds { get; set; }
}
}
Loading

0 comments on commit 42fd074

Please sign in to comment.