using System; using System.Collections.Generic; using System.Linq; using HibernateMVCTest.ServiceClasses; using linqExpression = System.Linq.Expressions; using System.Web; using System.Web.Mvc; using Castle.MicroKernel.Registration.Interceptor; using HibernateMVCTest.Models; using NHibernate; using NHibernate.Criterion; using Queries; using Rhino.Commons; using ProductModelActiveRecord; using NHibernate.Linq; namespace HibernateMVCTest.Controllers { public class CatalogController : Controller { public ActionResult Index() { return null; } public IList GetList(IList>> criteria) { var query = from item in UnitOfWork.CurrentSession.Linq() select item; foreach (var criterion in criteria) { query.Where(criterion); } return query.ToList(); } //this is only an example -- this stuff shouldn't be hardcoded public ActionResult ListPagedProduct(int? ID) { int itemsPerPage = 5; int startIndex = (ID.Value - 1)* itemsPerPage; var queryHandler = new QueryHandler(); var p = queryHandler.GetList().Skip(startIndex).Take(itemsPerPage).ToList(); return RenderView("ListProduct", p); } public ActionResult ListProduct() { var queryHandler = new QueryHandler(); var p = queryHandler.GetList().ToList(); return RenderView("ListProduct", p); } public ActionResult ViewProduct(string ID) { var queryHandler = new QueryHandler(); queryHandler.AddCriteria(item => item.Title == ID); var p = queryHandler.GetList().First(); if (p != null) { return RenderView("DisplayProduct", p); } return RenderView("DisplayProduct"); } //This controller is ugly. Do not make controllers like this. public ActionResult InsertProduct(string Title, string ImagePath, string Description, string RelatedProducts, string ProductGroups) { var p = new Product(); p.Title = Title; p.ImagePath = ImagePath; p.Description = Description; if (!string.IsNullOrEmpty(RelatedProducts)) { Product tmpProduct = null; foreach (string s in RelatedProducts.Split(',')) { tmpProduct = Repository.Get(s); if (tmpProduct != null) {p.RelatedProducts.Add(tmpProduct);} } } if (!string.IsNullOrEmpty(ProductGroups)) { ProductGroup tmpPG = null; foreach(string s in ProductGroups.Split(',')) { tmpPG = Repository.Get(s); if (tmpPG != null) {p.ProductGroups.Add(tmpPG); } } } Repository.Save(p); UnitOfWork.Current.Flush(); return RedirectToAction(new{Controller="Catalog", Action="ViewProduct", ID=p.Title}); } public ActionResult AddProduct(string ID) { var d = new AddProductViewData(); d.RelatedProducts = Repository.FindAll(); d.ProductGroups = Repository.FindAll(); return RenderView("AddProduct", d); } public ActionResult AddProductGroup() { return RenderView("AddProductGroup"); } public ActionResult InsertProductGroup(string Title) { ProductGroup pg = new ProductGroup(); pg.Title = Title; Repository.Save(pg); UnitOfWork.Current.Flush(); return RenderView("AddProductGroup"); } } }