Deal and Stock Controllers update

This commit is contained in:
thermalthrottle
2021-03-20 17:27:23 +02:00
parent e404892b4a
commit f559479200
2 changed files with 120 additions and 28 deletions

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Business.Business.UserManagment;
using Models;
using Models.Models;
@@ -12,11 +13,70 @@ namespace Business.Business.Sales
private LuminousContext context = new LuminousContext();
private User currentUser;
private ProductController productCtrl;
private UserController userctrl;
public DealController(User currentUser)
{
this.productCtrl = new ProductController(currentUser);
this.currentUser = currentUser;
this.productCtrl = new ProductController(currentUser);
this.userctrl = new UserController(currentUser);
}
public ICollection<Deal> GetAll()
{
return context.Deal.ToList();
}
public ICollection<Deal> GetByTime(DateTime startTime, DateTime endTime)
{
if (currentUser.RoleId > 1)
{
return context.Deal.Where(x => x.Time <= endTime && x.Time >= startTime).ToList();
}
else
{
throw new ArgumentException("Insufficient role!");
}
}
public ICollection<Deal> GetByUser(int id)
{
if (currentUser != null || currentUser.RoleId > 1)
{
var user = userctrl.Get(id);
if (user != null)
{
return GetAll().Where(u => u.UserId == user.Id).ToList();
}
else
{
throw new ArgumentException("User not found");
}
}
else
{
throw new ArgumentException("Insufficient role!");
}
}
public ICollection<Deal> GetByUser(string username)
{
if (currentUser != null || currentUser.RoleId > 1)
{
var user = userctrl.Get(username);
if (user != null)
{
return GetAll().Where(u => u.UserId == user.Id).ToList();
}
else
{
throw new ArgumentException("User not found");
}
}
else
{
throw new ArgumentException("Insufficient role!");
}
}
public void Add(int productId, double Amount, DateTime time)
{
if (Amount > 0)
@@ -31,6 +91,11 @@ namespace Business.Business.Sales
throw new ArgumentException("Amount cannot be negative");
}
}
public Deal Get(int id)
{
return context.Deal.Find(id);
}
public void Add(string productName, double Amount, DateTime time)
{
@@ -51,7 +116,7 @@ namespace Business.Business.Sales
public void Delete(int id)
{
if (currentUser.RoleId > 1)
if (currentUser != null || currentUser.RoleId > 1)
{
var deal = Get(id);
if (deal != null)
@@ -71,27 +136,5 @@ namespace Business.Business.Sales
throw new ArgumentException("Insufficient role!");
}
}
public Deal Get(int id)
{
return context.Deal.Find(id);
}
public ICollection<Deal> GetAll()
{
return context.Deal.ToList();
}
public ICollection<Deal> GetByTime(DateTime startTime, DateTime endTime)
{
if (currentUser.RoleId > 1)
{
return context.Deal.Where(x => x.Time <= endTime && x.Time >= startTime).ToList();
}
else
{
throw new ArgumentException("Insufficient role!");
}
}
}
}

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Business.Business.UserManagment;
using Microsoft.EntityFrameworkCore;
using Models;
using Models.Models;
@@ -13,16 +14,24 @@ namespace Business.Business.Sales
private LuminousContext context = new LuminousContext();
private User currentUser;
private ProductController productCtrl;
private UserController userctrl;
public StockController(User currentUser)
{
this.productCtrl = new ProductController(currentUser);
this.currentUser = currentUser;
this.productCtrl = new ProductController(currentUser);
this.userctrl = new UserController(currentUser);
}
public StockController(User currentUser, ProductController productctrl, UserController userctrl)
{
this.currentUser = currentUser;
this.productCtrl = new ProductController(currentUser);
this.userctrl = new UserController(currentUser);
}
public ICollection<Stock> GetAll()
{
if (currentUser.RoleId > 1)
if (currentUser != null || currentUser.RoleId > 1)
{
return context.Stock.ToList();
}
@@ -35,7 +44,7 @@ namespace Business.Business.Sales
public Stock Get(int id)
{
if (currentUser.RoleId > 1)
if (currentUser != null || currentUser.RoleId > 1)
{
return context.Stock.Find(id);
}
@@ -47,7 +56,7 @@ namespace Business.Business.Sales
public ICollection<Stock> GetByTime(DateTime startTime, DateTime endTime)
{
if (currentUser.RoleId > 1)
if (currentUser != null || currentUser.RoleId > 1)
{
return context.Stock.Where(x => x.Time <= endTime && x.Time >= startTime).ToList();
}
@@ -57,6 +66,46 @@ namespace Business.Business.Sales
}
}
public ICollection<Stock> GetByUser(int id)
{
if (currentUser != null || currentUser.RoleId == 3)
{
var user = userctrl.Get(id);
if (user != null)
{
return GetAll().Where(u => u.UserId == user.Id).ToList();
}
else
{
throw new ArgumentException("User not found");
}
}
else
{
throw new ArgumentException("Insufficient role!");
}
}
public ICollection<Stock> GetByUser(string username)
{
if (currentUser != null || currentUser.RoleId == 3)
{
var user = userctrl.Get(username);
if (user != null)
{
return GetAll().Where(u => u.UserId == user.Id).ToList();
}
else
{
throw new ArgumentException("User not found");
}
}
else
{
throw new ArgumentException("Insufficient role!");
}
}
public void Add(int productId, double Amount, DateTime time)
{
if (currentUser.RoleId > 1)