113 lines
4.0 KiB
C#
113 lines
4.0 KiB
C#
using gehGassi.Core.Interfaces;
|
|
using gehGassi.Domain.Ratings;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using gehGassi.Domain.Favourites;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace gehGassi.Core.Services
|
|
{
|
|
/// <summary>
|
|
/// Service der die Verwaltung von Favoriten ermöglicht
|
|
/// </summary>
|
|
public class FavouritesService : ServiceBase<Favourite>, IFavouriteService
|
|
{
|
|
/// <summary>
|
|
/// Erstellt eine Instanz
|
|
/// </summary>
|
|
/// <param name="unitOfWork">Instanz eines IUnitOfWork</param>
|
|
public FavouritesService(IUnitOfWork unitOfWork) : base(unitOfWork)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Erstellen eines Favoriten
|
|
/// </summary>
|
|
/// <returns>Rating</returns>
|
|
public Favourite Create()
|
|
{
|
|
var favourite = new Favourite()
|
|
{
|
|
Id = Guid.NewGuid().ToString("N"),
|
|
Created = DateTimeOffset.UtcNow
|
|
};
|
|
return favourite;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Erstellen eines Favoriten
|
|
/// </summary>
|
|
/// <param name="appUserId">Id des AppUsers</param>
|
|
/// <param name="key">Id des verknüpften Objekts</param>
|
|
/// <param name="table">Typ des verknüpften Objekts</param>
|
|
/// <returns>Rating</returns>
|
|
public Favourite Create(string appUserId, string key, string table)
|
|
{
|
|
var favourite = new Favourite()
|
|
{
|
|
Id = Guid.NewGuid().ToString("N"),
|
|
Created = DateTimeOffset.UtcNow,
|
|
AppUserId = appUserId,
|
|
Key = key,
|
|
Table = table
|
|
};
|
|
return favourite;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Liste der Favoriten eines Benutzers zurück. Kann Owner und Walker sein
|
|
/// </summary>
|
|
/// <param name="appUserId">Id des AppUsers</param>
|
|
/// <param name="lastUpdate">Letztes Update</param>
|
|
/// <returns>Liste der öffentlichen Anfragen</returns>
|
|
public async Task<List<Favourite>> GetForSyncAppAsync(string appUserId, DateTimeOffset? lastUpdate)
|
|
{
|
|
var query = Repository.Query(c => c.AppUserId == appUserId);
|
|
|
|
if (lastUpdate.HasValue)
|
|
query = query.Where(c => c.UpdatedAt > lastUpdate);
|
|
|
|
return await query.ToListAsync().ConfigureAwait(false);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Liste der Favoriten eines Benutzers zurück
|
|
/// </summary>
|
|
/// <param name="appUserId">Id des AppUsers</param>
|
|
/// <returns>Liste der Favoriten</returns>
|
|
public async Task<List<Favourite>> GetForAppUserAsync(string appUserId)
|
|
{
|
|
return (await Repository.FindAsync(c => c.AppUserId == appUserId && c.Deleted == false).ConfigureAwait(false)).ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Liste der Favoriten eines Benutzers zurück
|
|
/// </summary>
|
|
/// <param name="appUserId">Id des AppUsers</param>
|
|
/// <param name="table">Typ des Objektes das gesucht wird</param>
|
|
/// <returns>Liste der Favoriten</returns>
|
|
public async Task<List<Favourite>> GetForAppUserAsync(string appUserId, string table)
|
|
{
|
|
return (await Repository.FindAsync(c => c.AppUserId == appUserId && c.Table == table && c.Deleted == false).ConfigureAwait(false)).ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Alle Favoriten eines AppUsers löschen
|
|
/// </summary>
|
|
/// <param name="appUserId">Id des AppUsers</param>
|
|
/// <returns>Anzahl gelöscht</returns>
|
|
public async Task<int> DeleteAllForAppUserAsync(string appUserId)
|
|
{
|
|
var favourites = await Repository.FindAsync(c => c.AppUserId == appUserId).ConfigureAwait(false);
|
|
foreach (var favourite in favourites)
|
|
{
|
|
Repository.Remove(favourite);
|
|
}
|
|
return favourites.Count();
|
|
}
|
|
}
|
|
}
|