Exemplo n.º 1
0
        public IHttpActionResult AddBook(Book book)
        {
            //Check if there is any empty field
            if (book.ISBN == 0 || book.Title == "" || book.Author == "" ||
                book.PublishYear == "" || book.Publisher == "" || book.Image == "" ||
                book.Summary == "" || book.Category == "" || book.Language == "")
            {
                return(BadRequest("Insufficient input"));
            }

            //Validate ISBN must be exact 6 digits
            var hasDigits = Math.Floor(Math.Log10(book.ISBN) + 1);

            if (hasDigits < 6 || hasDigits > 6)
            {
                return(BadRequest("ISBN should be exact 6 digits"));
            }

            //Check if ISBN is greater than 0
            if (book.ISBN <= 0)
            {
                return(BadRequest("ISBN should be greater than 0"));
            }

            //Check if number of available books is not smaller or equal to 0
            if (book.NumberAvailable <= 0)
            {
                return(BadRequest("Number of available books must be greater than 0"));
            }

            //Calling the AddBook method to execute stored procedure
            string message = bookManagement.AddBook(book.ISBN, book.Title, book.Author, book.PublishYear,
                                                    book.NumberAvailable, book.Publisher, book.Image,
                                                    book.Summary, book.Category, book.Language);

            if (message.Contains("Error"))
            {
                return(BadRequest(message));
            }

            return(Ok(message));
        }