Skip to content

Commit

Permalink
Merge pull request #5 from GerardGargan/dev
Browse files Browse the repository at this point in the history
Merge dev to master
  • Loading branch information
GerardGargan authored Aug 18, 2024
2 parents 7e96eed + 580ae47 commit 4b61085
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Bookstore.Models/ApplicationUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class ApplicationUser : IdentityUser
public int? CompanyId { get; set; }
[ForeignKey("CompanyId")]
[ValidateNever]
public Company Company { get; set; }
public Company? Company { get; set; }
[NotMapped]
public string Role { get; set; }
}
}
52 changes: 52 additions & 0 deletions Bookstore.Web/Areas/Admin/Controllers/UserController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Bookstore.DataAccess.Data;
using Bookstore.DataAccess.Repository.IRepository;
using Bookstore.Models;
using Microsoft.AspNetCore.Mvc;

namespace BookstoreWeb.Areas.Admin.Controllers
{
[Area("Admin")]
public class UserController : Controller
{
private readonly IUnitOfWork _unitOfWork;
private readonly ApplicationDbContext _db;

public UserController(IUnitOfWork unitOfWork, ApplicationDbContext db)
{
_unitOfWork = unitOfWork;
_db = db;
}

public IActionResult Index()
{
return View();
}

#region

[HttpGet]
public IActionResult GetAll()
{
List<ApplicationUser> allUsers = _unitOfWork.User.GetAll(includeProperties: "Company").ToList();
// get all roles and user roles
var userRoles = _db.UserRoles.ToList();
var roles = _db.Roles.ToList();

foreach(var user in allUsers)
{
var roleId = userRoles.FirstOrDefault(x => x.UserId == user.Id).RoleId;
user.Role = roles.FirstOrDefault(x => x.Id == roleId).Name;

if(user.Company == null)
{
user.Company = new Company() { Name = "" };
}
}

return Json(new { data = allUsers });
}

#endregion

}
}
36 changes: 36 additions & 0 deletions Bookstore.Web/Areas/Admin/Views/User/Index.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<div class="card shadow border-0 my-4">
<div class="card-header bg-primary bg-gradient ml-0 py-3">
<div class="col-12 text-center">
<h2 class="text-white py-2">User List</h2>
</div>
</div>
<div class="card-body p-4">
<table id="tblData" class="table table-bordered table-striped" style="width:100%">
<thead>
<tr>
<th>
Name
</th>
<th>
Email
</th>
<th>
Phone
</th>
<th>
Company
</th>
<th>
Role
</th>
<th>
</th>
</tr>
</thead>
</table>
</div>
</div>

@section Scripts {
<script src="~/js/user.js"></script>
}
3 changes: 3 additions & 0 deletions Bookstore.Web/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
<li class="nav-item">
<a class="dropdown-item" asp-area="Identity" asp-page="/Account/Register">Create User</a>
</li>
<li class="nav-item">
<a class="dropdown-item" asp-area="Admin" asp-controller="User" asp-action="Index">Manage Users</a>
</li>
</ul>
</li>
}
Expand Down
25 changes: 25 additions & 0 deletions Bookstore.Web/wwwroot/js/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
$(document).ready(function () {
loadDataTable();
});

function loadDataTable() {
dataTable = $('#tblData').DataTable({
ajax: '/admin/user/getall',
"columns": [
{ data: 'name', "width": "15%" },
{ data: 'email', "width": "25%" },
{ data: 'phoneNumber', "width": "10%" },
{ data: 'company.name', "width": "10%" },
{ data: 'role', "width": "10%" },
{
data: 'id',
"render": function (data) {
return `<div class="w-75 btn-group" role="group">
<a href="/Admin/User/Upsert/${data}" class="btn btn-primary mx-1"><i class="bi bi-pencil-square"></i> Edit</a>
</div>`
},
"width": "25%"
}
]
});
}

0 comments on commit 4b61085

Please sign in to comment.