Created
April 16, 2018 04:28
-
-
Save gmartinezsan/7b656332294512fe4557d0bc693d4fb4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using BooksWebApi.Data; | |
using BooksWebApi.Entities; | |
using Microsoft.EntityFrameworkCore; | |
public class CrudRepository : ICrudRepository | |
{ | |
private readonly BooksCatalogDbContext _context; | |
public CrudRepository(BooksCatalogDbContext context) | |
{ | |
_context = context; | |
} | |
public void Add<T>(T entity) where T : class | |
{ | |
_context.Add(entity); | |
} | |
public void Delete<T>(T entity) where T : class | |
{ | |
_context.Remove(entity); | |
} | |
public Book GetBook(int id) | |
{ | |
return _context.Books | |
.Include(b => b.Category) | |
.Where(b => b.Id == id) | |
.FirstOrDefault(); | |
} | |
public IEnumerable<Book> GetBooks() | |
{ | |
return _context.Books | |
.Include(b => b.Category) | |
.ToList(); | |
} | |
public IEnumerable<Book> GetBooksByCategory(string category) | |
{ | |
return _context.Books | |
.Where(c => c.Category.Description.Equals(category, StringComparison.CurrentCultureIgnoreCase)) | |
.Include(c => c.Category) | |
.OrderBy(b => b.Name) | |
.ToList(); | |
} | |
public Category GetCategoryWithBooks(int id) | |
{ | |
return _context.Categories | |
.Where(c => c.Id == id) | |
.Include(c => c.Books) | |
.FirstOrDefault(); | |
} | |
public User GetUser(string userName) | |
{ | |
return _context.Users | |
.Where(u => u.UserName == userName) | |
.Cast<User>() | |
.FirstOrDefault(); | |
} | |
public async Task<bool> SaveAllAsync() | |
{ | |
return (await _context.SaveChangesAsync() > 0); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment