public void Create(Book book) { book.Id = (long)CommitMutation(new Mutation() { InsertAutoId = new Entity[] { book.ToEntity() } }).MutationResult.InsertAutoIdKeys.First().Path.First().Id; }
// [END commitmutation] // [START create] public void Create(Book book) { var result = CommitMutation(new Mutation() { InsertAutoId = new Entity[] { book.ToEntity() } }); book.Id = result.MutationResult.InsertAutoIdKeys.First().Path.First().Id.Value; }
public IActionResult Create(Book book) { if (ModelState.IsValid) { _store.Create(book); return RedirectToAction("Details", new { id = book.Id }); } return ViewForm("Create", "Create", book); }
public async Task<ActionResult> Create(Book book, HttpPostedFileBase image) { if (ModelState.IsValid) { _store.Create(book); // If book cover image submitted, save image to Cloud Storage if (image != null) { var imageUrl = await _imageUploader.UploadImage(image, book.Id); book.ImageUrl = imageUrl; _store.Update(book); } return RedirectToAction("Details", new { id = book.Id }); } return ViewForm("Create", "Create", book); }
// [END toentity] /// <summary> /// Unpack a book from a datastore entity. /// </summary> /// <param name="entity">An entity retrieved from datastore.</param> /// <returns>A book.</returns> public static Book ToBook(this Entity entity) { // TODO: Use reflection so we don't have to modify the code every time we add or drop // a property from Book. Book book = new Book(); book.Id = (long)entity.Key.Path.First().Id; book.Title = entity.Properties.GetValue("Title")?.StringValue; book.Author = entity.Properties.GetValue("Author")?.StringValue; book.PublishedDate = entity.Properties.GetValue("PublishedDate")?.DateTimeValue; book.ImageUrl = entity.Properties.GetValue("ImageUrl")?.StringValue; book.Description = entity.Properties.GetValue("Description")?.StringValue; book.CreatedById = entity.Properties.GetValue("CreatedById")?.StringValue; return book; }
/// <summary> /// Updates book with information parsed from a json response from Google's Books API. /// </summary> /// <param name="json">A response from Google's Books API.</param> /// <param name="book">Fields in book will be overwritten.</param> public static void UpdateBookFromJson(string json, Book book) { // There are many volumeInfos, and many are incomplete. So, to find a field like // "title", we use LINQ to scan multiple volumeInfos. JObject results = JObject.Parse(json); if (results.Property("items") == null) return; var infos = results["items"].Select(token => (JObject)token) .Where(obj => obj.Property("volumeInfo") != null) .Select(obj => obj["volumeInfo"]); Func<string, IEnumerable<JToken>> GetInfo = (propertyName) => { return infos.Select(token => (JObject)token) .Where(info => info.Property(propertyName) != null) .Select(info => info[propertyName]); }; foreach (var title in GetInfo("title").Take(1)) book.Title = title.ToString(); // Find the oldest publishedDate. var publishedDates = GetInfo("publishedDate") .Select(value => ParseDate(value.ToString())) .Where(date => date != null) .OrderBy(date => date); foreach (var date in publishedDates.Take(1)) book.PublishedDate = date; foreach (var authors in GetInfo("authors").Take(1)) book.Author = string.Join(", ", authors.Select(author => author.ToString())); foreach (var description in GetInfo("description").Take(1)) book.Description = description.ToString(); foreach (JObject imageLinks in GetInfo("imageLinks")) { if (imageLinks.Property("thumbnail") != null) { book.ImageUrl = imageLinks["thumbnail"].ToString(); break; } } }
public void Update(Book book) { _dbcontext.Entry(book).State = System.Data.Entity.EntityState.Modified; _dbcontext.SaveChanges(); }
public void Update(Book book) { CommitMutation(new Mutation() { Update = new Entity[] { book.ToEntity() } }); }
public void Update(Book book) { _dbcontext.Update(book); _dbcontext.SaveChanges(); }
// [START create] public void Create(Book book) { var trackBook = _dbcontext.Books.Add(book); _dbcontext.SaveChanges(); book.Id = trackBook.Id; }
public void Update(Book book) { UpdatedBook = book; }
public async Task<IActionResult> Edit(Book book, long id, [FromForm] IFormFile image) { if (ModelState.IsValid) { book.Id = id; // If book cover image submitted. save image to Cloud Storage if (image != null) { var imageUrl = await _imageUploader.UploadImage(image, book.Id); book.ImageUrl = imageUrl; } else { //No image submited so set ImageUrl to current stored value book.ImageUrl = _store.Read((long)id).ImageUrl; } _store.Update(book); return RedirectToAction("Details", new { id = book.Id }); } return ViewForm("Edit", "Edit", book); }
public IActionResult Edit(Book book, long id) { if (ModelState.IsValid) { book.Id = id; _store.Update(book); return RedirectToAction("Details", new { id = book.Id }); } return ViewForm("Edit", "Edit", book); }
// [START create_book] public async Task<ActionResult> Create(Book book, HttpPostedFileBase image) { if (ModelState.IsValid) { if (Request.IsAuthenticated) { // Track the user who created this book book.CreatedById = CurrentUser.UserId; } _store.Create(book); // ... // [END create_book] // If book cover image submitted, save image to Cloud Storage if (image != null) { var imageUrl = await _imageUploader.UploadImage(image, book.Id); book.ImageUrl = imageUrl; _store.Update(book); } return RedirectToAction("Details", new { id = book.Id }); } return ViewForm("Create", "Create", book); }
// [START create] public void Create(Book book) { var entity = book.ToEntity(); entity.Key = _db.CreateKeyFactory("Book").CreateIncompleteKey(); var keys = _db.Insert(new[] { entity }); book.Id = keys.First().Path.First().Id; }
public void Update(Book book) { _db.Update(book.ToEntity()); }
public void TestUpdateBookFromJsonRedFern() { var json = System.IO.File.ReadAllText(@"testdata\RedFern.json"); Book book = new Book(); BookDetailLookup.UpdateBookFromJson(json, book); Assert.Equal("Where the Red Fern Grows", book.Title); Assert.Equal("Wilson Rawls", book.Author); Assert.Equal(new DateTime(1978, 1, 1), book.PublishedDate); Assert.Contains("Ozarks", book.Description); Assert.Equal("http://books.google.com/books/content?" + "id=-ddvPQAACAAJ&printsec=frontcover&img=1&zoom=1&source=gbs_api", book.ImageUrl); }
public void TestUpdateBookFromJsonBackendError() { var json = System.IO.File.ReadAllText(@"testdata\BackendError.json"); Book book = new Book(); BookDetailLookup.UpdateBookFromJson(json, book); Assert.Equal(null, book.Title); Assert.Equal(null, book.Author); Assert.Equal(null, book.PublishedDate); Assert.Equal(null, book.Description); }
public async Task<ActionResult> Edit(Book book, long id, HttpPostedFileBase image) { if (ModelState.IsValid) { book.Id = id; if (image != null) { book.ImageUrl = await _imageUploader.UploadImage(image, book.Id); } _store.Update(book); return RedirectToAction("Details", new { id = book.Id }); } return ViewForm("Edit", "Edit", book); }
/// <summary> /// Dispays the common form used for the Edit and Create pages. /// </summary> /// <param name="action">The string to display to the user.</param> /// <param name="book">The asp-action value. Where will the form be submitted?</param> /// <returns>An IActionResult that displays the form.</returns> private IActionResult ViewForm(string action, string formAction, Book book = null) { var form = new ViewModels.Books.Form() { Action = action, Book = book ?? new Book(), IsValid = ModelState.IsValid, FormAction = formAction }; return View("/Views/Books/Form.cshtml", form); }
public void Create(Book book) { throw new NotImplementedException(); }