diff --git a/NG_2023_Kanban.BusinessLayer/Injecting.cs b/NG_2023_Kanban.BusinessLayer/Injecting.cs index 3dc1ce5..360eb88 100644 --- a/NG_2023_Kanban.BusinessLayer/Injecting.cs +++ b/NG_2023_Kanban.BusinessLayer/Injecting.cs @@ -9,6 +9,7 @@ namespace NG_2023_Kanban.BusinessLayer.Inject this IServiceCollection services) { services.AddAutoMapper(typeof(MappingProfile)); + services.AddScoped(); services.AddScoped(); } } diff --git a/NG_2023_Kanban.BusinessLayer/Interfaces/IBoardService.cs b/NG_2023_Kanban.BusinessLayer/Interfaces/IBoardService.cs new file mode 100644 index 0000000..1f8d77b --- /dev/null +++ b/NG_2023_Kanban.BusinessLayer/Interfaces/IBoardService.cs @@ -0,0 +1,10 @@ +using NG_2023_Kanban.BusinessLayer.Models; + +namespace NG_2023_Kanban.BusinessLayer.Interfaces +{ + public interface IBoardService + { + Task GetAsync(int id); + Task> GetAllAsync(); + } +} diff --git a/NG_2023_Kanban.BusinessLayer/Interfaces/IUserService.cs b/NG_2023_Kanban.BusinessLayer/Interfaces/IUserService.cs index 1f808f5..4e32dae 100644 --- a/NG_2023_Kanban.BusinessLayer/Interfaces/IUserService.cs +++ b/NG_2023_Kanban.BusinessLayer/Interfaces/IUserService.cs @@ -5,6 +5,7 @@ namespace NG_2023_Kanban.BusinessLayer.Interfaces public interface IUserService { Task GetAsync(int id); + Task> GetAllAsync(); Task LoginAsync(UserModel user); Task RegisterAsync(UserModel user); } diff --git a/NG_2023_Kanban.BusinessLayer/Service/BoardService.cs b/NG_2023_Kanban.BusinessLayer/Service/BoardService.cs new file mode 100644 index 0000000..149e208 --- /dev/null +++ b/NG_2023_Kanban.BusinessLayer/Service/BoardService.cs @@ -0,0 +1,31 @@ +using AutoMapper; +using NG_2023_Kanban.BusinessLayer.Interfaces; +using NG_2023_Kanban.BusinessLayer.Models; +using NG_2023_Kanban.DataLayer.Repositories; +using NG_2023_Kanban.DataLayer.Entities; +using NG_2023_Kanban.DataLayer.Interfaces; + +namespace NG_2023_Kanban.BusinessLayer.Services +{ + public class BoardService : IBoardService + { + private readonly IBoardRepository _boardRepository; + private readonly IMapper _mapper; + + public BoardService(IBoardRepository boardRepository, IMapper mapper) + { + _boardRepository = boardRepository; + _mapper = mapper; + } + + public async Task GetAsync(int id) + { + return _mapper.Map(await _boardRepository.GetAsync(id)); + } + + public async Task> GetAllAsync() + { + return _mapper.Map>(await _boardRepository.GetAllAsync()); + } + } +} diff --git a/NG_2023_Kanban.BusinessLayer/Service/UserService.cs b/NG_2023_Kanban.BusinessLayer/Service/UserService.cs index a6bc400..f375849 100644 --- a/NG_2023_Kanban.BusinessLayer/Service/UserService.cs +++ b/NG_2023_Kanban.BusinessLayer/Service/UserService.cs @@ -23,6 +23,11 @@ namespace NG_2023_Kanban.BusinessLayer.Services return _mapper.Map(await _userRepository.GetAsync(id)); } + public async Task> GetAllAsync() + { + return _mapper.Map>(await _userRepository.GetAllAsync()); + } + public async Task LoginAsync(UserModel user) { var data = await _userRepository.FindAsync(x => x.Username == user.Username && x.Password == user.Password); diff --git a/NG_2023_Kanban/Controllers/AdminController.cs b/NG_2023_Kanban/Controllers/AdminController.cs new file mode 100644 index 0000000..b7d5bba --- /dev/null +++ b/NG_2023_Kanban/Controllers/AdminController.cs @@ -0,0 +1,63 @@ +using System.Diagnostics; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Http; +using AutoMapper; +using NG_2023_Kanban.Models; +using NG_2023_Kanban.DTOs; +using NG_2023_Kanban.Enums; +using NG_2023_Kanban.BusinessLayer.Models; +using NG_2023_Kanban.BusinessLayer.Services; + +namespace NG_2023_Kanban.Controllers; + +public class AdminController : Controller +{ + private readonly UserService _userService; + private readonly BoardService _boardService; + private readonly ILogger _logger; + private readonly IMapper _mapper; + + public AdminController(ILogger logger, UserService userService, BoardService boardService, IMapper mapper) + { + _logger = logger; + _userService = userService; + _boardService = boardService; + _mapper = mapper; + } + + public async Task Boards() + { + var currentAccount = HttpContext.Session.GetInt32("Account"); + if (currentAccount.HasValue) + { + var account = _mapper.Map(await _userService.GetAsync(currentAccount.Value)); + if (account.Role < (int)Roles.Administrator) + return StatusCode(StatusCodes.Status403Forbidden); + ViewData["Account"] = account; + ViewData["Boards"] = _mapper.Map>(await _boardService.GetAllAsync()); + return View(); + } + return Redirect("/Auth/Login"); + } + + public async Task Users() + { + var currentAccount = HttpContext.Session.GetInt32("Account"); + if (currentAccount.HasValue) + { + var account = _mapper.Map(await _userService.GetAsync(currentAccount.Value)); + if (account.Role < (int)Roles.Administrator) + return StatusCode(StatusCodes.Status403Forbidden); + ViewData["Account"] = account; + ViewData["Users"] = _mapper.Map>(await _userService.GetAllAsync()); + return View(); + } + return Redirect("/Auth/Login"); + } + + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] + public IActionResult Error() + { + return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); + } +} diff --git a/NG_2023_Kanban/Controllers/AuthController.cs b/NG_2023_Kanban/Controllers/AuthController.cs new file mode 100644 index 0000000..fc8fbe9 --- /dev/null +++ b/NG_2023_Kanban/Controllers/AuthController.cs @@ -0,0 +1,98 @@ +using System.Diagnostics; +using Microsoft.AspNetCore.Mvc; +using AutoMapper; +using NG_2023_Kanban.Models; +using NG_2023_Kanban.DTOs; +using NG_2023_Kanban.BusinessLayer.Models; +using NG_2023_Kanban.BusinessLayer.Services; + +namespace NG_2023_Kanban.Controllers; + +public class AuthController : Controller +{ + private readonly UserService _userService; + private readonly ILogger _logger; + private readonly IMapper _mapper; + + public AuthController(ILogger logger, UserService userService, IMapper mapper) + { + _logger = logger; + _userService = userService; + _mapper = mapper; + } + + public IActionResult Login() + { + var currentAccount = HttpContext.Session.GetInt32("Account"); + if (currentAccount != null) + return Redirect("/Home/Index"); + + return View(); + } + + [HttpPost] + public async Task Login(UserDto user) + { + var currentAccount = HttpContext.Session.GetInt32("Account"); + if (currentAccount != null) + return Redirect("/Home/Index"); + + var model = _mapper.Map(user); + + var account = _mapper.Map(await _userService.LoginAsync(model)); + + if (account != null) + { + HttpContext.Session.SetInt32("Account", account.Id); + return Redirect("/Home/Index"); + } + else + { + ViewData["Error"] = "Invalid credentials."; + return View(); + } + } + + public IActionResult Logout() + { + HttpContext.Session.Remove("Account"); + return Redirect("/Auth/Login"); + } + + public IActionResult Register() + { + var currentAccount = HttpContext.Session.GetInt32("Account"); + if (currentAccount != null) + return Redirect("/Home/Index"); + + return View(); + } + + [HttpPost] + public async Task Register(UserDto user) + { + var currentAccount = HttpContext.Session.GetInt32("Account"); + if (currentAccount != null) + return Redirect("/Home/Index"); + + try + { + var model = _mapper.Map(user); + var account = _mapper.Map(await _userService.RegisterAsync(model)); + + HttpContext.Session.SetInt32("Account", account.Id); + return Redirect("/Home/Index"); + } + catch + { + ViewData["Error"] = "This name is already taken."; + return View(); + } + } + + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] + public IActionResult Error() + { + return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); + } +} diff --git a/NG_2023_Kanban/Controllers/HomeController.cs b/NG_2023_Kanban/Controllers/HomeController.cs index 49c0e34..ee39df0 100644 --- a/NG_2023_Kanban/Controllers/HomeController.cs +++ b/NG_2023_Kanban/Controllers/HomeController.cs @@ -29,76 +29,7 @@ public class HomeController : Controller ViewData["Account"] = _mapper.Map(await _userService.GetAsync(currentAccount.Value)); return View(); } - return Redirect("/Home/Login"); - } - - public IActionResult Login() - { - var currentAccount = HttpContext.Session.GetInt32("Account"); - if (currentAccount != null) - return Redirect("/Home/Index"); - - return View(); - } - - [HttpPost] - public async Task Login(UserDto user) - { - var currentAccount = HttpContext.Session.GetInt32("Account"); - if (currentAccount != null) - return Redirect("/Home/Index"); - - var model = _mapper.Map(user); - - var account = _mapper.Map(await _userService.LoginAsync(model)); - - if (account != null) - { - HttpContext.Session.SetInt32("Account", account.Id); - return Redirect("/Home/Index"); - } - else - { - ViewData["Error"] = "Invalid credentials."; - return View(); - } - } - - public IActionResult Logout() - { - HttpContext.Session.Remove("Account"); - return Redirect("/Home/Login"); - } - - public IActionResult Register() - { - var currentAccount = HttpContext.Session.GetInt32("Account"); - if (currentAccount != null) - return Redirect("/Home/Index"); - - return View(); - } - - [HttpPost] - public async Task Register(UserDto user) - { - var currentAccount = HttpContext.Session.GetInt32("Account"); - if (currentAccount != null) - return Redirect("/Home/Index"); - - try - { - var model = _mapper.Map(user); - var account = _mapper.Map(await _userService.RegisterAsync(model)); - - HttpContext.Session.SetInt32("Account", account.Id); - return Redirect("/Home/Index"); - } - catch - { - ViewData["Error"] = "This name is already taken."; - return View(); - } + return Redirect("/Auth/Login"); } public IActionResult Privacy() diff --git a/NG_2023_Kanban/Views/Admin/Boards.cshtml b/NG_2023_Kanban/Views/Admin/Boards.cshtml new file mode 100644 index 0000000..9e69ef1 --- /dev/null +++ b/NG_2023_Kanban/Views/Admin/Boards.cshtml @@ -0,0 +1,18 @@ +@using NG_2023_Kanban.DTOs +@{ + var boards = ViewData["Boards"] as ICollection; +} + +@{ + ViewData["Title"] = "Boards"; +} + +
+

Boards

+ @{ + foreach (var board in boards) + { +

@board.Name

+ } + } +
diff --git a/NG_2023_Kanban/Views/Admin/Users.cshtml b/NG_2023_Kanban/Views/Admin/Users.cshtml new file mode 100644 index 0000000..2b0a28a --- /dev/null +++ b/NG_2023_Kanban/Views/Admin/Users.cshtml @@ -0,0 +1,18 @@ +@using NG_2023_Kanban.DTOs +@{ + var users = ViewData["Users"] as ICollection; +} + +@{ + ViewData["Title"] = "Users"; +} + +
+

Users

+ @{ + foreach (var user in users) + { +

@user.Username

+ } + } +
diff --git a/NG_2023_Kanban/Views/Home/Login.cshtml b/NG_2023_Kanban/Views/Auth/Login.cshtml similarity index 100% rename from NG_2023_Kanban/Views/Home/Login.cshtml rename to NG_2023_Kanban/Views/Auth/Login.cshtml diff --git a/NG_2023_Kanban/Views/Home/Register.cshtml b/NG_2023_Kanban/Views/Auth/Register.cshtml similarity index 100% rename from NG_2023_Kanban/Views/Home/Register.cshtml rename to NG_2023_Kanban/Views/Auth/Register.cshtml diff --git a/NG_2023_Kanban/Views/Shared/_Layout.cshtml b/NG_2023_Kanban/Views/Shared/_Layout.cshtml index 6b703e4..dea1503 100644 --- a/NG_2023_Kanban/Views/Shared/_Layout.cshtml +++ b/NG_2023_Kanban/Views/Shared/_Layout.cshtml @@ -34,10 +34,10 @@ if (user.Role == (int)Roles.Administrator) { } } @@ -46,11 +46,11 @@