From ee0b063eece2e1c37d0d2825e8fc2b4a35ec3b81 Mon Sep 17 00:00:00 2001 From: Dimitar Byalkov Date: Fri, 12 May 2023 12:13:11 +0200 Subject: [PATCH] Refactoring - split data, logic and model layers; custom network exception --- .../AnnouncementRepository.cs | 13 +++--- .../CommentRepository.cs | 4 +- .../Data.csproj} | 5 ++- StudentHouseDashboard/Data/Exceptions.cs | 20 +++++++++ .../SqlConnectionHelper.cs | 7 ++-- .../Repositories => Data}/UserRepository.cs | 4 +- .../Managers => Logic}/AnnouncementManager.cs | 6 +-- .../Managers => Logic}/CommentManager.cs | 6 +-- StudentHouseDashboard/Logic/Logic.csproj | 18 ++++++++ .../Managers => Logic}/UserManager.cs | 6 +-- .../{HouseData => }/Models/Announcement.cs | 2 +- .../{HouseData => Models}/ClassDiagram1.cd | 0 .../{HouseData => }/Models/Comment.cs | 2 +- .../{HouseData => }/Models/Complaint.cs | 2 +- .../Models/ComplaintSeverity.cs | 2 +- .../{HouseData => }/Models/ComplaintStatus.cs | 2 +- .../{HouseData => }/Models/Event.cs | 2 +- .../{HouseData => }/Models/GenericMessage.cs | 5 +-- .../{HouseData => }/Models/IVotable.cs | 2 +- StudentHouseDashboard/Models/Models.csproj | 9 ++++ .../{HouseData => }/Models/User.cs | 6 ++- .../{HouseData => }/Models/UserRole.cs | 2 +- .../StudentHouseDashboard.sln | 30 +++++++++++--- StudentHouseDashboard/Tests/Tests.csproj | 19 +++++++++ StudentHouseDashboard/Tests/UnitTest1.cs | 11 +++++ StudentHouseDashboard/Tests/Usings.cs | 1 + StudentHouseDashboard/WebApp/Contact.cs | 22 ---------- .../WebApp/Pages/Announcement.cshtml | 2 +- .../WebApp/Pages/Announcement.cshtml.cs | 4 +- .../WebApp/Pages/Announcements.cshtml | 2 +- .../WebApp/Pages/Announcements.cshtml.cs | 4 +- .../WebApp/Pages/Contact.cshtml | 25 ----------- .../WebApp/Pages/Contact.cshtml.cs | 19 --------- .../WebApp/Pages/Login.cshtml.cs | 4 +- .../WebApp/Pages/Logout.cshtml | 6 +++ .../WebApp/Pages/Logout.cshtml.cs | 15 +++++++ .../WebApp/Pages/Privacy.cshtml | 6 ++- .../WebApp/Pages/Register.cshtml | 2 +- .../WebApp/Pages/Register.cshtml.cs | 4 +- .../WebApp/Pages/Shared/_Layout.cshtml | 33 +++++++++++---- StudentHouseDashboard/WebApp/WebApp.csproj | 11 +---- .../WinForms/AnnouncementForm.cs | 4 +- StudentHouseDashboard/WinForms/CommentForm.cs | 4 +- StudentHouseDashboard/WinForms/Dashboard.cs | 41 ++++++++++++++----- .../WinForms/Login.Designer.cs | 1 + StudentHouseDashboard/WinForms/Login.cs | 4 +- StudentHouseDashboard/WinForms/UserForm.cs | 14 +++++-- .../WinForms/WinForms.csproj | 3 +- 48 files changed, 256 insertions(+), 160 deletions(-) rename StudentHouseDashboard/{HouseData/Repositories => Data}/AnnouncementRepository.cs (92%) rename StudentHouseDashboard/{HouseData/Repositories => Data}/CommentRepository.cs (99%) rename StudentHouseDashboard/{HouseData/StudentHouseDashboard.csproj => Data/Data.csproj} (77%) create mode 100644 StudentHouseDashboard/Data/Exceptions.cs rename StudentHouseDashboard/{HouseData/Repositories => Data}/SqlConnectionHelper.cs (72%) rename StudentHouseDashboard/{HouseData/Repositories => Data}/UserRepository.cs (98%) rename StudentHouseDashboard/{HouseData/Managers => Logic}/AnnouncementManager.cs (91%) rename StudentHouseDashboard/{HouseData/Managers => Logic}/CommentManager.cs (91%) create mode 100644 StudentHouseDashboard/Logic/Logic.csproj rename StudentHouseDashboard/{HouseData/Managers => Logic}/UserManager.cs (93%) rename StudentHouseDashboard/{HouseData => }/Models/Announcement.cs (96%) rename StudentHouseDashboard/{HouseData => Models}/ClassDiagram1.cd (100%) rename StudentHouseDashboard/{HouseData => }/Models/Comment.cs (95%) rename StudentHouseDashboard/{HouseData => }/Models/Complaint.cs (94%) rename StudentHouseDashboard/{HouseData => }/Models/ComplaintSeverity.cs (83%) rename StudentHouseDashboard/{HouseData => }/Models/ComplaintStatus.cs (83%) rename StudentHouseDashboard/{HouseData => }/Models/Event.cs (93%) rename StudentHouseDashboard/{HouseData => }/Models/GenericMessage.cs (92%) rename StudentHouseDashboard/{HouseData => }/Models/IVotable.cs (88%) create mode 100644 StudentHouseDashboard/Models/Models.csproj rename StudentHouseDashboard/{HouseData => }/Models/User.cs (91%) rename StudentHouseDashboard/{HouseData => }/Models/UserRole.cs (89%) create mode 100644 StudentHouseDashboard/Tests/Tests.csproj create mode 100644 StudentHouseDashboard/Tests/UnitTest1.cs create mode 100644 StudentHouseDashboard/Tests/Usings.cs delete mode 100644 StudentHouseDashboard/WebApp/Contact.cs delete mode 100644 StudentHouseDashboard/WebApp/Pages/Contact.cshtml delete mode 100644 StudentHouseDashboard/WebApp/Pages/Contact.cshtml.cs create mode 100644 StudentHouseDashboard/WebApp/Pages/Logout.cshtml create mode 100644 StudentHouseDashboard/WebApp/Pages/Logout.cshtml.cs diff --git a/StudentHouseDashboard/HouseData/Repositories/AnnouncementRepository.cs b/StudentHouseDashboard/Data/AnnouncementRepository.cs similarity index 92% rename from StudentHouseDashboard/HouseData/Repositories/AnnouncementRepository.cs rename to StudentHouseDashboard/Data/AnnouncementRepository.cs index ebcf7ee..9eab875 100644 --- a/StudentHouseDashboard/HouseData/Repositories/AnnouncementRepository.cs +++ b/StudentHouseDashboard/Data/AnnouncementRepository.cs @@ -1,5 +1,4 @@ -using StudentHouseDashboard.Managers; -using StudentHouseDashboard.Models; +using Models; using System; using System.Collections.Generic; using System.Data.SqlClient; @@ -7,7 +6,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace StudentHouseDashboard.Repositories +namespace Data { public class AnnouncementRepository { @@ -15,7 +14,7 @@ namespace StudentHouseDashboard.Repositories public List GetAllAnnouncements() { List announcements = new List(); - UserManager userManager = new UserManager(); + UserRepository userRepository = new UserRepository(); using (SqlConnection conn = SqlConnectionHelper.CreateConnection()) { string sql = "SELECT * FROM Announcements;"; @@ -25,7 +24,7 @@ namespace StudentHouseDashboard.Repositories while (reader.Read()) { 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(), (DateTime)reader["PublishDate"], (bool)reader["IsImportant"], (bool)reader["IsSticky"]); @@ -41,7 +40,7 @@ namespace StudentHouseDashboard.Repositories public List GetAnnouncementsByPage(int? p, int? c) { List announcements = new List(); - UserManager userManager = new UserManager(); + UserRepository userRepository = new UserRepository(); if (c == null || c < 0) { c = 10; @@ -60,7 +59,7 @@ namespace StudentHouseDashboard.Repositories while (reader.Read()) { 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(), (DateTime)reader["PublishDate"], (bool)reader["IsImportant"], (bool)reader["IsSticky"])); diff --git a/StudentHouseDashboard/HouseData/Repositories/CommentRepository.cs b/StudentHouseDashboard/Data/CommentRepository.cs similarity index 99% rename from StudentHouseDashboard/HouseData/Repositories/CommentRepository.cs rename to StudentHouseDashboard/Data/CommentRepository.cs index e7739ea..9848bb3 100644 --- a/StudentHouseDashboard/HouseData/Repositories/CommentRepository.cs +++ b/StudentHouseDashboard/Data/CommentRepository.cs @@ -1,8 +1,8 @@ using System.ComponentModel.Design; using System.Data.SqlClient; -using StudentHouseDashboard.Models; +using Models; -namespace StudentHouseDashboard.Repositories; +namespace Data; public class CommentRepository { diff --git a/StudentHouseDashboard/HouseData/StudentHouseDashboard.csproj b/StudentHouseDashboard/Data/Data.csproj similarity index 77% rename from StudentHouseDashboard/HouseData/StudentHouseDashboard.csproj rename to StudentHouseDashboard/Data/Data.csproj index c149945..d55a82b 100644 --- a/StudentHouseDashboard/HouseData/StudentHouseDashboard.csproj +++ b/StudentHouseDashboard/Data/Data.csproj @@ -7,8 +7,11 @@ - + + + + diff --git a/StudentHouseDashboard/Data/Exceptions.cs b/StudentHouseDashboard/Data/Exceptions.cs new file mode 100644 index 0000000..e7c1dfb --- /dev/null +++ b/StudentHouseDashboard/Data/Exceptions.cs @@ -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) + { + } + } +} diff --git a/StudentHouseDashboard/HouseData/Repositories/SqlConnectionHelper.cs b/StudentHouseDashboard/Data/SqlConnectionHelper.cs similarity index 72% rename from StudentHouseDashboard/HouseData/Repositories/SqlConnectionHelper.cs rename to StudentHouseDashboard/Data/SqlConnectionHelper.cs index 3047e3e..d445913 100644 --- a/StudentHouseDashboard/HouseData/Repositories/SqlConnectionHelper.cs +++ b/StudentHouseDashboard/Data/SqlConnectionHelper.cs @@ -5,7 +5,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace StudentHouseDashboard.Repositories +namespace Data { public static class SqlConnectionHelper { @@ -17,9 +17,10 @@ namespace StudentHouseDashboard.Repositories { 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; diff --git a/StudentHouseDashboard/HouseData/Repositories/UserRepository.cs b/StudentHouseDashboard/Data/UserRepository.cs similarity index 98% rename from StudentHouseDashboard/HouseData/Repositories/UserRepository.cs rename to StudentHouseDashboard/Data/UserRepository.cs index 76b9c3c..d869b63 100644 --- a/StudentHouseDashboard/HouseData/Repositories/UserRepository.cs +++ b/StudentHouseDashboard/Data/UserRepository.cs @@ -4,11 +4,11 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.SqlClient; -using StudentHouseDashboard.Models; +using Models; using System.Data; using System.Xml.Linq; -namespace StudentHouseDashboard.Repositories +namespace Data { public class UserRepository { diff --git a/StudentHouseDashboard/HouseData/Managers/AnnouncementManager.cs b/StudentHouseDashboard/Logic/AnnouncementManager.cs similarity index 91% rename from StudentHouseDashboard/HouseData/Managers/AnnouncementManager.cs rename to StudentHouseDashboard/Logic/AnnouncementManager.cs index 8bb1ac1..2b52701 100644 --- a/StudentHouseDashboard/HouseData/Managers/AnnouncementManager.cs +++ b/StudentHouseDashboard/Logic/AnnouncementManager.cs @@ -1,5 +1,5 @@ -using StudentHouseDashboard.Models; -using StudentHouseDashboard.Repositories; +using Models; +using Data; using System; using System.Collections.Generic; using System.Data.SqlClient; @@ -7,7 +7,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace StudentHouseDashboard.Managers +namespace Logic { public class AnnouncementManager { diff --git a/StudentHouseDashboard/HouseData/Managers/CommentManager.cs b/StudentHouseDashboard/Logic/CommentManager.cs similarity index 91% rename from StudentHouseDashboard/HouseData/Managers/CommentManager.cs rename to StudentHouseDashboard/Logic/CommentManager.cs index 59fe596..7e12047 100644 --- a/StudentHouseDashboard/HouseData/Managers/CommentManager.cs +++ b/StudentHouseDashboard/Logic/CommentManager.cs @@ -1,12 +1,12 @@ -using StudentHouseDashboard.Models; -using StudentHouseDashboard.Repositories; +using Models; +using Data; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -namespace StudentHouseDashboard.Managers +namespace Logic { public class CommentManager { diff --git a/StudentHouseDashboard/Logic/Logic.csproj b/StudentHouseDashboard/Logic/Logic.csproj new file mode 100644 index 0000000..de87000 --- /dev/null +++ b/StudentHouseDashboard/Logic/Logic.csproj @@ -0,0 +1,18 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + + diff --git a/StudentHouseDashboard/HouseData/Managers/UserManager.cs b/StudentHouseDashboard/Logic/UserManager.cs similarity index 93% rename from StudentHouseDashboard/HouseData/Managers/UserManager.cs rename to StudentHouseDashboard/Logic/UserManager.cs index 71ea43e..07410bf 100644 --- a/StudentHouseDashboard/HouseData/Managers/UserManager.cs +++ b/StudentHouseDashboard/Logic/UserManager.cs @@ -1,6 +1,6 @@ using BCrypt.Net; -using StudentHouseDashboard.Models; -using StudentHouseDashboard.Repositories; +using Models; +using Data; using System; using System.Collections.Generic; using System.Data; @@ -11,7 +11,7 @@ using System.Text; using System.Threading.Tasks; using System.Xml.Linq; -namespace StudentHouseDashboard.Managers +namespace Logic { public class UserManager { diff --git a/StudentHouseDashboard/HouseData/Models/Announcement.cs b/StudentHouseDashboard/Models/Announcement.cs similarity index 96% rename from StudentHouseDashboard/HouseData/Models/Announcement.cs rename to StudentHouseDashboard/Models/Announcement.cs index 2ef3e40..05ef4ed 100644 --- a/StudentHouseDashboard/HouseData/Models/Announcement.cs +++ b/StudentHouseDashboard/Models/Announcement.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; -namespace StudentHouseDashboard.Models +namespace Models { public class Announcement : GenericMessage, IVotable { diff --git a/StudentHouseDashboard/HouseData/ClassDiagram1.cd b/StudentHouseDashboard/Models/ClassDiagram1.cd similarity index 100% rename from StudentHouseDashboard/HouseData/ClassDiagram1.cd rename to StudentHouseDashboard/Models/ClassDiagram1.cd diff --git a/StudentHouseDashboard/HouseData/Models/Comment.cs b/StudentHouseDashboard/Models/Comment.cs similarity index 95% rename from StudentHouseDashboard/HouseData/Models/Comment.cs rename to StudentHouseDashboard/Models/Comment.cs index bec9a88..96bb824 100644 --- a/StudentHouseDashboard/HouseData/Models/Comment.cs +++ b/StudentHouseDashboard/Models/Comment.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; -namespace StudentHouseDashboard.Models +namespace Models { public class Comment : GenericMessage, IVotable { diff --git a/StudentHouseDashboard/HouseData/Models/Complaint.cs b/StudentHouseDashboard/Models/Complaint.cs similarity index 94% rename from StudentHouseDashboard/HouseData/Models/Complaint.cs rename to StudentHouseDashboard/Models/Complaint.cs index 21a9bd9..83a728e 100644 --- a/StudentHouseDashboard/HouseData/Models/Complaint.cs +++ b/StudentHouseDashboard/Models/Complaint.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; -namespace StudentHouseDashboard.Models +namespace Models { public class Complaint : GenericMessage { diff --git a/StudentHouseDashboard/HouseData/Models/ComplaintSeverity.cs b/StudentHouseDashboard/Models/ComplaintSeverity.cs similarity index 83% rename from StudentHouseDashboard/HouseData/Models/ComplaintSeverity.cs rename to StudentHouseDashboard/Models/ComplaintSeverity.cs index 69fbb17..d2ebd96 100644 --- a/StudentHouseDashboard/HouseData/Models/ComplaintSeverity.cs +++ b/StudentHouseDashboard/Models/ComplaintSeverity.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; -namespace StudentHouseDashboard.Models +namespace Models { public enum ComplaintSeverity { diff --git a/StudentHouseDashboard/HouseData/Models/ComplaintStatus.cs b/StudentHouseDashboard/Models/ComplaintStatus.cs similarity index 83% rename from StudentHouseDashboard/HouseData/Models/ComplaintStatus.cs rename to StudentHouseDashboard/Models/ComplaintStatus.cs index 453ff93..d6b790b 100644 --- a/StudentHouseDashboard/HouseData/Models/ComplaintStatus.cs +++ b/StudentHouseDashboard/Models/ComplaintStatus.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; -namespace StudentHouseDashboard.Models +namespace Models { public enum ComplaintStatus { diff --git a/StudentHouseDashboard/HouseData/Models/Event.cs b/StudentHouseDashboard/Models/Event.cs similarity index 93% rename from StudentHouseDashboard/HouseData/Models/Event.cs rename to StudentHouseDashboard/Models/Event.cs index 3d161e6..2474992 100644 --- a/StudentHouseDashboard/HouseData/Models/Event.cs +++ b/StudentHouseDashboard/Models/Event.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; -namespace StudentHouseDashboard.Models +namespace Models { public class Event : GenericMessage { diff --git a/StudentHouseDashboard/HouseData/Models/GenericMessage.cs b/StudentHouseDashboard/Models/GenericMessage.cs similarity index 92% rename from StudentHouseDashboard/HouseData/Models/GenericMessage.cs rename to StudentHouseDashboard/Models/GenericMessage.cs index 7592308..82305e9 100644 --- a/StudentHouseDashboard/HouseData/Models/GenericMessage.cs +++ b/StudentHouseDashboard/Models/GenericMessage.cs @@ -1,10 +1,9 @@ -using StudentHouseDashboard.Models; -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; -namespace StudentHouseDashboard +namespace Models { public abstract class GenericMessage { diff --git a/StudentHouseDashboard/HouseData/Models/IVotable.cs b/StudentHouseDashboard/Models/IVotable.cs similarity index 88% rename from StudentHouseDashboard/HouseData/Models/IVotable.cs rename to StudentHouseDashboard/Models/IVotable.cs index 5b8c37c..559778f 100644 --- a/StudentHouseDashboard/HouseData/Models/IVotable.cs +++ b/StudentHouseDashboard/Models/IVotable.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; -namespace StudentHouseDashboard.Models +namespace Models { public interface IVotable { diff --git a/StudentHouseDashboard/Models/Models.csproj b/StudentHouseDashboard/Models/Models.csproj new file mode 100644 index 0000000..132c02c --- /dev/null +++ b/StudentHouseDashboard/Models/Models.csproj @@ -0,0 +1,9 @@ + + + + net6.0 + enable + enable + + + diff --git a/StudentHouseDashboard/HouseData/Models/User.cs b/StudentHouseDashboard/Models/User.cs similarity index 91% rename from StudentHouseDashboard/HouseData/Models/User.cs rename to StudentHouseDashboard/Models/User.cs index 36c563d..e2771b7 100644 --- a/StudentHouseDashboard/HouseData/Models/User.cs +++ b/StudentHouseDashboard/Models/User.cs @@ -1,10 +1,11 @@ using System; using System.Collections.Generic; using System.ComponentModel; +using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; -namespace StudentHouseDashboard.Models +namespace Models { public class User { @@ -34,7 +35,8 @@ namespace StudentHouseDashboard.Models get => name; set => name = value; } - [PasswordPropertyText(true)] + + [DataType(DataType.Password)] public string Password { get => password; diff --git a/StudentHouseDashboard/HouseData/Models/UserRole.cs b/StudentHouseDashboard/Models/UserRole.cs similarity index 89% rename from StudentHouseDashboard/HouseData/Models/UserRole.cs rename to StudentHouseDashboard/Models/UserRole.cs index 1496bee..542e350 100644 --- a/StudentHouseDashboard/HouseData/Models/UserRole.cs +++ b/StudentHouseDashboard/Models/UserRole.cs @@ -4,7 +4,7 @@ using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; -namespace StudentHouseDashboard.Models +namespace Models { public enum UserRole { diff --git a/StudentHouseDashboard/StudentHouseDashboard.sln b/StudentHouseDashboard/StudentHouseDashboard.sln index 2aded82..b9b90f3 100644 --- a/StudentHouseDashboard/StudentHouseDashboard.sln +++ b/StudentHouseDashboard/StudentHouseDashboard.sln @@ -5,9 +5,15 @@ VisualStudioVersion = 17.4.33403.182 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApp", "WebApp\WebApp.csproj", "{5A02E4A7-32C2-4372-BEB1-A5ED407E0B23}" 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 -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 Global 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}.Release|Any CPU.ActiveCfg = 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.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.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 GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/StudentHouseDashboard/Tests/Tests.csproj b/StudentHouseDashboard/Tests/Tests.csproj new file mode 100644 index 0000000..d0a09ad --- /dev/null +++ b/StudentHouseDashboard/Tests/Tests.csproj @@ -0,0 +1,19 @@ + + + + net6.0 + enable + enable + + false + true + + + + + + + + + + diff --git a/StudentHouseDashboard/Tests/UnitTest1.cs b/StudentHouseDashboard/Tests/UnitTest1.cs new file mode 100644 index 0000000..f91fc3b --- /dev/null +++ b/StudentHouseDashboard/Tests/UnitTest1.cs @@ -0,0 +1,11 @@ +namespace Tests +{ + [TestClass] + public class UnitTest1 + { + [TestMethod] + public void TestMethod1() + { + } + } +} \ No newline at end of file diff --git a/StudentHouseDashboard/Tests/Usings.cs b/StudentHouseDashboard/Tests/Usings.cs new file mode 100644 index 0000000..ab67c7e --- /dev/null +++ b/StudentHouseDashboard/Tests/Usings.cs @@ -0,0 +1 @@ +global using Microsoft.VisualStudio.TestTools.UnitTesting; \ No newline at end of file diff --git a/StudentHouseDashboard/WebApp/Contact.cs b/StudentHouseDashboard/WebApp/Contact.cs deleted file mode 100644 index 1e3f613..0000000 --- a/StudentHouseDashboard/WebApp/Contact.cs +++ /dev/null @@ -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; } - } -} diff --git a/StudentHouseDashboard/WebApp/Pages/Announcement.cshtml b/StudentHouseDashboard/WebApp/Pages/Announcement.cshtml index 254ad15..368957b 100644 --- a/StudentHouseDashboard/WebApp/Pages/Announcement.cshtml +++ b/StudentHouseDashboard/WebApp/Pages/Announcement.cshtml @@ -1,5 +1,5 @@ @page -@using StudentHouseDashboard.Models; +@using Models; @using System.Globalization @model WebApp.Pages.AnnouncementModel @{ diff --git a/StudentHouseDashboard/WebApp/Pages/Announcement.cshtml.cs b/StudentHouseDashboard/WebApp/Pages/Announcement.cshtml.cs index 2fab7ae..1035b2b 100644 --- a/StudentHouseDashboard/WebApp/Pages/Announcement.cshtml.cs +++ b/StudentHouseDashboard/WebApp/Pages/Announcement.cshtml.cs @@ -1,8 +1,8 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; -using StudentHouseDashboard.Managers; -using StudentHouseDashboard.Models; +using Logic; +using Models; namespace WebApp.Pages { diff --git a/StudentHouseDashboard/WebApp/Pages/Announcements.cshtml b/StudentHouseDashboard/WebApp/Pages/Announcements.cshtml index a1be590..16546c1 100644 --- a/StudentHouseDashboard/WebApp/Pages/Announcements.cshtml +++ b/StudentHouseDashboard/WebApp/Pages/Announcements.cshtml @@ -1,5 +1,5 @@ @page -@using StudentHouseDashboard.Models; +@using Models; @using System.Security.Claims; @model WebApp.Pages.AnnouncementsModel @{ diff --git a/StudentHouseDashboard/WebApp/Pages/Announcements.cshtml.cs b/StudentHouseDashboard/WebApp/Pages/Announcements.cshtml.cs index 38d37d9..5f13b08 100644 --- a/StudentHouseDashboard/WebApp/Pages/Announcements.cshtml.cs +++ b/StudentHouseDashboard/WebApp/Pages/Announcements.cshtml.cs @@ -1,8 +1,8 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; -using StudentHouseDashboard.Managers; -using StudentHouseDashboard.Models; +using Logic; +using Models; using System.Security.Claims; namespace WebApp.Pages diff --git a/StudentHouseDashboard/WebApp/Pages/Contact.cshtml b/StudentHouseDashboard/WebApp/Pages/Contact.cshtml deleted file mode 100644 index 611f611..0000000 --- a/StudentHouseDashboard/WebApp/Pages/Contact.cshtml +++ /dev/null @@ -1,25 +0,0 @@ -@page -@model WebApp.Pages.ContactModel -@{ - ViewData["Title"] = "Contact"; -} -

@ViewData["Title"]

- -@if (ViewData["confirm"] != null) -{ - -} - -
-
- - -
-
- - -
- -
diff --git a/StudentHouseDashboard/WebApp/Pages/Contact.cshtml.cs b/StudentHouseDashboard/WebApp/Pages/Contact.cshtml.cs deleted file mode 100644 index 894e489..0000000 --- a/StudentHouseDashboard/WebApp/Pages/Contact.cshtml.cs +++ /dev/null @@ -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}"; - } - } -} diff --git a/StudentHouseDashboard/WebApp/Pages/Login.cshtml.cs b/StudentHouseDashboard/WebApp/Pages/Login.cshtml.cs index 3979923..37452d6 100644 --- a/StudentHouseDashboard/WebApp/Pages/Login.cshtml.cs +++ b/StudentHouseDashboard/WebApp/Pages/Login.cshtml.cs @@ -2,8 +2,8 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.Cookies; -using StudentHouseDashboard.Models; -using StudentHouseDashboard.Managers; +using Models; +using Logic; using System.Security.Claims; namespace WebApp.Pages diff --git a/StudentHouseDashboard/WebApp/Pages/Logout.cshtml b/StudentHouseDashboard/WebApp/Pages/Logout.cshtml new file mode 100644 index 0000000..8351bd2 --- /dev/null +++ b/StudentHouseDashboard/WebApp/Pages/Logout.cshtml @@ -0,0 +1,6 @@ +@page +@model WebApp.Pages.LogoutModel +@{ +} +

Logout

+

You should be redirected to the homepage

\ No newline at end of file diff --git a/StudentHouseDashboard/WebApp/Pages/Logout.cshtml.cs b/StudentHouseDashboard/WebApp/Pages/Logout.cshtml.cs new file mode 100644 index 0000000..863c682 --- /dev/null +++ b/StudentHouseDashboard/WebApp/Pages/Logout.cshtml.cs @@ -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"); + } + } +} diff --git a/StudentHouseDashboard/WebApp/Pages/Privacy.cshtml b/StudentHouseDashboard/WebApp/Pages/Privacy.cshtml index cbd8c00..657a722 100644 --- a/StudentHouseDashboard/WebApp/Pages/Privacy.cshtml +++ b/StudentHouseDashboard/WebApp/Pages/Privacy.cshtml @@ -5,4 +5,8 @@ }

@ViewData["Title"]

-

No data is currently collected.

+

Collected personal data

+
  • +
      Password (hashed and salted)
    +
      User-generated content (posts)
    +
  • diff --git a/StudentHouseDashboard/WebApp/Pages/Register.cshtml b/StudentHouseDashboard/WebApp/Pages/Register.cshtml index b371200..c044f61 100644 --- a/StudentHouseDashboard/WebApp/Pages/Register.cshtml +++ b/StudentHouseDashboard/WebApp/Pages/Register.cshtml @@ -1,5 +1,5 @@ @page -@using StudentHouseDashboard.Models; +@using Models; @model WebApp.Pages.RegisterModel @{ ViewData["Title"] = "Register"; diff --git a/StudentHouseDashboard/WebApp/Pages/Register.cshtml.cs b/StudentHouseDashboard/WebApp/Pages/Register.cshtml.cs index 7c0fb3f..e349e95 100644 --- a/StudentHouseDashboard/WebApp/Pages/Register.cshtml.cs +++ b/StudentHouseDashboard/WebApp/Pages/Register.cshtml.cs @@ -1,7 +1,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; -using StudentHouseDashboard.Managers; -using StudentHouseDashboard.Models; +using Logic; +using Models; namespace WebApp.Pages { diff --git a/StudentHouseDashboard/WebApp/Pages/Shared/_Layout.cshtml b/StudentHouseDashboard/WebApp/Pages/Shared/_Layout.cshtml index b37d7fc..b268338 100644 --- a/StudentHouseDashboard/WebApp/Pages/Shared/_Layout.cshtml +++ b/StudentHouseDashboard/WebApp/Pages/Shared/_Layout.cshtml @@ -22,15 +22,30 @@ - - - + @if (!User.Identity.IsAuthenticated) + { + + + } + else + { + + if (User.IsInRole("ADMIN") || User.IsInRole("MANAGER")) + { + + } + + } diff --git a/StudentHouseDashboard/WebApp/WebApp.csproj b/StudentHouseDashboard/WebApp/WebApp.csproj index 232cde6..ea9c409 100644 --- a/StudentHouseDashboard/WebApp/WebApp.csproj +++ b/StudentHouseDashboard/WebApp/WebApp.csproj @@ -17,20 +17,13 @@ - - - - - - <_ContentIncludedByDefault Remove="Pages\Contact.cshtml" /> - - - + + diff --git a/StudentHouseDashboard/WinForms/AnnouncementForm.cs b/StudentHouseDashboard/WinForms/AnnouncementForm.cs index db610e0..38efd03 100644 --- a/StudentHouseDashboard/WinForms/AnnouncementForm.cs +++ b/StudentHouseDashboard/WinForms/AnnouncementForm.cs @@ -1,5 +1,5 @@ -using StudentHouseDashboard.Managers; -using StudentHouseDashboard.Models; +using Logic; +using Models; using System; using System.Collections.Generic; using System.ComponentModel; diff --git a/StudentHouseDashboard/WinForms/CommentForm.cs b/StudentHouseDashboard/WinForms/CommentForm.cs index 97e289c..ea21cd0 100644 --- a/StudentHouseDashboard/WinForms/CommentForm.cs +++ b/StudentHouseDashboard/WinForms/CommentForm.cs @@ -1,5 +1,5 @@ -using StudentHouseDashboard.Managers; -using StudentHouseDashboard.Models; +using Logic; +using Models; using System; using System.Collections.Generic; using System.ComponentModel; diff --git a/StudentHouseDashboard/WinForms/Dashboard.cs b/StudentHouseDashboard/WinForms/Dashboard.cs index 49361d3..7aecd7a 100644 --- a/StudentHouseDashboard/WinForms/Dashboard.cs +++ b/StudentHouseDashboard/WinForms/Dashboard.cs @@ -1,5 +1,5 @@ -using StudentHouseDashboard.Managers; -using StudentHouseDashboard.Models; +using Logic; +using Models; using System; using System.Collections.Generic; using System.ComponentModel; @@ -61,26 +61,47 @@ namespace WinForms private void btnDeleteUser_Click(object sender, EventArgs e) { - 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) + if (lbUsers.SelectedIndex == -1) { - UserManager userManager = new UserManager(); - userManager.DisableUser(currentUser.ID); + MessageBox.Show("Please select an item from the list"); + } + 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(); } private void btnUpdateUser_Click(object sender, EventArgs e) { - UserForm userForm = new UserForm((User)lbUsers.SelectedItem, false); - userForm.ShowDialog(); + if (lbUsers.SelectedIndex == -1) + { + MessageBox.Show("Please select an item from the list"); + } + else + { + UserForm userForm = new UserForm((User)lbUsers.SelectedItem, false); + userForm.ShowDialog(); + } RefreshLists(); } private void btnViewUser_Click(object sender, EventArgs e) { - UserForm userForm = new UserForm((User)lbUsers.SelectedItem, true); - userForm.ShowDialog(); + if (lbUsers.SelectedIndex == -1) + { + MessageBox.Show("Please select an item from the list"); + } + else + { + UserForm userForm = new UserForm((User)lbUsers.SelectedItem, true); + userForm.ShowDialog(); + } RefreshLists(); } diff --git a/StudentHouseDashboard/WinForms/Login.Designer.cs b/StudentHouseDashboard/WinForms/Login.Designer.cs index 8089343..3ad163f 100644 --- a/StudentHouseDashboard/WinForms/Login.Designer.cs +++ b/StudentHouseDashboard/WinForms/Login.Designer.cs @@ -74,6 +74,7 @@ // tbPassword.Location = new Point(81, 90); tbPassword.Name = "tbPassword"; + tbPassword.PasswordChar = '*'; tbPassword.Size = new Size(161, 23); tbPassword.TabIndex = 4; // diff --git a/StudentHouseDashboard/WinForms/Login.cs b/StudentHouseDashboard/WinForms/Login.cs index 641dbd5..77e552e 100644 --- a/StudentHouseDashboard/WinForms/Login.cs +++ b/StudentHouseDashboard/WinForms/Login.cs @@ -1,5 +1,5 @@ -using StudentHouseDashboard.Managers; -using StudentHouseDashboard.Models; +using Logic; +using Models; namespace WinForms { diff --git a/StudentHouseDashboard/WinForms/UserForm.cs b/StudentHouseDashboard/WinForms/UserForm.cs index 07c9ed8..0478c4d 100644 --- a/StudentHouseDashboard/WinForms/UserForm.cs +++ b/StudentHouseDashboard/WinForms/UserForm.cs @@ -1,5 +1,5 @@ -using StudentHouseDashboard.Managers; -using StudentHouseDashboard.Models; +using Logic; +using Models; using System; using System.Collections.Generic; using System.ComponentModel; @@ -34,7 +34,6 @@ namespace WinForms if (user != null) { tbUsername.Text = user.Name; - tbPassword.Text = user.Password; cbUserRole.SelectedIndex = (int)user.Role; } @@ -55,7 +54,14 @@ namespace WinForms } 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; } diff --git a/StudentHouseDashboard/WinForms/WinForms.csproj b/StudentHouseDashboard/WinForms/WinForms.csproj index e0fd031..51e0aa1 100644 --- a/StudentHouseDashboard/WinForms/WinForms.csproj +++ b/StudentHouseDashboard/WinForms/WinForms.csproj @@ -10,7 +10,8 @@ - + +