public async Task <Function> UpdateAsync(FunctionSubmit functionSubmit, Guid updatedByGuid) { // Start transactions to allow complete rollback in case of an error BeginAllTransactions(); try { var function = await functionRepository.GetByIdAsync(functionSubmit.Uuid); if (function == null) { throw new ItemNotFoundException($"Function {functionSubmit.Uuid} not found!"); } if (function.Name != functionSubmit.Name) { // Confirm the new name is available var checkExistingNameModel = await functionRepository.GetByNameAsync(functionSubmit.Name); if (checkExistingNameModel != null) { throw new ItemNotProcessableException($"Function with name '{functionSubmit.Name}' already exists."); } } function.Name = functionSubmit.Name; function.Description = functionSubmit.Description; function.FunctionPermissions = new List <FunctionPermissionModel>(); function.ChangedBy = updatedByGuid; await CheckForApplicationAndAssignToFunctionIfExists(function, functionSubmit); await CheckThatPermissionsExistAndAssignToFunction(function, functionSubmit); // All successful CommitAllTransactions(); return(mapper.Map <Function>(await functionRepository.UpdateAsync(function))); } catch (Exception ex) { logger.Error(ex); RollbackAllTransactions(); throw; } }