generated from egmont11/ASP.Net-Core-MVC-Template
Přemístění + dodělání funkčnosti webu.
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace StuzkovakWeb.Areas.Admin.Controllers;
|
||||
|
||||
[Area("Admin")]
|
||||
[Authorize(Roles = "Admin")]
|
||||
public abstract class AdminBaseController : Controller
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace StuzkovakWeb.Areas.Admin.Controllers;
|
||||
|
||||
public class DashboardController : AdminBaseController
|
||||
{
|
||||
public IActionResult Index()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using StuzkovakWeb.Data;
|
||||
using StuzkovakWeb.Entities;
|
||||
using StuzkovakWeb.ViewModels;
|
||||
|
||||
namespace StuzkovakWeb.Areas.Admin.Controllers;
|
||||
|
||||
public class InvitesController : AdminBaseController
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public InvitesController(AppDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<IActionResult> Index()
|
||||
{
|
||||
var invites = await _context.Invites.ToListAsync();
|
||||
return View(invites);
|
||||
}
|
||||
|
||||
public async Task<IActionResult> Details(Guid id)
|
||||
{
|
||||
var invite = await _context.Invites.FirstOrDefaultAsync(i => i.Id == id);
|
||||
if (invite == null) return NotFound();
|
||||
return View(new AdminInviteDetailViewModel
|
||||
{
|
||||
Id = invite.Id,
|
||||
TeacherName = invite.TeacherName,
|
||||
IsAccepted = invite.IsAccepted
|
||||
});
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Create(string teacherName)
|
||||
{
|
||||
if (string.IsNullOrEmpty(teacherName)) return BadRequest();
|
||||
var invite = new InviteEntity
|
||||
{
|
||||
TeacherName = teacherName,
|
||||
IsAccepted = null
|
||||
};
|
||||
await _context.Invites.AddAsync(invite);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
TempData["SuccessMessage"] = "Pozvánka byla vytvořena.";
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Delete(Guid id)
|
||||
{
|
||||
var invite = await _context.Invites.FirstOrDefaultAsync(i => i.Id == id);
|
||||
if (invite == null) return NotFound();
|
||||
_context.Invites.Remove(invite);
|
||||
await _context.SaveChangesAsync();
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Edit(Guid id, string name)
|
||||
{
|
||||
var invite = await _context.Invites.FirstOrDefaultAsync(i => i.Id == id);
|
||||
if (invite is null) return NotFound();
|
||||
|
||||
invite.TeacherName = name;
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
TempData["SuccessMessage"] = "Pozvánka byla upravena.";
|
||||
return RedirectToAction("Details", new { id });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using StuzkovakWeb.Entities;
|
||||
using StuzkovakWeb.Models;
|
||||
|
||||
namespace StuzkovakWeb.Areas.Admin.Controllers;
|
||||
|
||||
public class UsersController : AdminBaseController
|
||||
{
|
||||
private readonly UserManager<UserEntity> _userManager;
|
||||
private readonly RoleManager<IdentityRole> _roleManager;
|
||||
|
||||
public UsersController(UserManager<UserEntity> userManager, RoleManager<IdentityRole> roleManager)
|
||||
{
|
||||
_userManager = userManager;
|
||||
_roleManager = roleManager;
|
||||
}
|
||||
|
||||
public async Task<IActionResult> Index()
|
||||
{
|
||||
var allRoles = _roleManager.Roles.Select(r => r.Name!).ToList();
|
||||
var users = _userManager.Users.ToList();
|
||||
var viewModels = new List<UserWithRolesViewModel>();
|
||||
foreach (var user in users)
|
||||
{
|
||||
var roles = await _userManager.GetRolesAsync(user);
|
||||
viewModels.Add(new UserWithRolesViewModel { UserEntity = user, Roles = roles, AllRoles = allRoles });
|
||||
}
|
||||
return View(viewModels);
|
||||
}
|
||||
|
||||
public async Task<IActionResult> Details(string id)
|
||||
{
|
||||
var user = await _userManager.FindByIdAsync(id);
|
||||
if (user == null) return NotFound();
|
||||
|
||||
var roles = await _userManager.GetRolesAsync(user);
|
||||
var allRoles = _roleManager.Roles.Select(r => r.Name!).ToList();
|
||||
return View(new UserWithRolesViewModel { UserEntity = user, Roles = roles, AllRoles = allRoles });
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> UpdateRoles(string id, List<string> selectedRoles)
|
||||
{
|
||||
var user = await _userManager.FindByIdAsync(id);
|
||||
if (user == null) return NotFound();
|
||||
|
||||
var currentRoles = await _userManager.GetRolesAsync(user);
|
||||
await _userManager.RemoveFromRolesAsync(user, currentRoles);
|
||||
if (selectedRoles.Count > 0)
|
||||
await _userManager.AddToRolesAsync(user, selectedRoles);
|
||||
|
||||
TempData["SuccessMessage"] = "Roles updated successfully.";
|
||||
return RedirectToAction(nameof(Details), new { id });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user