using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Query;
using xCommons.Extensions;
using xExceptions.Constants;
using xIdentityModels.Extensions;
using xIdentityModels.Models;
using xModels.Base;
using xModels.Dtos;
using xPushService.Base;
using xPushService.Interfaces;
namespace xPushService.Providers
{
///
/// a Provided Service Used for Providing Data Based on Validations of Users ...
///
///
///
///
///
public abstract class XBaseProvided : IXBaseProvided
where TEntity : XBaseEntity
where TDto : XBaseEntityDto
where THub : XBaseDtoHub
{
//
#region Props ...
private readonly string[] permittedRoles;
private readonly IXBaseDtoProvider provider;
#endregion
//
#region Constructor ...
protected XBaseProvided(
IXBaseDtoProvider provider,
string[] permittedRoles = null
)
{
this.provider = provider;
this.permittedRoles = permittedRoles;
}
#endregion
///
/// Check an Items is Owned for User or not ...
///
///
///
///
public abstract bool IsOwned(
TDto item,
XUserClaimsInfoDto userInfo
);
public abstract bool IsOwned(
TEntity item,
XUserClaimsInfoDto userInfo
);
///
/// Check User Has Permission for Manipulate Data or not ...
///
///
///
public bool HasPermision(
XUserClaimsInfoDto userInfo
)
{
//
var result =
!userInfo.IsNullOrDefault() &&
userInfo.HasAccess(permittedRoles);
//
return result;
}
//
#region Provided Actions ...
///
/// Get Specified Item by ID ...
///
///
///
///
///
public virtual async Task Get(
TKey id,
Func, IIncludableQueryable> includeBuilder = null,
CancellationToken cancellationToken = default
)
{
//
var result = await provider.GetAsync(
id: id,
includeBuilder: null,
ignoreSoftDeleteds: true,
cancellationToken: cancellationToken
);
//
return result;
}
///
/// Get All Exists Items ...
///
///
///
///
///
public virtual async Task> GetAll(
Func, IOrderedQueryable> orderBuilder = null,
Func, IIncludableQueryable> includeBuilder = null,
CancellationToken cancellationToken = default
)
{
//
var result = await provider.GetAllAsync(
ignoreSoftDeleteds: true,
orderBuilder: orderBuilder,
includeBuilder: includeBuilder,
cancellationToken: cancellationToken
);
//
return result;
}
///
/// find an item by providing a Conditional Expression ...
///
///
///
///
///
public virtual async Task FindOne(
Expression> predicate,
Func, IIncludableQueryable> includeBuilder = null,
CancellationToken cancellationToken = default
)
{
//
var result = await provider.FindOneAsync(
predicate: predicate,
ignoreSoftDeleteds: true,
includeBuilder: includeBuilder,
cancellationToken: cancellationToken
);
//
return result;
}
///
/// find a collection of Items by proving a Conditional Expression ...
///
///
///
///
///
///
public virtual async Task> FindMany(
Expression> predicate,
Func, IOrderedQueryable> orderBuilder = null,
Func, IIncludableQueryable> includeBuilder = null,
CancellationToken cancellationToken = default
)
{
//
var result = await provider.FindManyAsync(
predicate: predicate,
ignoreSoftDeleteds: true,
orderBuilder: orderBuilder,
includeBuilder: includeBuilder,
cancellationToken: cancellationToken
);
//
return result;
}
///
/// retrieve Items based on XQuery Pagination structure ...
///
///
///
///
///
///
///
public virtual async Task> Query(
XQuery query,
Expression> predicate = null,
Func, IOrderedQueryable> orderBuilder = null,
Func, IIncludableQueryable> includeBuilder = null,
CancellationToken cancellationToken = default
)
{
//
var result = await provider.QueryAsync(
query: query,
predicate: predicate,
ignoreSoftDeleteds: true,
orderBuilder: orderBuilder,
includeBuilder: includeBuilder,
cancellationToken: cancellationToken
);
//
return result;
}
///
/// retrieve Owned Items based on XQuery Pagination structure ...
/// if supported ...
///
///
///
///
///
///
///
///
public virtual async Task> QueryOwned(
XQuery query,
XUserClaimsInfoDto userInfo = null,
Expression> predicate = null,
Func, IOrderedQueryable> orderBuilder = null,
Func, IIncludableQueryable> includeBuilder = null,
CancellationToken cancellationToken = default
)
{
//
Expression> predicateor = null;
if (!predicate.IsNull())
{
//
var predicateFnc = predicate.Compile();
predicateor = x =>
predicateFnc(x) &&
IsOwned(x, userInfo);
}
else
{
predicateor = x => IsOwned(x, userInfo);
}
//
var result = await provider.QueryAsync(
query: query,
predicate: predicateor,
ignoreSoftDeleteds: true,
orderBuilder: orderBuilder,
includeBuilder: includeBuilder,
cancellationToken: cancellationToken
);
//
return result;
}
///
/// Add an item values ...
///
///
///
///
///
///
public virtual async Task Add(
TDto item,
XUserClaimsInfoDto userInfo = null,
string connectionId = null,
CancellationToken cancellationToken = default
)
{
//
var result = await provider.AddAsync(
item: item,
saveChanges: true,
connectionId: connectionId,
cancellationToken: cancellationToken
);
//
return result;
}
///
/// Update an item values ...
///
///
///
///
///
///
///
public virtual async Task Update(
TKey id,
TDto item,
XUserClaimsInfoDto userInfo = null,
string connectionId = null,
CancellationToken cancellationToken = default
)
{
//
// Check Permission ...
var hasPermission =
IsOwned(item, userInfo) ||
HasPermision(userInfo);
if (!hasPermission)
{
XException.NotAllowed.Throw();
}
//
var result = await provider.UpdateAsync(
id: id,
item: item,
saveChanges: true,
connectionId: connectionId,
cancellationToken: cancellationToken
);
//
return result;
}
///
/// remove an Item ...
///
///
///
///
///
///
public virtual async Task Remove(
TKey id,
XUserClaimsInfoDto userInfo = null,
string connectionId = null,
CancellationToken cancellationToken = default
)
{
//
// Check Permission ...
var item = await Get(
id: id,
cancellationToken: cancellationToken
);
var hasPermission =
IsOwned(item, userInfo) ||
HasPermision(userInfo);
if (!hasPermission)
{
XException.NotAllowed.Throw();
}
//
var result = await provider.RemoveAsync(
id: id,
saveChanges: true,
softDelete: false,
connectionId: connectionId,
cancellationToken: cancellationToken
);
//
return result;
}
///
/// count all exists Items ...
///
///
///
public virtual async Task Count(
CancellationToken cancellationToken = default
)
{
//
var result = await provider.CountAsync(
ignoreSoftDeleteds: true,
cancellationToken: cancellationToken
);
//
return result;
}
///
/// Check an Item exists or not ...
///
///
///
///
public virtual async Task IsExists(
TKey id,
CancellationToken cancellationToken = default
)
{
//
var result = await provider.IsExistsAsync(
id: id,
cancellationToken: cancellationToken
);
//
return result;
}
#endregion
}
}