< Summary

Information
Class: BookstoreAPI.Repositories.BookRepository
Assembly: BookstoreAPI
File(s): /home/runner/work/dotnet-unit-test-webinar/dotnet-unit-test-webinar/BookstoreAPI/Repositories/BookRepository.cs
Line coverage
100%
Covered lines: 29
Uncovered lines: 0
Coverable lines: 29
Total lines: 52
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
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%11100%
AddBook(...)100%11100%
UpdateBook(...)100%22100%
DeleteBook(...)100%22100%

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    {
 9        public BookNotFoundException(string message) : base(message) { }
 10    }
 11    public class BookRepository : IBookRepository
 12    {
 13        private readonly BookDbContext bookDbContext;
 1414        public BookRepository(BookDbContext bookDbContext)
 1415        {
 1416            this.bookDbContext = bookDbContext;
 1417        }
 18
 119        public IEnumerable<Book> GetAllBooks() => bookDbContext.Books;
 20
 1221        public Book? GetBookById(int id) => bookDbContext.Books.Find(b => b.Id == id);
 22
 23        public void AddBook(Book book)
 124        {
 125            book.Id = bookDbContext.Books.Count + 1;
 126            bookDbContext.Books.Add(book);
 127        }
 28
 29        public void UpdateBook(Book book)
 330        {
 331            var existingBook = GetBookById(book.Id);
 232            if (existingBook != null)
 133            {
 134                existingBook.Title = book.Title;
 135                existingBook.Author = book.Author;
 136                existingBook.ISBN = book.ISBN;
 137                existingBook.Description = book.Description;
 138            }
 139            else throw new BookNotFoundException($"Book with id {book.Id} not found");
 140        }
 41
 42        public void DeleteBook(int id)
 343        {
 344            var book = GetBookById(id);
 245            if (book != null)
 146            {
 147                bookDbContext.Books.Remove(book);
 148            }
 149            else throw new BookNotFoundException($"Book with id {id} not found");
 150        }
 51    }
 52}