DealController, code style
This commit is contained in:
@@ -8,7 +8,7 @@ namespace Business.Business.Sales
|
||||
{
|
||||
ICollection<T> GetAll();
|
||||
T Get(int id);
|
||||
ICollection<T> GetByTime(DateTime time);
|
||||
ICollection<T> GetByTime(DateTime startTime, DateTime endTime);
|
||||
void Add(int productId, double Amount);
|
||||
void Add(string productName, double Amount);
|
||||
void Delete(int id);
|
||||
|
94
LuminousSales/Business/Business/Sales/DealController.cs
Normal file
94
LuminousSales/Business/Business/Sales/DealController.cs
Normal file
@@ -0,0 +1,94 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Models;
|
||||
using Models.Models;
|
||||
|
||||
namespace Business.Business.Sales
|
||||
{
|
||||
public class DealController : ISalesController<Deal>
|
||||
{
|
||||
private LuminousContext context = new LuminousContext();
|
||||
private User currentUser;
|
||||
private ProductController productCtrl;
|
||||
public DealController(User currentUser)
|
||||
{
|
||||
this.currentUser = currentUser;
|
||||
}
|
||||
public void Add(int productId, double Amount)
|
||||
{
|
||||
if (Amount > 0)
|
||||
{
|
||||
var deal = new Deal(currentUser.Id, productId, Amount);
|
||||
context.Deal.Add(deal);
|
||||
context.SaveChanges();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Amount cannot be negative");
|
||||
}
|
||||
}
|
||||
|
||||
public void Add(string productName, double Amount)
|
||||
{
|
||||
if (Amount > 0)
|
||||
{
|
||||
productCtrl = new ProductController(currentUser);
|
||||
var productId = productCtrl.Get(productName).Id;
|
||||
var deal = new Deal(currentUser.Id, productId, Amount);
|
||||
context.Deal.Add(deal);
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Amount cannot be negative");
|
||||
}
|
||||
}
|
||||
|
||||
public void Delete(int id)
|
||||
{
|
||||
if (currentUser.RoleId > 1)
|
||||
{
|
||||
var deal = Get(id);
|
||||
if (deal != null)
|
||||
{
|
||||
context.Deal.Remove(deal);
|
||||
context.SaveChanges();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("User not found");
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
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!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -21,22 +21,39 @@ namespace Business.Business.Sales
|
||||
|
||||
public ICollection<Stock> GetAll()
|
||||
{
|
||||
if (currentUser.RoleId > 1)
|
||||
return context.Stock.ToList();
|
||||
else throw new InvalidOperationException("Cannot return all stocks!");
|
||||
if (currentUser.RoleId > 1)
|
||||
{
|
||||
return context.Stock.ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidOperationException("Cannot return all stocks!");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public Stock Get(int id)
|
||||
{
|
||||
if (currentUser.RoleId > 1)
|
||||
return context.Stock.Find(id);
|
||||
else throw new InvalidOperationException("Cannot get stock!");
|
||||
{
|
||||
return context.Stock.Find(id);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidOperationException("Cannot get stock!");
|
||||
}
|
||||
}
|
||||
|
||||
public ICollection<Stock> GetByTime(DateTime time)
|
||||
public ICollection<Stock> GetByTime(DateTime startTime, DateTime endTime)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
if (currentUser.RoleId > 1)
|
||||
{
|
||||
return context.Stock.Where(x => x.Time <= endTime && x.Time >= startTime).ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Insufficient role!");
|
||||
}
|
||||
}
|
||||
|
||||
public void Add(int productId, double Amount)
|
||||
@@ -49,29 +66,40 @@ namespace Business.Business.Sales
|
||||
context.Stock.Add(stock);
|
||||
context.SaveChanges();
|
||||
}
|
||||
else throw new ArgumentException("Amount cannot be negative");
|
||||
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Amount cannot be negative");
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Insufficient role!");
|
||||
}
|
||||
else throw new ArgumentException("Insufficient role!");
|
||||
}
|
||||
|
||||
public void Add(string productName, double Amount)
|
||||
{
|
||||
if (currentUser.RoleId > 1)
|
||||
{
|
||||
if (Amount > 0)
|
||||
{
|
||||
if (Amount > 0)
|
||||
{
|
||||
productCtrl = new ProductController(currentUser);
|
||||
var productId = productCtrl.Get(productName).Id;
|
||||
var stock = new Stock(currentUser.Id, productId , Amount);
|
||||
var stock = new Stock(currentUser.Id, productId, Amount);
|
||||
context.Stock.Add(stock);
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
else throw new ArgumentException("Amount cannot be negative");
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Amount cannot be negative");
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Insufficient role!");
|
||||
}
|
||||
else throw new ArgumentException("Insufficient role!");
|
||||
|
||||
}
|
||||
|
||||
@@ -85,13 +113,17 @@ namespace Business.Business.Sales
|
||||
context.Stock.Remove(user);
|
||||
context.SaveChanges();
|
||||
}
|
||||
else throw new ArgumentException("User not found");
|
||||
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("User not found");
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException("Insufficient role!");
|
||||
}
|
||||
else throw new ArgumentException("Insufficient role!");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user