public async Task <IActionResult> CreateUpdate(OfferCreateUpdateViewModel model) { try { if (!CurrentPolyclinic.ServiceSupplyIds.Contains(model.ServiceSupplyId)) { throw new AwroNoreException("You don not have permission to add offer to this doctor"); } var startDateTime = DateTime.Parse($"{model.Date} {model.StartTime}"); var endDateTime = DateTime.Parse($"{model.Date} {model.Endtime}"); var serviceSupply = await _dbContext.ServiceSupplies.FindAsync(model.ServiceSupplyId); if (serviceSupply == null) { throw new AwroNoreException(Global.Err_DoctorNotFound); } _scheduleManager.EnsureHasSchedule(serviceSupply, model.ShiftCenterServiceId ?? 0, startDateTime, endDateTime); var success = false; var message = Global.Err_ErrorOccured; var strategy = _dbContext.Database.CreateExecutionStrategy(); await strategy.ExecuteAsync(async() => { using (var transaction = _dbContext.Database.BeginTransaction()) { if (model.Id != null) { var offer = await _offerRepository.GetByIdAsync(model.Id.Value); if (offer == null) { throw new Exception("Patient Not Found"); } offer.StartDateTime = startDateTime; offer.EndDateTime = endDateTime; offer.MaxCount = model.MaxCount; offer.Description = model.Description; offer.UpdatedAt = DateTime.Now; offer.ServiceSupplyId = model.ServiceSupplyId; offer.ShiftCenterServiceId = model.ShiftCenterServiceId; offer.Type = model.Type; _offerRepository.UpdateOffer(offer); if (model.ImageUpload != null) { var(newName, thumbName, dirPath, baseUrl) = _uploadService.GenerateOfferPhotoName(offer.Id, Lang.EN.ToString(), model.ImageUpload); offer.Photo = $"{baseUrl}/{newName}"; _dbContext.Offers.Attach(offer); _dbContext.Entry(offer).State = EntityState.Modified; await _dbContext.SaveChangesAsync(); await _uploadService.UploadOfferPhotoAsync(model.ImageUpload, dirPath, newName, thumbName); } message = Core.Resources.EntitiesResources.Messages.ItemUpdatedSuccessFully; } else { var uniqueCode = await _offerRepository.GenerateUniqueCodeAsync(); var offer = new Offer { StartDateTime = startDateTime, EndDateTime = endDateTime, MaxCount = model.MaxCount, RemainedCount = model.MaxCount, CreatedAt = DateTime.Now, Description = model.Description, ServiceSupplyId = model.ServiceSupplyId, ShiftCenterServiceId = model.ShiftCenterServiceId, Status = OfferStatus.PENDING, Code = uniqueCode, Type = model.Type, Photo = "", SendNotification = model.SendNotification, NotificationTitle = model.NotificationTitle, NotificationTitle_Ku = model.NotificationTitle_Ku, NotificationTitle_Ar = model.NotificationTitle_Ar, NotificationBody = model.NotificationBody, NotificationBody_Ku = model.NotificationBody_Ku, NotificationBody_Ar = model.NotificationBody_Ar }; await _offerRepository.InsertOfferAsync(offer); if (model.ImageUpload != null) { var(newName, thumbName, dirPath, baseUrl) = _uploadService.GenerateOfferPhotoName(offer.Id, Lang.EN.ToString(), model.ImageUpload); offer.Photo = $"{baseUrl}/{newName}"; _dbContext.Offers.Attach(offer); _dbContext.Entry(offer).State = EntityState.Modified; await _dbContext.SaveChangesAsync(); await _uploadService.UploadOfferPhotoAsync(model.ImageUpload, dirPath, newName, thumbName); } message = Core.Resources.EntitiesResources.Messages.ItemAddedSuccessFully; } transaction.Commit(); success = true; } }); return(Json(new { success, message })); } catch (Exception ex) { throw ex; } }