using Microsoft.Azure.Storage; using Serilog; using System; using System.Collections.Generic; using System.Linq; using webapi.Domain.AggregatesModel.FlatAggregate; using webapi.Domain.Events; using webapi.Infrastructure.Repositories; namespace webapi.Infractructure.Repositories { public class MaintenanceFeeRepository : BaseAzureRepository, IMaintenanceFeeRepository { public MaintenanceFeeRepository(ILogger log, string storageConnectionString, EventsHandlerFactory eventsFactory) : base(log, storageConnectionString, eventsFactory) { } public IEnumerable GetMaintenanceFees(Guid pmcId, string pmsCondoName, Guid flatId, string pmsUserName) { _log.Information($"Start GetMaintenanceFees"); if (Guid.Empty.Equals(pmcId)) throw new ArgumentNullException(nameof(pmcId)); if (string.IsNullOrEmpty(pmsCondoName)) throw new ArgumentNullException(nameof(pmsCondoName)); if (string.IsNullOrEmpty(pmsUserName)) throw new ArgumentNullException(nameof(pmsUserName)); Func func = (fee) => { if (fee.PMSCondoName == pmsCondoName && fee.FlatId == flatId) { var pmsUserNameParts = pmsUserName.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); var pmsUserNamePart1 = pmsUserNameParts.Length > 0 ? pmsUserNameParts[0].Trim() : null; var pmsUserNamePart2 = pmsUserNameParts.Length > 1 ? pmsUserNameParts[1].Trim() : null; var feeUserNameParts = fee.Payer.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); var feeUserNamePart1 = feeUserNameParts.Length > 0 ? feeUserNameParts[0].Trim() : null; var feeUserNamePart2 = feeUserNameParts.Length > 1 ? feeUserNameParts[1].Trim() : null; return ( (pmsUserNamePart1.Equals(feeUserNamePart1, StringComparison.InvariantCultureIgnoreCase) && pmsUserNamePart2.Equals(feeUserNamePart2, StringComparison.InvariantCultureIgnoreCase)) || (pmsUserNamePart1.Equals(feeUserNamePart2, StringComparison.InvariantCultureIgnoreCase) && pmsUserNamePart2.Equals(feeUserNamePart1, StringComparison.InvariantCultureIgnoreCase)) ); } return false; }; try { return _table.CreateQuery() .Where(x => x.PMCTaloyhtioId == pmcId) .ToArray() .Where(func) .GroupBy(x => x.PeriodOfTime) .OrderByDescending(x =>x.Key) .FirstOrDefault(); } catch (StorageException e) { throw e; } } public IEnumerable GetAllMaintenanceFeesByFlatId(Guid flatId) { try { _log.Information($"Start GetAllMaintenanceFeesByFlatId"); return _table.CreateQuery() .Where(x => x.FlatId == flatId) .ToArray(); } catch (StorageException e) { throw e; } } } }