0% found this document useful (0 votes)
16 views2 pages

Book Repository

The document defines a BookRepository class that implements the IBookRepository interface for managing book entities in a database context. It includes methods for adding, deleting, updating, and retrieving books by various criteria such as genre, ID, and name. The repository utilizes Entity Framework for data access and operations are performed on a BookDbContext instance.

Uploaded by

David Pavlovski
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views2 pages

Book Repository

The document defines a BookRepository class that implements the IBookRepository interface for managing book entities in a database context. It includes methods for adding, deleting, updating, and retrieving books by various criteria such as genre, ID, and name. The repository utilizes Entity Framework for data access and operations are performed on a BookDbContext instance.

Uploaded by

David Pavlovski
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

using BookApp.DataAccsess.

Abstraction;
using BookApp.DomainModels.Entities;

namespace BookApp.DataAccsess.Implementation
{

public class BookRepository : IBookRepository


{
private readonly BookDbContext _dbContext;
public BookRepository(BookDbContext dbContext)
{
_dbContext = dbContext;
}
public void Add(Book entity)
{
_dbContext.Books.Add(entity);
_dbContext.SaveChanges();
}

public void Delete(Book entity)


{
_dbContext.Books.Remove(entity);
_dbContext.SaveChanges();
}

public IEnumerable<Book> GetAll()


{
return _dbContext.Books;
}

public IEnumerable<Book> GetByGenre(string genre)


{
return GetAll().Where(x =>
x.Genre.ToLower().Contains(genre.ToLower()));
}

public Book GetById(int id)


{
return GetAll().SingleOrDefault(x => x.Id == id);
}

public Book GetByName(string name)


{
return GetAll().SingleOrDefault(x =>
x.Title.ToLower().Contains(name.ToLower()));
}

public FavoriteBooks GetFavoriteBook(int id)


{
return _dbContext.BookList.SingleOrDefault(x => x.Id == id);
}

public void Update(Book entity)


{
Book book = GetById(entity.Id);
_dbContext.Entry(book).CurrentValues.SetValues(entity);
_dbContext.SaveChanges();
}
}
}

You might also like