mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-07-09 03:04:20 -04:00
Implementing duplicate exceptions for every repository
This commit is contained in:
parent
19715f795c
commit
57ade5470f
@ -57,14 +57,15 @@ namespace Kyoo.Controllers
|
||||
if (obj == null)
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
|
||||
_database.Entry(obj).State = EntityState.Added;
|
||||
|
||||
try
|
||||
{
|
||||
await _database.Collections.AddAsync(obj);
|
||||
await _database.SaveChangesAsync();
|
||||
}
|
||||
catch (DbUpdateException ex)
|
||||
{
|
||||
if (ex.InnerException is PostgresException inner && inner.SqlState == PostgresErrorCodes.UniqueViolation)
|
||||
if (Helper.IsDuplicateException(ex))
|
||||
throw new DuplicatedItemException($"Trying to insert a duplicated collection (slug {obj.Slug} already exists).");
|
||||
throw;
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ using Kyoo.Models;
|
||||
using Kyoo.Models.Exceptions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Npgsql;
|
||||
|
||||
namespace Kyoo.Controllers
|
||||
{
|
||||
@ -83,7 +84,17 @@ namespace Kyoo.Controllers
|
||||
if (obj.Tracks != null)
|
||||
foreach (Track entry in obj.Tracks)
|
||||
_database.Entry(entry).State = EntityState.Added;
|
||||
|
||||
try
|
||||
{
|
||||
await _database.SaveChangesAsync();
|
||||
}
|
||||
catch (DbUpdateException ex)
|
||||
{
|
||||
if (Helper.IsDuplicateException(ex))
|
||||
throw new DuplicatedItemException($"Trying to insert a duplicated episode (slug {obj.Slug} already exists).");
|
||||
throw;
|
||||
}
|
||||
return obj.ID;
|
||||
}
|
||||
|
||||
|
@ -57,7 +57,18 @@ namespace Kyoo.Controllers
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
|
||||
await _database.Genres.AddAsync(obj);
|
||||
|
||||
try
|
||||
{
|
||||
await _database.SaveChangesAsync();
|
||||
}
|
||||
catch (DbUpdateException ex)
|
||||
{
|
||||
if (Helper.IsDuplicateException(ex))
|
||||
throw new DuplicatedItemException($"Trying to insert a duplicated genre (slug {obj.Slug} already exists).");
|
||||
throw;
|
||||
}
|
||||
|
||||
return obj.ID;
|
||||
}
|
||||
|
||||
|
14
Kyoo/Controllers/Repositories/Helper.cs
Normal file
14
Kyoo/Controllers/Repositories/Helper.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Npgsql;
|
||||
|
||||
namespace Kyoo.Controllers
|
||||
{
|
||||
public static class Helper
|
||||
{
|
||||
public static bool IsDuplicateException(DbUpdateException ex)
|
||||
{
|
||||
return ex.InnerException is PostgresException inner
|
||||
&& inner.SqlState == PostgresErrorCodes.UniqueViolation;
|
||||
}
|
||||
}
|
||||
}
|
@ -64,7 +64,18 @@ namespace Kyoo.Controllers
|
||||
if (obj.ProviderLinks != null)
|
||||
foreach (ProviderLink entry in obj.ProviderLinks)
|
||||
_database.Entry(entry).State = EntityState.Added;
|
||||
|
||||
try
|
||||
{
|
||||
await _database.SaveChangesAsync();
|
||||
}
|
||||
catch (DbUpdateException ex)
|
||||
{
|
||||
if (Helper.IsDuplicateException(ex))
|
||||
throw new DuplicatedItemException($"Trying to insert a duplicated library (slug {obj.Slug} already exists).");
|
||||
throw;
|
||||
}
|
||||
|
||||
return obj.ID;
|
||||
}
|
||||
|
||||
|
@ -63,7 +63,18 @@ namespace Kyoo.Controllers
|
||||
if (obj.ExternalIDs != null)
|
||||
foreach (MetadataID entry in obj.ExternalIDs)
|
||||
_database.Entry(entry).State = EntityState.Added;
|
||||
|
||||
try
|
||||
{
|
||||
await _database.SaveChangesAsync();
|
||||
}
|
||||
catch (DbUpdateException ex)
|
||||
{
|
||||
if (Helper.IsDuplicateException(ex))
|
||||
throw new DuplicatedItemException($"Trying to insert a duplicated people (slug {obj.Slug} already exists).");
|
||||
throw;
|
||||
}
|
||||
|
||||
return obj.ID;
|
||||
}
|
||||
|
||||
|
@ -56,8 +56,19 @@ namespace Kyoo.Controllers
|
||||
if (obj == null)
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
|
||||
await _database.Providers.AddAsync(obj);
|
||||
_database.Entry(obj).State = EntityState.Added;
|
||||
|
||||
try
|
||||
{
|
||||
await _database.SaveChangesAsync();
|
||||
}
|
||||
catch (DbUpdateException ex)
|
||||
{
|
||||
if (Helper.IsDuplicateException(ex))
|
||||
throw new DuplicatedItemException($"Trying to insert a duplicated provider (name {obj.Name} already exists).");
|
||||
throw;
|
||||
}
|
||||
|
||||
return obj.ID;
|
||||
}
|
||||
|
||||
|
@ -76,7 +76,18 @@ namespace Kyoo.Controllers
|
||||
if (obj.ExternalIDs != null)
|
||||
foreach (MetadataID entry in obj.ExternalIDs)
|
||||
_database.Entry(entry).State = EntityState.Added;
|
||||
|
||||
try
|
||||
{
|
||||
await _database.SaveChangesAsync();
|
||||
}
|
||||
catch (DbUpdateException ex)
|
||||
{
|
||||
if (Helper.IsDuplicateException(ex))
|
||||
throw new DuplicatedItemException($"Trying to insert a duplicated season (slug {obj.Slug} already exists).");
|
||||
throw;
|
||||
}
|
||||
|
||||
return obj.ID;
|
||||
}
|
||||
|
||||
|
@ -80,7 +80,18 @@ namespace Kyoo.Controllers
|
||||
if (obj.ExternalIDs != null)
|
||||
foreach (MetadataID entry in obj.ExternalIDs)
|
||||
_database.Entry(entry).State = EntityState.Added;
|
||||
|
||||
try
|
||||
{
|
||||
await _database.SaveChangesAsync();
|
||||
}
|
||||
catch (DbUpdateException ex)
|
||||
{
|
||||
if (Helper.IsDuplicateException(ex))
|
||||
throw new DuplicatedItemException($"Trying to insert a duplicated show (slug {obj.Slug} already exists).");
|
||||
throw;
|
||||
}
|
||||
|
||||
return obj.ID;
|
||||
}
|
||||
|
||||
|
@ -56,8 +56,18 @@ namespace Kyoo.Controllers
|
||||
if (obj == null)
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
|
||||
await _database.Studios.AddAsync(obj);
|
||||
_database.Entry(obj).State = EntityState.Added;
|
||||
|
||||
try
|
||||
{
|
||||
await _database.SaveChangesAsync();
|
||||
}
|
||||
catch (DbUpdateException ex)
|
||||
{
|
||||
if (Helper.IsDuplicateException(ex))
|
||||
throw new DuplicatedItemException($"Trying to insert a duplicated studio (slug {obj.Slug} already exists).");
|
||||
throw;
|
||||
}
|
||||
return obj.ID;
|
||||
}
|
||||
|
||||
|
@ -62,8 +62,18 @@ namespace Kyoo.Controllers
|
||||
if (obj.EpisodeID <= 0)
|
||||
throw new InvalidOperationException($"Can't store a track not related to any episode (episodeID: {obj.EpisodeID}).");
|
||||
|
||||
await _database.Tracks.AddAsync(obj);
|
||||
_database.Entry(obj).State = EntityState.Added;
|
||||
|
||||
try
|
||||
{
|
||||
await _database.SaveChangesAsync();
|
||||
}
|
||||
catch (DbUpdateException ex)
|
||||
{
|
||||
if (Helper.IsDuplicateException(ex))
|
||||
throw new DuplicatedItemException($"Trying to insert a duplicated track (slug {obj.Slug} already exists).");
|
||||
throw;
|
||||
}
|
||||
return obj.ID;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user