public Task<Book> UpdateAsync(Book item) { Book bookToUpdate = _books.Find(b => b.BookId == item.BookId); int ix = _books.IndexOf(bookToUpdate); _books[ix] = item; return Task.FromResult(_books[ix]); }
public MainPage() { this.InitializeComponent(); _theBook = new BooksService().GetTheBook(); _books = new ObservableCollection<Book>(new BooksService().GetBooks()); // this.DataContext = this; }
private void LoadBook(object sender, BookInfoEvent bookInfo) { if (bookInfo.BookId == 0) { int bookId = _booksService.Books.Select(b => b.BookId).Max() + 1; Book = new Book(bookId); } else { Book = _booksService.GetBook(bookInfo.BookId); } }
private async void OnSaveBook() { try { Book book = await _booksService.AddOrUpdateBookAsync(Book); Book = book; } catch (ArgumentNullException ex) { MessageBox.Show(ex.Message); } }
// TODO: 05 - await in catch private async void OnSaveBook() { try { Book book = await _booksService.AddOrUpdateBookAsync(Book); Book = book; } catch (ArgumentNullException) { var dlg = new MessageDialog("Add vor Speichern"); await dlg.ShowAsync(); } }
public void SpecialSave(Book book) { if (book.Title.StartsWith("99")) { throw new TitleException("Bad title") { MyErrorCode = 99 }; } else if (book.Title.Length == 5) { throw new TitleException("invalid title length") { MyErrorCode = 5 }; } else if (book.Publisher.StartsWith("42")) { throw new PublisherException("bad publisher") { MyErrorCode = 11 }; } }
// TODO: nameof operator public async Task<Book> AddOrUpdateBookAsync(Book book) { if (book == null) throw new ArgumentNullException("book"); Book old = _books.Where(b => b.BookId == book.BookId).SingleOrDefault(); if (old == null) { Book added = await _booksRepository.AddAsync(book); _books.Add(added); return added; } else { Book updated = await _booksRepository.UpdateAsync(book); int ix = _books.IndexOf(old); _books.RemoveAt(ix); _books.Insert(ix, updated); return updated; } }
// TODO: 05 - await in catch private async void OnSaveBook() { bool hasError = false; string errorMessage = string.Empty; try { Book book = await _booksService.AddOrUpdateBookAsync(Book); Book = book; } catch (ArgumentNullException) { hasError = true; errorMessage = "Add vor Speichern"; } if (hasError) { var dlg = new MessageDialog(errorMessage); await dlg.ShowAsync(); } }
public Task<Book> AddAsync(Book item) { _books.Add(item); return Task.FromResult(item); }