Taylohtio/IDP/webapi/webapi.Infrastructure/Repositories/UsersFlatApprovalRepository.cs

133 lines
4.5 KiB
C#

using Microsoft.Azure.Storage;
using Org.BouncyCastle.Math.EC.Rfc7748;
using Serilog;
using System;
using System.Collections.Generic;
using System.Linq;
using webapi.Domain.AggregatesModel.FlatApprovalAggregate;
using webapi.Domain.AggregatesModel.UsersFlatApprovalRequestAggregate;
using webapi.Domain.Events;
using webapi.Infrastructure.Repositories;
namespace webapi.Infractructure.Repositories
{
public class UsersFlatApprovalRepository : BaseAzureRepository<UsersFlatApprovalRequest>, IUsersFlatApprovalRepository
{
public UsersFlatApprovalRepository(ILogger log, string storageConnectionString, EventsHandlerFactory eventsFactory) :
base(log, storageConnectionString, eventsFactory)
{ }
public IEnumerable<UsersFlatApprovalRequest> GetAllApproval(IEnumerable<Guid> flatsId)
{
_log.Information($"Start GetApproval");
if (!flatsId?.Any() ?? true) throw new ArgumentNullException(nameof(flatsId));
try
{
return _table.CreateQuery<UsersFlatApprovalRequest>()
.ToArray()
.Where(d => flatsId.Contains(d.FlatId));
}
catch (StorageException e)
{
throw e;
}
}
public UsersFlatApprovalRequest GetApproval(Guid userId, Guid flatId)
{
_log.Information($"Start GetApproval");
if (Guid.Empty.Equals(userId)) throw new ArgumentNullException(nameof(userId));
if (Guid.Empty.Equals(flatId)) throw new ArgumentNullException(nameof(flatId));
try
{
return _table.CreateQuery<UsersFlatApprovalRequest>()
.Where(d => userId.Equals(d.UserId) && flatId.Equals(d.FlatId))
.FirstOrDefault();
}
catch (StorageException e)
{
throw e;
}
}
public IEnumerable<UsersFlatApprovalRequest> GetApprovalsById(IEnumerable<Guid> ids)
{
_log.Information($"Start GetApprovalsById");
if (ids == null || !ids.Any()) throw new ArgumentNullException(nameof(ids));
try
{
return _table.CreateQuery<UsersFlatApprovalRequest>()
.ToArray()
.Where(d => ids.Contains(d.Id));
}
catch (StorageException e)
{
throw e;
}
}
public IEnumerable<UsersFlatApprovalRequest> GetApprovedApprovals(IEnumerable<Guid> flatsId, Guid? userId)
{
_log.Information($"Start GetApprovedApprovals");
try
{
return _table.CreateQuery<UsersFlatApprovalRequest>()
.Where(d => d.Status == ApprovalStatus.Approved.Name)
.ToArray()
.Where((x => (!userId.HasValue || userId == x.UserId) &&
(!flatsId.Any() || flatsId.Contains(x.FlatId))));
}
catch (StorageException e)
{
throw e;
}
}
public IEnumerable<UsersFlatApprovalRequest> GetPendingApprovals(IEnumerable<Guid> flatsId, Guid? userId)
{
_log.Information($"Start GetPendingApprovals");
try
{
return _table.CreateQuery<UsersFlatApprovalRequest>()
.Where(d => d.Status == ApprovalStatus.Pending.Name) //userId.Equals(d.UserId) && flatId.Equals(d.FlatId))
.ToArray()
.Where((x => (!userId.HasValue || userId == x.UserId) &&
(!flatsId.Any() || flatsId.Contains(x.FlatId))));
}
catch (StorageException e)
{
throw e;
}
}
public IEnumerable<UsersFlatApprovalRequest> GetPendingApprovals(IEnumerable<Guid> requestIds)
{
_log.Information($"Start GetPendingApprovals");
if (requestIds == null) throw new ArgumentNullException(nameof(requestIds));
try
{
return _table.CreateQuery<UsersFlatApprovalRequest>()
//.Where(d => d.Status == ApprovalStatus.Pending) //userId.Equals(d.UserId) && flatId.Equals(d.FlatId))
.ToArray()
.Where(d => requestIds.Contains(d.Id));
}
catch (StorageException e)
{
throw e;
}
}
}
}