From 003a32e7552fbf1dc85055b704324624e363b497 Mon Sep 17 00:00:00 2001 From: Mikal Stordal Date: Thu, 5 Oct 2023 04:25:56 +0200 Subject: [PATCH] fix: validate main series for group when main series has changed or when you edit the group in v1 --- .../ShokoServiceImplementation_Entities.cs | 4 ++- Shoko.Server/Models/SVR_AnimeGroup.cs | 34 ++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/Shoko.Server/API/v1/Implementations/ShokoServiceImplementation/ShokoServiceImplementation_Entities.cs b/Shoko.Server/API/v1/Implementations/ShokoServiceImplementation/ShokoServiceImplementation_Entities.cs index 400d6bd48..96688f016 100755 --- a/Shoko.Server/API/v1/Implementations/ShokoServiceImplementation/ShokoServiceImplementation_Entities.cs +++ b/Shoko.Server/API/v1/Implementations/ShokoServiceImplementation/ShokoServiceImplementation_Entities.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using Microsoft.AspNetCore.Mvc; @@ -2150,6 +2150,8 @@ public CL_Response SaveGroup(CL_AnimeGroup_Save_Request cont grp.SortName = contract.SortName; } + grp.ValidateMainSeries(); + RepoFactory.AnimeGroup.Save(grp, true, true); var userRecord = grp.GetUserRecord(userID); diff --git a/Shoko.Server/Models/SVR_AnimeGroup.cs b/Shoko.Server/Models/SVR_AnimeGroup.cs index 13acb02d9..7e1e59010 100644 --- a/Shoko.Server/Models/SVR_AnimeGroup.cs +++ b/Shoko.Server/Models/SVR_AnimeGroup.cs @@ -273,8 +273,10 @@ public void SetMainSeries(SVR_AnimeSeries series) // the new id instead of the old. DefaultAnimeSeriesID = series?.AnimeSeriesID; + ValidateMainSeries(); + // Reset the name/description if the group is not manually named. - if (series == null && (IsManuallyNamed == 0 || OverrideDescription == 0)) + if (IsManuallyNamed == 0 || OverrideDescription == 0) series = GetMainSeries(); if (IsManuallyNamed == 0) GroupName = SortName = series!.GetSeriesName(); @@ -286,6 +288,36 @@ public void SetMainSeries(SVR_AnimeSeries series) RepoFactory.AnimeGroup.Save(this, false, false); } + public bool ValidateMainSeries() + { + var changed = false; + var allSeries = GetAllSeries(true); + + // User overridden main series. + if (DefaultAnimeSeriesID.HasValue) + { + var series = allSeries.Find(series => series.AnimeSeriesID == DefaultAnimeSeriesID.Value); + if (series != null) + { + DefaultAnimeSeriesID = null; + changed = true; + } + } + + // Auto selected main series. + if (MainAniDBAnimeID.HasValue) + { + var series = allSeries.Find(series => series.AniDB_ID == MainAniDBAnimeID.Value); + if (series != null) + { + MainAniDBAnimeID = null; + changed = true; + } + } + + return changed; + } + public List GetSeries(bool skipSorting = false) { if (skipSorting)