Debut correction route Quote

pull/6/head
kekentin 3 weeks ago
parent f53718694e
commit e3c668428e

@ -16,11 +16,18 @@ namespace Contextlib
{ {
private WTFContext _context; private WTFContext _context;
private GenericRepository<Quote> _repo; private GenericRepository<Quote> _repo;
private DbCharacterManager _dbC;
private DbSourceManager _dbS;
private DbImagesManager _dbI;
public DbQuoteManager(WTFContext context) public DbQuoteManager(WTFContext context)
{ {
_context = context ?? throw new ArgumentNullException(nameof(context), "Database context cannot be null."); _context = context ?? throw new ArgumentNullException(nameof(context), "Database context cannot be null.");
_repo = new GenericRepository<Quote>(_context); _repo = new GenericRepository<Quote>(_context);
_dbS = new DbSourceManager(_context);
_dbC = new DbCharacterManager(_context);
_dbI = new DbImagesManager(_context);
} }
@ -30,6 +37,16 @@ namespace Contextlib
{ {
throw new ArgumentNullException(nameof(quote), "quote cannot be null."); throw new ArgumentNullException(nameof(quote), "quote cannot be null.");
} }
var c = await _dbC.GetCharByName(quote.Character.Name);
if (c != null)
{
quote.IdCharacter = c.Id;
quote.Character = c;
}
//Image
//Source
_repo.Insert(quote); _repo.Insert(quote);
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
} }

@ -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();
}
}
}
}

@ -18,11 +18,10 @@ namespace Shared
// Retrieves a source by its date. // Retrieves a source by its date.
// 'date' is the date associated with the source to be retrieved. // 'date' is the date associated with the source to be retrieved.
Task<TSource> GetSourceByDate(string date); Task<PaginationResult<TSource>> GetSourceByDate(int date);
// Retrieves a source by its type. // Retrieves a source by its type.
// 'type' is the type of the source to be retrieved. // 'type' is the type of the source to be retrieved.
Task<TSource> GetSourceByType(string type); Task<TSource> GetSourceByType(int type);
// Retrieves all sources, with pagination support. // Retrieves all sources, with pagination support.
// This returns a list of all sources in the system. // This returns a list of all sources in the system.

Loading…
Cancel
Save