Upload project.
This commit is contained in:
22
ThAmCo-Stock/ThAmCo.Stock/Data/StockContext/IStockContext.cs
Normal file
22
ThAmCo-Stock/ThAmCo.Stock/Data/StockContext/IStockContext.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using ThAmCo.Stock.Models.Dto;
|
||||
|
||||
namespace ThAmCo.Stock.Data.StockContext
|
||||
{
|
||||
public interface IStockContext
|
||||
{
|
||||
Task<IEnumerable<ProductStockDto>> GetAll();
|
||||
Task<IEnumerable<Price>> GetAllPrices();
|
||||
Task<ProductStockDto> GetProductStockAsync(int id);
|
||||
Task<IEnumerable<OrderRequest>> GetAllOrderRequests();
|
||||
Task<OrderRequest> GetOrderRequest(int id);
|
||||
void AddProductStockAsync();
|
||||
Price AddPriceAsync(Price price);
|
||||
void AddOrderRequest(OrderRequest order);
|
||||
void UpdateProductStockAsync(ProductStock productStock);
|
||||
void UpdateOrderRequest(OrderRequest orderRequest);
|
||||
void ApproveOrderRequest(int id);
|
||||
void SaveAndUpdateContext();
|
||||
}
|
||||
}
|
||||
113
ThAmCo-Stock/ThAmCo.Stock/Data/StockContext/MockStockContext.cs
Normal file
113
ThAmCo-Stock/ThAmCo.Stock/Data/StockContext/MockStockContext.cs
Normal file
@@ -0,0 +1,113 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using ThAmCo.Stock.Models.Dto;
|
||||
|
||||
namespace ThAmCo.Stock.Data.StockContext
|
||||
{
|
||||
public class MockStockContext : IStockContext
|
||||
{
|
||||
private readonly List<ProductStock> _productStocks;
|
||||
private readonly List<Price> _prices;
|
||||
private readonly List<OrderRequest> _orderRequests;
|
||||
|
||||
public MockStockContext(List<ProductStock> productStocks, List<Price> prices, List<OrderRequest> orderRequests)
|
||||
{
|
||||
_productStocks = productStocks;
|
||||
_prices = prices;
|
||||
_orderRequests = orderRequests;
|
||||
}
|
||||
|
||||
public Task<IEnumerable<ProductStockDto>> GetAll()
|
||||
{
|
||||
var productStocks = new List<ProductStockDto>();
|
||||
//Janky List handling; Not linked so have to do this...
|
||||
foreach (var p in _productStocks)
|
||||
{
|
||||
productStocks.Add(new ProductStockDto
|
||||
{
|
||||
ProductStock = p,
|
||||
Price = _prices.FirstOrDefault(price => price.Id == p.PriceId)
|
||||
});
|
||||
}
|
||||
|
||||
return Task.FromResult(productStocks.AsEnumerable());
|
||||
}
|
||||
|
||||
public Task<IEnumerable<Price>> GetAllPrices()
|
||||
{
|
||||
return Task.FromResult(_prices.AsEnumerable());
|
||||
}
|
||||
|
||||
public Task<ProductStockDto> GetProductStockAsync(int id)
|
||||
{
|
||||
var prod = _productStocks.FirstOrDefault(p => p.Id == id);
|
||||
|
||||
if (prod == null)
|
||||
return Task.FromResult<ProductStockDto>(null);
|
||||
|
||||
return Task.FromResult(new ProductStockDto
|
||||
{
|
||||
ProductStock = prod,
|
||||
Price = _prices.FirstOrDefault(p => p.Id == prod.PriceId)
|
||||
});
|
||||
}
|
||||
|
||||
public Task<IEnumerable<OrderRequest>> GetAllOrderRequests()
|
||||
{
|
||||
return Task.FromResult(_orderRequests.AsEnumerable());
|
||||
}
|
||||
|
||||
public Task<OrderRequest> GetOrderRequest(int id)
|
||||
{
|
||||
return Task.FromResult(_orderRequests.FirstOrDefault(or => or.Id == id));
|
||||
}
|
||||
|
||||
public void AddProductStockAsync()
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
|
||||
public Price AddPriceAsync(Price price)
|
||||
{
|
||||
price.Id = _prices.OrderByDescending(p => p.Id).First().Id + 1;
|
||||
_prices.Add(price);
|
||||
return price;
|
||||
}
|
||||
|
||||
public void AddOrderRequest(OrderRequest order)
|
||||
{
|
||||
_orderRequests.Add(order);
|
||||
}
|
||||
|
||||
public void UpdateProductStockAsync(ProductStock productStock)
|
||||
{
|
||||
var update = _productStocks.FirstOrDefault(p => p.Id == productStock.Id);
|
||||
if (update != null && productStock != null)
|
||||
update.Id = productStock.Id;
|
||||
}
|
||||
|
||||
public void UpdateOrderRequest(OrderRequest orderRequest)
|
||||
{
|
||||
var update = _orderRequests.FirstOrDefault(or => or.Id == orderRequest.Id);
|
||||
if (update != null)
|
||||
update = orderRequest;
|
||||
}
|
||||
|
||||
public void ApproveOrderRequest(int id)
|
||||
{
|
||||
var approve = _orderRequests.FirstOrDefault(or => or.Id == id);
|
||||
if (approve != null)
|
||||
{
|
||||
approve.Approved = true;
|
||||
approve.ApprovedTime = DateTime.Now;
|
||||
}
|
||||
}
|
||||
|
||||
public void SaveAndUpdateContext()
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
104
ThAmCo-Stock/ThAmCo.Stock/Data/StockContext/StockContext.cs
Normal file
104
ThAmCo-Stock/ThAmCo.Stock/Data/StockContext/StockContext.cs
Normal file
@@ -0,0 +1,104 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ThAmCo.Stock.Models.Dto;
|
||||
|
||||
namespace ThAmCo.Stock.Data.StockContext
|
||||
{
|
||||
public class StockContext : IStockContext
|
||||
{
|
||||
private readonly StockDbContext _context;
|
||||
|
||||
public StockContext(StockDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<ProductStockDto>> GetAll()
|
||||
{
|
||||
var productStocks = new List<ProductStockDto>();
|
||||
//Janky Database handling; Not linked so have to do this...
|
||||
foreach (var p in _context.ProductStocks)
|
||||
{
|
||||
productStocks.Add(new ProductStockDto
|
||||
{
|
||||
ProductStock = p,
|
||||
Price = await _context.Prices.FirstOrDefaultAsync(price => price.Id == p.PriceId)
|
||||
});
|
||||
}
|
||||
|
||||
return productStocks;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Price>> GetAllPrices()
|
||||
{
|
||||
return await _context.Prices.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<ProductStockDto> GetProductStockAsync(int id)
|
||||
{
|
||||
var prod = await _context.ProductStocks.FirstOrDefaultAsync(p => p.Id == id);
|
||||
return new ProductStockDto
|
||||
{
|
||||
ProductStock = prod,
|
||||
Price = await _context.Prices.FirstOrDefaultAsync(p => p.Id == prod.PriceId)
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<OrderRequest>> GetAllOrderRequests()
|
||||
{
|
||||
return await _context.OrderRequests.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<OrderRequest> GetOrderRequest(int id)
|
||||
{
|
||||
return await _context.OrderRequests.FirstOrDefaultAsync(or => or.Id == id);
|
||||
}
|
||||
|
||||
public void AddProductStockAsync()
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
|
||||
public Price AddPriceAsync(Price price)
|
||||
{
|
||||
_context.Add(price);
|
||||
_context.SaveChanges();
|
||||
return price;
|
||||
}
|
||||
|
||||
public void AddOrderRequest(OrderRequest order)
|
||||
{
|
||||
_context.Add(order);
|
||||
_context.SaveChanges();
|
||||
}
|
||||
|
||||
public void UpdateProductStockAsync(ProductStock productStock)
|
||||
{
|
||||
_context.Update(productStock);
|
||||
SaveAndUpdateContext();
|
||||
}
|
||||
|
||||
public void UpdateOrderRequest(OrderRequest orderRequest)
|
||||
{
|
||||
_context.Update(orderRequest);
|
||||
_context.SaveChanges();
|
||||
}
|
||||
|
||||
public void ApproveOrderRequest(int id)
|
||||
{
|
||||
var productStock = GetOrderRequest(id).Result;
|
||||
if (productStock == null) return;
|
||||
productStock.Approved = true;
|
||||
productStock.ApprovedTime = DateTime.Now;
|
||||
_context.Update(productStock);
|
||||
}
|
||||
|
||||
public void SaveAndUpdateContext()
|
||||
{
|
||||
_context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user