gehgassi_backend/gehGassi.Core/Services/SubscriptionService.cs

250 lines
11 KiB
C#

using gehGassi.Core.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using gehGassi.Domain.Common;
using gehGassi.Domain.Dogs;
using gehGassi.Domain.Subscriptions;
using Microsoft.EntityFrameworkCore;
namespace gehGassi.Core.Services
{
/// <summary>
/// Service für Abonnements
/// </summary>
public class SubscriptionService : ServiceBase<Subscription>, ISubscriptionService
{
private readonly IRepository<SubscriptionWithNames> _namesRepository;
private readonly IRepository<AppUserSubscription> _appUserSubscriptionRepository;
private readonly IRepository<AppUserSubscriptionWithNames> _appUserSubscriptionNamesRepository;
/// <summary>
/// Erstellt eine neue Instanz
/// </summary>
/// <param name="unitOfWork">Instanz eines IUnitOfWork</param>
public SubscriptionService(IUnitOfWork unitOfWork) : base(unitOfWork)
{
_namesRepository = unitOfWork.GetRepository<SubscriptionWithNames>();
_appUserSubscriptionRepository = unitOfWork.GetRepository<AppUserSubscription>();
_appUserSubscriptionNamesRepository = unitOfWork.GetRepository<AppUserSubscriptionWithNames>();
}
/// <summary>
/// Gibt eine gefilterte Liste von Abonnements zurück
/// </summary>
/// <param name="filter">Filterbegriff der in bestimmten Feldern gesucht wird</param>
/// <returns>List betroffener Abonnements</returns>
public IQueryable<Subscription> Filter(string filter)
{
return Repository.Query(c => c.Name.Contains(filter) || c.NameLocalized.Contains(filter));
}
/// <summary>
/// Erstellen eines Abonnements
/// </summary>
/// <returns>Abonnement</returns>
public Subscription Create()
{
var item = new Subscription()
{
Id = Guid.NewGuid().ToString("N"),
};
return item;
}
/// <summary>
/// Gibt eine gefilterte Liste von Abonnements zurück.
/// </summary>
/// <param name="filter">Filterbegriff der in bestimmten Feldern gesucht wird</param>
/// <returns>List betroffener Abonnements</returns>
public async Task<List<Subscription>> SearchAsync(string filter)
{
return (await Repository.FindAsync(c => c.NameLocalized.Contains(filter) && c.Deleted == false)).ToList();
}
/// <summary>
/// Gibt eine gefilterte Liste von Abonnements mit Namen zurück in einer gewählten Sprache
/// </summary>
/// <param name="filter">Filterbegriff der in bestimmten Feldern gesucht wird</param>
/// <param name="language">Gewünschte Sprache</param>
/// <param name="fallback">Fallback Sprache</param>
/// <param name="includeDeleted">Sollen gelöschte inkludiert werden?</param>
/// <returns>Liste betroffener Abonnements</returns>
public IQueryable<SubscriptionWithNames> FilterWithNames(string filter, string language, string fallback, bool includeDeleted)
{
language = language.ToUpperInvariant();
fallback = fallback.ToUpperInvariant();
var baseQuery = _namesRepository.QueryStoredProcedure("SELECT * FROM tv_SubscriptionsWithNames({0},{1})", c => c.Name.Contains(filter) || c.Description.Contains(filter), language, fallback);
if (!includeDeleted)
baseQuery = baseQuery.Where(c => c.Deleted == false);
return baseQuery;
}
/// <summary>
/// Gibt eine Liste von Abonnements zurück, welche dem gesuchten AppMode entsprechend und aktiv sind
/// </summary>
/// <param name="appMode">Gesuchter App mode</param>
/// <param name="language">Gewünschte Sprache</param>
/// <param name="fallback">Fallback Sprache</param>
/// <returns>Liste Abonnements</returns>
public async Task<List<SubscriptionWithNames>> GetAllAsync(AppMode appMode, string language, string fallback)
{
language = language.ToUpperInvariant();
fallback = fallback.ToUpperInvariant();
var baseQuery = _namesRepository.QueryStoredProcedure("SELECT * FROM tv_SubscriptionsWithNames({0},{1})", c => c.AppMode == appMode && c.Enabled && c.Deleted == false, language, fallback);
var list = await baseQuery.ToListAsync();
return list;
}
/// <summary>
/// Gibt eine gefilterte Liste von Abonnements mit Namen zurück in einer gewählten Sprache
/// </summary>
/// <param name="filter">Filterbegriff der in bestimmten Feldern gesucht wird</param>
/// <param name="language">Gewünschte Sprache</param>
/// <param name="fallback">Fallback Sprache</param>
/// <returns>Liste betroffener Abonnements</returns>
public IQueryable<AppUserSubscriptionWithNames> FilterAppUserSubscritionsWithNames(string filter, string language, string fallback)
{
language = language.ToUpperInvariant();
fallback = fallback.ToUpperInvariant();
var baseQuery = _appUserSubscriptionNamesRepository.QueryStoredProcedure("SELECT * FROM tv_AppUserSubscriptionsWithNames({0},{1})", c => c.AppUserName.Contains(filter), language, fallback);
return baseQuery;
}
/// <summary>
/// Gibt eine Liste von Abonnements zurück, welche ein App-User abonniert hat
/// </summary>
/// <param name="appUserId">App user id</param>
/// <param name="language">Gewünschte Sprache</param>
/// <param name="fallback">Fallback Sprache</param>
/// <returns>Liste betroffener Abonnement-Buchungen</returns>
public async Task<List<AppUserSubscriptionWithNames>> GetAllAppUserSubscriptionsAsync(string appUserId, string language, string fallback)
{
language = language.ToUpperInvariant();
fallback = fallback.ToUpperInvariant();
var baseQuery = _appUserSubscriptionNamesRepository.QueryStoredProcedure("SELECT * FROM tv_AppUserSubscriptionsWithNames({0},{1})", c => c.AppUserId == appUserId, language, fallback);
return await baseQuery.ToListAsync();
}
/// <summary>
/// Gibt eine Liste von Abonnements zurück, welche ein App-User abonniert hat die gültig und aktiv sind
/// </summary>
/// <param name="appUserId">App user id</param>
/// <param name="language">Gewünschte Sprache</param>
/// <param name="fallback">Fallback Sprache</param>
/// <returns>Liste betroffener Abonnement-Buchungen</returns>
public async Task<List<AppUserSubscriptionWithNames>> GetAllAppUserSubscriptionsValidAsync(string appUserId, string language, string fallback)
{
language = language.ToUpperInvariant();
fallback = fallback.ToUpperInvariant();
var baseQuery = _appUserSubscriptionNamesRepository.QueryStoredProcedure("SELECT * FROM tv_AppUserSubscriptionsWithNames({0},{1})", c => c.AppUserId == appUserId && c.IsValid, language, fallback);
return await baseQuery.ToListAsync();
}
/// <summary>
/// Gibt eine Abonnementbuchung mit Namen zurück
/// </summary>
/// <param name="id">Id der Buchung</param>
/// <param name="language"></param>
/// <param name="fallback"></param>
/// <returns>AppUserSubscriptionWithNames</returns>
public async Task<AppUserSubscriptionWithNames> GetAppUserSubscriptionAsync(long id, string language, string fallback)
{
language = language.ToUpperInvariant();
fallback = fallback.ToUpperInvariant();
var baseQuery = _appUserSubscriptionNamesRepository.QueryStoredProcedure("SELECT * FROM tv_AppUserSubscriptionsWithNames({0},{1})", c => c.Id == id, language, fallback);
return await baseQuery.FirstOrDefaultAsync();
}
/// <summary>
/// Erstellen eines Abonnements für einen App-User
/// </summary>
/// <returns>Abonnement</returns>
public AppUserSubscription CreateAppUserSubscription()
{
var item = new AppUserSubscription()
{
};
return item;
}
/// <summary>
/// Gibt ein App-User-Abonnement zurück
/// </summary>
/// <param name="id">AppUserSubcription id</param>
/// <returns>AppUserSubscription</returns>
public async Task<AppUserSubscription> GetAppUserSubscriptionAsync(long id)
{
return await _appUserSubscriptionRepository.GetAsync(id);
}
/// <summary>
/// Gibt ein App-User-Abonnement zurück
/// </summary>
/// <param name="appUserId">App user id</param>
/// <param name="subscriptionId">Abo-Id</param>
/// <returns>AppUserSubscription oder null, wenn nicht gefunden</returns>
public async Task<AppUserSubscription> GetAppUserSubscriptionAsync(string appUserId, string subscriptionId)
{
return await _appUserSubscriptionRepository.FirstOrDefaultAsync(c => c.AppUserId == appUserId && c.SubscriptionId == subscriptionId);
}
/// <summary>
/// Fügt eine Entität zum Repository hinzu
/// </summary>
/// <param name="entity">Entität</param>
public void AddAppUserSubscription(AppUserSubscription entity)
{
_appUserSubscriptionRepository.Add(entity);
}
/// <summary>
/// Entfernt eine Entität aus dem Repository
/// </summary>
/// <param name="entity">Zu entfernende Entität</param>
public void RemoveAppUserSubscription(AppUserSubscription entity)
{
_appUserSubscriptionRepository.Remove(entity);
}
/// <summary>
/// Prüft ob ein App-User ein Abonnement hat für einen AppMode das noch gültig ist
/// </summary>
/// <param name="appUserId">App user id</param>
/// <param name="appMode">AppMode</param>
/// <returns>true wenn ja, false else</returns>
public async Task<bool> AppUserHasActiveBookingAsync(string appUserId, AppMode appMode)
{
var baseQuery = _appUserSubscriptionNamesRepository.QueryStoredProcedure("SELECT * FROM tv_AppUserSubscriptionsWithNames({0},{1})", c => c.AppUserId == appUserId && c.SubscriptionAppMode == appMode && c.IsValid, "DE", "DE");
var items = await baseQuery.CountAsync();
return items != 0;
}
/// <summary>
/// Setzt Abo-Buchungen die abgelaufen sind auf ungültig
/// </summary>
/// <returns></returns>
public async Task<int> InvalidateExipredAppUserSubscriptionsAsync(DateTimeOffset date)
{
var items = await _appUserSubscriptionRepository.FindAsync(c => c.IsValid && c.ExpirationDateWithGrace <= date);
foreach (var item in items)
{
item.IsValid = false;
}
return items.Count();
}
}
}