示例#1
0
        public IHttpActionResult AddBook(BookDto bookDto)
        {
            var Name = GetEmailFromRequestHeader();

            if (Name == "*****@*****.**")
            {
                return(BadRequest("This user cannot make changes to the database!"));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            if (bookDto == null)
            {
                return(BadRequest());
            }

            var book = ConversionUtility.BookDtoToBook(bookDto);

            var bookInDb = _context.Books.Add(book);

            _context.SaveChanges();

            //needed to return author data alongside book data
            var addedBook = _context.Books.Include(b => b.Author).Single(b => b.Id == bookInDb.Id);

            var bookToReturn = ConversionUtility.BookToBookDto(addedBook);

            return(Ok(bookToReturn));
        }
示例#2
0
        public IHttpActionResult UpdateBook(int id, BookDto bookDto)
        {
            var Name = GetEmailFromRequestHeader();

            if (Name == "*****@*****.**")
            {
                return(BadRequest("This user cannot make changes to the database!"));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            if (id != bookDto.Id)
            {
                return(BadRequest());
            }

            var book = ConversionUtility.BookDtoToBook(bookDto);

            _context.Entry(book).State = EntityState.Modified;

            _context.SaveChanges();

            //needed to return author data alongside book data
            book = _context.Books.Include(b => b.Author).Single(b => b.Id == book.Id);

            var bookToReturn = ConversionUtility.BookToBookDto(book);

            return(Ok(bookToReturn));
        }
示例#3
0
        public IHttpActionResult GetBook(int id)
        {
            var book = _context.Books.Include(b => b.Author).SingleOrDefault(b => b.Id == id);

            if (book == null)
            {
                return(NotFound());
            }

            var bookDto = ConversionUtility.BookToBookDto(book);

            return(Ok(bookDto));
        }