// Kyoo - A portable and vast media library solution.
// Copyright (c) Kyoo.
//
// See AUTHORS.md and LICENSE file in the project root for full license information.
//
// Kyoo is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// Kyoo is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Kyoo. If not, see .
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Kyoo.Abstractions.Models;
using Kyoo.Abstractions.Models.Exceptions;
using Kyoo.Abstractions.Models.Utils;
namespace Kyoo.Abstractions.Controllers
{
///
/// A common repository for every resources.
///
/// The resource's type that this repository manage.
public interface IRepository : IBaseRepository
where T : class, IResource, IQuery
{
///
/// The event handler type for all events of this repository.
///
/// The resource created/modified/deleted
/// A representing the asynchronous operation.
public delegate Task ResourceEventHandler(T resource);
///
/// Get a resource from it's ID.
///
/// The id of the resource
/// The related fields to include.
/// If the item could not be found.
/// The resource found
Task Get(int id, Include? include = default);
///
/// Get a resource from it's slug.
///
/// The slug of the resource
/// The related fields to include.
/// If the item could not be found.
/// The resource found
Task Get(string slug, Include? include = default);
///
/// Get the first resource that match the predicate.
///
/// A predicate to filter the resource.
/// The related fields to include.
/// If the item could not be found.
/// The resource found
Task Get(Expression> where, Include? include = default);
///
/// Get a resource from it's ID or null if it is not found.
///
/// The id of the resource
/// The related fields to include.
/// The resource found
Task GetOrDefault(int id, Include? include = default);
///
/// Get a resource from it's slug or null if it is not found.
///
/// The slug of the resource
/// The related fields to include.
/// The resource found
Task GetOrDefault(string slug, Include? include = default);
///
/// Get the first resource that match the predicate or null if it is not found.
///
/// A predicate to filter the resource.
/// The related fields to include.
/// A custom sort method to handle cases where multiples items match the filters.
/// The resource found
Task GetOrDefault(Expression> where,
Include? include = default,
Sort? sortBy = default);
///
/// Search for resources with the database.
///
/// The query string.
/// The related fields to include.
/// A list of resources found
Task> Search(string query, Include? include = default);
///
/// Get every resources that match all filters
///
/// A filter predicate
/// Sort information about the query (sort by, sort order)
/// How pagination should be done (where to start and how many to return)
/// The related fields to include.
/// A list of resources that match every filters
Task> GetAll(Expression>? where = null,
Sort? sort = default,
Pagination? limit = default,
Include? include = default);
///
/// Get the number of resources that match the filter's predicate.
///
/// A filter predicate
/// How many resources matched that filter
Task GetCount(Expression>? where = null);
///
/// Map a list of ids to a list of items (keep the order).
///
/// The list of items id.
/// The related fields to include.
/// A list of resources mapped from ids.
Task> FromIds(IList ids, Include? include = default);
///
/// Create a new resource.
///
/// The item to register
/// The resource registers and completed by database's information (related items and so on)
Task Create(T obj);
///
/// Create a new resource if it does not exist already. If it does, the existing value is returned instead.
///
/// The object to create
/// The newly created item or the existing value if it existed.
Task CreateIfNotExists(T obj);
///
/// Called when a resource has been created.
///
static event ResourceEventHandler OnCreated;
///
/// Callback that should be called after a resource has been created.
///
/// The resource newly created.
/// A representing the asynchronous operation.
protected static Task OnResourceCreated(T obj)
=> OnCreated?.Invoke(obj) ?? Task.CompletedTask;
///
/// Edit a resource and replace every property
///
/// The resource to edit, it's ID can't change.
/// If the item is not found
/// The resource edited and completed by database's information (related items and so on)
Task Edit(T edited);
///
/// Edit only specific properties of a resource
///
/// The id of the resource to edit
///
/// A method that will be called when you need to update every properties that you want to
/// persist. It can return false to abort the process via an ArgumentException
///
/// If the item is not found
/// The resource edited and completed by database's information (related items and so on)
Task Patch(int id, Func> patch);
///
/// Called when a resource has been edited.
///
static event ResourceEventHandler OnEdited;
///
/// Callback that should be called after a resource has been edited.
///
/// The resource newly edited.
/// A representing the asynchronous operation.
protected static Task OnResourceEdited(T obj)
=> OnEdited?.Invoke(obj) ?? Task.CompletedTask;
///
/// Delete a resource by it's ID
///
/// The ID of the resource
/// If the item is not found
/// A representing the asynchronous operation.
Task Delete(int id);
///
/// Delete a resource by it's slug
///
/// The slug of the resource
/// If the item is not found
/// A representing the asynchronous operation.
Task Delete(string slug);
///
/// Delete a resource
///
/// The resource to delete
/// If the item is not found
/// A representing the asynchronous operation.
Task Delete(T obj);
///
/// Delete all resources that match the predicate.
///
/// A predicate to filter resources to delete. Every resource that match this will be deleted.
/// A representing the asynchronous operation.
Task DeleteAll(Expression> where);
///
/// Called when a resource has been edited.
///
static event ResourceEventHandler OnDeleted;
///
/// Callback that should be called after a resource has been deleted.
///
/// The resource newly deleted.
/// A representing the asynchronous operation.
protected static Task OnResourceDeleted(T obj)
=> OnDeleted?.Invoke(obj) ?? Task.CompletedTask;
}
///
/// A base class for repositories. Every service implementing this will be handled by the .
///
public interface IBaseRepository
{
///
/// The type for witch this repository is responsible or null if non applicable.
///
Type RepositoryType { get; }
}
}