diff --git a/WF_EF_Api/Contextlib/DbQuoteManager.cs b/WF_EF_Api/Contextlib/DbQuoteManager.cs index 9be5119..4287574 100644 --- a/WF_EF_Api/Contextlib/DbQuoteManager.cs +++ b/WF_EF_Api/Contextlib/DbQuoteManager.cs @@ -16,11 +16,18 @@ namespace Contextlib { private WTFContext _context; private GenericRepository _repo; + private DbCharacterManager _dbC; + private DbSourceManager _dbS; + private DbImagesManager _dbI; public DbQuoteManager(WTFContext context) { _context = context ?? throw new ArgumentNullException(nameof(context), "Database context cannot be null."); _repo = new GenericRepository(_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."); } + var c = await _dbC.GetCharByName(quote.Character.Name); + if (c != null) + { + quote.IdCharacter = c.Id; + quote.Character = c; + } + + //Image + //Source + _repo.Insert(quote); await _context.SaveChangesAsync(); } diff --git a/WF_EF_Api/Contextlib/DbSourceManager.cs b/WF_EF_Api/Contextlib/DbSourceManager.cs new file mode 100644 index 0000000..707a0bf --- /dev/null +++ b/WF_EF_Api/Contextlib/DbSourceManager.cs @@ -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 + { + private WTFContext _context; + private GenericRepository _repo; + + public DbSourceManager(WTFContext context) + { + _context = context ?? throw new ArgumentNullException(nameof(context), "Database context cannot be null."); + _repo = new GenericRepository(_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> GetAll() + { + List srcLst = _repo.GetItems(0, _repo.Count(), []).ToList(); + return new PaginationResult(srcLst.Count, 0, srcLst.Count, srcLst); + } + + public async Task GetLastSourceId() + { + throw new NotImplementedException(); + } + + public async Task> GetSourceByDate(int date) + { + + var srcLst = _repo.GetItems(item => item.Year == date, 0, _repo.Count(), []).ToList(); + + return new PaginationResult(srcLst.Count, 0, srcLst.Count, srcLst); + } + + public async Task 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 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 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(); + } + } + } +} diff --git a/WF_EF_Api/Shared/ISourceService.cs b/WF_EF_Api/Shared/ISourceService.cs index 228078e..9657db7 100644 --- a/WF_EF_Api/Shared/ISourceService.cs +++ b/WF_EF_Api/Shared/ISourceService.cs @@ -18,11 +18,10 @@ namespace Shared // Retrieves a source by its date. // 'date' is the date associated with the source to be retrieved. - Task GetSourceByDate(string date); - + Task> GetSourceByDate(int date); // Retrieves a source by its type. // 'type' is the type of the source to be retrieved. - Task GetSourceByType(string type); + Task GetSourceByType(int type); // Retrieves all sources, with pagination support. // This returns a list of all sources in the system.