public
Last active

Partially Refactored controller

  • Download Gist
BloatedController.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
public class ReducedController : Controller
{
private readonly IContainProducts productRepository;
private readonly ISearchForProducts productsIndex;
private readonly IContainCustomers customerRepository;
private readonly IShipProducts productShipper;
private readonly ICoordinateSales salesCatalog;
 
public ReducedController(
IContainProducts productRepository,
ISearchForProducts productsIndex,
IContainCustomers customerRepository,
IShipProducts productShipper,
ICoordinateSales salesCatalog)
{
this.productRepository = productRepository;
this.salesCatalog = salesCatalog;
this.productShipper = productShipper;
this.customerRepository = customerRepository;
this.productsIndex = productsIndex;
}
 
public ActionResult Create(CreateProductCommand command)
{
if(command.ValidateInto(this.ModelState))
{
var newProduct = command.CreateProduct();
productRepository.Add(newProduct);
}
return View();
}
public ActionResult SingleItem(SingleItemQuery query)
{
if (!query.MakesSense()) {
return RedirectToAction("Search");
}
var productView = query.From(productsIndex);
return View(productView);
}
 
public ActionResult Search(SearchProductsQuery query)
{
var searchView = query.From(productsIndex);
return View(searchView);
}
 
public ActionResult WhatsNew()
{
var results = productsIndex.Query()
.Where(x => x.DateAdded < DateTime.Now.Subtract(TimeSpan.FromDays(2)))
.As<RecentProductsView>()
.ToArray();
return View(results);
}
 
[Authorize]
public ActionResult Ship(ShipProductToCustomerCommand command)
{
if(command.ValidateInto(this.ModelState))
{
var product = productRepository.Get(command.ProductId);
var customer = customerRepository.Get(command.CustomerId);
productShipper.ShipProductToCustomer(product, customer);
}
return View();
}
 
[Authorize(Roles="Admin")]
public ActionResult CreateSeasonalOffer(CreateSeasonalOfferCommand command)
{
if(command.ValidateInto(this.ModelState))
{
var product = productRepository.Get(command.ProductId);
salesCatalog.CreateSeasonalOffer(product, command.PercentageOff, command.Duration);
}
return View();
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.