228 lines
9.0 KiB
C#
228 lines
9.0 KiB
C#
using Microsoft.Azure.CosmosDB.Table;
|
|
using Microsoft.Azure.Storage;
|
|
using Serilog;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using webapi.Domain.AggregatesModel.FlatMappingAggregate;
|
|
using webapi.Domain.Events;
|
|
using webapi.Infrastructure.Repositories;
|
|
|
|
namespace webapi.Infractructure.Repositories
|
|
{
|
|
public class CondoMappingRepository : BaseAzureRepository<CondoMapping>, ICondoMappingRepository
|
|
{
|
|
public CondoMappingRepository(ILogger log, string storageConnectionString, EventsHandlerFactory eventsFactory = null) :
|
|
base(log, storageConnectionString, eventsFactory)
|
|
{}
|
|
|
|
public async Task<CondoMapping> CreateMappingAsync(CondoMapping entity)
|
|
{
|
|
_log.Information($"Start CreateMappingAsync");
|
|
if (entity == null)
|
|
{
|
|
_log.Error($"{nameof(entity)} is null");
|
|
throw new ArgumentNullException(nameof(entity));
|
|
}
|
|
try
|
|
{
|
|
// Create the InsertOrReplace table operation
|
|
TableOperation insertOrMergeOperation = TableOperation.InsertOrMerge(entity);
|
|
|
|
_log.Information($"Execute the operation.");
|
|
TableResult result = await _table.ExecuteAsync(insertOrMergeOperation);
|
|
_log.Information($"End CreateMappingAsync");
|
|
return result.Result as CondoMapping;
|
|
}
|
|
catch (StorageException e)
|
|
{
|
|
_log.Error($"Error CreateMappingAsync StorageException {e.Message} {e.StackTrace} {e.InnerException}", e.Message);
|
|
throw e;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
_log.Error($"Error CreateMappingAsync Exception {e.Message} {e.StackTrace} {e.InnerException}", e.Message);
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
public CondoMapping GetMapping(Guid taloyhtioPMCId, Guid taloyhtioCondoId, string pmsCondoName) {
|
|
|
|
_log.Information($"Start GetMappingAsync");
|
|
if (string.IsNullOrEmpty(pmsCondoName)) throw new ArgumentNullException(nameof(pmsCondoName));
|
|
if (Guid.Empty.Equals(taloyhtioPMCId)) throw new ArgumentNullException(nameof(taloyhtioPMCId));
|
|
if (Guid.Empty.Equals(taloyhtioCondoId)) throw new ArgumentNullException(nameof(taloyhtioCondoId));
|
|
|
|
try
|
|
{
|
|
var query = _table.CreateQuery<CondoMapping>()
|
|
.Where(d => d.PMSCondoName == pmsCondoName
|
|
&& d.TaloyhtioPMCId == taloyhtioPMCId
|
|
&& d.TaloyhtioCondoId == taloyhtioCondoId);
|
|
|
|
_log.Information($"End GetMappingAsync");
|
|
return query.FirstOrDefault();
|
|
}
|
|
catch (StorageException e)
|
|
{
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
public CondoMapping GetMappingById(Guid condoMappingId)
|
|
{
|
|
_log.Information($"Start GetMappingAsync");
|
|
if (Guid.Empty.Equals(condoMappingId)) throw new ArgumentNullException(nameof(condoMappingId));
|
|
|
|
try
|
|
{
|
|
var query = _table.CreateQuery<CondoMapping>()
|
|
.Where(d => d.Id.Equals(condoMappingId));
|
|
_log.Information($"End GetMappingAsync");
|
|
return query.FirstOrDefault();
|
|
}
|
|
catch (StorageException e)
|
|
{
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
public async Task<CondoMapping> EditMappingAsync(CondoMapping condoMapping) {
|
|
|
|
if (condoMapping == null)
|
|
{
|
|
_log.Error($"{nameof(condoMapping)} is null");
|
|
throw new ArgumentNullException(nameof(condoMapping));
|
|
}
|
|
try
|
|
{
|
|
_log.Information($"Start EditMappingAsync");
|
|
TableOperation insertOrMergeOperation = TableOperation.InsertOrMerge(condoMapping);
|
|
_log.Information($"Execute the operation.");
|
|
TableResult result = await _table.ExecuteAsync(insertOrMergeOperation);
|
|
_log.Information($"End EditMappingAsync");
|
|
// Get the request units consumed by the current operation. RequestCharge of a TableResult is only applied to Azure Cosmos DB
|
|
//if (result.RequestCharge.HasValue)
|
|
//{
|
|
// Console.WriteLine("Request Charge of InsertOrMerge Operation: " + result.RequestCharge);
|
|
//}
|
|
return result.Result as CondoMapping;
|
|
}
|
|
catch (StorageException e)
|
|
{
|
|
_log.Error($"Error EditMappingAsync StorageException {e.Message} {e.StackTrace} {e.InnerException}", e.Message);
|
|
throw e;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
_log.Error($"Error EditMappingAsync Exception {e.Message} {e.StackTrace} {e.InnerException}", e.Message);
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
//public async Task DeleteCondoMappingAsync(CondoMapping condoMapping) {
|
|
// try
|
|
// {
|
|
// if (condoMapping == null) throw new ArgumentNullException(nameof(condoMapping));
|
|
|
|
// TableOperation deleteOperation = TableOperation.Delete(condoMapping);
|
|
// TableResult result = await _table.ExecuteAsync(deleteOperation);
|
|
|
|
// // Get the request units consumed by the current operation. RequestCharge of a TableResult is only applied to Azure CosmoS DB
|
|
// //if (result.RequestCharge.HasValue)
|
|
// //{
|
|
// // Console.WriteLine("Request Charge of Delete Operation: " + result.RequestCharge);
|
|
// //}
|
|
|
|
// }
|
|
// catch (StorageException e)
|
|
// {
|
|
// //Console.WriteLine(e.Message);
|
|
// //Console.ReadLine();
|
|
// throw e;
|
|
// }
|
|
//}
|
|
|
|
public IEnumerable<CondoMapping> GetMappings(IEnumerable<string> pmsCondosName = null)
|
|
{
|
|
_log.Information($"Start GetMappings");
|
|
try
|
|
{
|
|
var allCondos = _table.CreateQuery<CondoMapping>()
|
|
.ToArray();
|
|
|
|
if (pmsCondosName != null) {
|
|
return allCondos
|
|
.Where(x => pmsCondosName.Contains(x.PMSCondoName));
|
|
}
|
|
return allCondos;
|
|
}
|
|
catch (StorageException e)
|
|
{
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
public CondoMapping GetMappingByTaloyhtioCondoId(Guid taloyhtioPMCId, Guid taloyhtioCondoId)
|
|
{
|
|
_log.Information($"Start GetMappingByTaloyhtioCondoId");
|
|
if (Guid.Empty.Equals(taloyhtioPMCId)) throw new ArgumentNullException(nameof(taloyhtioPMCId));
|
|
if (Guid.Empty.Equals(taloyhtioCondoId)) throw new ArgumentNullException(nameof(taloyhtioCondoId));
|
|
|
|
try
|
|
{
|
|
var query = _table.CreateQuery<CondoMapping>()
|
|
.Where(d => d.TaloyhtioCondoId == taloyhtioCondoId
|
|
&& d.TaloyhtioPMCId == taloyhtioPMCId);
|
|
|
|
_log.Information($"End GetMappingByTaloyhtioCondoId");
|
|
return query.FirstOrDefault();
|
|
}
|
|
catch (StorageException e)
|
|
{
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
public CondoMapping GetMappingByPMSCondoName(Guid taloyhtioPMCId, string condoPMS)
|
|
{
|
|
_log.Information($"Start GetMappingByPMSCondoName {taloyhtioPMCId}, {condoPMS}");
|
|
if (Guid.Empty.Equals(taloyhtioPMCId)) throw new ArgumentNullException(nameof(taloyhtioPMCId));
|
|
if (string.IsNullOrEmpty(condoPMS)) throw new ArgumentNullException(nameof(condoPMS));
|
|
|
|
try
|
|
{
|
|
var query = _table.CreateQuery<CondoMapping>()
|
|
.Where(d => taloyhtioPMCId.Equals(d.TaloyhtioPMCId) && condoPMS.Equals(d.PMSCondoName));
|
|
|
|
_log.Information($"End GetMappingByPMSCondoName");
|
|
return query.FirstOrDefault();
|
|
}
|
|
catch (StorageException e)
|
|
{
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
public string GetPMSCondoNameByCondoId(Guid taloyhtioPMCId, Guid taloyhtioCondoId)
|
|
{
|
|
_log.Information($"Start GetPMSCondoNameByCondoId {taloyhtioPMCId}, {taloyhtioCondoId}");
|
|
if (Guid.Empty.Equals(taloyhtioPMCId)) throw new ArgumentNullException(nameof(taloyhtioPMCId));
|
|
if (Guid.Empty.Equals(taloyhtioCondoId)) throw new ArgumentNullException(nameof(taloyhtioCondoId));
|
|
|
|
try
|
|
{
|
|
var query = _table.CreateQuery<CondoMapping>()
|
|
.Where(d => taloyhtioPMCId.Equals(d.TaloyhtioPMCId) && taloyhtioCondoId.Equals(d.TaloyhtioCondoId));
|
|
|
|
_log.Information($"End GetPMSCondoNameByCondoId");
|
|
return query.FirstOrDefault()?.PMSCondoName;
|
|
}
|
|
catch (StorageException e)
|
|
{
|
|
throw e;
|
|
}
|
|
}
|
|
}
|
|
} |