Fixing an error on invalid slug with the crud api

This commit is contained in:
Zoe Roux 2021-07-21 18:50:20 +02:00
parent 50307352a9
commit 42863bc403
2 changed files with 13 additions and 1 deletions

View File

@ -128,6 +128,7 @@ namespace Kyoo.Controllers
/// <param name="id">The id of the resource</param> /// <param name="id">The id of the resource</param>
/// <exception cref="ItemNotFoundException">If the item could not be found.</exception> /// <exception cref="ItemNotFoundException">If the item could not be found.</exception>
/// <returns>The resource found</returns> /// <returns>The resource found</returns>
[ItemNotNull]
Task<T> Get(int id); Task<T> Get(int id);
/// <summary> /// <summary>
/// Get a resource from it's slug. /// Get a resource from it's slug.
@ -135,6 +136,7 @@ namespace Kyoo.Controllers
/// <param name="slug">The slug of the resource</param> /// <param name="slug">The slug of the resource</param>
/// <exception cref="ItemNotFoundException">If the item could not be found.</exception> /// <exception cref="ItemNotFoundException">If the item could not be found.</exception>
/// <returns>The resource found</returns> /// <returns>The resource found</returns>
[ItemNotNull]
Task<T> Get(string slug); Task<T> Get(string slug);
/// <summary> /// <summary>
/// Get the first resource that match the predicate. /// Get the first resource that match the predicate.
@ -142,6 +144,7 @@ namespace Kyoo.Controllers
/// <param name="where">A predicate to filter the resource.</param> /// <param name="where">A predicate to filter the resource.</param>
/// <exception cref="ItemNotFoundException">If the item could not be found.</exception> /// <exception cref="ItemNotFoundException">If the item could not be found.</exception>
/// <returns>The resource found</returns> /// <returns>The resource found</returns>
[ItemNotNull]
Task<T> Get(Expression<Func<T, bool>> where); Task<T> Get(Expression<Func<T, bool>> where);
/// <summary> /// <summary>
@ -149,18 +152,21 @@ namespace Kyoo.Controllers
/// </summary> /// </summary>
/// <param name="id">The id of the resource</param> /// <param name="id">The id of the resource</param>
/// <returns>The resource found</returns> /// <returns>The resource found</returns>
[ItemCanBeNull]
Task<T> GetOrDefault(int id); Task<T> GetOrDefault(int id);
/// <summary> /// <summary>
/// Get a resource from it's slug or null if it is not found. /// Get a resource from it's slug or null if it is not found.
/// </summary> /// </summary>
/// <param name="slug">The slug of the resource</param> /// <param name="slug">The slug of the resource</param>
/// <returns>The resource found</returns> /// <returns>The resource found</returns>
[ItemCanBeNull]
Task<T> GetOrDefault(string slug); Task<T> GetOrDefault(string slug);
/// <summary> /// <summary>
/// Get the first resource that match the predicate or null if it is not found. /// Get the first resource that match the predicate or null if it is not found.
/// </summary> /// </summary>
/// <param name="where">A predicate to filter the resource.</param> /// <param name="where">A predicate to filter the resource.</param>
/// <returns>The resource found</returns> /// <returns>The resource found</returns>
[ItemCanBeNull]
Task<T> GetOrDefault(Expression<Func<T, bool>> where); Task<T> GetOrDefault(Expression<Func<T, bool>> where);
/// <summary> /// <summary>
@ -168,6 +174,7 @@ namespace Kyoo.Controllers
/// </summary> /// </summary>
/// <param name="query">The query string.</param> /// <param name="query">The query string.</param>
/// <returns>A list of resources found</returns> /// <returns>A list of resources found</returns>
[ItemNotNull]
Task<ICollection<T>> Search(string query); Task<ICollection<T>> Search(string query);
/// <summary> /// <summary>
@ -177,6 +184,7 @@ namespace Kyoo.Controllers
/// <param name="sort">Sort information about the query (sort by, sort order)</param> /// <param name="sort">Sort information about the query (sort by, sort order)</param>
/// <param name="limit">How pagination should be done (where to start and how many to return)</param> /// <param name="limit">How pagination should be done (where to start and how many to return)</param>
/// <returns>A list of resources that match every filters</returns> /// <returns>A list of resources that match every filters</returns>
[ItemNotNull]
Task<ICollection<T>> GetAll(Expression<Func<T, bool>> where = null, Task<ICollection<T>> GetAll(Expression<Func<T, bool>> where = null,
Sort<T> sort = default, Sort<T> sort = default,
Pagination limit = default); Pagination limit = default);
@ -187,6 +195,7 @@ namespace Kyoo.Controllers
/// <param name="sort">A sort by predicate. The order is ascending.</param> /// <param name="sort">A sort by predicate. The order is ascending.</param>
/// <param name="limit">How pagination should be done (where to start and how many to return)</param> /// <param name="limit">How pagination should be done (where to start and how many to return)</param>
/// <returns>A list of resources that match every filters</returns> /// <returns>A list of resources that match every filters</returns>
[ItemNotNull]
Task<ICollection<T>> GetAll([Optional] Expression<Func<T, bool>> where, Task<ICollection<T>> GetAll([Optional] Expression<Func<T, bool>> where,
Expression<Func<T, object>> sort, Expression<Func<T, object>> sort,
Pagination limit = default Pagination limit = default
@ -205,6 +214,7 @@ namespace Kyoo.Controllers
/// </summary> /// </summary>
/// <param name="obj">The item to register</param> /// <param name="obj">The item to register</param>
/// <returns>The resource registers and completed by database's information (related items & so on)</returns> /// <returns>The resource registers and completed by database's information (related items & so on)</returns>
[ItemNotNull]
Task<T> Create([NotNull] T obj); Task<T> Create([NotNull] T obj);
/// <summary> /// <summary>
@ -212,6 +222,7 @@ namespace Kyoo.Controllers
/// </summary> /// </summary>
/// <param name="obj">The object to create</param> /// <param name="obj">The object to create</param>
/// <returns>The newly created item or the existing value if it existed.</returns> /// <returns>The newly created item or the existing value if it existed.</returns>
[ItemNotNull]
Task<T> CreateIfNotExists([NotNull] T obj); Task<T> CreateIfNotExists([NotNull] T obj);
/// <summary> /// <summary>
@ -221,6 +232,7 @@ namespace Kyoo.Controllers
/// <param name="resetOld">Should old properties of the resource be discarded or should null values considered as not changed?</param> /// <param name="resetOld">Should old properties of the resource be discarded or should null values considered as not changed?</param>
/// <exception cref="ItemNotFoundException">If the item is not found</exception> /// <exception cref="ItemNotFoundException">If the item is not found</exception>
/// <returns>The resource edited and completed by database's information (related items & so on)</returns> /// <returns>The resource edited and completed by database's information (related items & so on)</returns>
[ItemNotNull]
Task<T> Edit([NotNull] T edited, bool resetOld); Task<T> Edit([NotNull] T edited, bool resetOld);
/// <summary> /// <summary>

View File

@ -38,7 +38,7 @@ namespace Kyoo.CommonApi
[PartialPermission(Kind.Read)] [PartialPermission(Kind.Read)]
public virtual async Task<ActionResult<T>> Get(string slug) public virtual async Task<ActionResult<T>> Get(string slug)
{ {
T ret = await _repository.Get(slug); T ret = await _repository.GetOrDefault(slug);
if (ret == null) if (ret == null)
return NotFound(); return NotFound();
return ret; return ret;