Skip to content

Commit

Permalink
[Added] Create User
Browse files Browse the repository at this point in the history
  • Loading branch information
nobir committed Jul 17, 2022
1 parent f4eba29 commit 1ed4900
Show file tree
Hide file tree
Showing 3 changed files with 523 additions and 1 deletion.
102 changes: 101 additions & 1 deletion Controllers/AdminController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,116 @@
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ERPSystemTimologio.EF;
using ERPSystemTimologio.Models;

namespace ERPSystemTimologio.Controllers
{
[IsAdmin]
[LoggedIn, IsAdmin]
public class AdminController : Controller
{
private readonly TimologioEntities db = new TimologioEntities();
// GET: Admin
public ActionResult Index()
{
return View();
}

[HttpGet]
public ActionResult CreateUser()
{
ViewBag.Branches = this.db.Branches.ToList();
ViewBag.Regions = this.db.Regions.ToList();
ViewBag.Permissions = this.db.Permissions.ToList();

return View();
}

[HttpPost]
public ActionResult CreateUser(UserCreateAdminModel _user)
{
ViewBag.Branches = this.db.Branches.ToList();
ViewBag.Regions = this.db.Regions.ToList();
ViewBag.Permissions = this.db.Permissions.ToList();

if (_user.PermissionIds.Count() > 0)
{
_user.PermissionIds.RemoveAll(n => n == 0);
_user.PermissionIds = _user.PermissionIds.Distinct().ToList();
}

if (!ModelState.IsValid)
{
return View(_user);
}

User user = new User
{
Verified = _user.Verified,
Name = _user.Name,
Username = _user.Username,
Email = _user.Email,
Salary = _user.Salary,
Password = _user.Password,
Type = _user.Type,
HireDate = _user.HireDate
};

if (_user.RegionId != null && _user.BranchId == null)
{
if(db.Regions.Where(r => r.Id == _user.RegionId).Count() == 0)
{
TempData["error_message"] = "Region not found";
return RedirectToAction("CreateUser", "Admin");
}

user.RegionId = _user.RegionId;
}
else if(_user.BranchId != null)
{
if (db.Branches.Where(b => b.Id == _user.BranchId).Count() == 0)
{
TempData["error_message"] = "Branch not found";
return RedirectToAction("CreateUser", "Admin");
}

user.BranchId = _user.BranchId;
}

Address address = new Address
{
LocalAddress = _user.LocalAddress,
PoliceStation = _user.PoliceStation,
City = _user.City,
Country = _user.Country,
ZipCode = _user.ZipCode,
};

db.Addresses.Add(address);

db.SaveChanges();

user.AddressId = address.Id;

db.Users.Add(user);

db.SaveChanges();

for (var i = 0; i < _user.PermissionIds.Count(); ++i)
{
var id = _user.PermissionIds[i];
var permission = db.Permissions.Where(p => p.Id == id).SingleOrDefault();

db.Permissions.Attach(permission);
user.Permissions.Add(permission);
db.SaveChanges();
}

db.SaveChanges();

TempData["success_message"] = "Successfully created user";

return RedirectToAction("CreateUser", "Admin");
}
}
}
4 changes: 4 additions & 0 deletions ERPSystemTimologio.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
<Compile Include="Models\UniqueEmailValidation.cs" />
<Compile Include="Models\UniqueUsernameValidation.cs" />
<Compile Include="Models\UserCreateAdminModel.cs" />
<Compile Include="Models\UserLoginModel.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down Expand Up @@ -274,6 +277,7 @@
<Content Include="Views\Dashboard\Index.cshtml" />
<Content Include="Views\Shared\_SideMenu.cshtml" />
<Content Include="Views\Admin\Index.cshtml" />
<Content Include="Views\Admin\CreateUser.cshtml" />
</ItemGroup>
<ItemGroup>
<Folder Include="App_Data\" />
Expand Down
Loading

0 comments on commit 1ed4900

Please sign in to comment.