Removed unsued properties from user controller index

This commit is contained in:
Dimitar Todorov
2022-04-08 10:52:17 +03:00
parent 4e25e34b99
commit e1f462fb1d
13 changed files with 109 additions and 269 deletions

View File

@@ -7,9 +7,21 @@ using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Data;
using Data.Entities;
using Microsoft.AspNetCore.Authorization;
namespace WebApp.Controllers
{
public class UserWithRoles
{
public string UserName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string PersonalNumber { get; set; }
public string PhoneNumber { get; set; }
public string Role { get; set; }
}
public class UsersController : Controller
{
private readonly RentACarDbContext _context;
@@ -20,52 +32,36 @@ namespace WebApp.Controllers
}
// GET: Users
[Authorize(Roles = "Admin")]
public async Task<IActionResult> Index()
{
return View(await _context.Users.ToListAsync());
}
ICollection<User> users = await _context.Users.ToListAsync();
// GET: Users/Details/5
public async Task<IActionResult> Details(string id)
{
if (id == null)
{
return NotFound();
}
ICollection<UserWithRoles> usersWithRole = new List<UserWithRoles>();
var user = await _context.Users
.FirstOrDefaultAsync(m => m.Id == id);
if (user == null)
{
return NotFound();
}
//foreach (var item in users)
//{
// var userRole = await _context.UserRoles.FirstOrDefaultAsync(userRoles => userRoles.UserId == item.Id);
// var role = await _context.Roles.FirstOrDefaultAsync(role => role.Id == userRole.RoleId);
// usersWithRole.Add(
// new UserWithRoles()
// {
// UserName = item.UserName,
// FirstName = item.FirstName,
// LastName = item.LastName,
// Email = item.Email,
// PersonalNumber = item.PersonalNumber,
// PhoneNumber = item.PhoneNumber,
// Role = role.Name
// }
// );
//}
return View(user);
}
// GET: Users/Create
public IActionResult Create()
{
return View();
}
// POST: Users/Create
// To protect from overposting attacks, enable the specific properties you want to bind to.
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("FirstName,LastName,PersonalNumber,Id,UserName,NormalizedUserName,Email,NormalizedEmail,EmailConfirmed,PasswordHash,SecurityStamp,ConcurrencyStamp,PhoneNumber,PhoneNumberConfirmed,TwoFactorEnabled,LockoutEnd,LockoutEnabled,AccessFailedCount")] User user)
{
if (ModelState.IsValid)
{
_context.Add(user);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(user);
return View(users);
}
// GET: Users/Edit/5
[Authorize(Roles = "Admin")]
public async Task<IActionResult> Edit(string id)
{
if (id == null)
@@ -86,6 +82,7 @@ namespace WebApp.Controllers
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize(Roles = "Admin")]
public async Task<IActionResult> Edit(string id, [Bind("FirstName,LastName,PersonalNumber,Id,UserName,NormalizedUserName,Email,NormalizedEmail,EmailConfirmed,PasswordHash,SecurityStamp,ConcurrencyStamp,PhoneNumber,PhoneNumberConfirmed,TwoFactorEnabled,LockoutEnd,LockoutEnabled,AccessFailedCount")] User user)
{
if (id != user.Id)
@@ -117,6 +114,7 @@ namespace WebApp.Controllers
}
// GET: Users/Delete/5
[Authorize(Roles = "Admin")]
public async Task<IActionResult> Delete(string id)
{
if (id == null)
@@ -137,6 +135,7 @@ namespace WebApp.Controllers
// POST: Users/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
[Authorize(Roles = "Admin")]
public async Task<IActionResult> DeleteConfirmed(string id)
{
var user = await _context.Users.FindAsync(id);