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

@@ -0,0 +1,40 @@
using Models;
using Data;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Logic
{
public class AnnouncementManager
{
private AnnouncementRepository announcementRepository;
public AnnouncementManager()
{
announcementRepository = new AnnouncementRepository();
}
public List<Announcement> GetAllAnnouncements()
{
return announcementRepository.GetAllAnnouncements();
}
public List<Announcement> GetAnnouncementsByPage(int? p, int? c)
{
return announcementRepository.GetAnnouncementsByPage(p, c);
}
public bool CreateAnnouncement(string title, string description, User author, DateTime publishDate, bool isImportant, bool isSticky)
{
return announcementRepository.CreateAnnouncement(title, description, author, publishDate, isImportant, isSticky);
}
public bool UpdateAnnouncement(int id, string title, string description, bool isImportant, bool isSticky)
{
return announcementRepository.UpdateAnnouncement(id, title, description, isImportant, isSticky);
}
public bool DeleteAnnouncement(int id)
{
return announcementRepository.DeleteAnnouncement(id);
}
}
}

View File

@@ -0,0 +1,40 @@
using Models;
using Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Logic
{
public class CommentManager
{
private CommentRepository commentRepository;
public CommentManager()
{
commentRepository = new CommentRepository();
}
public void CreateCommentToAnnouncement(User author, string description, string title, DateTime publishDate, int announcementId)
{
commentRepository.CreateCommentOnAnnouncement(author, description, title, publishDate, announcementId);
}
public void CreateResponseToComment(User author, string description, string title, DateTime publishDate, int commentId)
{
commentRepository.CreateResponseOnComment(author, description, title, publishDate, commentId);
}
public void UpdateComment(int id, string description)
{
commentRepository.UpdateComment(id, description);
}
public void DeleteCommentOnAnnouncement(int commentId, int announcementId)
{
commentRepository.DeleteCommentOnAnnouncement(commentId, announcementId);
}
public void DeleteResponseOnComment(int responseId, int commentId)
{
commentRepository.DeleteResponseOnComment(responseId, commentId);
}
}
}

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

@@ -0,0 +1,65 @@
using BCrypt.Net;
using Models;
using Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net.Http;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace Logic
{
public class UserManager
{
private UserRepository userRepository;
public UserManager()
{
userRepository = new UserRepository();
}
public List<User> GetAllUsers()
{
return userRepository.GetAllUsers();
}
public User GetUserById(int id)
{
return userRepository.GetUserById(id);
}
public List<User> GetUsersByPage(int? p, int? c)
{
return userRepository.GetUsersByPage(p, c);
}
public User? AuthenticatedUser(string name, string password)
{
List<User> users = userRepository.GetAllUsers();
User user = users.Find(x => x.Name == name);
if (user == null)
{
return null;
}
else
{
if (BCrypt.Net.BCrypt.Verify(password, user.Password))
{
return user;
}
else return null;
}
}
public bool CreateUser(string name, string password, UserRole role)
{
return userRepository.CreateUser(name, password, role);
}
public void UpdateUser(int id, string name, string password, UserRole role)
{
userRepository.UpdateUser(id, name, password, role);
}
public void DisableUser(int id)
{
userRepository.DisableUser(id);
}
}
}