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.FlatAggregate; using webapi.Domain.Events; using webapi.Infrastructure.Repositories; namespace webapi.Infractructure.Repositories { public class FlatRepository : BaseAzureRepository, IFlatRepository { public FlatRepository(ILogger log, string storageConnectionString, EventsHandlerFactory eventsFactory = null) : base(log, storageConnectionString, eventsFactory) {} public async Task CreateFlatAsync(Flat flat) { _log.Information($"Start CreateFlatAsync"); if (flat == null) throw new ArgumentNullException(nameof(flat)); return await SaveChanges(flat); //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 CreateFlatAsync"); // return result.Result as Flat; //} //catch (StorageException e) //{ // _log.Error($"Error CreateFlatAsync StorageException {e.Message} {e.StackTrace} {e.InnerException}", e.Message); // throw e; //} //catch (Exception e) //{ // _log.Error($"Error CreateFlatAsync Exception {e.Message} {e.StackTrace} {e.InnerException}", e.Message); // throw e; //} } public Flat GetFlat(Guid taloyhtioPMCId, string pmsCondoName, string flatTitle) { _log.Information($"Start GetFlatAsync"); if (Guid.Empty.Equals(taloyhtioPMCId)) throw new ArgumentNullException(nameof(taloyhtioPMCId)); if (string.IsNullOrEmpty(pmsCondoName)) throw new ArgumentNullException(nameof(pmsCondoName)); if (string.IsNullOrEmpty(flatTitle)) throw new ArgumentNullException(nameof(flatTitle)); try { var query = _table.CreateQuery() .Where(d => d.FlatTitle == flatTitle && d.PMCTaloyhtioId == taloyhtioPMCId && d.CondoPMS == pmsCondoName); _log.Information($"End GetFlatAsync"); return query.FirstOrDefault(); } catch (StorageException e) { throw e; } } public Flat GetFlatById(Guid flatId) { _log.Information($"Start GetFlatById"); if (Guid.Empty.Equals(flatId)) throw new ArgumentNullException(nameof(flatId)); try { var query = _table.CreateQuery() .Where(d => d.Id == flatId); _log.Information($"End GetFlatById"); return query.FirstOrDefault(); } catch (StorageException e) { throw e; } } public IEnumerable GetFlats(IEnumerable flatIds) { _log.Information($"Start GetFlats"); try { return _table.CreateQuery() .ToArray() .Where(d => flatIds.Contains(d.Id)).ToArray(); } catch (StorageException e) { throw e; } } public IEnumerable GetFlats(Guid taloyhtioPMCId, string pmsCondoName) { _log.Information($"Start GetFlats"); if (Guid.Empty.Equals(taloyhtioPMCId)) throw new ArgumentNullException(nameof(taloyhtioPMCId)); if (string.IsNullOrEmpty(pmsCondoName)) throw new ArgumentNullException(nameof(pmsCondoName)); try { var query = _table.CreateQuery() .Where(d => d.PMCTaloyhtioId == taloyhtioPMCId && d.CondoPMS == pmsCondoName) .ToArray() .OrderBy(o => o.FlatTitle); _log.Information($"End GetFlats"); return query.ToArray(); } catch (StorageException e) { throw e; } } public async Task EditFlatAsync(Flat flat) { if (flat == null) { _log.Error($"{nameof(flat)} is null"); throw new ArgumentNullException(nameof(flat)); } return await SaveChanges(flat); //try //{ // _log.Information($"Start EditFlatAsync"); // TableOperation insertOrMergeOperation = TableOperation.InsertOrMerge(flat); // _log.Information($"Execute the operation."); // TableResult result = await _table.ExecuteAsync(insertOrMergeOperation); // _log.Information($"End EditFlatAsync"); // // 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 Flat; //} //catch (StorageException e) //{ // _log.Error($"Error EditFlatAsync StorageException {e.Message} {e.StackTrace} {e.InnerException}", e.Message); // throw e; //} //catch (Exception e) //{ // _log.Error($"Error EditFlatAsync Exception {e.Message} {e.StackTrace} {e.InnerException}", e.Message); // throw e; //} } //public async Task DeleteFlatAsync(Flat flat) { // try // { // if (flat == null) throw new ArgumentNullException(nameof(flat)); // TableOperation deleteOperation = TableOperation.Delete(flat); // 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 GetFlatWithUserInfo(Guid pmcId) { _log.Information($"Start GetFlatWithUserInfo"); try { return _table.CreateQuery() //.ToArray() .Where(d => d.FlatUsers != "" && d.PMCTaloyhtioId== pmcId) .ToArray(); } catch (StorageException e) { throw e; } } public IEnumerable GetAllPMSCondoNames(Guid taloyhtioPMCId) { _log.Information($"Start GetFlats"); if (Guid.Empty.Equals(taloyhtioPMCId)) throw new ArgumentNullException(nameof(taloyhtioPMCId)); try { var query = _table.CreateQuery() .Where(d => d.PMCTaloyhtioId == taloyhtioPMCId); _log.Information($"End GetFlats"); return query.Select(x => x.CondoPMS) .ToArray() .Distinct(); } catch (StorageException e) { throw e; } } } }