< Summary

Information
Class: BookstoreAPI.Repositories.BookNotFoundException
Assembly: BookstoreAPI
File(s): /home/runner/work/dotnet-unit-test-webinar/dotnet-unit-test-webinar/BookstoreAPI/Repositories/BookRepository.cs
Line coverage
100%
Covered lines: 1
Uncovered lines: 0
Coverable lines: 1
Total lines: 52
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%

File(s)

/home/runner/work/dotnet-unit-test-webinar/dotnet-unit-test-webinar/BookstoreAPI/Repositories/BookRepository.cs

#LineLine coverage
 1using BookstoreAPI.Models;
 2using System.Collections.Generic;
 3using System.Linq;
 4
 5namespace BookstoreAPI.Repositories
 6{
 7    public class BookNotFoundException : System.Exception
 8    {
 69        public BookNotFoundException(string message) : base(message) { }
 10    }
 11    public class BookRepository : IBookRepository
 12    {
 13        private readonly BookDbContext bookDbContext;
 14        public BookRepository(BookDbContext bookDbContext)
 15        {
 16            this.bookDbContext = bookDbContext;
 17        }
 18
 19        public IEnumerable<Book> GetAllBooks() => bookDbContext.Books;
 20
 21        public Book? GetBookById(int id) => bookDbContext.Books.Find(b => b.Id == id);
 22
 23        public void AddBook(Book book)
 24        {
 25            book.Id = bookDbContext.Books.Count + 1;
 26            bookDbContext.Books.Add(book);
 27        }
 28
 29        public void UpdateBook(Book book)
 30        {
 31            var existingBook = GetBookById(book.Id);
 32            if (existingBook != null)
 33            {
 34                existingBook.Title = book.Title;
 35                existingBook.Author = book.Author;
 36                existingBook.ISBN = book.ISBN;
 37                existingBook.Description = book.Description;
 38            }
 39            else throw new BookNotFoundException($"Book with id {book.Id} not found");
 40        }
 41
 42        public void DeleteBook(int id)
 43        {
 44            var book = GetBookById(id);
 45            if (book != null)
 46            {
 47                bookDbContext.Books.Remove(book);
 48            }
 49            else throw new BookNotFoundException($"Book with id {id} not found");
 50        }
 51    }
 52}

Methods/Properties

.ctor(System.String)