public async Task <DTO.AddressOutput> RemoveEntityByIdAsync(long id, string userName, string normalizedUserName) { try { await TransactionHandler.BeginTransactionAsync(); var foundAddress = await CheckAccessRightsAndGetAddressAsync(id, userName, normalizedUserName); var removedAddress = await AddressRepository.RemoveEntityAsync(foundAddress); await TransactionHandler.CommitTransactionAsync(); return(Mapper.Map <DTO.AddressOutput>(removedAddress)); } catch { await TransactionHandler.RollbackTransactionAsync(); throw; } finally { await TransactionHandler.DisposeTransactionAsync(); } }
public async Task <DTO.CountryOutput> RemoveEntityByIdAsync(long id) { try { await TransactionHandler.BeginTransactionAsync(); var countryToRemove = await CountryRepository.GetEntityAsync(filter : c => c.Id == id, includes : Includes); if (countryToRemove.Addresses != null && countryToRemove.Addresses.Count > 0) { throw new BadRequestException("This country is used by another address."); } var removedCountry = await CountryRepository.RemoveEntityAsync(countryToRemove); await TransactionHandler.CommitTransactionAsync(); return(Mapper.Map <DTO.CountryOutput>(removedCountry)); } catch { await TransactionHandler.RollbackTransactionAsync(); throw; } finally { await TransactionHandler.DisposeTransactionAsync(); } }
public async Task <DTO.AddressOutput> UpdateEntityAsync(long id, DTO.AddressInput address, string userName, string normalizedUserName) { try { await TransactionHandler.BeginTransactionAsync(); var foundAddress = await CheckAccessRightsAndGetAddressAsync(id, userName, normalizedUserName); var(city, country, state) = await InsertOrGetAddressRelationsAsync(address); foundAddress.City = city; foundAddress.Country = country; foundAddress.State = state; foundAddress.DoorNumber = address.DoorNumber; foundAddress.Street = address.Street; await AddressRepository.UpdateEntityAsync(foundAddress); await TransactionHandler.CommitTransactionAsync(); return(Mapper.Map <DTO.AddressOutput>(foundAddress)); } catch { await TransactionHandler.RollbackTransactionAsync(); throw; } finally { await TransactionHandler.DisposeTransactionAsync(); } }
public async Task <DTO.CountryOutput> UpdateEntityAsync(long id, DTO.CountryInput entity) { try { await TransactionHandler.BeginTransactionAsync(); var foundCountry = await CountryRepository.GetEntityByIdAsync(id); if (foundCountry == null) { throw new NotFoundException("This country does not exist."); } foundCountry.Code = entity.Code; foundCountry.Name = entity.Name; await CountryRepository.UpdateEntityAsync(foundCountry); await TransactionHandler.CommitTransactionAsync(); return(Mapper.Map <DTO.CountryOutput>(foundCountry)); } catch { await TransactionHandler.RollbackTransactionAsync(); throw; } finally { await TransactionHandler.DisposeTransactionAsync(); } }
public async Task <DTO.UnitOutput> RemoveEntityByIdAsync(long id) { try { await TransactionHandler.BeginTransactionAsync(); var unitToRemove = await UnitRepository.GetEntityAsync(filter : u => u.Id == id, includes : Includes); if (unitToRemove.Articles != null && unitToRemove.Articles.Count > 0) { throw new BadRequestException("This unit is used by another article."); } var removedUnit = await UnitRepository.RemoveEntityAsync(unitToRemove); await TransactionHandler.CommitTransactionAsync(); return(Mapper.Map <DTO.UnitOutput>(removedUnit)); } catch { await TransactionHandler.RollbackTransactionAsync(); throw; } finally { await TransactionHandler.DisposeTransactionAsync(); } }
public async Task <DTO.ArticleOutput> InsertEntityAsync(DTO.ArticleInput entity, string userName, string normalizedUserName) { try { await TransactionHandler.BeginTransactionAsync(); var person = new DAO.Person { UserName = userName, NormalizedUserName = normalizedUserName }; var managedPerson = await PersonRepository.InsertOrGetEntityAsync(person); var convertedUnit = Mapper.Map <DAO.Unit>(entity.Unit); var managedUnit = await UnitRepository.InsertOrGetEntityAsync(convertedUnit); var currentDateTime = DateTime.UtcNow; var article = new DAO.Article { Person = managedPerson, Unit = managedUnit, Name = entity.Name, Description = entity.Description, Price = entity.Price, Amount = entity.Amount, Size = entity.Size, CreatedAt = currentDateTime, UpdatedAt = currentDateTime, ExpirationDate = entity.ExpirationDate }; var managedArticle = await ArticleRepository.InsertEntityAsync(article); await TransactionHandler.CommitTransactionAsync(); return(Mapper.Map <DTO.ArticleOutput>(managedArticle)); } catch { await TransactionHandler.RollbackTransactionAsync(); throw; } finally { await TransactionHandler.DisposeTransactionAsync(); } }
public async Task <DTO.AddressOutput> InsertEntityAsync(DTO.AddressInput entity, string userName, string normalizedUserName) { try { await TransactionHandler.BeginTransactionAsync(); var person = new DAO.Person { UserName = userName, NormalizedUserName = normalizedUserName }; var managedPerson = await PersonRepository.InsertOrGetEntityAsync(person); var(city, country, state) = await InsertOrGetAddressRelationsAsync(entity); var address = new DAO.Address { Person = managedPerson, City = city, Country = country, State = state, DoorNumber = entity.DoorNumber, Street = entity.Street }; var managedAddress = await AddressRepository.InsertEntityAsync(address); await TransactionHandler.CommitTransactionAsync(); return(Mapper.Map <DTO.AddressOutput>(managedAddress)); } catch { await TransactionHandler.RollbackTransactionAsync(); throw; } finally { await TransactionHandler.DisposeTransactionAsync(); } }
public async Task <DTO.ArticleOutput> UpdateEntityAsync(long id, DTO.ArticleInput entity, string userName, string normalizedUserName) { try { await TransactionHandler.BeginTransactionAsync(); var foundArticle = await CheckAccessRightsAndGetArticleAsync(id, userName, normalizedUserName); var convertedUnit = Mapper.Map <DAO.Unit>(entity.Unit); var managedUnit = await UnitRepository.InsertOrGetEntityAsync(convertedUnit); foundArticle.Unit = managedUnit; foundArticle.Name = entity.Name; foundArticle.Description = entity.Description; foundArticle.Price = entity.Price; foundArticle.Amount = entity.Amount; foundArticle.Size = entity.Size; foundArticle.UpdatedAt = DateTime.UtcNow; foundArticle.ExpirationDate = entity.ExpirationDate; await ArticleRepository.UpdateEntityAsync(foundArticle); await TransactionHandler.CommitTransactionAsync(); return(Mapper.Map <DTO.ArticleOutput>(foundArticle)); } catch { await TransactionHandler.RollbackTransactionAsync(); throw; } finally { await TransactionHandler.DisposeTransactionAsync(); } }
public async Task <DTO.CountryOutput> InsertEntityAsync(DTO.CountryInput entity) { try { await TransactionHandler.BeginTransactionAsync(); var convertedCountry = Mapper.Map <DAO.Country>(entity); var insertedCountry = await CountryRepository.InsertEntityAsync(convertedCountry); await TransactionHandler.CommitTransactionAsync(); return(Mapper.Map <DTO.CountryOutput>(insertedCountry)); } catch { await TransactionHandler.RollbackTransactionAsync(); throw; } finally { await TransactionHandler.DisposeTransactionAsync(); } }