From d2f98096342580d3a11a9dd6627bafe424a1f31a Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Sat, 6 Jun 2020 00:50:06 +0200 Subject: [PATCH] Implementing the library repository --- .../Repositories/LibraryRepository.cs | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 Kyoo/Controllers/Repositories/LibraryRepository.cs diff --git a/Kyoo/Controllers/Repositories/LibraryRepository.cs b/Kyoo/Controllers/Repositories/LibraryRepository.cs new file mode 100644 index 00000000..bac6e6ba --- /dev/null +++ b/Kyoo/Controllers/Repositories/LibraryRepository.cs @@ -0,0 +1,101 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Kyoo.Models; +using Kyoo.Models.Exceptions; +using Microsoft.EntityFrameworkCore; + +namespace Kyoo.Controllers +{ + public class LibraryRepository : ILibraryRepository + { + private readonly DatabaseContext _database; + private readonly IProviderRepository _providers; + + + public LibraryRepository(DatabaseContext database, IProviderRepository providers) + { + _database = database; + _providers = providers; + } + + public Task Get(long id) + { + return _database.Libraries.FirstOrDefaultAsync(x => x.ID == id); + } + + public Task Get(string slug) + { + return _database.Libraries.FirstOrDefaultAsync(x => x.Name == slug); + } + + public async Task> Search(string query) + { + return await _database.Libraries + .Where(x => EF.Functions.Like(x.Name, $"%{query}%")) + .Take(20) + .ToListAsync(); + } + + public async Task> GetAll() + { + return await _database.Libraries.ToListAsync(); + } + + public async Task Create(Library obj) + { + if (obj == null) + throw new ArgumentNullException(nameof(obj)); + + obj.Links = null; + await Validate(obj); + await _database.Libraries.AddAsync(obj); + await _database.SaveChangesAsync(); + return obj.ID; + } + + public async Task CreateIfNotExists(Library obj) + { + if (obj == null) + throw new ArgumentNullException(nameof(obj)); + + Library old = await Get(obj.Name); + if (old != null) + return old.ID; + return await Create(obj); + } + + public async Task Edit(Library edited, bool resetOld) + { + if (edited == null) + throw new ArgumentNullException(nameof(edited)); + + Library old = await Get(edited.Name); + + if (old == null) + throw new ItemNotFound($"No library found with the name {edited.Name}."); + + if (resetOld) + Utility.Nullify(old); + Utility.Merge(old, edited); + await Validate(old); + await _database.SaveChangesAsync(); + } + + private async Task Validate(Library obj) + { + obj.ProviderLinks = (await Task.WhenAll(obj.ProviderLinks.Select(async x => + { + x.ProviderID = await _providers.CreateIfNotExists(x.Provider); + return x; + }))).ToList(); + } + + public async Task Delete(Library obj) + { + _database.Libraries.Remove(obj); + await _database.SaveChangesAsync(); + } + } +} \ No newline at end of file