using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Web; using Weee.DAL; using Weee.Models; namespace Weee.Service { public class WeeeProductDataService :WeeeDataAuthorizeService { public WeeeProductDataService(WeeeDataContext d) : base(d) { } public Product GetProduct(int id) { return _db.Products.Where(x => x.ID == id).First(); } public IEnumerable GetProducts(string searchString = "") { IEnumerable ret; if (loggedInUserCompanyAdmin) ret = _db.Products.Where(x => x.CompanyID == CurrentCompany.ID).OrderByDescending(p => p.ID).ToList(); else ret = _db.Products.Where(x => x.UserId == loggedInUserId).OrderByDescending(p => p.ID).ToList(); return ret; } //CFT-22 public IEnumerable GetProductsByOrder(string orderBy = "", string orderDir = "") { var productList = _db.Products.Where(x => x.CompanyID == CurrentCompany.ID).OrderByDescending(p => p.ID).ToList(); if (orderBy != "") { if (orderDir != "") { productList = OrderProducts(productList, orderBy, orderDir); return productList; } } return productList; } public Product SaveProduct(Product toBeSave) { toBeSave.CompanyID = CurrentCompany.ID; var entry = _db.Entry(toBeSave); if (toBeSave.ID == 0) entry.State = EntityState.Added; else if (AuthorizedProducts.Contains(toBeSave.ID)) { entry.State = EntityState.Modified; entry.Property(x => x.CompanyID).IsModified = false; } _db.SaveChanges(); return toBeSave; } public List OrderProducts(List list, string orderBy, string orderDir) { if (orderDir.Trim().ToLower() == "asc") { list = list.OrderBy(p => p.GetType().GetProperty(orderBy).GetValue(p, null)).ToList(); } else { list = list.OrderByDescending(p => p.GetType().GetProperty(orderBy).GetValue(p, null)).ToList(); } return list; } public bool CheckNameExist(string name, int id) { var exist = (from a in _db.Products where a.Name == name && a.ID != id select a).Any(); return exist; } } }