public async Task <int> CreateUpdateInsuranceAsync(InsuranceCRUDViewModel model) { var strategy = _insuranceRepository.Database.CreateExecutionStrategy(); await strategy.ExecuteAsync(async() => { using (var transaction = _insuranceRepository.Database.BeginTransaction()) { if (model.Id != null) { var insurance = await _insuranceRepository.GetByIdAsync(model.Id.Value); if (insurance == null) { throw new Exception("Insurance Not Found"); } insurance.Name = model.Name; insurance.Name_Ar = model.Name_Ar; insurance.Name_Ku = model.Name_Ku; insurance.Description = model.Description; insurance.Description_Ar = model.Description_Ar; insurance.Description_Ku = model.Description_Ku; _insuranceRepository.Update(insurance); if (model.Image != null) { var(newName, thumbName, dirPath, baseUrl) = _uploadService.GenerateInsuranceLogoName(insurance.Id, model.Image); insurance.Logo = $"{baseUrl}/{newName}"; _insuranceRepository.Update(insurance); await _uploadService.UploadInsuranceLogoAsync(model.Image, dirPath, newName, thumbName); } } else { var insurance = new Insurance { Name = model.Name, Name_Ar = model.Name_Ar, Name_Ku = model.Name_Ku, Description = model.Description, Description_Ar = model.Description_Ar, Description_Ku = model.Description_Ku, CreatedAt = DateTime.Now, }; await _insuranceRepository.InsertAsync(insurance); if (model.Image != null) { var(newName, thumbName, dirPath, baseUrl) = _uploadService.GenerateInsuranceLogoName(insurance.Id, model.Image); insurance.Logo = $"{baseUrl}/{newName}"; _insuranceRepository.Update(insurance); await _uploadService.UploadInsuranceLogoAsync(model.Image, dirPath, newName, thumbName); } model.Id = insurance.Id; } transaction.Commit(); } }); return(model.Id.Value); }