107 lines
4.2 KiB
C#
107 lines
4.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using gehGassi.Core.Interfaces;
|
|
using gehGassi.Domain.Common;
|
|
using gehGassi.Domain.Dogs;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace gehGassi.Core.Services
|
|
{
|
|
/// <summary>
|
|
/// Service die Verwaltung von Relationen zwischen Entitäten ermöglicht
|
|
/// </summary>
|
|
public class AppUserRelationService : ServiceBase<AppUserRelation>, IAppUserRelationService
|
|
{
|
|
/// <summary>
|
|
/// Erstellt eine Instanz
|
|
/// </summary>
|
|
/// <param name="unitOfWork">Instanz eines IUnitOfWork</param>
|
|
public AppUserRelationService(IUnitOfWork unitOfWork) : base(unitOfWork)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Fügt eine Relation hinzu wenn diese nicht bereits existiert.
|
|
/// Dabei werden beide Richtungen geprüft.
|
|
/// </summary>
|
|
/// <param name="leftId">Id der ersten Entität</param>
|
|
/// <param name="rightId">Id der zweiten Entität</param>
|
|
/// <param name="userName">Benutzer der die Änderung vorgenommen hat</param>
|
|
/// <returns>Neue Relation oder gefundene Relation</returns>
|
|
public async Task<AppUserRelation> AddIfNotExistsAsync(string leftId, string rightId, string userName)
|
|
{
|
|
var query = Repository.Query(c => (c.LeftId == leftId && c.RightId == rightId) || (c.LeftId == rightId && c.RightId == leftId));
|
|
|
|
var found = await query.FirstOrDefaultAsync().ConfigureAwait(false);
|
|
if (found == null)
|
|
{
|
|
found = new AppUserRelation
|
|
{
|
|
LeftId = leftId,
|
|
RightId = rightId,
|
|
Created = DateTime.Now
|
|
};
|
|
Repository.Add(found);
|
|
await CommitAsync(userName);
|
|
}
|
|
|
|
return found;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Setzen einer Relation auf gelöscht - einseitig.
|
|
/// Für den Initiator wird "gelöscht" gesetzt.
|
|
/// Der Initiator wird dabei aber in Left und Right gesucht.
|
|
/// </summary>
|
|
/// <param name="initiatorId">Id des Initiators</param>
|
|
/// <param name="otherId">Id der anderen Entität</param>
|
|
/// <param name="userName">Benutzer der die Änderung vorgenommen hat</param>
|
|
/// <returns>true wenn auf gelöscht gesetzt, false sonst</returns>
|
|
public async Task<bool> DeleteAsync(string initiatorId, string otherId, string userName)
|
|
{
|
|
var query = Repository.Query(c => (c.LeftId == initiatorId && c.RightId == otherId) || (c.LeftId == otherId && c.RightId == initiatorId));
|
|
|
|
var found = await query.FirstOrDefaultAsync().ConfigureAwait(false);
|
|
if (found != null)
|
|
{
|
|
if (found.LeftId == initiatorId)
|
|
found.LeftDeleted = true;
|
|
else
|
|
found.RightDeleted = true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Liste von Relationszielen zurück, zu welcher der Initiator eine Beziehung hat
|
|
/// </summary>
|
|
/// <param name="initiatorId">Id des Initiators</param>
|
|
/// <param name="includeDeleted">Sollen gelöschte inkludiert werden?</param>
|
|
/// <returns>Liste von Relationszielen</returns>
|
|
public async Task<List<AppUserRelationTarget>> GetRelations(string initiatorId, bool includeDeleted = false)
|
|
{
|
|
var resultList = new List<AppUserRelationTarget>();
|
|
|
|
var query = Repository.Query(c => c.LeftId == initiatorId || c.RightId == initiatorId );
|
|
|
|
if (includeDeleted == false)
|
|
query = query.Where(c => c.RightDeleted == false && c.LeftDeleted == false);
|
|
|
|
var items = await query.ToListAsync().ConfigureAwait(false);
|
|
foreach (var item in items)
|
|
{
|
|
if(item.LeftId == initiatorId)
|
|
resultList.Add(new AppUserRelationTarget(){Id = item.Id, TargetId = item.RightId});
|
|
else
|
|
resultList.Add(new AppUserRelationTarget() { Id = item.Id, TargetId = item.LeftId});
|
|
}
|
|
|
|
return resultList;
|
|
}
|
|
}
|
|
}
|