Refactoring: Added interfaces, custom exceptions, UserManager unit tests, dependency injection/inversion; Regex match search by date, keywords
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
using Logic;
|
||||
using Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace Tests.Mocks
|
||||
{
|
||||
public class AnnouncementRepositoryFake : IAnnouncementRepository
|
||||
{
|
||||
private List<Announcement> announcements;
|
||||
private int currentId;
|
||||
|
||||
public AnnouncementRepositoryFake()
|
||||
{
|
||||
announcements = new List<Announcement>();
|
||||
currentId = 1;
|
||||
}
|
||||
|
||||
public void CreateAnnouncement(string title, string description, User author, DateTime publishDate, bool isImportant, bool isSticky)
|
||||
{
|
||||
announcements.Add(new Announcement(currentId, author, description, title, publishDate, isImportant, isSticky));
|
||||
currentId++;
|
||||
}
|
||||
|
||||
public void DeleteAnnouncement(int id)
|
||||
{
|
||||
announcements.RemoveAt(id--);
|
||||
}
|
||||
|
||||
public List<Announcement> GetAllAnnouncements()
|
||||
{
|
||||
return announcements;
|
||||
}
|
||||
|
||||
public Announcement GetAnnouncementById(int id)
|
||||
{
|
||||
return announcements.FirstOrDefault(x => x.ID == id);
|
||||
}
|
||||
|
||||
public List<Announcement> GetAnnouncementsByPage(int p, int c)
|
||||
{
|
||||
return announcements.GetRange(p + c, c);
|
||||
}
|
||||
|
||||
public void UpdateAnnouncement(int id, string title, string description, bool isImportant, bool isSticky)
|
||||
{
|
||||
Announcement announcement = announcements.First(x => x.ID == id);
|
||||
announcement.Title = title;
|
||||
announcement.Description = description;
|
||||
announcement.IsImportant = isImportant;
|
||||
announcement.IsSticky = isSticky;
|
||||
}
|
||||
}
|
||||
}
|
64
StudentHouseDashboard/Tests/Mocks/UserRepositoryFake.cs
Normal file
64
StudentHouseDashboard/Tests/Mocks/UserRepositoryFake.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using Models;
|
||||
using Logic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Tests.Mocks
|
||||
{
|
||||
public class UserRepositoryFake : IUserRepository
|
||||
{
|
||||
private List<User> users;
|
||||
private int currentId;
|
||||
public UserRepositoryFake()
|
||||
{
|
||||
users = new List<User>();
|
||||
currentId = 1;
|
||||
}
|
||||
|
||||
public User CreateUser(string name, string password, UserRole role)
|
||||
{
|
||||
User user = new User(currentId, name, password, role);
|
||||
users.Add(user);
|
||||
currentId++;
|
||||
return user;
|
||||
}
|
||||
|
||||
public void DisableUser(int id)
|
||||
{
|
||||
User user = users.First(x => x.ID == id);
|
||||
user.Name = $"Deleted User {user.ID}";
|
||||
user.Password = "0";
|
||||
}
|
||||
|
||||
public List<User> GetAllUsers()
|
||||
{
|
||||
return users;
|
||||
}
|
||||
|
||||
public User GetUserById(int id)
|
||||
{
|
||||
return users.First(x => x.ID == id);
|
||||
}
|
||||
|
||||
public User GetUserByName(string userName)
|
||||
{
|
||||
return users.FirstOrDefault(x => x.Name == userName);
|
||||
}
|
||||
|
||||
public List<User> GetUsersByPage(int p, int c)
|
||||
{
|
||||
return users.GetRange(p + c, c);
|
||||
}
|
||||
|
||||
public void UpdateUser(int id, string name, string password, UserRole role)
|
||||
{
|
||||
User user = users.First(x => x.ID == id);
|
||||
user.Name = name;
|
||||
user.Password = password;
|
||||
user.Role = role;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user