private static void TestBookShelfApi() { var api = new BookShelf(); var book1 = new Book() { Author = "A. Miln", Isbn = "000-0-000-00000-2", Title = "Winnie the Pooh" }; // TODO: Uncomment for the first run; //var addResult = api.AddBook(book1); //Console.WriteLine(addResult.Success); //Console.WriteLine("AddBooks books: Success - {0}, ErrorMessages - {1}", addResult.Success, String.Join("\t", addResult.ErrorMessages)); var getBooksResult = api.GetBooks(); Console.WriteLine("Get books: Success - {0}, ErrorMessages - {1}", getBooksResult.Success, String.Join("\t", getBooksResult.ErrorMessages)); foreach (var book in getBooksResult.Result) { Console.WriteLine("{0}, {1}, {2}, {3}", book.Author, book.Isbn, book.Title, book.Loaned); } var loanBook = api.LoanBook("000-0-000-00000-2"); Console.WriteLine("Loan book: Success - {0}, ErrorMessages - {1}", loanBook.Success, String.Join("\t", loanBook.ErrorMessages)); // TODO: Uncomment for the second run var returnBook = api.ReturnBook("000-0-000-00000-2"); Console.WriteLine("Return book: Success - {0}, ErrorMessages - {1}", returnBook.Success, String.Join("; ", returnBook.ErrorMessages)); Console.ReadLine(); }
public ApiResult<Book> AddBook(Book book) { try { using (var db = new BookShelfDbContext()) { db.Books.Add(book); db.SaveChanges(); return new ApiResult<Book>() { Success = true, Result = book }; } } catch(Exception e) { return new ApiResult<Book>() { Success = false, ErrorMessages = new List<string>(){e.Message} }; } }