Skip to content

Instantly share code, notes, and snippets.

@robashton
Created June 13, 2011 16:32
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save robashton/1023120 to your computer and use it in GitHub Desktop.
Save robashton/1023120 to your computer and use it in GitHub Desktop.
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