public static void AddImage(ImageModel image, int? albumID, int userID) { using (var context = new Gallery.Data.GalleryContext()) { User user = context.Users.FirstOrDefault(u => u.ID == userID); if (user == null) { throw new ServerErrorException("Invalid session"); } Image newImage = new Image { Title = image.Title, Url = image.Url, DateUploaded = image.DateUploaded }; context.Users.Find(userID).Galleries.First().Images.Add(newImage); if (albumID != null) { context.Users.Find(userID).Galleries.First().Albums .First(a => a.ID == albumID).Images.Add(newImage); } context.Images.Add(newImage); context.SaveChanges(); } }
public static void DeleteImage(int imageID, int userID) { using (var context = new Gallery.Data.GalleryContext()) { var user = context.Users.Include("Galleries").Include("Galleries.Images") .FirstOrDefault(u => u.ID == userID); if (user == null) { throw new ServerErrorException("Invalid user"); } var gallery = user.Galleries; if (gallery.Count == 0) { throw new ServerErrorException("User has no gallery"); } Image img = gallery.First().Images.FirstOrDefault(i => i.ID == imageID); gallery.First().Images.Remove(img); context.SaveChanges(); } }