public void RemoveTags(string photoId, Tag[] tags) { var context = new PhotoAlbumDataContext(); foreach (var tag in tags) { var photoTag = new PhotoTagRow(photoId, tag.Name); context.AttachTo(PhotoAlbumDataContext.PhotoTagTable, photoTag, "*"); context.DeleteObject(photoTag); } try { // continue trying to delete, even if not found context.SaveChanges(SaveChangesOptions.ContinueOnError); } catch (DataServiceRequestException ex) { foreach (var resp in ex.Response) { // to be more robust, we will ignore 404 errors when // the entity might have already been deleted (due to an // incomplete operation earliet). if (resp.StatusCode != (int)HttpStatusCode.NotFound && resp.StatusCode != (int)HttpStatusCode.OK) { throw; } } } }
public void CreateTags(string photoId, Tag[] tags) { var context = new PhotoAlbumDataContext(); foreach (var tag in tags) { // add the tag and associate to a picture for later searching context.AddObject(PhotoAlbumDataContext.TagTable, new TagRow(tag)); context.AddObject(PhotoAlbumDataContext.PhotoTagTable, new PhotoTagRow(photoId, tag.Name)); } try { // we want to continue - even if conflict is detected... context.SaveChanges(SaveChangesOptions.ContinueOnError); } catch (DataServiceRequestException ex) { foreach (var resp in ex.Response) { // we might get a conflict here, which is ok (tag exists) // the alternative is to query everytime to see if the tag // exists, which is less efficient that just trying and // handling exception. if (resp.StatusCode != (int)HttpStatusCode.Conflict && resp.StatusCode != (int)HttpStatusCode.Created) { throw; } } } }
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 void UpdateAlbum(Album album) { var context = new PhotoAlbumDataContext(); var albumRow = new AlbumRow(album); // attach and update the photo row context.AttachTo(PhotoAlbumDataContext.AlbumTable, albumRow, "*"); context.UpdateObject(albumRow); context.SaveChanges(); }
public void CreateAlbum(string albumName, string owner) { var context = new PhotoAlbumDataContext(); var album = new Album { AlbumId = SlugHelper.GetSlug(albumName), Owner = owner.ToLowerInvariant(), Title = albumName }; context.AddObject(PhotoAlbumDataContext.AlbumTable, new AlbumRow(album)); context.SaveChanges(); }
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 DeleteAlbum(string albumName, string owner) { var context = new PhotoAlbumDataContext(); // find the album by name and owner (we don't pass in ugly GUIDs for direct access var album = context.Albums .Where(a => a.AlbumId == albumName && a.PartitionKey == owner.ToLowerInvariant()).AsTableServiceQuery() .Single(); context.DeleteObject(album); context.SaveChanges(); // tell the worker role to clean up blobs and tags this.SendToQueue( Constants.AlbumCleanupQueue, string.Format(CultureInfo.InvariantCulture, "{0}|{1}", owner, album.AlbumId)); }
public void Add(Photo photo, Stream binary, string mimeType, string name) { // get just the file name and ignore the path var file = name.Substring(name.LastIndexOf("\\", StringComparison.OrdinalIgnoreCase) + 1); var context = new PhotoAlbumDataContext(); try { // add the photo to table storage context.AddObject(PhotoAlbumDataContext.PhotoTable, new PhotoRow(photo)); context.SaveChanges(); } catch (Exception ex) { if (ex.ToString().Contains("EntityAlreadyExists")) { throw new PhotoNameAlreadyInUseException(photo.AlbumId, photo.Title); } else { throw; } } // add the binary to blob storage var storage = this.storageAccount.CreateCloudBlobClient(); var container = storage.GetContainerReference(photo.Owner.ToLowerInvariant()); container.CreateIfNotExist(); container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob }); var blob = container.GetBlobReference(file); blob.Properties.ContentType = mimeType; blob.UploadFromStream(binary); // post a message to the queue so it can process tags and the sizing operations this.SendToQueue( Constants.PhotoQueue, string.Format(CultureInfo.InvariantCulture, "{0}|{1}|{2}|{3}", photo.Owner, photo.AlbumId, photo.PhotoId, file)); }