Skip to content

Commit

Permalink
Section 6 - Include Properties
Browse files Browse the repository at this point in the history
  • Loading branch information
bhrugen committed Sep 23, 2021
1 parent 13fe8db commit ef3be3b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
4 changes: 2 additions & 2 deletions BulkyBook.DataAccess/Repository/IRepository/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ namespace BulkyBook.DataAccess.Repository.IRepository
public interface IRepository<T> where T : class
{
//T - Category
T GetFirstOrDefault(Expression<Func<T, bool>> filter);
IEnumerable<T> GetAll();
T GetFirstOrDefault(Expression<Func<T, bool>> filter, string? includeProperties = null);
IEnumerable<T> GetAll(string? includeProperties = null);
void Add(T entity);
void Remove(T entity);
void RemoveRange(IEnumerable<T> entity);
Expand Down
22 changes: 18 additions & 4 deletions BulkyBook.DataAccess/Repository/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,39 @@ public class Repository<T> : IRepository<T> where T : class
public Repository(ApplicationDbContext db)
{
_db= db;
//_db.Products.Include(u => u.Category).Include(u=>u.CoverType);
this.dbSet= _db.Set<T>();
}
public void Add(T entity)
{
dbSet.Add(entity);
}

public IEnumerable<T> GetAll()
//includeProp - "Category,CoverType"
public IEnumerable<T> GetAll(string? includeProperties = null)
{
IQueryable<T> query = dbSet;
if (includeProperties != null)
{
foreach(var includeProp in includeProperties.Split(new char[] { ','}, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProp);
}
}
return query.ToList();
}

public T GetFirstOrDefault(Expression<Func<T, bool>> filter)
public T GetFirstOrDefault(Expression<Func<T, bool>> filter, string? includeProperties = null)
{
IQueryable<T> query = dbSet;

query = query.Where(filter);

if (includeProperties != null)
{
foreach (var includeProp in includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProp);
}
}
return query.FirstOrDefault();
}

Expand Down
2 changes: 1 addition & 1 deletion BulkyBookWeb/Areas/Admin/Controllers/ProductController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public IActionResult DeletePOST(int? id)
[HttpGet]
public IActionResult GetAll()
{
var productList = _unitOfWork.Product.GetAll();
var productList = _unitOfWork.Product.GetAll(includeProperties:"Category,CoverType");
return Json(new { data = productList });
}
#endregion
Expand Down
2 changes: 1 addition & 1 deletion BulkyBookWeb/Areas/Admin/Views/Product/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<th>ISBN</th>
<th>Price</th>
<th>Author</th>
@*<th>Category</th>*@
<th>Category</th>
@*<th></th>*@
</tr>
</thead>
Expand Down
1 change: 1 addition & 0 deletions BulkyBookWeb/wwwroot/js/product.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function loadDataTable() {
{ "data": "isbn", "width": "15%" },
{ "data": "price", "width": "15%" },
{ "data": "author", "width": "15%" },
{ "data": "category.name", "width": "15%" },
]
});
}

0 comments on commit ef3be3b

Please sign in to comment.