Taylohtio/IDP/webapi/webapi.Infrastructure.Common/Repositories/UserFlatMappingRepository.cs

90 lines
2.9 KiB
C#

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<UserFlatMapping>, IUserFlatMappingRepository
{
public UserFlatMappingRepository(ILogger log, string storageConnectionString, EventsHandlerFactory eventsFactory) :
base(log, storageConnectionString, eventsFactory)
{ }
public IEnumerable<UserFlatMapping> GetAllUserFlatsMappings(IEnumerable<Guid> flatIds)
{
_log.Information($"Start GetAllUserFlatsMappings");
if (!flatIds?.Any() ?? true) throw new ArgumentNullException(nameof(flatIds));
try
{
return _table.CreateQuery<UserFlatMapping>()
.ToArray()
.Where(d =>
flatIds.Contains(d.FlatId));
}
catch (StorageException e)
{
throw e;
}
}
public IEnumerable<UserFlatMapping> 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<UserFlatMapping>().Any()) return Array.Empty<UserFlatMapping>();
if((flatId ?? Guid.Empty) == Guid.Empty )
{
return _table.CreateQuery<UserFlatMapping>()
.Where(d => d.UserId == userId).ToArray();
}
else
{
return _table.CreateQuery<UserFlatMapping>()
.Where(d => userId.Equals(d.UserId))
.ToArray()
.Where(d => flatId.Equals(d.FlatId))
;
}
}
catch (StorageException e)
{
throw e;
}
}
public IEnumerable<UserFlatMapping> GetUserFlatsMappingsById(IEnumerable<Guid> Ids)
{
_log.Information($"Start GetUserFlatsMappingsById");
if (!Ids?.Any() ?? true) throw new ArgumentNullException(nameof(Ids));
try
{
return _table.CreateQuery<UserFlatMapping>()
.ToArray()
.Where(d =>
Ids.Contains(d.Id));
}
catch (StorageException e)
{
throw e;
}
}
}
}