public void UpdateAlbumData(string owner, string albumId, string thumbnailUrl)
        {
            var context = new PhotoAlbumDataContext();

            var albumRow = new AlbumRow(new Album()
            {
                AlbumId = albumId, Owner = owner
            });
            var album = context.Albums.Where(a => a.PartitionKey == albumRow.PartitionKey && a.RowKey == albumRow.RowKey && a.ThumbnailUrl == thumbnailUrl)
                        .AsTableServiceQuery()
                        .FirstOrDefault();

            if (album != null)
            {
                // get the first the photo in the album to update the thumbnail
                var photoRow = new PhotoRow(new Photo {
                    Owner = owner.ToLowerInvariant(), AlbumId = albumId
                });
                var otherPhoto = context.Photos.Where(p => p.PartitionKey == photoRow.PartitionKey).Take(1).SingleOrDefault();

                // if the album is empty, we set the HasPhotos property to false to hide it from the UI
                if (otherPhoto != null)
                {
                    album.ThumbnailUrl = otherPhoto.ThumbnailUrl;
                }
                else
                {
                    album.ThumbnailUrl = string.Empty;
                    album.HasPhotos    = false;
                }

                this.UpdateAlbum(album.ToModel());
            }
        }
        public void UpdatePhotoData(Photo photo)
        {
            var context  = new PhotoAlbumDataContext();
            var photoRow = new PhotoRow(photo);

            // attach and update the photo row
            context.AttachTo(PhotoAlbumDataContext.PhotoTable, photoRow, "*");
            context.UpdateObject(photoRow);
            context.SaveChanges();
        }
        public Photo GetPhotoByOwner(string owner, string albumId, string photoId)
        {
            var context = new PhotoAlbumDataContext();

            // use this partial one to correctly construct partition keys
            var temp = new PhotoRow(new Photo { Owner = owner.ToLowerInvariant(), AlbumId = albumId, PhotoId = photoId });

            // we add the 'true' predicate in order to not get 404 if photo is missing (we just want a null)
            return context.Photos.Where(p => p.PartitionKey == temp.PartitionKey && p.RowKey == temp.RowKey && true).AsTableServiceQuery()
                .AsEnumerable()
                .ToModel()
                .SingleOrDefault();
        }
        public IEnumerable<Photo> GetPhotosByAlbum(string owner, string albumId)
        {
            var context = new PhotoAlbumDataContext();

            // use this partial one to correctly construct partition keys
            var temp = new PhotoRow(new Photo { Owner = owner.ToLowerInvariant(), AlbumId = albumId });

            return context.Photos.Where(p => p.PartitionKey == temp.PartitionKey).AsTableServiceQuery()
                .AsEnumerable()
                .ToModel();

            // TODO: Handle Paging
        }
        public void Delete(Photo photo)
        {
            var context  = new PhotoAlbumDataContext();
            var photoRow = new PhotoRow(photo);

            context.AttachTo(PhotoAlbumDataContext.PhotoTable, photoRow, "*");
            context.DeleteObject(photoRow);
            context.SaveChanges();

            // tell the worker role to clean up blobs and tags
            this.SendToQueue(
                Constants.PhotoCleanupQueue,
                string.Format(CultureInfo.InvariantCulture, "{0}|{1}|{2}|{3}|{4}|{5}", photo.PhotoId, photo.Owner, photo.Url, photo.RawTags, photo.ThumbnailUrl, photo.AlbumId));
        }
 public static Photo ToModel(this PhotoRow row)
 {
     return(new Photo()
     {
         PhotoId = row.PhotoId,
         AlbumId = row.AlbumId,
         Owner = row.Owner,
         ThumbnailUrl = row.ThumbnailUrl,
         Title = row.Title,
         Url = row.Url,
         Description = row.Description,
         RawTags = row.RawTags
     });
 }
        public Photo GetPhotoByOwner(string owner, string albumId, string photoId)
        {
            var context = new PhotoAlbumDataContext();

            // use this partial one to correctly construct partition keys
            var temp = new PhotoRow(new Photo {
                Owner = owner.ToLowerInvariant(), AlbumId = albumId, PhotoId = photoId
            });

            // we add the 'true' predicate in order to not get 404 if photo is missing (we just want a null)
            return(context.Photos.Where(p => p.PartitionKey == temp.PartitionKey && p.RowKey == temp.RowKey && true).AsTableServiceQuery()
                   .AsEnumerable()
                   .ToModel()
                   .SingleOrDefault());
        }
        public IEnumerable <Photo> GetPhotosByAlbum(string owner, string albumId)
        {
            var context = new PhotoAlbumDataContext();

            // use this partial one to correctly construct partition keys
            var temp = new PhotoRow(new Photo {
                Owner = owner.ToLowerInvariant(), AlbumId = albumId
            });

            return(context.Photos.Where(p => p.PartitionKey == temp.PartitionKey).AsTableServiceQuery()
                   .AsEnumerable()
                   .ToModel());

            // TODO: Handle Paging
        }
        public void Delete(string owner, string album, string photoId)
        {
            var context = new PhotoAlbumDataContext();

            // we use this to help calculate partition keys, rowkeys in query later
            var temp = new PhotoRow(new Photo {
                PhotoId = photoId, AlbumId = album, Owner = owner.ToLowerInvariant()
            });

            // see if the photo exists
            var photo = context.Photos.Where(p => p.PartitionKey == temp.PartitionKey && p.RowKey == temp.RowKey && true).AsTableServiceQuery()
                        .ToModel()
                        .SingleOrDefault();

            if (photo != null)
            {
                this.Delete(photo);
            }
        }
        public void Delete(Photo photo)
        {
            var context = new PhotoAlbumDataContext();
            var photoRow = new PhotoRow(photo);

            context.AttachTo(PhotoAlbumDataContext.PhotoTable, photoRow, "*");
            context.DeleteObject(photoRow);
            context.SaveChanges();

            // tell the worker role to clean up blobs and tags
            this.SendToQueue(
                Constants.PhotoCleanupQueue,
                string.Format(CultureInfo.InvariantCulture, "{0}|{1}|{2}|{3}|{4}|{5}", photo.PhotoId, photo.Owner, photo.Url, photo.RawTags, photo.ThumbnailUrl, photo.AlbumId));
        }
        public void Delete(string owner, string album, string photoId)
        {
            var context = new PhotoAlbumDataContext();

            // we use this to help calculate partition keys, rowkeys in query later
            var temp = new PhotoRow(new Photo { PhotoId = photoId, AlbumId = album, Owner = owner.ToLowerInvariant() });

            // see if the photo exists
            var photo = context.Photos.Where(p => p.PartitionKey == temp.PartitionKey && p.RowKey == temp.RowKey && true).AsTableServiceQuery()
                .ToModel()
                .SingleOrDefault();

            if (photo != null)
            {
                this.Delete(photo);
            }
        }
        public void UpdateAlbumData(string owner, string albumId, string thumbnailUrl)
        {
            var context = new PhotoAlbumDataContext();

            var albumRow = new AlbumRow(new Album() { AlbumId = albumId, Owner = owner });
            var album = context.Albums.Where(a => a.PartitionKey == albumRow.PartitionKey && a.RowKey == albumRow.RowKey && a.ThumbnailUrl == thumbnailUrl)
                .AsTableServiceQuery()
                .FirstOrDefault();

            if (album != null)
            {
                // get the first the photo in the album to update the thumbnail
                var photoRow = new PhotoRow(new Photo { Owner = owner.ToLowerInvariant(), AlbumId = albumId });
                var otherPhoto = context.Photos.Where(p => p.PartitionKey == photoRow.PartitionKey).Take(1).SingleOrDefault();

                // if the album is empty, we set the HasPhotos property to false to hide it from the UI
                if (otherPhoto != null)
                {
                    album.ThumbnailUrl = otherPhoto.ThumbnailUrl;
                }
                else
                {
                    album.ThumbnailUrl = string.Empty;
                    album.HasPhotos = false;
                }

                this.UpdateAlbum(album.ToModel());
            }
        }
        public void UpdatePhotoData(Photo photo)
        {
            var context = new PhotoAlbumDataContext();
            var photoRow = new PhotoRow(photo);

            // attach and update the photo row
            context.AttachTo(PhotoAlbumDataContext.PhotoTable, photoRow, "*");
            context.UpdateObject(photoRow);
            context.SaveChanges();
        }