private AlbumPhotoViewModel GetPhotoViewModel(AlbumPhoto photo, bool uncommittedPhotoImages) { // TODO: Use proper URL routing here, do not hard wire these URLs. string imageUrl = null; if (uncommittedPhotoImages) { imageUrl = string.Format("/uploads/{0}?t={0}", photo.ThumbnailImageUploadId); } else { imageUrl = string.Format("/elements/{0}/uploads/{1}?format=thumbnail&t={1}", photo.ElementId, photo.AlbumPhotoId); } AlbumPhotoViewModel photoViewModel = new AlbumPhotoViewModel { AlbumPhotoId = photo.AlbumPhotoId.ToString(), ThumbnailImageUploadId = photo.ThumbnailImageUploadId.ToString(), PreviewImageUploadId = photo.PreviewImageUploadId.ToString(), ImageUploadId = photo.ImageUploadId.ToString(), Name = photo.Name, Description = photo.Description, ImageUrl = imageUrl }; return(photoViewModel); }
public AlbumPhoto ReadPhoto(long tenantId, long elementId, long albumPhotoId, IUnitOfWork unitOfWork = null) { IDatabaseManager dbm = _databaseManagerFactory.GetDatabaseManager(unitOfWork); try { AlbumPhoto photo = null; dbm.SetSQL(_sqlManager.GetSql("Sql.ReadAlbumPhoto.sql")); dbm.AddParameter("@TenantId", FieldType.BigInt, tenantId); dbm.AddParameter("@ElementId", FieldType.BigInt, elementId); dbm.AddParameter("@AlbumPhotoId", FieldType.BigInt, albumPhotoId); dbm.ExecuteReader(); if (dbm.Read()) { photo = GetAlbumPhotoFromDatabaseManager(dbm); } return(photo); } finally { if (unitOfWork == null) { dbm.Dispose(); } } }
public void ValidatePhoto(AlbumPhoto photo, string keyPrefix = null) { // Check that image specified if (photo.ImageTenantId == 0 || photo.ThumbnailImageUploadId == 0 || photo.PreviewImageUploadId == 0 || photo.ImageUploadId == 0) { throw new ValidationErrorException(new ValidationError(AlbumPropertyNames.Image, ElementResource.AlbumImageRequiredMessage, keyPrefix)); } }
public Upload ReadUpload(long tenantId, long elementId, long uploadId, string format, IUnitOfWork unitOfWork = null) { AlbumPhoto photo = _albumRepository.ReadPhoto(tenantId, elementId, uploadId, unitOfWork); if (photo == null) { return(null); } switch (format) { case "preview": return((Image)_uploadService.Read(photo.ImageTenantId, photo.PreviewImageUploadId, GetAlbumPhotoStorageHierarchy(elementId), unitOfWork)); case "thumbnail": return((Image)_uploadService.Read(photo.ImageTenantId, photo.ThumbnailImageUploadId, GetAlbumPhotoStorageHierarchy(elementId), unitOfWork)); default: return(null); } }
private string PostPhotoForm(Form form, long tenantId, long pageId, long elementId) { // Get album photo details long albumPhotoId = Convert.ToInt64(form.Data); string uploadIds = ((UploadField)form.Fields["upload"]).Value; AlbumPhoto photo = new AlbumPhoto { TenantId = tenantId, ElementId = elementId, AlbumPhotoId = albumPhotoId, Name = ((TextField)form.Fields["name"]).Value, Description = ((MultiLineTextField)form.Fields["description"]).Value }; if (!string.IsNullOrWhiteSpace(uploadIds)) { string[] uploadParts = uploadIds.Split('|'); photo.ImageTenantId = tenantId; photo.ThumbnailImageUploadId = Convert.ToInt64(uploadParts[0]); photo.PreviewImageUploadId = Convert.ToInt64(uploadParts[1]); photo.ImageUploadId = Convert.ToInt64(uploadParts[2]); } // Validate supplied data _albumValidator.ValidatePhoto(photo); // Determine whether or not there are uncommitted photo images bool uncommittedPhotoImages = true; if (albumPhotoId > 0) { IAlbumService albumService = (IAlbumService)_elementFactory.GetElementService(FormId); AlbumPhoto currentPhoto = albumService.ReadPhoto(tenantId, elementId, albumPhotoId); uncommittedPhotoImages = currentPhoto.ImageUploadId != photo.ImageUploadId; } // Get photo view model and return it's JSON representation as form result status AlbumPhotoViewModel photoViewModel = GetPhotoViewModel(photo, uncommittedPhotoImages); return(JsonConvert.SerializeObject(photoViewModel)); }
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 AlbumSettings albumSettings = (AlbumSettings)settings; foreach (AlbumPhoto photo in albumSettings.Photos) { _albumValidator.ValidatePhoto(photo); } // Get current album settings AlbumSettings currentAlbumSettings = (AlbumSettings)New(settings.TenantId); currentAlbumSettings.ElementId = settings.ElementId; _albumRepository.Read(currentAlbumSettings, unitOfWork ?? localUnitOfWork); // Get photos to delete (i.e. photos that were in current settings, but not in the new settings) // Get photos with updated images List <AlbumPhoto> photosToDelete = new List <AlbumPhoto>(); List <AlbumPhoto> photosWithUpdatedImages = new List <AlbumPhoto>(); List <AlbumPhoto> currentPhotosWithUpdatedImages = new List <AlbumPhoto>(); Dictionary <long, AlbumPhoto> photosById = albumSettings.Photos.Where(p => p.AlbumPhotoId != 0).GroupBy(s => s.AlbumPhotoId).ToDictionary(u => u.Key, u => u.First()); foreach (AlbumPhoto currentPhoto in currentAlbumSettings.Photos) { if (!photosById.ContainsKey(currentPhoto.AlbumPhotoId)) { photosToDelete.Add(currentPhoto); } else { AlbumPhoto photo = photosById[currentPhoto.AlbumPhotoId]; if (photo.ImageUploadId != currentPhoto.ImageUploadId) { photosWithUpdatedImages.Add(photo); currentPhotosWithUpdatedImages.Add(currentPhoto); } } } // Get new photos List <AlbumPhoto> photosToCreate = albumSettings.Photos.Where(s => s.AlbumPhotoId == 0).ToList(); // Commit new images photosToCreate.AddRange(photosWithUpdatedImages); foreach (AlbumPhoto photo in photosToCreate) { _uploadService.Commit(photo.ImageTenantId, photo.ThumbnailImageUploadId, GetAlbumPhotoStorageHierarchy(photo.ElementId), unitOfWork ?? localUnitOfWork); _uploadService.Commit(photo.ImageTenantId, photo.PreviewImageUploadId, GetAlbumPhotoStorageHierarchy(photo.ElementId), unitOfWork ?? localUnitOfWork); _uploadService.Commit(photo.ImageTenantId, photo.ImageUploadId, GetAlbumPhotoStorageHierarchy(photo.ElementId), unitOfWork ?? localUnitOfWork); } // Update database _albumRepository.Update((AlbumSettings)settings, unitOfWork ?? localUnitOfWork); // Delete uploads that are no longer required photosToDelete.AddRange(currentPhotosWithUpdatedImages); foreach (AlbumPhoto currentPhoto in photosToDelete) { _uploadService.Delete(currentPhoto.ImageTenantId, currentPhoto.ThumbnailImageUploadId, GetAlbumPhotoStorageHierarchy(currentPhoto.ElementId), unitOfWork ?? localUnitOfWork); _uploadService.Delete(currentPhoto.ImageTenantId, currentPhoto.PreviewImageUploadId, GetAlbumPhotoStorageHierarchy(currentPhoto.ElementId), unitOfWork ?? localUnitOfWork); _uploadService.Delete(currentPhoto.ImageTenantId, currentPhoto.ImageUploadId, GetAlbumPhotoStorageHierarchy(currentPhoto.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(); } } }