Add merge UI

This commit is contained in:
Amelia 2025-05-07 17:41:10 +02:00
parent 29e2879153
commit 6ec7e80a43
No known key found for this signature in database
GPG key ID: D6D0ECE365407EAA
13 changed files with 275 additions and 40 deletions

View file

@ -2,6 +2,7 @@
using System.Linq;
using System.Threading.Tasks;
using API.Data;
using API.Data.Repositories;
using API.DTOs;
using API.Entities.Enums;
using API.Extensions;
@ -186,16 +187,16 @@ public class PersonController : BaseApiController
/// <param name="dto"></param>
/// <returns></returns>
[HttpPost("merge")]
public async Task<ActionResult> MergePersons(PersonMergeDto dto)
public async Task<ActionResult<PersonDto>> MergePersons(PersonMergeDto dto)
{
var dst = await _unitOfWork.PersonRepository.GetPersonById(dto.DestId);
var dst = await _unitOfWork.PersonRepository.GetPersonById(dto.DestId, PersonIncludes.All);
if (dst == null) return BadRequest();
var src = await _unitOfWork.PersonRepository.GetPersonById(dto.SrcId);
var src = await _unitOfWork.PersonRepository.GetPersonById(dto.SrcId, PersonIncludes.All);
if (src == null) return BadRequest();
await _personService.MergePeopleAsync(dst, src);
return Ok();
return Ok(_mapper.Map<PersonDto>(dst));
}
[HttpGet("alias/{personId}/{alias}")]

View file

@ -20,6 +20,10 @@ public enum PersonIncludes
{
None = 1 << 0,
Aliases = 1 << 1,
ChapterPeople = 1 << 2,
SeriesPeople = 1 << 3,
All = Aliases | ChapterPeople | SeriesPeople,
}
public interface IPersonRepository

View file

@ -331,6 +331,16 @@ public static class IncludesExtensions
queryable = queryable.Include(p => p.Aliases);
}
if (includeFlags.HasFlag(PersonIncludes.ChapterPeople))
{
queryable = queryable.Include(p => p.ChapterPeople);
}
if (includeFlags.HasFlag(PersonIncludes.SeriesPeople))
{
queryable = queryable.Include(p => p.SeriesMetadataPeople);
}
return queryable;
}
}

View file

@ -14,11 +14,12 @@ public interface IPersonService
/// </summary>
/// <param name="dst">Remaining person</param>
/// <param name="src">Merged person</param>
/// <remarks>The entities passed as arguments **must** include all relations</remarks>
/// <returns></returns>
Task MergePeopleAsync(Person dst, Person src);
/// <summary>
/// Adds the alias to the person, requires that the alias is not shared with anyone else
/// Adds the alias to the person, requires that the aliases are not shared with anyone else
/// </summary>
/// <remarks>This method does NOT commit changes</remarks>
/// <param name="person"></param>
@ -32,6 +33,7 @@ public class PersonService(IUnitOfWork unitOfWork): IPersonService
public async Task MergePeopleAsync(Person dst, Person src)
{
if (dst.Id == src.Id) return;
if (string.IsNullOrWhiteSpace(dst.Description) && !string.IsNullOrWhiteSpace(src.Description))
{
@ -68,7 +70,7 @@ public class PersonService(IUnitOfWork unitOfWork): IPersonService
dst.ChapterPeople.Add(new ChapterPeople
{
Role = chapter.Role,
Chapter = chapter.Chapter,
ChapterId = chapter.ChapterId,
Person = dst,
KavitaPlusConnection = chapter.KavitaPlusConnection,
OrderWeight = chapter.OrderWeight,
@ -79,6 +81,7 @@ public class PersonService(IUnitOfWork unitOfWork): IPersonService
{
dst.SeriesMetadataPeople.Add(new SeriesMetadataPeople
{
SeriesMetadataId = series.SeriesMetadataId,
Role = series.Role,
Person = dst,
KavitaPlusConnection = series.KavitaPlusConnection,
@ -92,6 +95,11 @@ public class PersonService(IUnitOfWork unitOfWork): IPersonService
NormalizedAlias = src.NormalizedName,
});
foreach (var alias in src.Aliases)
{
dst.Aliases.Add(alias);
}
unitOfWork.PersonRepository.Remove(src);
unitOfWork.PersonRepository.Update(dst);
await unitOfWork.CommitAsync();