Refactoring: Added interfaces, custom exceptions, UserManager unit tests, dependency injection/inversion; Regex match search by date, keywords
This commit is contained in:
@@ -1,20 +1,20 @@
|
||||
using Models;
|
||||
using Data;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Logic
|
||||
{
|
||||
public class AnnouncementManager
|
||||
{
|
||||
private AnnouncementRepository announcementRepository;
|
||||
public AnnouncementManager()
|
||||
private IAnnouncementRepository announcementRepository;
|
||||
public AnnouncementManager(IAnnouncementRepository announcementRepository)
|
||||
{
|
||||
announcementRepository = new AnnouncementRepository();
|
||||
this.announcementRepository = announcementRepository;
|
||||
}
|
||||
public List<Announcement> GetAllAnnouncements()
|
||||
{
|
||||
@@ -24,22 +24,44 @@ namespace Logic
|
||||
{
|
||||
return announcementRepository.GetAnnouncementById(id);
|
||||
}
|
||||
public List<Announcement> GetAnnouncementsByPage(int? p, int? c)
|
||||
public List<Announcement> GetAnnouncementsByPage(int p = 0, int c = 10)
|
||||
{
|
||||
return announcementRepository.GetAnnouncementsByPage(p, c);
|
||||
}
|
||||
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)
|
||||
{
|
||||
return announcementRepository.CreateAnnouncement(title, description, author, publishDate, isImportant, isSticky);
|
||||
announcementRepository.CreateAnnouncement(title, description, author, publishDate, isImportant, isSticky);
|
||||
}
|
||||
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)
|
||||
{
|
||||
description += $"{Environment.NewLine}{Environment.NewLine}Updated: {DateTime.Now.ToString("g")}";
|
||||
return announcementRepository.UpdateAnnouncement(id, title, description, isImportant, isSticky);
|
||||
announcementRepository.UpdateAnnouncement(id, title, description, isImportant, isSticky);
|
||||
}
|
||||
public bool DeleteAnnouncement(int id)
|
||||
public void DeleteAnnouncement(int id)
|
||||
{
|
||||
return announcementRepository.DeleteAnnouncement(id);
|
||||
announcementRepository.DeleteAnnouncement(id);
|
||||
}
|
||||
public List<Announcement> SearchAnnouncements(string query)
|
||||
{
|
||||
if (string.IsNullOrEmpty(query))
|
||||
{
|
||||
return new List<Announcement>();
|
||||
}
|
||||
else
|
||||
{
|
||||
var match = Regex.Match(query, "(?<=date:)[0-9]{4}-[0-9]{2}-[0-9]{2}");
|
||||
DateTime date;
|
||||
if (DateTime.TryParse(match.Groups[0].Value, out date))
|
||||
{
|
||||
query = Regex.Replace(query, "date:[0-9]{4}-[0-9]{2}-[0-9]{2}", "");
|
||||
if (string.IsNullOrEmpty(query))
|
||||
{
|
||||
return announcementRepository.GetAllAnnouncements().Where(x => x.PublishDate.Date == date.Date).ToList();
|
||||
}
|
||||
else return announcementRepository.SearchAnnouncement(query).Where(x => x.PublishDate.Date == date.Date).ToList();
|
||||
}
|
||||
else return announcementRepository.SearchAnnouncement(query);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user