0% found this document useful (0 votes)
2 views

UserRepository

The document defines a UserRepository class that implements IUserRepository for managing User entities in a BookDbContext. It includes methods for adding, deleting, updating, and retrieving users, as well as fetching all users with their associated books. The repository uses Entity Framework Core for database operations and ensures changes are saved to the database after each operation.

Uploaded by

David Pavlovski
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

UserRepository

The document defines a UserRepository class that implements IUserRepository for managing User entities in a BookDbContext. It includes methods for adding, deleting, updating, and retrieving users, as well as fetching all users with their associated books. The repository uses Entity Framework Core for database operations and ensures changes are saved to the database after each operation.

Uploaded by

David Pavlovski
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

using BookApp.DataAccsess.

Abstraction;
using BookApp.DomainModels.Entities;
using Microsoft.EntityFrameworkCore;

namespace BookApp.DataAccsess.Implementation
{
public class UserRepository : IUserRepository
{
private readonly BookDbContext _dbContext;
public UserRepository(BookDbContext dbContext)
{
_dbContext = dbContext;
}
public void Add(User entity)
{
_dbContext.Users.Add(entity);
_dbContext.SaveChanges();
}

public void Delete(User entity)


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

public IEnumerable<User> GetAll()


{
return _dbContext.Users.Include(x => x.BooksList).ThenInclude(x =>
x.Book);
}

public User GetById(int id)


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

public User GetUser(string username)


{
return GetAll().SingleOrDefault(x => x.Username == username);
}

public void Update(User entity)


{
User user = GetById(entity.Id);
_dbContext.Entry(user).CurrentValues.SetValues(entity);
_dbContext.SaveChanges();
}
}
}

You might also like