|
|
@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
using Entity;
|
|
|
|
|
|
|
|
using Shared;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Contextlib
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
internal class DbSourceManager : ISourceService<Source>
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
private WTFContext _context;
|
|
|
|
|
|
|
|
private GenericRepository<Source> _repo;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public DbSourceManager(WTFContext context)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_context = context ?? throw new ArgumentNullException(nameof(context), "Database context cannot be null.");
|
|
|
|
|
|
|
|
_repo = new GenericRepository<Source>(_context);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task AddSource(Source source)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (source == null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
throw new ArgumentNullException(nameof(source), "character cannot be null.");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_repo.Insert(source);
|
|
|
|
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<PaginationResult<Source>> GetAll()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
List<Source> srcLst = _repo.GetItems(0, _repo.Count(), []).ToList();
|
|
|
|
|
|
|
|
return new PaginationResult<Source>(srcLst.Count, 0, srcLst.Count, srcLst);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<int> GetLastSourceId()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<PaginationResult<Source>> GetSourceByDate(int date)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var srcLst = _repo.GetItems(item => item.Year == date, 0, _repo.Count(), []).ToList();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return new PaginationResult<Source>(srcLst.Count, 0, srcLst.Count, srcLst);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<Source> GetSourceById(int id)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Source? source = _repo.GetById(id, item => item.Id == id, []);
|
|
|
|
|
|
|
|
if (source == null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
throw new KeyNotFoundException($"Error : No source found with the ID: {id}.");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return source;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<Source> GetSourceByTitle(string title)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var source = _repo.GetItems(item => item.Title == title, 0, 1, []).FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (source == null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
throw new KeyNotFoundException($"Error : No source found with the title: {title}.");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return source;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<Source> GetSourceByType(int type)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var source = _repo.GetItems(item => item.TypeSrc == (TypeSrcEnum)type, 0, 1, []).FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (source == null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
throw new KeyNotFoundException($"Error : No source found with the type: {(TypeSrcEnum)type}.");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return source;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task RemoveSource(int id)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_repo.Delete(id);
|
|
|
|
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task UpdateSource(int id, Source source)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Source? src = _repo.GetById(id);
|
|
|
|
|
|
|
|
if (src != null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
bool change = false;
|
|
|
|
|
|
|
|
if (src.Title != source.Title)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
src.Title = source.Title;
|
|
|
|
|
|
|
|
change = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (src.Year != source.Year)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
src.Year = source.Year;
|
|
|
|
|
|
|
|
change = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (src.TypeSrc != source.TypeSrc)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
src.TypeSrc = source.TypeSrc;
|
|
|
|
|
|
|
|
change = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_repo.Update(src);
|
|
|
|
|
|
|
|
if (change) _context.SaveChanges();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|