Skip to content

@robashton /BloatedController.cs
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Partially Refactored controller
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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.