mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-31 20:24:27 -04:00
Allowing shows to be edited
This commit is contained in:
parent
9b42680b31
commit
7d7265ba12
@ -55,6 +55,7 @@ namespace Kyoo.Controllers
|
|||||||
//Register values
|
//Register values
|
||||||
long RegisterCollection(Collection collection);
|
long RegisterCollection(Collection collection);
|
||||||
long RegisterShow(Show show);
|
long RegisterShow(Show show);
|
||||||
|
long EditShow(Show show);
|
||||||
long RegisterMovie(Episode movie);
|
long RegisterMovie(Episode movie);
|
||||||
long RegisterSeason(Season season);
|
long RegisterSeason(Season season);
|
||||||
long RegisterEpisode(Episode episode);
|
long RegisterEpisode(Episode episode);
|
||||||
|
14
Kyoo.Common/Models/Exceptions/ItemNotFound.cs
Normal file
14
Kyoo.Common/Models/Exceptions/ItemNotFound.cs
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Kyoo.Models.Exceptions
|
||||||
|
{
|
||||||
|
public class ItemNotFound : Exception
|
||||||
|
{
|
||||||
|
public override string Message { get; }
|
||||||
|
|
||||||
|
public ItemNotFound(string message)
|
||||||
|
{
|
||||||
|
Message = message;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -11,6 +11,8 @@ namespace Kyoo.Models
|
|||||||
|
|
||||||
// public IEnumerable<Show> Shows { get; set; }
|
// public IEnumerable<Show> Shows { get; set; }
|
||||||
|
|
||||||
|
public Genre() {}
|
||||||
|
|
||||||
public Genre(string name)
|
public Genre(string name)
|
||||||
{
|
{
|
||||||
Slug = Utility.ToSlug(name);
|
Slug = Utility.ToSlug(name);
|
||||||
|
@ -31,7 +31,7 @@ namespace Kyoo.Models
|
|||||||
|
|
||||||
public bool IsCollection;
|
public bool IsCollection;
|
||||||
|
|
||||||
[JsonIgnore] public virtual IEnumerable<Genre> Genres
|
public virtual IEnumerable<Genre> Genres
|
||||||
{
|
{
|
||||||
get { return GenreLinks?.Select(x => x.Genre).OrderBy(x => x.Name); }
|
get { return GenreLinks?.Select(x => x.Genre).OrderBy(x => x.Name); }
|
||||||
set { GenreLinks = value?.Select(x => new GenreLink(this, x)).ToList(); }
|
set { GenreLinks = value?.Select(x => new GenreLink(this, x)).ToList(); }
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
using Kyoo.Models;
|
using System;
|
||||||
|
using Kyoo.Models;
|
||||||
using Kyoo.Models.Watch;
|
using Kyoo.Models.Watch;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using Kyoo.Models.Exceptions;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
namespace Kyoo.Controllers
|
namespace Kyoo.Controllers
|
||||||
@ -347,29 +350,70 @@ namespace Kyoo.Controllers
|
|||||||
{
|
{
|
||||||
if (show == null)
|
if (show == null)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (!_database.Entry(show).IsKeySet)
|
if (!_database.Entry(show).IsKeySet)
|
||||||
_database.Shows.Add(show);
|
_database.Shows.Add(show);
|
||||||
|
|
||||||
if (show.Studio != null) // Do not query it if the studio is already attached. // Do not set this value if the database does not found the studio.
|
|
||||||
show.Studio = _database.Studios.FirstOrDefault(x => x.Slug == show.Studio.Slug);
|
|
||||||
// show.GenreLinks = show.GenreLinks?.Select(x =>
|
|
||||||
// {
|
|
||||||
// x.Genre = _database.Genres.FirstOrDefault(y => y.Slug == x.Genre.Slug) ?? x.Genre;
|
|
||||||
// return x;
|
|
||||||
// });
|
|
||||||
// show.People = show.People?.Select(x =>
|
|
||||||
// {
|
|
||||||
// x.People = _database.Peoples.FirstOrDefault(y => y.Slug == x.Slug) ?? x.People;
|
|
||||||
// return x;
|
|
||||||
// });
|
|
||||||
// show.Seasons = show.Seasons?.Select(x => _database.Seasons.FirstOrDefault(y => y.SeasonNumber == x.SeasonNumber) ?? x);
|
|
||||||
// show.Episodes = show.Episodes?.Select(x => _database.Episodes.FirstOrDefault(y => y.EpisodeNumber == x.EpisodeNumber && y.SeasonNumber == x.SeasonNumber) ?? x);
|
|
||||||
//
|
|
||||||
_database.SaveChanges();
|
_database.SaveChanges();
|
||||||
return show.ID;
|
return show.ID;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public long EditShow(Show edited)
|
||||||
|
{
|
||||||
|
if (edited == null)
|
||||||
|
throw new ArgumentNullException(nameof(edited));
|
||||||
|
|
||||||
|
_database.ChangeTracker.LazyLoadingEnabled = false;
|
||||||
|
_database.ChangeTracker.AutoDetectChangesEnabled = false;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Show show = _database.Entry(edited).IsKeySet
|
||||||
|
? _database.Shows.FirstOrDefault(x => x.ID == edited.ID)
|
||||||
|
: GetShowBySlug(edited.Slug);
|
||||||
|
|
||||||
|
if (show == null)
|
||||||
|
throw new ItemNotFound($"No show could be found with the id {edited.ID} or the slug {edited.Slug}");
|
||||||
|
|
||||||
|
Utility.Complete(show, edited);
|
||||||
|
|
||||||
|
Studio tmp = _database.Studios.FirstOrDefault(x => x.Slug == edited.Studio.Slug);
|
||||||
|
if (tmp != null)
|
||||||
|
show.Studio = tmp;
|
||||||
|
|
||||||
|
show.GenreLinks = edited.GenreLinks?.Select(x =>
|
||||||
|
{
|
||||||
|
x.Genre = _database.Genres.FirstOrDefault(y => y.Slug == x.Genre.Slug) ?? x.Genre;
|
||||||
|
x.GenreID = x.Genre.ID;
|
||||||
|
return x;
|
||||||
|
}).ToList();
|
||||||
|
show.People = edited.People?.Select(x =>
|
||||||
|
{
|
||||||
|
x.People = _database.Peoples.FirstOrDefault(y => y.Slug == x.People.Slug) ?? x.People;
|
||||||
|
return x;
|
||||||
|
}).ToList();
|
||||||
|
show.Seasons = edited.Seasons?.Select(x =>
|
||||||
|
{
|
||||||
|
return _database.Seasons.FirstOrDefault(y => y.ShowID == x.ShowID
|
||||||
|
&& y.SeasonNumber == x.SeasonNumber) ?? x;
|
||||||
|
}).ToList();
|
||||||
|
show.Episodes = edited.Episodes?.Select(x =>
|
||||||
|
{
|
||||||
|
return _database.Episodes.FirstOrDefault(y => y.ShowID == x.ShowID
|
||||||
|
&& y.SeasonNumber == x.SeasonNumber
|
||||||
|
&& y.EpisodeNumber == x.EpisodeNumber) ?? x;
|
||||||
|
}).ToList();
|
||||||
|
|
||||||
|
_database.ChangeTracker.DetectChanges();
|
||||||
|
_database.SaveChanges();
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_database.ChangeTracker.LazyLoadingEnabled = true;
|
||||||
|
_database.ChangeTracker.AutoDetectChangesEnabled = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return edited.ID;
|
||||||
|
}
|
||||||
|
|
||||||
public long RegisterMovie(Episode movie)
|
public long RegisterMovie(Episode movie)
|
||||||
{
|
{
|
||||||
if (movie == null)
|
if (movie == null)
|
||||||
|
@ -1,11 +1,10 @@
|
|||||||
using Kyoo.Models;
|
using System;
|
||||||
|
using Kyoo.Models;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
|
||||||
using Kyoo.Controllers;
|
using Kyoo.Controllers;
|
||||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
using Kyoo.Models.Exceptions;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
|
|
||||||
namespace Kyoo.Api
|
namespace Kyoo.Api
|
||||||
{
|
{
|
||||||
@ -42,17 +41,21 @@ namespace Kyoo.Api
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost("edit/{slug}")]
|
[HttpPost("edit/{slug}")]
|
||||||
// [Authorize(Policy="Write")]
|
[Authorize(Policy="Write")]
|
||||||
public IActionResult EditShow(string slug, [FromBody] Show show)
|
public IActionResult EditShow(string slug, [FromBody] Show show)
|
||||||
{
|
{
|
||||||
if (!ModelState.IsValid)
|
if (!ModelState.IsValid)
|
||||||
return BadRequest(show);
|
return BadRequest(show);
|
||||||
show.ID = 0;
|
show.ID = 0;
|
||||||
Show old = _libraryManager.GetShowBySlug(slug);
|
show.Slug = slug;
|
||||||
if (old == null)
|
try
|
||||||
|
{
|
||||||
|
_libraryManager.EditShow(show);
|
||||||
|
}
|
||||||
|
catch (ItemNotFound)
|
||||||
|
{
|
||||||
return NotFound();
|
return NotFound();
|
||||||
//old = Utility.Complete(old, show);
|
}
|
||||||
_libraryManager.RegisterShow(old);
|
|
||||||
return Ok();
|
return Ok();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,6 @@
|
|||||||
"peoplePath": "people",
|
"peoplePath": "people",
|
||||||
"profilePicturePath": "users/",
|
"profilePicturePath": "users/",
|
||||||
"plugins": "plugins/",
|
"plugins": "plugins/",
|
||||||
"defaultPermissions": "read,play",
|
"defaultPermissions": "read,play,write,admin",
|
||||||
"regex": "(\\/(?<Collection>.*)\\/)?.*\\/(?<ShowTitle>.+?)(( S(?<Season>\\d+)E(?<Episode>\\d+)| (?<Absolute>\\d+)))?\\."
|
"regex": "(\\/(?<Collection>.*)\\/)?.*\\/(?<ShowTitle>.+?)(( S(?<Season>\\d+)E(?<Episode>\\d+)| (?<Absolute>\\d+)))?\\."
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user