Refactoring: Added interfaces, custom exceptions, UserManager unit tests, dependency injection/inversion; Regex match search by date, keywords

This commit is contained in:
Dimitar Byalkov
2023-06-06 17:52:36 +02:00
parent 180b261d37
commit 53c42a35d8
43 changed files with 668 additions and 211 deletions

View File

@@ -1,4 +1,5 @@
using Logic;
using Data;
using Logic;
using Models;
using System;
using System.Collections.Generic;
@@ -56,7 +57,7 @@ namespace WinForms
private void btnSave_Click(object sender, EventArgs e)
{
AnnouncementManager announcementManager = new AnnouncementManager();
AnnouncementManager announcementManager = new AnnouncementManager(new AnnouncementRepository());
if (string.IsNullOrEmpty(tbTitle.Text))
{
MessageBox.Show("Please enter a title");
@@ -114,7 +115,7 @@ namespace WinForms
if (MessageBox.Show($"Are you sure you want to delete\n{currentComment.Title}\nCreated at {currentComment.PublishDate.ToString("g")} by {currentComment.Author.Name}",
"Delete announcement", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
CommentManager commentManager = new CommentManager();
CommentManager commentManager = new CommentManager(new CommentRepository());
commentManager.DeleteCommentOnAnnouncement(currentComment.ID, announcement.ID);
}
RefreshComments();

View File

@@ -1,4 +1,5 @@
using Logic;
using Data;
using Logic;
using Models;
using System;
using System.Collections.Generic;
@@ -43,13 +44,10 @@ namespace WinForms
btnViewComment.Enabled = false;
}
if (comment != null && currentUser != null)
if (comment != null && currentUser != null && currentUser.ID != comment.Author.ID)
{
if (currentUser.ID != comment.Author.ID)
{
// restriction: only edit personal comments
readOnly = true;
}
// restriction: only edit personal comments
readOnly = true;
}
if (readOnly)
@@ -71,7 +69,7 @@ namespace WinForms
}
private void btnSave_Click(object sender, EventArgs e)
{
CommentManager commentManager = new CommentManager();
CommentManager commentManager = new CommentManager(new CommentRepository());
if (string.IsNullOrEmpty(tbTitle.Text) || string.IsNullOrEmpty(tbDescription.Text))
{
MessageBox.Show("Please enter a title and comment text");
@@ -135,7 +133,7 @@ namespace WinForms
if (MessageBox.Show($"Are you sure you want to delete\n{currentResponse.Title}\nCreated at {currentResponse.PublishDate.ToString("g")} by {currentResponse.Author.Name}",
"Delete announcement", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
CommentManager commentManager = new CommentManager();
CommentManager commentManager = new CommentManager(new CommentRepository());
commentManager.DeleteResponseOnComment(currentResponse.ID, comment.ID);
}
RefreshComments();

View File

@@ -1,4 +1,5 @@
using Logic;
using Data;
using Logic;
using Models;
using System;
using System.Collections.Generic;
@@ -70,7 +71,7 @@ namespace WinForms
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 userManager = new UserManager(new UserRepository());
userManager.DisableUser(currentUser.ID);
}
}
@@ -107,13 +108,13 @@ namespace WinForms
private void RefreshLists()
{
UserManager userManager = new UserManager();
UserManager userManager = new UserManager(new UserRepository());
lbUsers.Items.Clear();
foreach (User _user in userManager.GetAllUsers())
{
lbUsers.Items.Add(_user);
}
AnnouncementManager announcementManager = new AnnouncementManager();
AnnouncementManager announcementManager = new AnnouncementManager(new AnnouncementRepository());
lbAnnouncements.Items.Clear();
foreach (Announcement announcement in announcementManager.GetAllAnnouncements())
{
@@ -145,7 +146,7 @@ namespace WinForms
if (MessageBox.Show($"Are you sure you want to delete\n{currentAnnouncement.Title}\nCreated at {currentAnnouncement.PublishDate.ToString("g")} by {currentAnnouncement.Author.Name}",
"Delete announcement", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
AnnouncementManager announcementManager = new AnnouncementManager();
AnnouncementManager announcementManager = new AnnouncementManager(new AnnouncementRepository());
announcementManager.DeleteAnnouncement(currentAnnouncement.ID);
}
RefreshLists();

View File

@@ -1,3 +1,4 @@
using Data;
using Logic;
using Models;
@@ -12,7 +13,7 @@ namespace WinForms
private void btnLogin_Click(object sender, EventArgs e)
{
UserManager userManager = new UserManager();
UserManager userManager = new UserManager(new UserRepository());
User? user = userManager.AuthenticatedUser(tbUsername.Text, tbPassword.Text);
if (user == null)
{

View File

@@ -1,4 +1,5 @@
using Logic;
using Data;
using Logic;
using Models;
using System;
using System.Collections.Generic;
@@ -41,7 +42,7 @@ namespace WinForms
private void btnSave_Click(object sender, EventArgs e)
{
UserManager userManager = new UserManager();
UserManager userManager = new UserManager(new UserRepository());
if (string.IsNullOrEmpty(tbUsername.Text) || string.IsNullOrEmpty(tbPassword.Text) || cbUserRole.SelectedIndex == -1)
{
MessageBox.Show("Please enter data in all fields");

View File

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