DealController, code style

This commit is contained in:
Dimitar Byalkov
2021-03-19 00:59:46 +02:00
parent a6fb81d1eb
commit 6ed05ff82f
3 changed files with 150 additions and 24 deletions

View File

@@ -8,7 +8,7 @@ namespace Business.Business.Sales
{ {
ICollection<T> GetAll(); ICollection<T> GetAll();
T Get(int id); T Get(int id);
ICollection<T> GetByTime(DateTime time); ICollection<T> GetByTime(DateTime startTime, DateTime endTime);
void Add(int productId, double Amount); void Add(int productId, double Amount);
void Add(string productName, double Amount); void Add(string productName, double Amount);
void Delete(int id); void Delete(int id);

View 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!");
}
}
}
}

View File

@@ -21,22 +21,39 @@ namespace Business.Business.Sales
public ICollection<Stock> GetAll() public ICollection<Stock> GetAll()
{ {
if (currentUser.RoleId > 1) if (currentUser.RoleId > 1)
return context.Stock.ToList(); {
else throw new InvalidOperationException("Cannot return all stocks!"); return context.Stock.ToList();
}
else
{
throw new InvalidOperationException("Cannot return all stocks!");
}
} }
public Stock Get(int id) public Stock Get(int id)
{ {
if (currentUser.RoleId > 1) 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) public void Add(int productId, double Amount)
@@ -49,29 +66,40 @@ namespace Business.Business.Sales
context.Stock.Add(stock); context.Stock.Add(stock);
context.SaveChanges(); 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) public void Add(string productName, double Amount)
{ {
if (currentUser.RoleId > 1) if (currentUser.RoleId > 1)
{ {
if (Amount > 0) if (Amount > 0)
{ {
productCtrl = new ProductController(currentUser); productCtrl = new ProductController(currentUser);
var productId = productCtrl.Get(productName).Id; 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.Stock.Add(stock);
context.SaveChanges(); context.SaveChanges();
} }
else
else throw new ArgumentException("Amount cannot be negative"); {
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.Stock.Remove(user);
context.SaveChanges(); 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!");
} }
} }
} }