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 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 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)); }