< Summary

Information
Class: BookstoreAPI.Controllers.BooksController
Assembly: BookstoreAPI
File(s): /home/runner/work/dotnet-unit-test-webinar/dotnet-unit-test-webinar/BookstoreAPI/Controllers/BooksController.cs
Line coverage
100%
Covered lines: 57
Uncovered lines: 0
Coverable lines: 57
Total lines: 104
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
GetAllBooks()100%11100%
GetBookById(...)100%22100%
AddBook(...)100%11100%
UpdateBook(...)100%22100%
DeleteBook(...)100%11100%
GetBookDetailsFromExternalApiAsync()100%22100%

File(s)

/home/runner/work/dotnet-unit-test-webinar/dotnet-unit-test-webinar/BookstoreAPI/Controllers/BooksController.cs

#LineLine coverage
 1using BookstoreAPI.Models;
 2using BookstoreAPI.Repositories;
 3using BookstoreAPI.Services;
 4using Microsoft.AspNetCore.Mvc;
 5
 6namespace BookstoreAPI.Controllers
 7{
 8    [ApiController]
 9    [Route("api/[controller]")]
 10    public class BooksController : ControllerBase
 11    {
 12        private readonly IBookService _bookService;
 13
 1414        public BooksController(IBookService bookService)
 1415        {
 1416            _bookService = bookService;
 1417        }
 18
 19        [HttpGet]
 20        public ActionResult<IEnumerable<Book>> GetAllBooks()
 121        {
 122            return Ok(_bookService.GetAllBooks());
 123        }
 24
 25        [HttpGet("{id}")]
 26        public ActionResult<Book> GetBookById(int id)
 227        {
 228            var book = _bookService.GetBookById(id);
 229            if (book == null)
 130            {
 131                return NotFound();
 32            }
 133            return Ok(book);
 234        }
 35
 36        [HttpPost]
 37        public ActionResult AddBook(Book book)
 138        {
 139            _bookService.AddBook(book);
 140            return CreatedAtAction(nameof(GetBookById), new { id = book.Id }, book);
 141        }
 42
 43        [HttpPut("{id}")]
 44        public ActionResult UpdateBook(int id, Book book)
 445        {
 46            try
 447            {
 448                if (id != book.Id)
 149                {
 150                    return BadRequest();
 51                }
 352                _bookService.UpdateBook(book);
 153                return NoContent();
 54            }
 155            catch (BookNotFoundException)
 156            {
 157                return NotFound();
 58            }
 159            catch
 160            {
 161                return StatusCode(500);
 62            }
 463        }
 64
 65        [HttpDelete("{id}")]
 66        public ActionResult DeleteBook(int id)
 367        {
 68            try
 369            {
 370                _bookService.DeleteBook(id);
 171                return NoContent();
 72            }
 173            catch (BookNotFoundException)
 174            {
 175                return NotFound();
 76            }
 177            catch
 178            {
 179                return StatusCode(500);
 80            }
 381        }
 82
 83        [HttpGet("external/{isbn}")]
 84        public async Task<ActionResult<Book>> GetBookDetailsFromExternalApiAsync(string isbn)
 385        {
 86            try
 387            {
 388                var book = await _bookService.GetBookDetailsFromExternalApiAsync(isbn);
 289                if (book == null)
 190                {
 191                    return NotFound();
 92                }
 193                return Ok(book);
 94            }
 195            catch (ExternalBookHttpClientException e)
 196            {
 197                return StatusCode(500, new
 198                {
 199                    message = e.Message
 1100                });
 101            }
 3102        }
 103    }
 104}