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 { /// /// Service für Abonnements /// public class SubscriptionService : ServiceBase, ISubscriptionService { private readonly IRepository _namesRepository; private readonly IRepository _appUserSubscriptionRepository; private readonly IRepository _appUserSubscriptionNamesRepository; /// /// Erstellt eine neue Instanz /// /// Instanz eines IUnitOfWork public SubscriptionService(IUnitOfWork unitOfWork) : base(unitOfWork) { _namesRepository = unitOfWork.GetRepository(); _appUserSubscriptionRepository = unitOfWork.GetRepository(); _appUserSubscriptionNamesRepository = unitOfWork.GetRepository(); } /// /// Gibt eine gefilterte Liste von Abonnements zurück /// /// Filterbegriff der in bestimmten Feldern gesucht wird /// List betroffener Abonnements public IQueryable Filter(string filter) { return Repository.Query(c => c.Name.Contains(filter) || c.NameLocalized.Contains(filter)); } /// /// Erstellen eines Abonnements /// /// Abonnement public Subscription Create() { var item = new Subscription() { Id = Guid.NewGuid().ToString("N"), }; return item; } /// /// Gibt eine gefilterte Liste von Abonnements zurück. /// /// Filterbegriff der in bestimmten Feldern gesucht wird /// List betroffener Abonnements public async Task> SearchAsync(string filter) { return (await Repository.FindAsync(c => c.NameLocalized.Contains(filter) && c.Deleted == false)).ToList(); } /// /// Gibt eine gefilterte Liste von Abonnements mit Namen zurück in einer gewählten Sprache /// /// Filterbegriff der in bestimmten Feldern gesucht wird /// Gewünschte Sprache /// Fallback Sprache /// Sollen gelöschte inkludiert werden? /// Liste betroffener Abonnements public IQueryable 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; } /// /// Gibt eine Liste von Abonnements zurück, welche dem gesuchten AppMode entsprechend und aktiv sind /// /// Gesuchter App mode /// Gewünschte Sprache /// Fallback Sprache /// Liste Abonnements public async Task> 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; } /// /// Gibt eine gefilterte Liste von Abonnements mit Namen zurück in einer gewählten Sprache /// /// Filterbegriff der in bestimmten Feldern gesucht wird /// Gewünschte Sprache /// Fallback Sprache /// Liste betroffener Abonnements public IQueryable 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; } /// /// Gibt eine Liste von Abonnements zurück, welche ein App-User abonniert hat /// /// App user id /// Gewünschte Sprache /// Fallback Sprache /// Liste betroffener Abonnement-Buchungen public async Task> 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(); } /// /// Gibt eine Liste von Abonnements zurück, welche ein App-User abonniert hat die gültig und aktiv sind /// /// App user id /// Gewünschte Sprache /// Fallback Sprache /// Liste betroffener Abonnement-Buchungen public async Task> 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(); } /// /// Gibt eine Abonnementbuchung mit Namen zurück /// /// Id der Buchung /// /// /// AppUserSubscriptionWithNames public async Task 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(); } /// /// Erstellen eines Abonnements für einen App-User /// /// Abonnement public AppUserSubscription CreateAppUserSubscription() { var item = new AppUserSubscription() { }; return item; } /// /// Gibt ein App-User-Abonnement zurück /// /// AppUserSubcription id /// AppUserSubscription public async Task GetAppUserSubscriptionAsync(long id) { return await _appUserSubscriptionRepository.GetAsync(id); } /// /// Gibt ein App-User-Abonnement zurück /// /// App user id /// Abo-Id /// AppUserSubscription oder null, wenn nicht gefunden public async Task GetAppUserSubscriptionAsync(string appUserId, string subscriptionId) { return await _appUserSubscriptionRepository.FirstOrDefaultAsync(c => c.AppUserId == appUserId && c.SubscriptionId == subscriptionId); } /// /// Fügt eine Entität zum Repository hinzu /// /// Entität public void AddAppUserSubscription(AppUserSubscription entity) { _appUserSubscriptionRepository.Add(entity); } /// /// Entfernt eine Entität aus dem Repository /// /// Zu entfernende Entität public void RemoveAppUserSubscription(AppUserSubscription entity) { _appUserSubscriptionRepository.Remove(entity); } /// /// Prüft ob ein App-User ein Abonnement hat für einen AppMode das noch gültig ist /// /// App user id /// AppMode /// true wenn ja, false else public async Task 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; } /// /// Setzt Abo-Buchungen die abgelaufen sind auf ungültig /// /// public async Task 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(); } } }