Refactoring: Added interfaces, custom exceptions, UserManager unit tests, dependency injection/inversion; Regex match search by date, keywords
This commit is contained in:
@@ -1,14 +1,17 @@
|
||||
using Models;
|
||||
using Logic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Logic.Exceptions;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Data
|
||||
{
|
||||
public class AnnouncementRepository
|
||||
public class AnnouncementRepository : IAnnouncementRepository
|
||||
{
|
||||
public AnnouncementRepository() { }
|
||||
public List<Announcement> GetAllAnnouncements()
|
||||
@@ -58,21 +61,21 @@ namespace Data
|
||||
return announcement;
|
||||
}
|
||||
}
|
||||
public List<Announcement> GetAnnouncementsByPage(int? p, int? c)
|
||||
public List<Announcement> GetAnnouncementsByPage(int p, int c)
|
||||
{
|
||||
List<Announcement> announcements = new List<Announcement>();
|
||||
UserRepository userRepository = new UserRepository();
|
||||
if (c == null || c < 0)
|
||||
if (c == null)
|
||||
{
|
||||
c = 10;
|
||||
throw new DatabaseOperationException("Get announcements: Invalid item count");
|
||||
}
|
||||
if (p == null || p < 0)
|
||||
if (p == null)
|
||||
{
|
||||
p = 0;
|
||||
throw new DatabaseOperationException("Get announcements: Invalid page number");
|
||||
}
|
||||
using (SqlConnection conn = SqlConnectionHelper.CreateConnection())
|
||||
{
|
||||
string sql = "SELECT * FROM Announcements ORDER BY ID OFFSET @start ROWS FETCH NEXT @count ROWS ONLY;";
|
||||
string sql = "SELECT * FROM Announcements ORDER BY ID DESC OFFSET @start ROWS FETCH NEXT @count ROWS ONLY;";
|
||||
SqlCommand sqlCommand = new SqlCommand(sql, conn);
|
||||
sqlCommand.Parameters.AddWithValue("@start", p * c);
|
||||
sqlCommand.Parameters.AddWithValue("@count", c);
|
||||
@@ -89,7 +92,7 @@ namespace Data
|
||||
}
|
||||
return announcements;
|
||||
}
|
||||
public bool CreateAnnouncement(string title, string description, User author, DateTime publishDate, bool isImportant, bool isSticky)
|
||||
public void CreateAnnouncement(string title, string description, User author, DateTime publishDate, bool isImportant, bool isSticky)
|
||||
{
|
||||
using (SqlConnection conn = SqlConnectionHelper.CreateConnection())
|
||||
{
|
||||
@@ -102,15 +105,13 @@ namespace Data
|
||||
cmd.Parameters.AddWithValue("@important", isImportant);
|
||||
cmd.Parameters.AddWithValue("@sticky", isSticky);
|
||||
int writer = cmd.ExecuteNonQuery();
|
||||
|
||||
if (writer == 1)
|
||||
if (writer != 1)
|
||||
{
|
||||
return true;
|
||||
throw new DatabaseOperationException("Database error: Announcement not created");
|
||||
}
|
||||
else return false;
|
||||
}
|
||||
}
|
||||
public bool UpdateAnnouncement(int id, string title, string description, bool isImportant, bool isSticky)
|
||||
public void UpdateAnnouncement(int id, string title, string description, bool isImportant, bool isSticky)
|
||||
{
|
||||
using (SqlConnection conn = SqlConnectionHelper.CreateConnection())
|
||||
{
|
||||
@@ -124,15 +125,13 @@ namespace Data
|
||||
cmd.Parameters.AddWithValue("@sticky", isSticky);
|
||||
cmd.Parameters.AddWithValue("@id", id);
|
||||
int writer = cmd.ExecuteNonQuery();
|
||||
|
||||
if (writer == 1)
|
||||
if (writer != 1)
|
||||
{
|
||||
return true;
|
||||
throw new DatabaseOperationException("Database error: Announcement not updated");
|
||||
}
|
||||
else return false;
|
||||
}
|
||||
}
|
||||
public bool DeleteAnnouncement(int id)
|
||||
public void DeleteAnnouncement(int id)
|
||||
{
|
||||
using (SqlConnection conn = SqlConnectionHelper.CreateConnection())
|
||||
{
|
||||
@@ -140,13 +139,54 @@ namespace Data
|
||||
SqlCommand cmd = new SqlCommand(sql, conn);
|
||||
cmd.Parameters.AddWithValue("@id", id);
|
||||
int writer = cmd.ExecuteNonQuery();
|
||||
|
||||
if (writer == 1)
|
||||
if (writer != 1)
|
||||
{
|
||||
return true;
|
||||
throw new DatabaseOperationException("Database error: Announcement not deleted");
|
||||
}
|
||||
else return false;
|
||||
}
|
||||
}
|
||||
|
||||
public List<Announcement> SearchAnnouncement(string query)
|
||||
{
|
||||
if (string.IsNullOrEmpty(query))
|
||||
{
|
||||
throw new DatabaseOperationException("Search announements error: Search query is empty");
|
||||
}
|
||||
List<Announcement> announcements = new List<Announcement>();
|
||||
UserRepository userRepository = new UserRepository();
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.Append("SELECT * FROM Announcements ");
|
||||
string[] searchStrings = query.Trim().Split(' ');
|
||||
for (int i = 0; i < searchStrings.Length; i++)
|
||||
{
|
||||
if (i == 0)
|
||||
{
|
||||
sql.Append($"WHERE Title LIKE @query{i} OR Description LIKE @query{i} ");
|
||||
}
|
||||
else
|
||||
{
|
||||
sql.Append($"OR Title LIKE @query{i} OR Description LIKE @query{i} ");
|
||||
}
|
||||
}
|
||||
sql.Append(';');
|
||||
using (SqlConnection conn = SqlConnectionHelper.CreateConnection())
|
||||
{
|
||||
SqlCommand sqlCommand = new SqlCommand(sql.ToString(), conn);
|
||||
for (int i = 0; i < searchStrings.Length; i++)
|
||||
{
|
||||
sqlCommand.Parameters.AddWithValue($"@query{i}", $"%{searchStrings[i]}%");
|
||||
}
|
||||
var reader = sqlCommand.ExecuteReader();
|
||||
while (reader.Read())
|
||||
{
|
||||
announcements.Add(new Announcement(Convert.ToInt32(reader["ID"]),
|
||||
userRepository.GetUserById(Convert.ToInt32(reader["Author"])),
|
||||
reader["Description"].ToString(), reader["Title"].ToString(),
|
||||
(DateTime)reader["PublishDate"], (bool)reader["IsImportant"],
|
||||
(bool)reader["IsSticky"]));
|
||||
}
|
||||
}
|
||||
return announcements;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,10 +1,12 @@
|
||||
using System.ComponentModel.Design;
|
||||
using System.Data.SqlClient;
|
||||
using Models;
|
||||
using Logic;
|
||||
using Logic.Exceptions;
|
||||
|
||||
namespace Data;
|
||||
|
||||
public class CommentRepository
|
||||
public class CommentRepository : ICommentRepository
|
||||
{
|
||||
public CommentRepository()
|
||||
{
|
||||
@@ -100,10 +102,14 @@ public class CommentRepository
|
||||
cmd.Parameters.AddWithValue("@desc", description);
|
||||
cmd.Parameters.AddWithValue("@id", id);
|
||||
int writer = cmd.ExecuteNonQuery();
|
||||
if (writer != 1)
|
||||
{
|
||||
throw new DatabaseOperationException("Database error: Comment not updated");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Comment CreateComment(User author, string description, string title, DateTime publishDate)
|
||||
public Comment CreateComment(User author, string description, string title, DateTime publishDate)
|
||||
{
|
||||
using (SqlConnection connection = SqlConnectionHelper.CreateConnection())
|
||||
{
|
||||
@@ -128,6 +134,10 @@ public class CommentRepository
|
||||
cmd.Parameters.AddWithValue("@announcementID", announcementId);
|
||||
cmd.Parameters.AddWithValue("@commentID", comment.ID);
|
||||
int writer = cmd.ExecuteNonQuery();
|
||||
if (writer != 1)
|
||||
{
|
||||
throw new DatabaseOperationException("Database error: Announcement comment not created");
|
||||
}
|
||||
}
|
||||
}
|
||||
public void CreateResponseOnComment(User author, string description, string title, DateTime publishDate, int commentId)
|
||||
@@ -140,10 +150,14 @@ public class CommentRepository
|
||||
cmd.Parameters.AddWithValue("@commentID", commentId);
|
||||
cmd.Parameters.AddWithValue("@responseID", response.ID);
|
||||
int writer = cmd.ExecuteNonQuery();
|
||||
if (writer != 1)
|
||||
{
|
||||
throw new DatabaseOperationException("Database error: Comment response not created");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void DeleteComment(int id)
|
||||
public void DeleteComment(int id)
|
||||
{
|
||||
using (SqlConnection connection = SqlConnectionHelper.CreateConnection())
|
||||
{
|
||||
@@ -152,6 +166,10 @@ public class CommentRepository
|
||||
SqlCommand cmd = new SqlCommand(sql, connection);
|
||||
cmd.Parameters.AddWithValue("@id", id);
|
||||
int writer = cmd.ExecuteNonQuery();
|
||||
if (writer != 1)
|
||||
{
|
||||
throw new DatabaseOperationException("Database error: Comment not deleted");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -165,6 +183,10 @@ public class CommentRepository
|
||||
cmd.Parameters.AddWithValue("@commentId", commentId);
|
||||
cmd.Parameters.AddWithValue("@announcementId", announcementId);
|
||||
int writer = cmd.ExecuteNonQuery();
|
||||
if (writer != 1)
|
||||
{
|
||||
throw new DatabaseOperationException("Database error: Announcement comment not deleted");
|
||||
}
|
||||
}
|
||||
DeleteComment(commentId);
|
||||
}
|
||||
@@ -178,6 +200,10 @@ public class CommentRepository
|
||||
cmd.Parameters.AddWithValue("@commentId", commentId);
|
||||
cmd.Parameters.AddWithValue("@responseId", responseId);
|
||||
int writer = cmd.ExecuteNonQuery();
|
||||
if (writer != 1)
|
||||
{
|
||||
throw new DatabaseOperationException("Database error: Announcement not created");
|
||||
}
|
||||
}
|
||||
DeleteComment(commentId);
|
||||
}
|
||||
|
@@ -11,6 +11,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Logic\Logic.csproj" />
|
||||
<ProjectReference Include="..\Models\Models.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
@@ -1,20 +0,0 @@
|
||||
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)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using Logic.Exceptions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
@@ -20,7 +21,6 @@ namespace Data
|
||||
catch (SqlException e)
|
||||
{
|
||||
throw new DatabaseNetworkException("Unable to access FHICT VDI database", e);
|
||||
// Console.WriteLine("Database connection error. Are you connected to the VDI VPN?");
|
||||
}
|
||||
|
||||
return connection;
|
||||
|
@@ -7,10 +7,11 @@ using System.Data.SqlClient;
|
||||
using Models;
|
||||
using System.Data;
|
||||
using System.Xml.Linq;
|
||||
using Logic;
|
||||
|
||||
namespace Data
|
||||
{
|
||||
public class UserRepository
|
||||
public class UserRepository : IUserRepository
|
||||
{
|
||||
public UserRepository() { }
|
||||
public List<User> GetAllUsers()
|
||||
@@ -54,17 +55,9 @@ namespace Data
|
||||
(UserRole)reader["Role"]);
|
||||
}
|
||||
}
|
||||
public List<User> GetUsersByPage(int? p, int? c)
|
||||
public List<User> GetUsersByPage(int p, int c)
|
||||
{
|
||||
List<User> users = new List<User>();
|
||||
if (c == null || c < 0)
|
||||
{
|
||||
c = 10;
|
||||
}
|
||||
if (p == null || p < 0)
|
||||
{
|
||||
p = 0;
|
||||
}
|
||||
using (SqlConnection conn = SqlConnectionHelper.CreateConnection())
|
||||
{
|
||||
string sql = "SELECT * FROM Users ORDER BY ID OFFSET @start ROWS FETCH NEXT @count ROWS ONLY;";
|
||||
@@ -81,25 +74,20 @@ namespace Data
|
||||
}
|
||||
return users;
|
||||
}
|
||||
public bool CreateUser(string name, string password, UserRole role)
|
||||
public User CreateUser(string name, string password, UserRole role)
|
||||
{
|
||||
using (SqlConnection conn = SqlConnectionHelper.CreateConnection())
|
||||
{
|
||||
string sql = "INSERT INTO Users (Name, Password, Role) VALUES (@name, @pass, @role);";
|
||||
string sql = "INSERT INTO Users (Name, Password, Role) VALUES (@name, @pass, @role) " +
|
||||
"SELECT SCOPE_IDENTITY();";
|
||||
SqlCommand cmd = new SqlCommand(sql, conn);
|
||||
cmd.Parameters.AddWithValue("@name", name);
|
||||
cmd.Parameters.AddWithValue("@pass", password);
|
||||
cmd.Parameters.AddWithValue("@role", (int)role);
|
||||
int writer = cmd.ExecuteNonQuery();
|
||||
|
||||
if (writer == 1)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else return false;
|
||||
return GetUserById(Convert.ToInt32(cmd.ExecuteScalar()));
|
||||
}
|
||||
}
|
||||
public bool UpdateUser(int id, string name, string password, UserRole role)
|
||||
public void UpdateUser(int id, string name, string password, UserRole role)
|
||||
{
|
||||
using (SqlConnection conn = SqlConnectionHelper.CreateConnection())
|
||||
{
|
||||
@@ -111,16 +99,10 @@ namespace Data
|
||||
cmd.Parameters.AddWithValue("@pass", password);
|
||||
cmd.Parameters.AddWithValue("@role", (int)role);
|
||||
cmd.Parameters.AddWithValue("@id", id);
|
||||
int writer = cmd.ExecuteNonQuery();
|
||||
|
||||
if (writer == 1)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else return false;
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
public bool DisableUser(int id)
|
||||
public void DisableUser(int id)
|
||||
{
|
||||
using (SqlConnection conn = SqlConnectionHelper.CreateConnection())
|
||||
{
|
||||
@@ -129,13 +111,21 @@ namespace Data
|
||||
"WHERE ID = @id;";
|
||||
SqlCommand cmd = new SqlCommand(sql, conn);
|
||||
cmd.Parameters.AddWithValue("@id", id.ToString());
|
||||
int writer = cmd.ExecuteNonQuery();
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
|
||||
if (writer == 1)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else return false;
|
||||
public User GetUserByName(string userName)
|
||||
{
|
||||
using (SqlConnection conn = SqlConnectionHelper.CreateConnection())
|
||||
{
|
||||
string sql = "SELECT * FROM Users WHERE Name = @userName;";
|
||||
SqlCommand cmd = new SqlCommand(sql, conn);
|
||||
cmd.Parameters.AddWithValue("@userName", userName);
|
||||
var reader = cmd.ExecuteReader();
|
||||
|
||||
return new User(Convert.ToInt32(reader["ID"]), reader["Name"].ToString(),
|
||||
reader["Password"].ToString(), (UserRole)reader["Role"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user