/SSL.Application/StorePersistenceService.cs
http://smartshoppinglist.codeplex.com · C# · 269 lines · 233 code · 35 blank · 1 comment · 53 complexity · 1e762df4f2841d614069eb55875824e3 MD5 · raw file
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using CBSI.SSL.Domain;
- using CBSI.DataAccess;
- using CBSI.Common;
- using System.Linq.Expressions;
- using CBSI.SSL.Commands;
- using CBSI.Common.Application;
- using CBSI.SSL.Rules;
-
- namespace CBSI.SSL.Application
- {
- [UnitOfWorkConversation]
- public class StorePersistenceService : PersistenceService, IStorePersistenceService
- {
- readonly IStoreRepository _StoreRepository;
- public StorePersistenceService(IStoreRepository storeRepository)
- {
- if (storeRepository == null)
- throw new ArgumentNullException("storeRepository", "StoreRepository is required!");
- _StoreRepository = storeRepository;
- }
-
- [UnitOfWorkConversationPart]
- public IList<Store> GetList(Expression<Func<Store, bool>> expression)
- {
- return _StoreRepository.GetAll(expression);
- }
-
- [UnitOfWorkConversationPart]
- public Store GetSingle(Expression<Func<Store, bool>> expression)
- {
- return _StoreRepository.Get(expression);
- }
-
- [UnitOfWorkConversationPart]
- public long Count(Shopper shopper)
- {
- return _StoreRepository.Count(s => s.Shopper.ID == shopper.ID);
- }
-
- [UnitOfWorkConversationPart]
- public IList<Store> GetPage(Shopper shopper, int page, int pageSize)
- {
- return _StoreRepository.GetPage(shopper, page, pageSize);
- }
-
- #region Product Categories
-
- [UnitOfWorkConversationPart]
- public ProductCategory Handle(AddNewProductCategory commandMessage)
- {
- var store = GetSingle(g => g.ID== commandMessage.StoreID);
- if (store == null)
- throw new ApplicationException(string.Format("Could not retrieve Store with ID = '{0}'", commandMessage.StoreID));
-
- ProductCategory superCategory = null;
- if (commandMessage.SuperCategoryID != Guid.Empty)
- superCategory = GetCategory(store, commandMessage.SuperCategoryID);
-
- var category = new ProductCategory(store, commandMessage, superCategory);
- Validate(store, category);
- store.AddProductCategory(category);
- store = _StoreRepository.Modify(store);
- return GetCategory(store, commandMessage.Name);
- }
-
- [UnitOfWorkConversationPart]
- public void Handle(ChangeProductCategory commandMessage)
- {
- var store = GetStore(commandMessage.StoreID);
- var category = GetCategory(store, commandMessage.ID);
-
- ProductCategory superCategory = null;
- if (commandMessage.SuperCategoryID != Guid.Empty)
- superCategory = GetCategory(store, commandMessage.SuperCategoryID);
-
- category.Change(commandMessage, superCategory);
- Validate(store, category);
- _StoreRepository.Modify(store);
- }
-
- [UnitOfWorkConversationPart]
- public void Handle(DeleteProductCategory commandMessage)
- {
- var store = GetStore(commandMessage.StoreID);
- var category = GetCategory(store, commandMessage.ID);
- var products = store.Products.Where(p => p.Category.ID==category.ID).ToList();
- if (products.Count > 0)
- throw new ApplicationException(string.Format("Can not delete {0} because it has product dependencies!", category.Name));
- store.RemoveProductCategory(category);
- _StoreRepository.Modify(store);
- }
-
- [UnitOfWorkConversationPart]
- public long Handle(ImportProductCategories commandMessage)
- {
- if (commandMessage.SelectedProductCategories == null | commandMessage.SelectedProductCategories.Length <= 0)
- throw new ApplicationException("There are no Product Categories to import!");
- long count = 0;
- var store = GetStore(commandMessage.ID);
- var systemStore = GetSystemStore();
- foreach (var guid in commandMessage.SelectedProductCategories)
- {
- var systemProductCategory = GetCategory(systemStore, guid);
- var productCategory = store.Categories.Where(pc => pc.Name == systemProductCategory.Name).SingleOrDefault();
- if (productCategory==null)
- {
- var productCategoryCommand = new AddNewProductCategory { StoreID = store.ID, Name = systemProductCategory.Name, IsActive=true };
- productCategory = new ProductCategory(store, productCategoryCommand, systemProductCategory.SuperCategory);
- store.AddProductCategory(productCategory);
- count++;
- }
- if (commandMessage.IsImportProducts)
- {
- // Get all product available for this "System Defined" Store for this product category
- foreach (var systemProduct in systemStore.Products.Where(p=>p.Category.ID==systemProductCategory.ID))
- {
- var product = store.Products.Where(p => p.Name == systemProduct.Name).SingleOrDefault();
- if (product == null)
- {
- var productCommand = new AddNewProduct { StoreID = store.ID, Name = systemProduct.Name, ProductCategoryID = productCategory.ID };
- product = new Product(store, productCategory, productCommand);
- store.AddProduct(product);
- }
- }
- }
- }
- store = _StoreRepository.Modify(store);
- return count;
- }
-
- #endregion Product Categories
-
- #region Products
-
- [UnitOfWorkConversationPart]
- public Product Handle(AddNewProduct commandMessage)
- {
- var store = GetSingle(g => g.ID == commandMessage.StoreID);
- if (store == null)
- throw new ApplicationException(string.Format("Could not retrieve Store with ID = '{0}'", commandMessage.StoreID));
-
- ProductCategory productCategory = GetCategory(store, commandMessage.ProductCategoryID);
- var product = new Product(store, productCategory, commandMessage);
- Validate(store, product);
- store.AddProduct(product);
- store = _StoreRepository.Modify(store);
- return GetProduct(store, commandMessage.Name);
- }
-
- [UnitOfWorkConversationPart]
- public void Handle(ChangeProduct commandMessage)
- {
- var store = GetStore(commandMessage.StoreID);
- var product = GetProduct(store, commandMessage.ID);
-
- ProductCategory productCategory = GetCategory(store, commandMessage.ProductCategoryID);
-
- product.Change(commandMessage, productCategory);
- Validate(store, product);
- _StoreRepository.Modify(store);
- }
-
- [UnitOfWorkConversationPart]
- public void Handle(DeleteProduct commandMessage)
- {
- var store = GetStore(commandMessage.StoreID);
- var product = GetProduct(store, commandMessage.ID);
-
- var shoppingLists = store.Shopper.ShoppingLists;
- foreach (var sl in shoppingLists)
- {
- foreach (var sld in sl.Detail)
- {
- if (sld.Product.ID == product.ID)
- {
- throw new ApplicationException(string.Format("Can not delete {0} because it has Shopping List dependencies!", product.Name));
- }
- }
- }
-
- store.RemoveProduct(product);
- _StoreRepository.Modify(store);
- }
-
- #endregion Products
-
- Store GetStore(Guid storeID)
- {
- var store = GetSingle(s => s.ID == storeID);
- if (store == null)
- throw new ApplicationException(string.Format("Could not retrieve Store with ID = '{0}'", storeID));
- return store;
- }
-
- Store GetSystemStore()
- {
- var store = GetSingle(s => s.Name == "System Defined");
- if (store == null)
- throw new ApplicationException("Could not retrieve System Store!");
- return store;
- }
-
- ProductCategory GetCategory(Store store, Guid pcID)
- {
- var category = store.Categories.Where(s => s.ID == pcID).SingleOrDefault();
- if (category == null)
- throw new ApplicationException(string.Format("Could not retrieve Product Category with ID = '{0}'", pcID));
- return category;
- }
-
- ProductCategory GetCategory(Store store, string name)
- {
- var category = store.Categories.Where(s => s.Name == name).SingleOrDefault();
- if (category == null)
- throw new ApplicationException(string.Format("Could not retrieve Product Category with Name = '{0}'", name));
- return category;
- }
-
- void Validate(Store store, ProductCategory category)
- {
- CheckIfAlreadyExists(store, category);
- string validationErrors = EntityValidatorService<ProductCategory>.Validate(new ProductCategoryRules(), category);
- if (!string.IsNullOrEmpty(validationErrors))
- throw new ApplicationException(validationErrors);
- }
-
- void CheckIfAlreadyExists(Store store, ProductCategory category)
- {
- var p = store.Categories.Where(c => c.Name == category.Name).SingleOrDefault();
- if (p != null && category.ID != p.ID)
- throw new ApplicationException(string.Format("There is already a Category with name '{0}' in '{1}'!", category.Name, store.Name));
- }
-
- void Validate(Store store, Product product)
- {
- CheckIfAlreadyExists(store, product);
- string validationErrors = EntityValidatorService<Product>.Validate(new ProductRules(), product);
- if (!string.IsNullOrEmpty(validationErrors))
- throw new ApplicationException(validationErrors);
- }
-
- void CheckIfAlreadyExists(Store store, Product product)
- {
- var p = store.Products.Where(c => c.Name == product.Name).SingleOrDefault();
- if (p != null && product.ID != p.ID)
- throw new ApplicationException(string.Format("There is already a Product with name '{0}' in '{1}'!", product.Name, store.Name));
- }
-
- Product GetProduct(Store store, string name)
- {
- var product = store.Products.Where(s => s.Name == name).SingleOrDefault();
- if (product == null)
- throw new ApplicationException(string.Format("Could not retrieve Product with Name = '{0}'", name));
- return product;
- }
-
- Product GetProduct(Store store, Guid pcID)
- {
- var product = store.Products.Where(s => s.ID == pcID).SingleOrDefault();
- if (product == null)
- throw new ApplicationException(string.Format("Could not retrieve Product with ID = '{0}'", pcID));
- return product;
- }
- }
- }