using gehGassi.Core.Interfaces;
using gehGassi.Domain.Favourites;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using gehGassi.Domain.Common;
using gehGassi.Domain.Devices;
namespace gehGassi.Core.Services
{
///
/// Service der die Verwaltung von Devices der App-User ermöglicht
///
public class DeviceService : ServiceBase, IDeviceService
{
///
/// Erstellt eine Instanz
///
/// Instanz eines IUnitOfWork
public DeviceService(IUnitOfWork unitOfWork) : base(unitOfWork)
{
}
///
/// Anlegen oder Aktualisieren eines Devices
///
/// ID des AppUsers
/// eindeutige InstallationsID
/// Platform (OS)
/// Platform (PNS)
/// Token des Users
/// Sprache die am Gerät eingestellt ist
/// Soll Commit untertrückt werden
/// Device
public async Task CreateOrUpdateAsync(string appUserId, string installationId, Platform platform, NotificationPlatform notificationPlatform, string channel, string language, bool suppresCommit)
{
var device = await Repository.FirstOrDefaultAsync(c => c.AppUserId == appUserId && c.InstallationId == installationId);
if (device != null)
{
device.Platform = platform;
device.NotificationPlatform = notificationPlatform;
device.Channel = channel;
device.Language = language;
device.LastUpdate = DateTimeOffset.UtcNow;
}
else
{
device = new Device
{
AppUserId = appUserId,
InstallationId = installationId,
Platform = platform,
NotificationPlatform = notificationPlatform,
Channel = channel,
Language = language,
Created = DateTimeOffset.UtcNow
};
Repository.Add(device);
}
if (!suppresCommit)
await CommitAsync("System");
return device;
}
///
/// Löschen eines Devices eines Benutzers mit einer InstallationsId
///
///
///
/// Soll Commit untertrückt werden
///
public async Task DeleteAsync(string appUserId, string installationId, bool suppresCommit)
{
var device = await Repository.FirstOrDefaultAsync(c => c.AppUserId == appUserId && c.InstallationId == installationId);
if (device != null)
{
Remove(device);
if (!suppresCommit)
await CommitAsync("System");
return true;
}
return false;
}
///
/// Gibt eine Liste aller Devices eines App-Users zurück
///
/// Id des AppUsers
/// Liste von Devices
public async Task> GetAllAsync(string appUserId)
{
return (await Repository.FindAsync(c => c.AppUserId == appUserId).ConfigureAwait(false)).ToList();
}
///
/// Gibt ein Device für einen App-User zurück
///
/// Id des App-Users
/// InstallationsId des Devices
/// Device oder null, wenn nicht gefunden
public async Task GetAsycn(string appUserId, string installationId)
{
return await Repository.FirstOrDefaultAsync(c => c.AppUserId == appUserId && c.InstallationId == installationId);
}
}
}