using Microsoft.Azure.CosmosDB.Table; using Microsoft.Azure.Storage; using Serilog; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using webapi.Domain.AggregatesModel.UserFlatMappingAggregate; using webapi.Domain.Events; using webapi.Infractructure.Core; using webapi.Infrastructure.Repositories; namespace webapi.Infractructure.Repositories { public class UserFlatMappingRepository: BaseAzureRepository, IUserFlatMappingRepository { public UserFlatMappingRepository(ILogger log, string storageConnectionString, EventsHandlerFactory eventsFactory) : base(log, storageConnectionString, eventsFactory) { } public IEnumerable GetAllUserFlatsMappings(IEnumerable flatIds) { _log.Information($"Start GetAllUserFlatsMappings"); if (!flatIds?.Any() ?? true) throw new ArgumentNullException(nameof(flatIds)); try { return _table.CreateQuery() .ToArray() .Where(d => flatIds.Contains(d.FlatId)); } catch (StorageException e) { throw e; } } public IEnumerable GetUserFlatsMappings(Guid userId, Guid? flatId = null) { _log.Information($"Start GetUserFlatsMappings"); if (Guid.Empty.Equals(userId)) throw new ArgumentNullException(nameof(userId)); try { //_table.CreateIfNotExists(); //if (!_table.Exists() || !_table.CreateQuery().Any()) return Array.Empty(); if((flatId ?? Guid.Empty) == Guid.Empty ) { return _table.CreateQuery() .Where(d => d.UserId == userId).ToArray(); } else { return _table.CreateQuery() .Where(d => userId.Equals(d.UserId)) .ToArray() .Where(d => flatId.Equals(d.FlatId)) ; } } catch (StorageException e) { throw e; } } public IEnumerable GetUserFlatsMappingsById(IEnumerable Ids) { _log.Information($"Start GetUserFlatsMappingsById"); if (!Ids?.Any() ?? true) throw new ArgumentNullException(nameof(Ids)); try { return _table.CreateQuery() .ToArray() .Where(d => Ids.Contains(d.Id)); } catch (StorageException e) { throw e; } } } }