private static void ShowSuccessMessage(Book book)
 {
     var message = String.Format("\"{0}\" successfully saved.",
         book.Title);
     Toaster.ShowToast("New Book saved",
         message);
 }           
 public bool Exists(Book book)
 {
     using (var store = ObjectStore.GetInstance<Book>())
     {
         return store.LoadSingle(x => x.Equals(book)) != null;
     }
 }
 public void Add(Book book)
 {
     using (var store = ObjectStore.GetInstance<Book>())
     {
         store.SaveOnSubmit(book);
     }
 }
 private Result NewSuccessResult(Book book)
 {
     return new Result
     {
         Book = book,
         Success = true,
         ErrorMessage = String.Empty
     };
 }
        public async void Execute(object parameter)
        {
            var book = new Book
            {
                Author = _mainViewModel.Author.Trim(),
                Title = _mainViewModel.Title.Trim(),
                Isbn = _mainViewModel.Isbn.Trim()
            };

            try
            {
                var result = await SubmitService.SubmitAsync(book);
                if (result.Success)
                {
                    ShowSuccessMessage(book);
                    await _mainViewModel.RetryService.RetryPublishAsync();
                }
                else
                {
                    // an exception occurred on the server; 
                    // ErrorCode 1 means duplicate
                    // anithing else means we have to retry
                    if (result.ErrorCode > 1)
                    {
                        throw new Exception(
                            string.Format("Server Error: {0}({1})",
                                result.ErrorMessage,
                                result.ErrorCode));
                    }

                    throw new BookExistsException();
                }
            }
            catch (BookExistsException)
            {
                ShowBookExistsMessage(book);
            }
            catch (Exception)
            {
                // catch all exceptions to add the book to 
                // the retry service in case of an error
                if (_mainViewModel.RetryService.Exists(book))
                {
                    ShowBookExistsMessage(book);
                }
                else
                {
                    _mainViewModel.RetryService.Add(book);

                    ShowBooksSavedLocallyMessage(book);
                }
            }

            _mainViewModel.ClearForm.Execute(null);


        }
 private Result NewFailResult(Book book, Exception ex)
 {
     return new Result
     {
         Book = book,
         Success = false,
         ErrorCode = 2,
         ErrorMessage = ex.Message
     };
 }
 private Result NewDuplicateResult(Book book)
 {
     return new Result
     {
         Book = book,
         Success = false,
         ErrorCode = 1,
         ErrorMessage = String.Format("Book \"{0}\" allready exists", book.Title)
     };
 }
        public static async Task<Result> SubmitAsync(Book book)
        {
            var client = new HttpClient
            {
                BaseAddress = new Uri("http://localhost:33300")
            };
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));

            var response = await client.PostAsJsonAsync("/api/Books", book);
            response.EnsureSuccessStatusCode(); // throws an exception on error

            var result = JsonConvert.DeserializeObject<Result>(
                await response.Content.ReadAsStringAsync());

            return result;
        }
        // POST api/books
        public Result Post(Book book)
        {
            using (var store = ObjectStore.GetInstance<Book>())
            {
                try
                {
                    if (BookExists(store, book))
                    {
                        // tell us if the book already exists
                        return NewDuplicateResult(book);
                    }

                    store.SaveOnSubmit(book);

                    return NewSuccessResult(book);
                }
                catch (Exception ex)
                {
                    // tell us if something unexpected happened
                    return NewFailResult(book, ex);
                }
            }
        }
 private bool BookExists(IObjectStore<Book> store, Book book)
 {
     var existing = store.LoadSingle(x => x.Equals(book));
     return existing != null;
 }
 private static void ShowBookExistsMessage(Book book)
 {
     var message = String.Format("\"{0}\" allready exists.",
         book.Title);
     Toaster.ShowToast("New Book exists!",
         message);
 }
 private static void ShowBooksSavedLocallyMessage(Book book)
 {
     var message = String.Format("\"{0}\" saved locally.",
         book.Title);
     Toaster.ShowToast("New Book locally saved",
         message);
 }