661 lines
29 KiB
C#
661 lines
29 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 gehGassi.Domain.Ratings;
|
|
using gehGassi.Domain.Users;
|
|
using gehGassi.Dto.DogWalkers;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
|
|
using NetTopologySuite.Geometries;
|
|
using NetTopologySuite;
|
|
using gehGassi.Common.Extensions;
|
|
using System.Collections;
|
|
using gehGassi.Domain.Messages;
|
|
using gehGassi.Dto.Common;
|
|
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
|
|
|
|
namespace gehGassi.Core.Services
|
|
{
|
|
/// <summary>
|
|
/// Service der die Verwaltung von App-Usern realisiert
|
|
/// </summary>
|
|
public class AppUserService : ServiceBase<AppUser>, IAppUserService
|
|
{
|
|
private readonly IRepository<DogWalkerProfile> _profileRepository;
|
|
private readonly IRepository<Certificate> _certificateRepository;
|
|
private readonly IRepository<DogWalker> _walkerRepository;
|
|
private readonly IRepository<AppUserBlock> _blockRepository;
|
|
private readonly IRepository<AppUserBlockWithNames> _blockWithNamesRepository;
|
|
|
|
/// <summary>
|
|
/// Erstellt eine Instanz
|
|
/// </summary>
|
|
/// <param name="unitOfWork">Instanz eines IUnitOfWork</param>
|
|
public AppUserService(IUnitOfWork unitOfWork) : base(unitOfWork)
|
|
{
|
|
_profileRepository = unitOfWork.GetRepository<DogWalkerProfile>();
|
|
_certificateRepository = unitOfWork.GetRepository<Certificate>();
|
|
_walkerRepository = unitOfWork.GetRepository<DogWalker>();
|
|
_blockRepository = unitOfWork.GetRepository<AppUserBlock>();
|
|
_blockWithNamesRepository = unitOfWork.GetRepository<AppUserBlockWithNames>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine gefilterte Liste von App-Usern zurück
|
|
/// </summary>
|
|
/// <param name="filter">Filterbegriff der in bestimmten Feldern gesucht wird</param>
|
|
/// <param name="includeDeleted">Sollen gelöschte inkludiert werden?</param>
|
|
/// <returns>List betroffener App-User</returns>
|
|
public IQueryable<AppUser> Filter(string filter, bool includeDeleted)
|
|
{
|
|
if(includeDeleted)
|
|
return Repository.Query(c => c.FirstName.Contains(filter) || c.LastName.Contains(filter) || c.Contact.Email.Contains(filter));
|
|
else
|
|
return Repository.Query(c => c.Deleted == false && (c.FirstName.Contains(filter) || c.LastName.Contains(filter) || c.Contact.Email.Contains(filter)));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Erstellen eines App-Users
|
|
/// </summary>
|
|
/// <returns>App-User</returns>
|
|
public AppUser Create()
|
|
{
|
|
var item = new AppUser()
|
|
{
|
|
Id = Guid.NewGuid().ToString("N"),
|
|
Created = DateTimeOffset.UtcNow,
|
|
Contact = new ContactMin(),
|
|
Address = new Address(),
|
|
SocialMedia = new SocialMedia(),
|
|
RatingStatistics = new RatingStatistics(),
|
|
RatingStatisticsWalker = new RatingStatistics(),
|
|
Type = AppUserType.DogOwner
|
|
};
|
|
return item;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine gefilterte Liste von App-Usern zurück. Sucht nur im Namen
|
|
/// </summary>
|
|
/// <param name="filter">Filterbegriff der in bestimmten Feldern gesucht wird</param>
|
|
/// <param name="includeDeleted">Sollen gelöschte inkludiert werden?</param>
|
|
/// <returns>List betroffener App-User</returns>
|
|
public async Task<List<AppUser>> SearchAsync(string filter, bool includeDeleted)
|
|
{
|
|
if(includeDeleted)
|
|
return (await Repository.FindAsync(c => c.FirstName.Contains(filter) || c.LastName.Contains(filter) || c.Contact.Email.Contains(filter))).ToList();
|
|
else
|
|
return (await Repository.FindAsync(c => c.Deleted == false && (c.FirstName.Contains(filter) || c.LastName.Contains(filter) || c.Contact.Email.Contains(filter)))).ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt die nächste freie Nummer für einen App-User zurück
|
|
/// </summary>
|
|
/// <returns>App-User-Nummer</returns>
|
|
public string GetNextNumber()
|
|
{
|
|
long number = 1;
|
|
var total = Repository.Count();
|
|
number = total + 1;
|
|
return $"AU-{DateTime.UtcNow.Year:0000}{DateTime.UtcNow.Month:00}-{number:0000000000}";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt die Anzahl von App-UserN im System zurück
|
|
/// </summary>
|
|
/// <param name="includeDeleted">Sollen gelöschte inkludiert werden?</param>
|
|
/// <returns>Anzahl App-User</returns>
|
|
public async Task<int> CountAsync(bool includeDeleted)
|
|
{
|
|
if (includeDeleted)
|
|
return await Repository.CountAsync().ConfigureAwait(false);
|
|
else
|
|
return await Repository.CountAsync(c => c.Deleted == false).ConfigureAwait(false);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt die Anzahl der AppUser im System zurück welche einem Typ entsprechen
|
|
/// </summary>
|
|
/// <param name="includeDeleted">Sollen gelöschte inkludiert werden?</param>
|
|
/// <param name="type">Typ der gesucht wird</param>
|
|
/// <param name="lastUpdate">Optional: wenn angegeben, dann muss das Datum des letzten Updates größer oder gleich dem lastUpdate Datum sein</param>
|
|
/// <returns>Anzahl AppUser</returns>
|
|
public async Task<int> CountAsync(bool includeDeleted, AppUserType type, DateTimeOffset? lastUpdate)
|
|
{
|
|
var query = Repository.Query(c => true);
|
|
if (!includeDeleted)
|
|
query = query.Where(c => c.Deleted == false);
|
|
if(type == AppUserType.DogWalker)
|
|
query = query.Where(c => c.Type == AppUserType.DogWalker);
|
|
else if (type == AppUserType.DogOwner)
|
|
query = query.Where(c => c.Type == AppUserType.DogOwner);
|
|
if (lastUpdate.HasValue)
|
|
query = query.Where(c => c.UpdatedAt >= lastUpdate.Value);
|
|
var count = await query.CountAsync().ConfigureAwait(false);
|
|
return count;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Entsperren von gesperrten App-Usern deren LockedUntil-Datum erreicht wurde.
|
|
/// </summary>
|
|
/// <returns>Anzahl entsperrte App-User</returns>
|
|
public async Task<int> AutoUnlockAsync()
|
|
{
|
|
var lockedAppUsers = await Repository.FindAsync(c => c.Locked && c.LockedUntil != null && c.LockedUntil <= DateTimeOffset.UtcNow);
|
|
var appUsers = lockedAppUsers.ToList();
|
|
foreach (var appUser in appUsers)
|
|
{
|
|
appUser.Locked = false;
|
|
appUser.LockedUntil = null;
|
|
appUser.LockedReason = string.Empty;
|
|
}
|
|
|
|
return appUsers.Count();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Liste von App-Usern zurück, die für die automatische Auszahlung in Frage kommen
|
|
/// </summary>
|
|
/// <returns>List App-User</returns>
|
|
public async Task<List<AppUser>> GetForAutoPayoutAsync()
|
|
{
|
|
var users = await Repository.FindAsync(c => c.AutoPayout && !string.IsNullOrWhiteSpace(c.PaymentId) && !string.IsNullOrWhiteSpace(c.BankId) && c.TermsAccepted && c.PrivacyAccepted && c.PaymentTermsAccepted && c.KycPassed && !c.Deleted).ConfigureAwait(false);
|
|
return users.ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt zurück ob ein App-User gesperrt ist
|
|
/// </summary>
|
|
/// <param name="appUserId">Id des App-Users</param>
|
|
/// <returns>true wenn gesperrt, false sonst</returns>
|
|
public async Task<bool> IsLockedAsync(string appUserId)
|
|
{
|
|
var appUser = await Repository.FirstOrDefaultAsync(c => c.Id == appUserId);
|
|
if(appUser != null && appUser.Deleted == false)
|
|
return appUser.Locked;
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine List von AppUsern zurück
|
|
/// </summary>
|
|
/// <param name="skip">skip</param>
|
|
/// <param name="take">take</param>
|
|
/// <param name="type">Typ der gesucht wird</param>
|
|
/// <param name="lastUpdate">Optional: wenn angegeben, dann muss das Datum des letzten Updates größer oder gleich dem lastUpdate Datum sein</param>
|
|
/// <param name="includeDeleted">Sollen gelöschte inkludiert werden?</param>
|
|
/// <returns>Liste AppUser</returns>
|
|
public async Task<List<AppUser>> GetAllAsync(int skip, int take, AppUserType type, DateTimeOffset? lastUpdate, bool includeDeleted)
|
|
{
|
|
var query = Repository.Query(c => true);
|
|
if (!includeDeleted)
|
|
query = query.Where(c => c.Deleted == false);
|
|
if (type == AppUserType.DogWalker)
|
|
query = query.Where(c => c.Type == AppUserType.DogWalker);
|
|
else if (type == AppUserType.DogOwner)
|
|
query = query.Where(c => c.Type == AppUserType.DogOwner);
|
|
if (lastUpdate.HasValue)
|
|
query = query.Where(c => c.UpdatedAt >= lastUpdate.Value);
|
|
|
|
query = query.Skip(skip).Take(take);
|
|
var list = await query.ToListAsync().ConfigureAwait(false);
|
|
return list;
|
|
}
|
|
|
|
|
|
#region Profile
|
|
|
|
/// <summary>
|
|
/// Erstellt ein DogWalker Profil wenn es noch nicht existiert
|
|
/// </summary>
|
|
/// <param name="id">Id des App-Users</param>
|
|
/// <returns>DogWalker Propfil</returns>
|
|
public async Task<DogWalkerProfile> CreateWalkerProfileIfNotExistsAsync(string id)
|
|
{
|
|
var profile = await _profileRepository.GetAsync(id);
|
|
if (profile == null)
|
|
{
|
|
profile = new DogWalkerProfile()
|
|
{
|
|
Id = id,
|
|
UpdatedAt = DateTimeOffset.UtcNow,
|
|
Deleted = false,
|
|
Radius = 10,
|
|
About = string.Empty,
|
|
AllowedRequests = DogWalkRequestType.Walking,
|
|
PuppyAllowed = false,
|
|
DifficultAllowed = false,
|
|
AggressionLevel = DogAggressionLevel.Low,
|
|
AcceptedSize = DogSize.Small,
|
|
PriceWalk = 10,
|
|
PriceDay = 7,
|
|
PriceSitting = 40,
|
|
NotAvailable = false,
|
|
NotAvailableInfo = string.Empty
|
|
};
|
|
_profileRepository.Add(profile);
|
|
}
|
|
|
|
return profile;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt ein Walker-Profil für einen App-User zurück
|
|
/// </summary>
|
|
/// <param name="id">Id des App-Users</param>
|
|
/// <returns>DogWalker Propfil</returns>
|
|
public async Task<DogWalkerProfile> GetWalkerProfileAsync(string id)
|
|
{
|
|
var profile = await _profileRepository.GetAsync(id);
|
|
return profile;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Löschen eines Walker-Profils für einen App-User
|
|
/// </summary>
|
|
/// <param name="id">Id des App-Users</param>
|
|
/// <returns>DogWalker Propfil</returns>
|
|
public async Task RemoveWalkerProfileAsync(string id)
|
|
{
|
|
var profile = await _profileRepository.GetAsync(id);
|
|
if(profile != null)
|
|
_profileRepository.Remove(profile);
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region DogWalkers
|
|
|
|
/// <summary>
|
|
/// Gibt eine Liste Dogwalkern zurück - sortiert nach Distanz
|
|
/// </summary>
|
|
/// <param name="ignoreId">Id des App-Users der ignoriert werden soll, falls ein DogOwner auch Walker ist</param>
|
|
/// <param name="lat">Optional: Breitengrad</param>
|
|
/// <param name="lng">Optional: Längengrad</param>
|
|
/// <param name="take">Wie viele sollen max gewählt werden?</param>
|
|
/// <returns>Liste passender Dogwalker</returns>
|
|
public async Task<List<DogWalker>> GetWalkersForAppAsync(string ignoreId, double lat, double lng, int take)
|
|
{
|
|
//Lokalisierungsmodus --> Andere Abfrage weil wir die Distanz zu den Geokoordinaten brauchen
|
|
var geometryFactory = NtsGeometryServices.Instance.CreateGeometryFactory(srid: 4326);
|
|
var location = geometryFactory.CreatePoint(new Coordinate(lng, lat));
|
|
|
|
var walkersQuery = _walkerRepository.QueryView(c => c.Deleted == false && c.Id != ignoreId);
|
|
walkersQuery = walkersQuery.OrderBy(c => c.Location.Distance(location));
|
|
var walkers = await walkersQuery.Take(take).ToListAsync().ConfigureAwait(false);
|
|
|
|
return walkers;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt einen Dogwalker zurück
|
|
/// </summary>
|
|
/// <param name="dogWalkerId">Id des DogWalkers</param>
|
|
/// <returns>DogWalker oder null wenn nicht gefunden</returns>
|
|
public async Task<DogWalker> GetWalkerAsync(string dogWalkerId)
|
|
{
|
|
var baseQuery = _walkerRepository.QueryView(c => c.Id == dogWalkerId);
|
|
return await baseQuery.FirstOrDefaultAsync().ConfigureAwait(false);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Liste von Dogwalkern zurück
|
|
/// </summary>
|
|
/// <param name="query">Abfrage-Objekt für Dogwalker - Beinhaltet alle Filter und auch Paging</param>
|
|
/// <returns>Liste gefudene Walks</returns>
|
|
public async Task<(int total, List<DogWalker> list)> GetWalkersAsync(DogWalkerQueryDto query)
|
|
{
|
|
var geometryFactory = NtsGeometryServices.Instance.CreateGeometryFactory(srid: 4326);
|
|
var location = geometryFactory.CreatePoint(new Coordinate(query.Location.Lng.Value, query.Location.Lat.Value));
|
|
|
|
//var baseQuery = _walkerRepository.QueryView(c => c.Deleted == false);
|
|
var baseQuery = _walkerRepository.QueryStoredProcedure("SELECT * FROM tv_DogWalkers({0})", c => c.Deleted == false && c.Locked == false, query.IgnoreId);
|
|
|
|
if (!string.IsNullOrWhiteSpace(query.IgnoreId))
|
|
baseQuery = baseQuery.Where(c => c.Id != query.IgnoreId);
|
|
|
|
if (!string.IsNullOrWhiteSpace(query.Name))
|
|
baseQuery = baseQuery.Where(c => c.FirstName.Contains(query.Name) || c.LastName.Contains(query.Name));
|
|
|
|
if (!string.IsNullOrWhiteSpace(query.City))
|
|
baseQuery = baseQuery.Where(c => c.Address_City.Contains(query.City) || c.Address_Zip.Contains(query.City));
|
|
|
|
if (!string.IsNullOrWhiteSpace(query.Country))
|
|
baseQuery = baseQuery.Where(c => c.Address_CountryCode == query.Country.ToUpperInvariant());
|
|
|
|
baseQuery = baseQuery.Where(c => c.RatingStatisticsWalker_AverageRating >= query.MinRating);
|
|
|
|
var total = await baseQuery.CountAsync().ConfigureAwait(false);
|
|
|
|
if (query.DynamicSortOrder.Any())
|
|
{
|
|
var isFirst = true;
|
|
var orderedBaseQuery = baseQuery.SpecialOrderBy(query.DynamicSortOrder.First().Property, (SortDirection)query.DynamicSortOrder.First().SortDirection); ;
|
|
foreach (var sortOrder in query.DynamicSortOrder)
|
|
{
|
|
if (isFirst)
|
|
{
|
|
isFirst = false;
|
|
continue;
|
|
}
|
|
else
|
|
{
|
|
orderedBaseQuery = orderedBaseQuery.SpecialThenBy(sortOrder.Property, (SortDirection)sortOrder.SortDirection);
|
|
}
|
|
}
|
|
var list = await orderedBaseQuery.ThenBy(c => c.Location.Distance(location)).Skip(query.Skip).Take(query.Take).ToListAsync().ConfigureAwait(false);
|
|
return (total, list);
|
|
}
|
|
else
|
|
{
|
|
var list = await baseQuery.OrderBy(c => c.Location.Distance(location)).Skip(query.Skip).Take(query.Take).ToListAsync().ConfigureAwait(false);
|
|
return (total, list);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Liste von Dogwalkern zurück welche über eine Adresse verfügen und deren Standort vom angegebenen Standort nicht weiter als der definierte Radius entfernt sind
|
|
/// </summary>
|
|
/// <param name="ignoreId">Id des Walkers der ignoriert werden soll, falls der hundebesitzer auch Walker ist</param>
|
|
/// <param name="location">Location für die Abholung des Hundes - darf nicht weiter als definierter Radius entfernt sein</param>
|
|
/// <returns>Liste gefundene Walker</returns>
|
|
public async Task<List<DogWalker>> GetWalkersForNotificationAsync(string ignoreId, Point location)
|
|
{
|
|
var baseQuery = _walkerRepository.QueryView(c => c.Deleted == false && c.Id != ignoreId
|
|
&& !string.IsNullOrWhiteSpace(c.Address_AddressLine1)
|
|
&& !string.IsNullOrWhiteSpace(c.Address_Zip)
|
|
&& !string.IsNullOrWhiteSpace(c.Address_City)
|
|
&& c.Location != null);
|
|
|
|
baseQuery = baseQuery.Where(c => c.Location.IsWithinDistance(location, c.Radius * 1000));
|
|
|
|
return await baseQuery.ToListAsync().ConfigureAwait(false);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Systemnachrichten
|
|
|
|
/// <summary>
|
|
/// Abfrage des App-Users für eine Systemnachricht
|
|
/// </summary>
|
|
/// <param name="query">Abfrageparameter</param>
|
|
/// <returns>Liste der App-User</returns>
|
|
public async Task<List<AppUser>> GetForSystemMessageAsync(SystemMessageAppUserQuery query)
|
|
{
|
|
var baseQuery = Repository.QueryView(c => c.Deleted == false);
|
|
|
|
if (query.AppUserType != AppUserType.Both)
|
|
{
|
|
baseQuery = baseQuery.Where(c => c.Type == query.AppUserType || c.Type == AppUserType.Both);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(query.City))
|
|
baseQuery = baseQuery.Where(c => c.Address.City.Contains(query.City));
|
|
|
|
if (!string.IsNullOrWhiteSpace(query.Zip))
|
|
baseQuery = baseQuery.Where(c => c.Address.Zip.Contains(query.Zip));
|
|
|
|
if (!string.IsNullOrWhiteSpace(query.Country))
|
|
baseQuery = baseQuery.Where(c => c.Address.CountryCode == query.Country.ToUpperInvariant());
|
|
|
|
if (!string.IsNullOrWhiteSpace(query.State))
|
|
baseQuery = baseQuery.Where(c => c.Address.State == query.State.ToUpperInvariant());
|
|
|
|
if(query.VerifiedOnly)
|
|
baseQuery = baseQuery.Where(c => c.Verified);
|
|
|
|
if (query.Sex.HasValue && query.Sex.Value != Sex.Undefined)
|
|
baseQuery = baseQuery.Where(c => c.Sex == query.Sex.Value);
|
|
|
|
return await baseQuery.ToListAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt zurück wie viele App-User für eine Systemnachricht gefunden werden
|
|
/// </summary>
|
|
/// <param name="query">Abfrageparameter</param>
|
|
/// <returns>Anzahl der App-User</returns>
|
|
public async Task<int> CountForSystemMessageAsync(SystemMessageAppUserQuery query)
|
|
{
|
|
var baseQuery = Repository.QueryView(c => c.Deleted == false);
|
|
|
|
if (query.AppUserType != AppUserType.Both)
|
|
{
|
|
baseQuery = baseQuery.Where(c => c.Type == query.AppUserType || c.Type == AppUserType.Both);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(query.City))
|
|
baseQuery = baseQuery.Where(c => c.Address.City.Contains(query.City));
|
|
|
|
if (!string.IsNullOrWhiteSpace(query.Zip))
|
|
baseQuery = baseQuery.Where(c => c.Address.Zip.Contains(query.Zip));
|
|
|
|
if (!string.IsNullOrWhiteSpace(query.Country))
|
|
baseQuery = baseQuery.Where(c => c.Address.CountryCode == query.Country.ToUpperInvariant());
|
|
|
|
if (!string.IsNullOrWhiteSpace(query.State))
|
|
baseQuery = baseQuery.Where(c => c.Address.State == query.State.ToUpperInvariant());
|
|
|
|
if (query.VerifiedOnly)
|
|
baseQuery = baseQuery.Where(c => c.Verified);
|
|
|
|
if (query.Sex.HasValue && query.Sex.Value != Sex.Undefined)
|
|
baseQuery = baseQuery.Where(c => c.Sex == query.Sex.Value);
|
|
|
|
return await baseQuery.CountAsync();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Blocks
|
|
|
|
/// <summary>
|
|
/// Erstellen einer Blockierung eines App-Users
|
|
/// </summary>
|
|
/// <returns>AppUserBlock</returns>
|
|
public AppUserBlock CreateBlock()
|
|
{
|
|
var item = new AppUserBlock
|
|
{
|
|
Created = DateTimeOffset.UtcNow
|
|
};
|
|
return item;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Hinzufügen eines Blocks eine App-Users
|
|
/// </summary>
|
|
/// <param name="block">AppUserBlock</param>
|
|
public void AddBlock(AppUserBlock block)
|
|
{
|
|
_blockRepository.Add(block);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Blockiert einen App-User für einen anderen, wenn noch kein Block existiert
|
|
/// </summary>
|
|
/// <param name="blockingAppUserId">Id des App-Users der die Blockierung vornimmt</param>
|
|
/// <param name="blockedAppUserId">Id des App-Users der blockiert wird</param>
|
|
/// <param name="createdBy">Von wem erstellt?</param>
|
|
/// <param name="blockUntil">Optional: Bis wann soll der App-User blockiert werden?</param>
|
|
/// <param name="reportId">Optional: Verweis auf eine Meldung</param>
|
|
/// <returns>Erstellte oder existierende Blockierung</returns>
|
|
public async Task<AppUserBlock> BlockAppUserAsync(string blockingAppUserId, string blockedAppUserId, string createdBy, DateTimeOffset? blockUntil = null, long? reportId = null)
|
|
{
|
|
var block = await _blockRepository.FirstOrDefaultAsync(c => c.BlockingAppUserId == blockingAppUserId && c.BlockedAppUserId == blockedAppUserId).ConfigureAwait(false);
|
|
if (block == null)
|
|
{
|
|
block = CreateBlock();
|
|
block.BlockingAppUserId = blockingAppUserId;
|
|
block.BlockedAppUserId = blockedAppUserId;
|
|
block.CreatedBy = createdBy;
|
|
block.AppUserReportId = reportId;
|
|
block.BlockedUntil = null;
|
|
|
|
AddBlock(block);
|
|
}
|
|
return block;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Aufheben der Blockierung eines App-Users durch einen anderen, wenn vorhanden
|
|
/// </summary>
|
|
/// <param name="blockingAppUserId">Id des App-Users der die Blockierung vornimmt</param>
|
|
/// <param name="blockedAppUserId">Id des App-Users der blockiert wird</param>
|
|
/// <returns>true wenn erfolgreich, false sonst</returns>
|
|
public async Task<bool> UnblockAppUserAsync(string blockingAppUserId, string blockedAppUserId)
|
|
{
|
|
var block = await _blockRepository.FirstOrDefaultAsync(c => c.BlockingAppUserId == blockingAppUserId && c.BlockedAppUserId == blockedAppUserId).ConfigureAwait(false);
|
|
if (block != null)
|
|
{
|
|
RemoveBlock(block);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Löschen einer Blockierung
|
|
/// </summary>
|
|
/// <param name="block">AppUserBlock</param>
|
|
public void RemoveBlock(AppUserBlock block)
|
|
{
|
|
_blockRepository.Remove(block);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Löschen einer Blockierung
|
|
/// </summary>
|
|
/// <param name="id">Id der Blockierung</param>
|
|
public async Task RemoveBlockByIdAsync(long id)
|
|
{
|
|
var block = await _blockRepository.FirstOrDefaultAsync(c => c.Id == id).ConfigureAwait(false);
|
|
if (block != null)
|
|
_blockRepository.Remove(block);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Entfernen von Blocks eines App-Users der blockiert hat
|
|
/// </summary>
|
|
/// <param name="id">Id des AppUsers der blockiert hat</param>
|
|
/// <returns>Task</returns>
|
|
public async Task RemoveBlocksByBlockingUserAsync(string id)
|
|
{
|
|
var blocks = await _blockRepository.FindAsync(c => c.BlockingAppUserId == id).ConfigureAwait(false);
|
|
foreach (var appUserBlock in blocks)
|
|
{
|
|
_blockRepository.Remove(appUserBlock);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Entfernen von Blocks eines App-Users der blockiert wurde
|
|
/// </summary>
|
|
/// <param name="id">Id des AppUsers der blockiert wurde</param>
|
|
/// <returns>Task</returns>
|
|
public async Task RemoveBlocksByBlockedUserAsync(string id)
|
|
{
|
|
var blocks = await _blockRepository.FindAsync(c => c.BlockedAppUserId == id).ConfigureAwait(false);
|
|
foreach (var appUserBlock in blocks)
|
|
{
|
|
_blockRepository.Remove(appUserBlock);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Löschen von abgelaufenen Blockierungen, welche ein Ablaufdatum haben
|
|
/// </summary>
|
|
/// <param name="date">Datum das erreicht werden muss</param>
|
|
/// <returns>Anzahl gelösche Blockierungen</returns>
|
|
public async Task<int> RemoveExpiredBlocksAsync(DateTimeOffset date)
|
|
{
|
|
var blocks = (await _blockRepository.FindAsync(c => c.BlockedUntil.HasValue && c.BlockedUntil.Value <= date).ConfigureAwait(false)).ToList();
|
|
foreach (var appUserBlock in blocks)
|
|
{
|
|
_blockRepository.Remove(appUserBlock);
|
|
}
|
|
return blocks.Count;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt zurück ob ein App-User von einem anderen blockiert wurde
|
|
/// </summary>
|
|
/// <param name="blockingAppUserId">Id des App-Users der blockiert hat</param>
|
|
/// <param name="blockedAppUserId">Id des App-Users der blockiert wurde</param>
|
|
/// <returns>true wenn blockiert, false sonst</returns>
|
|
public async Task<bool> IsBlockedAsync(string blockingAppUserId, string blockedAppUserId)
|
|
{
|
|
return await _blockRepository.FirstOrDefaultAsync(c => c.BlockingAppUserId == blockingAppUserId && c.BlockedAppUserId == blockedAppUserId).ConfigureAwait(false) != null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Liste von blockierten App-Usern für einen App-User zurück
|
|
/// </summary>
|
|
/// <param name="query">Abfrageparameter</param>
|
|
/// <returns>Liste von gesperrten App-Usern</returns>
|
|
public async Task<(int total, List<AppUserBlockWithNames> list)> GetBlockedAppUsersAsync(BlockedQueryDto query)
|
|
{
|
|
var geometryFactory = NtsGeometryServices.Instance.CreateGeometryFactory(srid: 4326);
|
|
var location = geometryFactory.CreatePoint(new Coordinate(query.Location.Lng.Value, query.Location.Lat.Value));
|
|
|
|
//var baseQuery = _walkerRepository.QueryView(c => c.Deleted == false);
|
|
var baseQuery = _blockWithNamesRepository.QueryStoredProcedure("SELECT * FROM tv_AppUserBlocksWithNames({0})", c => c.Deleted == false && c.Locked == false, query.BlockingAppUserId);
|
|
|
|
var total = await baseQuery.CountAsync().ConfigureAwait(false);
|
|
|
|
if (query.DynamicSortOrder.Any())
|
|
{
|
|
var isFirst = true;
|
|
var orderedBaseQuery = baseQuery.SpecialOrderBy(query.DynamicSortOrder.First().Property, (SortDirection)query.DynamicSortOrder.First().SortDirection); ;
|
|
foreach (var sortOrder in query.DynamicSortOrder)
|
|
{
|
|
if (isFirst)
|
|
{
|
|
isFirst = false;
|
|
continue;
|
|
}
|
|
else
|
|
{
|
|
orderedBaseQuery = orderedBaseQuery.SpecialThenBy(sortOrder.Property, (SortDirection)sortOrder.SortDirection);
|
|
}
|
|
}
|
|
var list = await orderedBaseQuery.ThenBy(c => c.Location.Distance(location)).Skip(query.Skip).Take(query.Take).ToListAsync().ConfigureAwait(false);
|
|
return (total, list);
|
|
}
|
|
else
|
|
{
|
|
var list = await baseQuery.OrderBy(c => c.Location.Distance(location)).Skip(query.Skip).Take(query.Take).ToListAsync().ConfigureAwait(false);
|
|
return (total, list);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Liste von AppUsern zurück welche als geöscht markiert sind
|
|
/// </summary>
|
|
/// <returns>Liste AppUser</returns>
|
|
public async Task<List<AppUser>> GetDeletedAsync()
|
|
{
|
|
return (await Repository.FindAsync(c => c.Deleted).ConfigureAwait(false)).ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt eine Liste von AppUsern zurück deren Location nicht valide ist
|
|
/// </summary>
|
|
/// <returns>Liste AppUser</returns>
|
|
public async Task<List<AppUser>> GetWithInvalidLocationAsync()
|
|
{
|
|
var appUsers = await Repository.FindAsync(c => (c.Location == null || c.Location.X == 0 && c.Location.Y == 0) && c.Deleted == false).ConfigureAwait(false);
|
|
return appUsers.ToList();
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|