Creating the interface for where, sort & pages for the repository

This commit is contained in:
Zoe Roux 2020-06-28 19:37:25 +02:00
parent b33acc5c2d
commit e21f5d44a8
5 changed files with 58 additions and 4 deletions

View File

@ -1,18 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Kyoo.Models;
namespace Kyoo.Controllers
{
public struct Pagination
{
public int Count;
public int AfterID;
}
public readonly struct Sort<T>
{
public string Key { get; }
public bool Descendant { get; }
public Sort(string key, bool descendant = false)
{
Key = key;
Descendant = descendant;
}
public Sort(Expression<Func<T, object>> key)
{
Key = Utility.GetMemberName(key);
Descendant = false;
}
public static implicit operator Sort<T>([NotNull] Expression<Func<T, object>> key)
{
return new Sort<T>(Utility.GetMemberName(key));
}
}
public interface IRepository<T> : IDisposable, IAsyncDisposable
{
Task<T> Get(int id);
Task<T> Get(string slug);
Task<ICollection<T>> Search(string query);
Task<ICollection<T>> GetAll();
Task<ICollection<T>> GetAll(Expression<Func<T, bool>> where = null,
Sort<T> sort = default,
Pagination page = default);
Task<ICollection<T>> GetAll(Expression<Func<T, bool>> where = null,
Expression<Func<T, object>> sort = default,
Pagination page = default) => GetAll(where, new Sort<T>(sort), page);
Task<int> Create([NotNull] T obj);
Task<int> CreateIfNotExists([NotNull] T obj);
Task Edit([NotNull] T edited, bool resetOld);

View File

@ -101,7 +101,7 @@ namespace Kyoo.Models
if (EpisodeNumber >= episode.Season.Episodes.Count())
{
NextEpisode = episode.Show.Seasons
.FirstOrDefault(x => x.SeasonNumber == SeasonNumber - 1)?.Episodes
.FirstOrDefault(x => x.SeasonNumber == SeasonNumber + 1)?.Episodes
.FirstOrDefault(x => x.EpisodeNumber == 1);
}
else

View File

@ -3,6 +3,7 @@ using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
@ -224,5 +225,16 @@ namespace Kyoo
yield return ret;
}
}
public static string GetMemberName<T>(Expression<Func<T, object>> key)
{
if (key == null)
throw new ArgumentNullException(nameof(key));
if (!(key.Body is MemberExpression member))
throw new ArgumentException("Key should be a member of the object.");
return member.Member.Name;
}
}
}

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Kyoo.Models;
@ -363,7 +364,7 @@ namespace Kyoo.Controllers
return _shows.Delete(show);
}
public Task DeleteSeason(Season season)
public Task DeleteSeason(Season season, IShowRepository repo)
{
return _seasons.Delete(season);
}

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Kyoo.Models;
using Kyoo.Models.Exceptions;
@ -46,7 +47,9 @@ namespace Kyoo.Controllers
.ToListAsync();
}
public async Task<ICollection<Collection>> GetAll()
public async Task<ICollection<Collection>> GetAll(Expression<Func<Collection, bool>> where = null,
Sort<Collection> sort = default,
Pagination page = default)
{
return await _database.Collections.ToListAsync();
}