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, IUsersFlatApprovalRepository { public UsersFlatApprovalRepository(ILogger log, string storageConnectionString, EventsHandlerFactory eventsFactory) : base(log, storageConnectionString, eventsFactory) { } public IEnumerable GetAllApproval(IEnumerable flatsId) { _log.Information($"Start GetApproval"); if (!flatsId?.Any() ?? true) throw new ArgumentNullException(nameof(flatsId)); try { return _table.CreateQuery() .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() .Where(d => userId.Equals(d.UserId) && flatId.Equals(d.FlatId)) .FirstOrDefault(); } catch (StorageException e) { throw e; } } public IEnumerable GetApprovalsById(IEnumerable ids) { _log.Information($"Start GetApprovalsById"); if (ids == null || !ids.Any()) throw new ArgumentNullException(nameof(ids)); try { return _table.CreateQuery() .ToArray() .Where(d => ids.Contains(d.Id)); } catch (StorageException e) { throw e; } } public IEnumerable GetApprovedApprovals(IEnumerable flatsId, Guid? userId) { _log.Information($"Start GetApprovedApprovals"); try { return _table.CreateQuery() .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 GetPendingApprovals(IEnumerable flatsId, Guid? userId) { _log.Information($"Start GetPendingApprovals"); try { return _table.CreateQuery() .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 GetPendingApprovals(IEnumerable requestIds) { _log.Information($"Start GetPendingApprovals"); if (requestIds == null) throw new ArgumentNullException(nameof(requestIds)); try { return _table.CreateQuery() //.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; } } } }