Skip to content

Commit

Permalink
Implement repositories; add tests to models and repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
kathulhur committed Dec 12, 2023
1 parent 46b69fc commit 7c4cc36
Show file tree
Hide file tree
Showing 107 changed files with 2,804 additions and 573 deletions.
Binary file modified .vs/ProjectEvaluation/storemanagementsystemx.metadata.v7.bin
Binary file not shown.
Binary file modified .vs/ProjectEvaluation/storemanagementsystemx.projects.v7.bin
Binary file not shown.
Binary file modified .vs/StoreManagementSystemX/DesignTimeBuild/.dtbcache.v2
Binary file not shown.
Binary file modified .vs/StoreManagementSystemX/v17/.futdcache.v2
Binary file not shown.
Binary file modified .vs/StoreManagementSystemX/v17/.suo
Binary file not shown.
Binary file modified .vs/StoreManagementSystemX/v17/TestStore/0/testlog.manifest
Binary file not shown.
99 changes: 99 additions & 0 deletions StoreManagementSystemX.Database/DAL/BaseRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using Microsoft.EntityFrameworkCore;
using StoreManagementSystemX.Database.DAL.Interfaces;
using StoreManagementSystemX.Database.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace StoreManagementSystemX.Database.DAL
{
public class BaseRepository<TEntity> : IRepository<TEntity> where TEntity : class
{
public BaseRepository(Context context)
{
_context = context;
_dbSet = context.Set<TEntity>();
}

protected readonly Context _context;
protected readonly DbSet<TEntity> _dbSet;

public virtual void Delete(Guid instanceId)
{
var entityToDelete = _dbSet.Find(instanceId);
if (entityToDelete != null)
{
Delete(entityToDelete);
}

}

public virtual void Delete(TEntity entityToDelete)
{
if (_context.Entry(entityToDelete).State == EntityState.Detached)
{
_dbSet.Attach(entityToDelete);
}

_dbSet.Remove(entityToDelete);
}



public TEntity? GetById(Guid instanceId)
{

return _dbSet.Find(instanceId);
}

public void Insert(TEntity newInstance)
{
_dbSet.Add(newInstance);
}

public virtual void Update(TEntity entityToUpdate)
{
_dbSet.Attach(entityToUpdate);
_context.Entry(entityToUpdate).State = EntityState.Modified;
}

public void Attach(TEntity entity)
{
_dbSet.Attach(entity);
}

public void Save()
{
_context.SaveChanges();
}

public IEnumerable<TEntity> Get(
Expression<Func<TEntity, bool>>? filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>>? orderBy = null,
string includeProperties = "")
{
IQueryable<TEntity> query = _dbSet;

if(filter != null)
{
query = query.Where(filter);
}

foreach(var includeProperty in includeProperties.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries))
{
query = query.Include(includeProperty);
}

if (orderBy != null)
{
return orderBy(query).ToList();
} else
{
return query.ToList();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace StoreManagementSystemX.Database.DAL.Interfaces;

public interface IProductRepository: IRepository<Product>
public interface IProductRepository : IRepository<Product>
{
Product? GetByBarcode(string barcode);

Expand Down
16 changes: 11 additions & 5 deletions StoreManagementSystemX.Database/DAL/Interfaces/IRepository.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
using System;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace StoreManagementSystemX.Database.DAL.Interfaces
{
public interface IRepository<TModel>
public interface IRepository<TEntity>
{
public IEnumerable<TModel> GetAll();
public TModel? GetById(Guid instanceId);
public void Insert(TModel newInstance);
public IEnumerable<TEntity> Get(
Expression<Func<TEntity, bool>>? filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>>? orderBy = null,
string includeProperties = "");

public TEntity? GetById(Guid instanceId);
public void Insert(TEntity newInstance);
public void Delete(Guid instanceId);
public void Save();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ namespace StoreManagementSystemX.Database.DAL.Interfaces;

public interface IStockPurchaseRepository : IRepository<StockPurchase>
{

public StockPurchase? GetById(Guid stockPurchaseId, string includeProperties = "");
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ namespace StoreManagementSystemX.Database.DAL
{
public interface ITransactionRepository: IRepository<Transaction>
{

public IEnumerable<Transaction> Find(Expression<Func<Transaction, bool>> predicate);

public Transaction? GetById(Guid transactionId, string includeProperties = "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,5 @@

namespace StoreManagementSystemX.Database.DAL.Interfaces
{
public interface IUserRepository : IRepository<User>
{

public User? Find(Expression<Func<User, bool>> predicate);

}
public interface IUserRepository : IRepository<User> { }
}
32 changes: 2 additions & 30 deletions StoreManagementSystemX.Database/DAL/PayLaterRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,13 @@

namespace StoreManagementSystemX.Database.DAL
{
public class PayLaterRepository : IPayLaterRepository
public class PayLaterRepository : BaseRepository<PayLater>, IPayLaterRepository
{

public PayLaterRepository(Context context)
{
_context = context;
}
public PayLaterRepository(Context context) : base(context) { }

private readonly Context _context;

public void Delete(Guid instanceId)
{
PayLater payLater = _context.PayLaters.Find(instanceId);
_context.PayLaters.Remove(payLater);
}

public IEnumerable<PayLater> GetAll()
{
return _context.PayLaters.ToList();
}

public PayLater? GetById(Guid instanceId)
{
return _context.PayLaters.Find(instanceId);
}

public void Insert(PayLater newInstance)
{
_context.PayLaters.Add(newInstance);
}

public void Save()
{
_context.SaveChanges();
}

}
}
65 changes: 3 additions & 62 deletions StoreManagementSystemX.Database/DAL/ProductRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,72 +9,13 @@

namespace StoreManagementSystemX.Database.DAL
{
public class ProductRepository : IProductRepository, IDisposable
public class ProductRepository : BaseRepository<Product>, IProductRepository
{

public ProductRepository(Context context)
{
_context = context;
}

private readonly Context _context;

public ProductRepository(Context context) : base(context) { }

public Product? GetByBarcode(string barcode)
=> _context.Products.Where(p => p.Barcode == barcode).FirstOrDefault();

public IEnumerable<Product> GetAll()
=> _context.Products.ToList();

public Product? GetById(Guid instanceId)
=> _context.Products.Find(instanceId);

public void Insert(Product newInstance)
{
_context.Products.Add(newInstance);
}


public void Delete(Guid instanceId)
{
Product product = _context.Products.Find(instanceId);
_context.Products.Remove(product);
}

public void Save()
{
_context.SaveChanges();
}

private bool disposed = false;

protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
_context.Dispose();
}
}
disposed = true;
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

public void Attach(Product product)
{
_context.Attach(product);
}

public void Detach(Product product)
{
_context.Entry<Product>(product).State = EntityState.Detached;
}
=> Get(p => p.Barcode == barcode).FirstOrDefault();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,10 @@

namespace StoreManagementSystemX.Database.DAL
{
public class StockPurchaseProductRepository : IStockPurchaseProductRepository
public class StockPurchaseProductRepository : BaseRepository<StockPurchaseProduct>, IStockPurchaseProductRepository
{

public StockPurchaseProductRepository(Context context)
{
_context = context;
}

private readonly Context _context;

public void Delete(Guid instanceId)
{
StockPurchaseProduct stockPurchaseProduct = _context.StockPurchaseProducts.Find(instanceId);
if (stockPurchaseProduct != null)
{
_context.StockPurchaseProducts.Remove(stockPurchaseProduct);
}
}

public IEnumerable<StockPurchaseProduct> GetAll()
{
return _context.StockPurchaseProducts.ToList();
}

public StockPurchaseProduct? GetById(Guid instanceId)
{
throw new NotImplementedException();
}

public StockPurchaseProduct? GetById(Guid stockPurchaseId, Guid productId)
{
return _context.StockPurchaseProducts.Find(stockPurchaseId, productId);
}

public void Insert(StockPurchaseProduct newInstance)
{
_context.StockPurchaseProducts.Add(newInstance);
}

public void Save()
{
_context.SaveChanges();
}
public StockPurchaseProductRepository(Context context) : base(context) { }

}
}
42 changes: 10 additions & 32 deletions StoreManagementSystemX.Database/DAL/StockPurchaseRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,22 @@

namespace StoreManagementSystemX.Database.DAL
{
public class StockPurchaseRepository : IStockPurchaseRepository
public class StockPurchaseRepository : BaseRepository<StockPurchase>, IStockPurchaseRepository
{

public StockPurchaseRepository(Context context)
{
_context = context;
}

private readonly Context _context;

public void Delete(Guid stockPurchaseId)
{
StockPurchase purchase = _context.StockPurchases.Find(stockPurchaseId);
_context.Remove(purchase);
}

public IEnumerable<StockPurchase> GetAll()
{
return _context.StockPurchases.Include(sp => sp.StockPurchaseProducts).ThenInclude(e => e.Product).ToList();
}
public StockPurchaseRepository(Context context) : base(context) { }

public StockPurchase? GetById(Guid stockPurchaseId)
public StockPurchase? GetById(Guid stockPurchaseId, string includeProperties = "")
{
return _context.StockPurchases
.Include(e => e.StockPurchaseProducts)
.ThenInclude(e => e.Product)
.Where(e => e.Id == stockPurchaseId).FirstOrDefault();
}
IQueryable<StockPurchase> query = _dbSet.Where(t => t.Id == stockPurchaseId);

public void Insert(StockPurchase stockPurchase)
{
_context.StockPurchases.Add(stockPurchase);
}
foreach (var property in includeProperties.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries))
{
query = query.Include(property);
}

public void Save()
{
_context.SaveChanges();
var entityFound = query.FirstOrDefault();
return entityFound;
}

}
}
Loading

0 comments on commit 7c4cc36

Please sign in to comment.