Exemplo n.º 1
0
        public IHttpActionResult Delete(int readerID, int bookID)
        {
            BookService   bookService   = new BookService();
            ReaderService readerService = new ReaderService();

            // check if the reader really exists
            Reader reader = readerService.GetReaderByID(readerID);

            if (reader == null)
            {
                return(BadRequest($"invalid readerID: {readerID}"));
            }

            // check if the book really exists
            Book book = bookService.GetBookByID(bookID);

            if (reader == null)
            {
                return(BadRequest($"invalid bookID: {bookID}"));
            }

            readerService.ReturnBook(readerID, bookID);

            return(Ok());
        }
Exemplo n.º 2
0
        public IHttpActionResult GetBorrowedBooksByReader(int readerID)
        {
            BookService   bookService   = new BookService();
            ReaderService readerService = new ReaderService();

            // check if the reader really exists
            Reader reader = readerService.GetReaderByID(readerID);

            if (reader == null)
            {
                return(BadRequest($"invalid readerID: {readerID}"));
            }

            var allBooks = bookService.GetBorrowedBooksByReader(readerID)
                           .Select(c => new BookModel(c))
                           .ToList();

            return(Ok(allBooks));
        }
Exemplo n.º 3
0
        public IHttpActionResult GetByID(int?readerID)
        {
            if (readerID == null)
            {
                return(BadRequest("the parameter readerID is empty"));
            }

            ReaderService ReaderService = new ReaderService();
            Reader        reader        = ReaderService.GetReaderByID(readerID.Value);

            if (reader == null)
            {
                return(BadRequest($"Could not find Reader with ID: {readerID}"));
            }

            ReaderModel apiReader = new ReaderModel(reader);

            return(Ok(apiReader));
        }
Exemplo n.º 4
0
        public IHttpActionResult Delete(int readerID)
        {
            try
            {
                ReaderService readerService = new ReaderService();
                Reader        dbBook        = readerService.GetReaderByID(readerID);
                if (dbBook == null)
                {
                    return(NotFound());
                }

                readerService.DeleteReader(readerID);

                return(StatusCode(HttpStatusCode.NoContent));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
Exemplo n.º 5
0
        public IHttpActionResult Put(ReaderModel reader)
        {
            try
            {
                ReaderService readerService = new ReaderService();
                Reader        dbReader      = readerService.GetReaderByID(reader.ID);
                if (dbReader == null)
                {
                    return(NotFound());
                }

                reader.CopyValuesToEntity(dbReader);
                readerService.EditReader(dbReader);

                return(StatusCode(HttpStatusCode.NoContent)); // or use Ok()
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.GetBaseException().Message));
            }
        }
Exemplo n.º 6
0
        public IHttpActionResult Delete(int readerID)
        {
            try
            {
                ReaderService readerService = new ReaderService();
                Reader        dbReader      = readerService.GetReaderByID(readerID);
                if (dbReader == null)
                {
                    return(NotFound());
                }

                readerService.DeleteReader(readerID);

                // we decide to return no message for information
                // so the response has empty body with status 204 - success
                return(StatusCode(HttpStatusCode.NoContent));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.GetBaseException().Message));
            }
        }
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            using (BookService service = new BookService())
            {
                List <Book> books = service.GetAll();

                // test if we get date from the DB
                Book book = books.FirstOrDefault();

                // edit a book
                book.Quantity++;
                service.EditBook(book);

                // get book by ID
                Book book2 = service.GetBookByID(book.ID);

                // add new book
                Book newBook = new Book
                {
                    Author      = "Microsoft",
                    Title       = "Creating API",
                    Description = "книга за програмиране",
                    Genre       = "programming",
                    Quantity    = 1,
                    CreatedDate = DateTime.Now
                };
                service.AddBook(newBook);

                // find book by author and delete if we have > 1
                var booksByAuthor = service.GetByAuthor("Microsoft");
                if (booksByAuthor.Count > 1)
                {
                    int bookID = booksByAuthor.Last().ID;
                    service.DeleteBook(bookID);
                }
            }

            //// another way to get all books - do not use this way, use BookService
            //LibraryContext context = new LibraryContext();
            //var x = context.Books.ToList();


            using (ReaderService readerService = new ReaderService())
            {
                // add new reader
                Reader newReader = new Reader
                {
                    FirstName   = "reader",
                    LastName    = DateTime.Now.ToShortTimeString(),
                    PhoneNumber = "08812345678",
                    CreatedDate = DateTime.Now
                };
                readerService.AddReader(newReader);

                int readerID = newReader.ID;

                // get reader by ID
                Reader reader2 = readerService.GetReaderByID(readerID);

                // edit reader
                reader2.FirstName = "reader " + readerID;
                readerService.EditReader(reader2);
            }

            using (BookService bookService = new BookService())
                using (ReaderService readerService = new ReaderService())
                {
                    // get some valid IDs
                    int bookID   = bookService.GetByAuthor("Microsoft").First().ID;
                    int readerID = readerService.GetAll().Last().ID;

                    // borrow book
                    readerService.BorrowBook(readerID, bookID);

                    // return book
                    Thread.Sleep(50);
                    readerService.ReturnBook(readerID, bookID);
                }
        }