private string PostSlideForm(Form form, long tenantId, long pageId, long elementId) { // Get carousel slide details long carouselSlideId = Convert.ToInt64(form.Data); string uploadIds = ((UploadField)form.Fields["upload"]).Value; string pageValue = ((SelectListField <string>)form.Fields["page"]).Value; CarouselSlide slide = new CarouselSlide { TenantId = tenantId, ElementId = elementId, CarouselSlideId = carouselSlideId, Name = ((TextField)form.Fields["name"]).Value, Description = ((MultiLineTextField)form.Fields["description"]).Value, PageText = ((TextField)form.Fields["pageText"]).Value, PageId = pageValue == string.Empty ? null : (long?)Convert.ToInt64(pageValue), PageTenantId = pageValue == string.Empty ? null : (long?)tenantId }; if (!string.IsNullOrWhiteSpace(uploadIds)) { string[] uploadParts = uploadIds.Split('|'); slide.ImageTenantId = tenantId; slide.ThumbnailImageUploadId = Convert.ToInt64(uploadParts[0]); slide.PreviewImageUploadId = Convert.ToInt64(uploadParts[1]); slide.ImageUploadId = Convert.ToInt64(uploadParts[2]); } // Validate supplied data _carouselValidator.ValidateSlide(slide); // Determine whether or not there are uncommitted slide images bool uncommittedSlideImages = true; if (carouselSlideId > 0) { ICarouselService carouselService = (ICarouselService)_elementFactory.GetElementService(FormId); CarouselSlide currentSlide = carouselService.ReadSlide(tenantId, elementId, carouselSlideId); uncommittedSlideImages = currentSlide.ImageUploadId != slide.ImageUploadId; } // Get slide view model and return it's JSON representation as form result status CarouselSlideViewModel slideViewModel = GetSlideViewModel(slide, uncommittedSlideImages); return(JsonConvert.SerializeObject(slideViewModel)); }
public void Update(IElementSettings settings, IUnitOfWork unitOfWork = null) { // If we don't have a unit of work in place, create one now so that we can rollback all changes in case of failure IUnitOfWork localUnitOfWork = unitOfWork == null?_unitOfWorkFactory.CreateUnitOfWork() : null; // Begin work try { // Validate slides CarouselSettings carouselSettings = (CarouselSettings)settings; foreach (CarouselSlide slide in carouselSettings.Slides) { _carouselValidator.ValidateSlide(slide); } // Get current carousel settings CarouselSettings currentCarouselSettings = (CarouselSettings)New(settings.TenantId); currentCarouselSettings.ElementId = settings.ElementId; _carouselRepository.Read(currentCarouselSettings, unitOfWork ?? localUnitOfWork); // Get slides to delete (i.e. slides that were in current settings, but not in the new settings) // Get slides with updated images List <CarouselSlide> slidesToDelete = new List <CarouselSlide>(); List <CarouselSlide> slidesWithUpdatedImages = new List <CarouselSlide>(); List <CarouselSlide> currentSlidesWithUpdatedImages = new List <CarouselSlide>(); Dictionary <long, CarouselSlide> slidesById = carouselSettings.Slides.Where(s => s.CarouselSlideId != 0).GroupBy(s => s.CarouselSlideId).ToDictionary(u => u.Key, u => u.First()); foreach (CarouselSlide currentSlide in currentCarouselSettings.Slides) { if (!slidesById.ContainsKey(currentSlide.CarouselSlideId)) { slidesToDelete.Add(currentSlide); } else { CarouselSlide slide = slidesById[currentSlide.CarouselSlideId]; if (slide.ImageUploadId != currentSlide.ImageUploadId) { slidesWithUpdatedImages.Add(slide); currentSlidesWithUpdatedImages.Add(currentSlide); } } } // Get new slides List <CarouselSlide> slidesToCreate = carouselSettings.Slides.Where(s => s.CarouselSlideId == 0).ToList(); // Commit new images slidesToCreate.AddRange(slidesWithUpdatedImages); foreach (CarouselSlide slide in slidesToCreate) { _uploadService.Commit(slide.ImageTenantId, slide.ThumbnailImageUploadId, GetCarouselSlideStorageHierarchy(slide.ElementId), unitOfWork ?? localUnitOfWork); _uploadService.Commit(slide.ImageTenantId, slide.PreviewImageUploadId, GetCarouselSlideStorageHierarchy(slide.ElementId), unitOfWork ?? localUnitOfWork); _uploadService.Commit(slide.ImageTenantId, slide.ImageUploadId, GetCarouselSlideStorageHierarchy(slide.ElementId), unitOfWork ?? localUnitOfWork); } // Update database _carouselRepository.Update((CarouselSettings)settings, unitOfWork ?? localUnitOfWork); // Delete uploads that are no longer required slidesToDelete.AddRange(currentSlidesWithUpdatedImages); foreach (CarouselSlide currentSlide in slidesToDelete) { _uploadService.Delete(currentSlide.ImageTenantId, currentSlide.ThumbnailImageUploadId, GetCarouselSlideStorageHierarchy(currentSlide.ElementId), unitOfWork ?? localUnitOfWork); _uploadService.Delete(currentSlide.ImageTenantId, currentSlide.PreviewImageUploadId, GetCarouselSlideStorageHierarchy(currentSlide.ElementId), unitOfWork ?? localUnitOfWork); _uploadService.Delete(currentSlide.ImageTenantId, currentSlide.ImageUploadId, GetCarouselSlideStorageHierarchy(currentSlide.ElementId), unitOfWork ?? localUnitOfWork); } // Commit work if local unit of work in place and return result if (localUnitOfWork != null) { localUnitOfWork.Commit(); } } catch (ValidationErrorException) { if (localUnitOfWork != null) { localUnitOfWork.Rollback(); } throw; } catch (Exception ex) { if (localUnitOfWork != null) { localUnitOfWork.Rollback(); } throw new ValidationErrorException(new ValidationError(null, ApplicationResource.UnexpectedErrorMessage), ex); } finally { if (localUnitOfWork != null) { localUnitOfWork.Dispose(); } } }