Refactoring - split data, logic and model layers; custom network exception

This commit is contained in:
Dimitar Byalkov
2023-05-12 12:13:11 +02:00
parent 81109f3d6c
commit ee0b063eec
48 changed files with 256 additions and 160 deletions

View File

@@ -1,5 +1,4 @@
using StudentHouseDashboard.Managers; using Models;
using StudentHouseDashboard.Models;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data.SqlClient; using System.Data.SqlClient;
@@ -7,7 +6,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace StudentHouseDashboard.Repositories namespace Data
{ {
public class AnnouncementRepository public class AnnouncementRepository
{ {
@@ -15,7 +14,7 @@ namespace StudentHouseDashboard.Repositories
public List<Announcement> GetAllAnnouncements() public List<Announcement> GetAllAnnouncements()
{ {
List<Announcement> announcements = new List<Announcement>(); List<Announcement> announcements = new List<Announcement>();
UserManager userManager = new UserManager(); UserRepository userRepository = new UserRepository();
using (SqlConnection conn = SqlConnectionHelper.CreateConnection()) using (SqlConnection conn = SqlConnectionHelper.CreateConnection())
{ {
string sql = "SELECT * FROM Announcements;"; string sql = "SELECT * FROM Announcements;";
@@ -25,7 +24,7 @@ namespace StudentHouseDashboard.Repositories
while (reader.Read()) while (reader.Read())
{ {
Announcement announcement = new Announcement(Convert.ToInt32(reader["ID"]), Announcement announcement = new Announcement(Convert.ToInt32(reader["ID"]),
userManager.GetUserById(Convert.ToInt32(reader["Author"])), userRepository.GetUserById(Convert.ToInt32(reader["Author"])),
reader["Description"].ToString(), reader["Title"].ToString(), reader["Description"].ToString(), reader["Title"].ToString(),
(DateTime)reader["PublishDate"], (bool)reader["IsImportant"], (DateTime)reader["PublishDate"], (bool)reader["IsImportant"],
(bool)reader["IsSticky"]); (bool)reader["IsSticky"]);
@@ -41,7 +40,7 @@ namespace StudentHouseDashboard.Repositories
public List<Announcement> GetAnnouncementsByPage(int? p, int? c) public List<Announcement> GetAnnouncementsByPage(int? p, int? c)
{ {
List<Announcement> announcements = new List<Announcement>(); List<Announcement> announcements = new List<Announcement>();
UserManager userManager = new UserManager(); UserRepository userRepository = new UserRepository();
if (c == null || c < 0) if (c == null || c < 0)
{ {
c = 10; c = 10;
@@ -60,7 +59,7 @@ namespace StudentHouseDashboard.Repositories
while (reader.Read()) while (reader.Read())
{ {
announcements.Add(new Announcement(Convert.ToInt32(reader["ID"]), announcements.Add(new Announcement(Convert.ToInt32(reader["ID"]),
userManager.GetUserById(Convert.ToInt32(reader["Author"])), userRepository.GetUserById(Convert.ToInt32(reader["Author"])),
reader["Description"].ToString(), reader["Title"].ToString(), reader["Description"].ToString(), reader["Title"].ToString(),
(DateTime)reader["PublishDate"], (bool)reader["IsImportant"], (DateTime)reader["PublishDate"], (bool)reader["IsImportant"],
(bool)reader["IsSticky"])); (bool)reader["IsSticky"]));

View File

@@ -1,8 +1,8 @@
using System.ComponentModel.Design; using System.ComponentModel.Design;
using System.Data.SqlClient; using System.Data.SqlClient;
using StudentHouseDashboard.Models; using Models;
namespace StudentHouseDashboard.Repositories; namespace Data;
public class CommentRepository public class CommentRepository
{ {

View File

@@ -7,8 +7,11 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.5" /> <PackageReference Include="System.Data.SqlClient" Version="4.8.5" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Models\Models.csproj" />
</ItemGroup>
</Project> </Project>

View File

@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace Data
{
public class DatabaseNetworkException : WebException
{
public DatabaseNetworkException(string? message) : base(message)
{
}
public DatabaseNetworkException(string? message, Exception? innerException) : base(message, innerException)
{
}
}
}

View File

@@ -5,7 +5,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace StudentHouseDashboard.Repositories namespace Data
{ {
public static class SqlConnectionHelper public static class SqlConnectionHelper
{ {
@@ -17,9 +17,10 @@ namespace StudentHouseDashboard.Repositories
{ {
connection.Open(); connection.Open();
} }
catch (Exception) catch (SqlException e)
{ {
Console.WriteLine("Database connection error. Are you connected to the VDI VPN?"); throw new DatabaseNetworkException("Unable to access FHICT VDI database", e);
// Console.WriteLine("Database connection error. Are you connected to the VDI VPN?");
} }
return connection; return connection;

View File

@@ -4,11 +4,11 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Data.SqlClient; using System.Data.SqlClient;
using StudentHouseDashboard.Models; using Models;
using System.Data; using System.Data;
using System.Xml.Linq; using System.Xml.Linq;
namespace StudentHouseDashboard.Repositories namespace Data
{ {
public class UserRepository public class UserRepository
{ {

View File

@@ -1,5 +1,5 @@
using StudentHouseDashboard.Models; using Models;
using StudentHouseDashboard.Repositories; using Data;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data.SqlClient; using System.Data.SqlClient;
@@ -7,7 +7,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace StudentHouseDashboard.Managers namespace Logic
{ {
public class AnnouncementManager public class AnnouncementManager
{ {

View File

@@ -1,12 +1,12 @@
using StudentHouseDashboard.Models; using Models;
using StudentHouseDashboard.Repositories; using Data;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace StudentHouseDashboard.Managers namespace Logic
{ {
public class CommentManager public class CommentManager
{ {

View File

@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Data\Data.csproj" />
<ProjectReference Include="..\Models\Models.csproj" />
</ItemGroup>
</Project>

View File

@@ -1,6 +1,6 @@
using BCrypt.Net; using BCrypt.Net;
using StudentHouseDashboard.Models; using Models;
using StudentHouseDashboard.Repositories; using Data;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
@@ -11,7 +11,7 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Xml.Linq; using System.Xml.Linq;
namespace StudentHouseDashboard.Managers namespace Logic
{ {
public class UserManager public class UserManager
{ {

View File

@@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
namespace StudentHouseDashboard.Models namespace Models
{ {
public class Announcement : GenericMessage, IVotable public class Announcement : GenericMessage, IVotable
{ {

View File

@@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
namespace StudentHouseDashboard.Models namespace Models
{ {
public class Comment : GenericMessage, IVotable public class Comment : GenericMessage, IVotable
{ {

View File

@@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
namespace StudentHouseDashboard.Models namespace Models
{ {
public class Complaint : GenericMessage public class Complaint : GenericMessage
{ {

View File

@@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
namespace StudentHouseDashboard.Models namespace Models
{ {
public enum ComplaintSeverity public enum ComplaintSeverity
{ {

View File

@@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
namespace StudentHouseDashboard.Models namespace Models
{ {
public enum ComplaintStatus public enum ComplaintStatus
{ {

View File

@@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
namespace StudentHouseDashboard.Models namespace Models
{ {
public class Event : GenericMessage public class Event : GenericMessage
{ {

View File

@@ -1,10 +1,9 @@
using StudentHouseDashboard.Models; using System;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
namespace StudentHouseDashboard namespace Models
{ {
public abstract class GenericMessage public abstract class GenericMessage
{ {

View File

@@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
namespace StudentHouseDashboard.Models namespace Models
{ {
public interface IVotable public interface IVotable
{ {

View File

@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -1,10 +1,11 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
namespace StudentHouseDashboard.Models namespace Models
{ {
public class User public class User
{ {
@@ -34,7 +35,8 @@ namespace StudentHouseDashboard.Models
get => name; get => name;
set => name = value; set => name = value;
} }
[PasswordPropertyText(true)]
[DataType(DataType.Password)]
public string Password public string Password
{ {
get => password; get => password;

View File

@@ -4,7 +4,7 @@ using System.ComponentModel.DataAnnotations;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
namespace StudentHouseDashboard.Models namespace Models
{ {
public enum UserRole public enum UserRole
{ {

View File

@@ -5,9 +5,15 @@ VisualStudioVersion = 17.4.33403.182
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApp", "WebApp\WebApp.csproj", "{5A02E4A7-32C2-4372-BEB1-A5ED407E0B23}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApp", "WebApp\WebApp.csproj", "{5A02E4A7-32C2-4372-BEB1-A5ED407E0B23}"
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StudentHouseDashboard", "HouseData\StudentHouseDashboard.csproj", "{9A1E1400-9B85-416B-B3B2-2282E0060CE3}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WinForms", "WinForms\WinForms.csproj", "{3967DDCF-BA8E-45CD-895D-626CE346D419}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinForms", "WinForms\WinForms.csproj", "{3967DDCF-BA8E-45CD-895D-626CE346D419}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Logic", "Logic\Logic.csproj", "{1AA9B921-CE97-4139-995E-95435B3110C0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Data", "Data\Data.csproj", "{FDF2FED1-3113-4BA6-8582-BBE97C301D66}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Models", "Models\Models.csproj", "{F7DC7AD1-7A0A-403B-A535-24267BD07628}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{5AB881E8-B8FE-43A5-A74B-40CF15B20AE8}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -19,14 +25,26 @@ Global
{5A02E4A7-32C2-4372-BEB1-A5ED407E0B23}.Debug|Any CPU.Build.0 = Debug|Any CPU {5A02E4A7-32C2-4372-BEB1-A5ED407E0B23}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5A02E4A7-32C2-4372-BEB1-A5ED407E0B23}.Release|Any CPU.ActiveCfg = Release|Any CPU {5A02E4A7-32C2-4372-BEB1-A5ED407E0B23}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5A02E4A7-32C2-4372-BEB1-A5ED407E0B23}.Release|Any CPU.Build.0 = Release|Any CPU {5A02E4A7-32C2-4372-BEB1-A5ED407E0B23}.Release|Any CPU.Build.0 = Release|Any CPU
{9A1E1400-9B85-416B-B3B2-2282E0060CE3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9A1E1400-9B85-416B-B3B2-2282E0060CE3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9A1E1400-9B85-416B-B3B2-2282E0060CE3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9A1E1400-9B85-416B-B3B2-2282E0060CE3}.Release|Any CPU.Build.0 = Release|Any CPU
{3967DDCF-BA8E-45CD-895D-626CE346D419}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {3967DDCF-BA8E-45CD-895D-626CE346D419}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3967DDCF-BA8E-45CD-895D-626CE346D419}.Debug|Any CPU.Build.0 = Debug|Any CPU {3967DDCF-BA8E-45CD-895D-626CE346D419}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3967DDCF-BA8E-45CD-895D-626CE346D419}.Release|Any CPU.ActiveCfg = Release|Any CPU {3967DDCF-BA8E-45CD-895D-626CE346D419}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3967DDCF-BA8E-45CD-895D-626CE346D419}.Release|Any CPU.Build.0 = Release|Any CPU {3967DDCF-BA8E-45CD-895D-626CE346D419}.Release|Any CPU.Build.0 = Release|Any CPU
{1AA9B921-CE97-4139-995E-95435B3110C0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1AA9B921-CE97-4139-995E-95435B3110C0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1AA9B921-CE97-4139-995E-95435B3110C0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1AA9B921-CE97-4139-995E-95435B3110C0}.Release|Any CPU.Build.0 = Release|Any CPU
{FDF2FED1-3113-4BA6-8582-BBE97C301D66}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FDF2FED1-3113-4BA6-8582-BBE97C301D66}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FDF2FED1-3113-4BA6-8582-BBE97C301D66}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FDF2FED1-3113-4BA6-8582-BBE97C301D66}.Release|Any CPU.Build.0 = Release|Any CPU
{F7DC7AD1-7A0A-403B-A535-24267BD07628}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F7DC7AD1-7A0A-403B-A535-24267BD07628}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F7DC7AD1-7A0A-403B-A535-24267BD07628}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F7DC7AD1-7A0A-403B-A535-24267BD07628}.Release|Any CPU.Build.0 = Release|Any CPU
{5AB881E8-B8FE-43A5-A74B-40CF15B20AE8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5AB881E8-B8FE-43A5-A74B-40CF15B20AE8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5AB881E8-B8FE-43A5-A74B-40CF15B20AE8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5AB881E8-B8FE-43A5-A74B-40CF15B20AE8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View File

@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.0.2" />
<PackageReference Include="MSTest.TestFramework" Version="3.0.2" />
<PackageReference Include="coverlet.collector" Version="3.2.0" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,11 @@
namespace Tests
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
}
}
}

View File

@@ -0,0 +1 @@
global using Microsoft.VisualStudio.TestTools.UnitTesting;

View File

@@ -1,22 +0,0 @@
using System.ComponentModel.DataAnnotations;
namespace WebApp
{
public class Contact
{
public Contact()
{
}
public Contact(string name, string email)
{
Name = name;
Email = email;
}
[Required]
public string Name { get; set; }
[Required]
[EmailAddress]
public string Email { get; set; }
}
}

View File

@@ -1,5 +1,5 @@
@page @page
@using StudentHouseDashboard.Models; @using Models;
@using System.Globalization @using System.Globalization
@model WebApp.Pages.AnnouncementModel @model WebApp.Pages.AnnouncementModel
@{ @{

View File

@@ -1,8 +1,8 @@
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.RazorPages;
using StudentHouseDashboard.Managers; using Logic;
using StudentHouseDashboard.Models; using Models;
namespace WebApp.Pages namespace WebApp.Pages
{ {

View File

@@ -1,5 +1,5 @@
@page @page
@using StudentHouseDashboard.Models; @using Models;
@using System.Security.Claims; @using System.Security.Claims;
@model WebApp.Pages.AnnouncementsModel @model WebApp.Pages.AnnouncementsModel
@{ @{

View File

@@ -1,8 +1,8 @@
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.RazorPages;
using StudentHouseDashboard.Managers; using Logic;
using StudentHouseDashboard.Models; using Models;
using System.Security.Claims; using System.Security.Claims;
namespace WebApp.Pages namespace WebApp.Pages

View File

@@ -1,25 +0,0 @@
@page
@model WebApp.Pages.ContactModel
@{
ViewData["Title"] = "Contact";
}
<h1>@ViewData["Title"]</h1>
@if (ViewData["confirm"] != null)
{
<div class="alert alert-primary" role="alert">
@ViewData["confirm"]
</div>
}
<form method="post">
<div class="mb-3">
<label asp-for="Contact.Name" class="form-label">Name: </label>
<input asp-for="Contact.Name" class="form-control" />
</div>
<div class="mb-3">
<label asp-for="Contact.Email" class="form-label">Email</label>
<input asp-for="Contact.Email" class="form-control" />
</div>
<input type="submit" value="Submit" class="btn btn-primary" />
</form>

View File

@@ -1,19 +0,0 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace WebApp.Pages
{
public class ContactModel : PageModel
{
[BindProperty]
public Contact Contact { get; set; }
public void OnGet()
{
}
public void OnPost()
{
ViewData["confirm"] = $"Thank you for contacting us, {Contact.Name}! We promise to return a response in a timely manner to the following e-mail address: {Contact.Email}";
}
}
}

View File

@@ -2,8 +2,8 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication.Cookies;
using StudentHouseDashboard.Models; using Models;
using StudentHouseDashboard.Managers; using Logic;
using System.Security.Claims; using System.Security.Claims;
namespace WebApp.Pages namespace WebApp.Pages

View File

@@ -0,0 +1,6 @@
@page
@model WebApp.Pages.LogoutModel
@{
}
<h1>Logout</h1>
<p>You should be redirected to the <a href="/">homepage</a></p>

View File

@@ -0,0 +1,15 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace WebApp.Pages
{
public class LogoutModel : PageModel
{
public IActionResult OnGet()
{
HttpContext.SignOutAsync();
return RedirectToPage("Index");
}
}
}

View File

@@ -5,4 +5,8 @@
} }
<h1>@ViewData["Title"]</h1> <h1>@ViewData["Title"]</h1>
<p>No data is currently collected.</p> <h2>Collected personal data</h2>
<li>
<ul>Password (hashed and salted)</ul>
<ul>User-generated content (posts)</ul>
</li>

View File

@@ -1,5 +1,5 @@
@page @page
@using StudentHouseDashboard.Models; @using Models;
@model WebApp.Pages.RegisterModel @model WebApp.Pages.RegisterModel
@{ @{
ViewData["Title"] = "Register"; ViewData["Title"] = "Register";

View File

@@ -1,7 +1,7 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.RazorPages;
using StudentHouseDashboard.Managers; using Logic;
using StudentHouseDashboard.Models; using Models;
namespace WebApp.Pages namespace WebApp.Pages
{ {

View File

@@ -22,15 +22,30 @@
<li class="nav-item"> <li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/Index">Home</a> <a class="nav-link text-dark" asp-area="" asp-page="/Index">Home</a>
</li> </li>
<li class="nav-item"> @if (!User.Identity.IsAuthenticated)
<a class="nav-link text-dark" asp-area="" asp-page="/Announcements">Announcements</a> {
</li> <li class="nav-item">
<li class="nav-item"> <a class="nav-link text-dark" asp-area="" asp-page="/Login">Login</a>
<a class="nav-link text-dark" asp-area="" asp-page="/Login">Login</a> </li>
</li> <li class="nav-item">
<li class="nav-item"> <a class="nav-link text-dark" asp-area="" asp-page="/Register">Register</a>
<a class="nav-link text-dark" asp-area="" asp-page="/Register">Register</a> </li>
</li> }
else
{
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/Announcements">Announcements</a>
</li>
if (User.IsInRole("ADMIN") || User.IsInRole("MANAGER"))
{
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/Register">Create user</a>
</li>
}
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/Logout">Logout</a>
</li>
}
</ul> </ul>
</div> </div>
</div> </div>

View File

@@ -17,20 +17,13 @@
</Content> </Content>
</ItemGroup> </ItemGroup>
<ItemGroup>
<UpToDateCheckInput Remove="Pages\Contact.cshtml" />
</ItemGroup>
<ItemGroup>
<_ContentIncludedByDefault Remove="Pages\Contact.cshtml" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" /> <PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\HouseData\StudentHouseDashboard.csproj" /> <ProjectReference Include="..\Logic\Logic.csproj" />
<ProjectReference Include="..\Models\Models.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -1,5 +1,5 @@
using StudentHouseDashboard.Managers; using Logic;
using StudentHouseDashboard.Models; using Models;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;

View File

@@ -1,5 +1,5 @@
using StudentHouseDashboard.Managers; using Logic;
using StudentHouseDashboard.Models; using Models;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;

View File

@@ -1,5 +1,5 @@
using StudentHouseDashboard.Managers; using Logic;
using StudentHouseDashboard.Models; using Models;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
@@ -61,26 +61,47 @@ namespace WinForms
private void btnDeleteUser_Click(object sender, EventArgs e) private void btnDeleteUser_Click(object sender, EventArgs e)
{ {
User currentUser = (User)lbUsers.SelectedItem; if (lbUsers.SelectedIndex == -1)
if (MessageBox.Show($"Are you sure you want to delete\n{currentUser.Name}\n{currentUser.Role}", "Delete user", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{ {
UserManager userManager = new UserManager(); MessageBox.Show("Please select an item from the list");
userManager.DisableUser(currentUser.ID); }
else
{
User currentUser = (User)lbUsers.SelectedItem;
if (MessageBox.Show($"Are you sure you want to delete\n{currentUser.Name}\n{currentUser.Role}", "Delete user", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
UserManager userManager = new UserManager();
userManager.DisableUser(currentUser.ID);
}
} }
RefreshLists(); RefreshLists();
} }
private void btnUpdateUser_Click(object sender, EventArgs e) private void btnUpdateUser_Click(object sender, EventArgs e)
{ {
UserForm userForm = new UserForm((User)lbUsers.SelectedItem, false); if (lbUsers.SelectedIndex == -1)
userForm.ShowDialog(); {
MessageBox.Show("Please select an item from the list");
}
else
{
UserForm userForm = new UserForm((User)lbUsers.SelectedItem, false);
userForm.ShowDialog();
}
RefreshLists(); RefreshLists();
} }
private void btnViewUser_Click(object sender, EventArgs e) private void btnViewUser_Click(object sender, EventArgs e)
{ {
UserForm userForm = new UserForm((User)lbUsers.SelectedItem, true); if (lbUsers.SelectedIndex == -1)
userForm.ShowDialog(); {
MessageBox.Show("Please select an item from the list");
}
else
{
UserForm userForm = new UserForm((User)lbUsers.SelectedItem, true);
userForm.ShowDialog();
}
RefreshLists(); RefreshLists();
} }

View File

@@ -74,6 +74,7 @@
// //
tbPassword.Location = new Point(81, 90); tbPassword.Location = new Point(81, 90);
tbPassword.Name = "tbPassword"; tbPassword.Name = "tbPassword";
tbPassword.PasswordChar = '*';
tbPassword.Size = new Size(161, 23); tbPassword.Size = new Size(161, 23);
tbPassword.TabIndex = 4; tbPassword.TabIndex = 4;
// //

View File

@@ -1,5 +1,5 @@
using StudentHouseDashboard.Managers; using Logic;
using StudentHouseDashboard.Models; using Models;
namespace WinForms namespace WinForms
{ {

View File

@@ -1,5 +1,5 @@
using StudentHouseDashboard.Managers; using Logic;
using StudentHouseDashboard.Models; using Models;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
@@ -34,7 +34,6 @@ namespace WinForms
if (user != null) if (user != null)
{ {
tbUsername.Text = user.Name; tbUsername.Text = user.Name;
tbPassword.Text = user.Password;
cbUserRole.SelectedIndex = (int)user.Role; cbUserRole.SelectedIndex = (int)user.Role;
} }
@@ -55,7 +54,14 @@ namespace WinForms
} }
else else
{ {
userManager.UpdateUser(this.user.ID, tbUsername.Text, BCrypt.Net.BCrypt.HashPassword(tbPassword.Text), (UserRole)cbUserRole.SelectedItem); if (string.IsNullOrEmpty(tbPassword.Text))
{
//userManager.UpdateUser(this.user.ID, tbUsername.Text,)
}
else
{
userManager.UpdateUser(this.user.ID, tbUsername.Text, BCrypt.Net.BCrypt.HashPassword(tbPassword.Text), (UserRole)cbUserRole.SelectedItem);
}
} }
this.DialogResult = DialogResult.OK; this.DialogResult = DialogResult.OK;
} }

View File

@@ -10,7 +10,8 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\HouseData\StudentHouseDashboard.csproj" /> <ProjectReference Include="..\Logic\Logic.csproj" />
<ProjectReference Include="..\Models\Models.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>