public Image GetImageById(int imageId) { using (StoreDBcontext context = new StoreDBcontext()) { var entityImage = context.Images.Where(e => e.ID == imageId).FirstOrDefault(); return((entityImage == null) ? null : entityImage.ConvertToApplicationModel()); } }
public Product GetProductById(int id) { using (StoreDBcontext context = new StoreDBcontext()) { ProductEntity product = context.Products.AsNoTracking() .Where(e => e.ProductId == id).FirstOrDefault(); return((product == null) ? null : product.ConvertToApplicationModel()); } }
public void DeleteImage(int imageId) { using (StoreDBcontext context = new StoreDBcontext()) { ImageEntity image = new ImageEntity { ID = imageId }; context.Entry(image).State = System.Data.Entity.EntityState.Deleted; context.SaveChanges(); } }
public void AddUser(User user) { if (user != null) { using (StoreDBcontext context = new StoreDBcontext()) { context.UserEntities.Add((UserEntity) new UserEntity().ConvertFromApplicationModel(user)); context.SaveChanges(); } } }
public void UpdateCookies(User user, string cookie) { if (user != null && cookie != null) { using (StoreDBcontext context = new StoreDBcontext()) { context.UserEntities.Where(e => e.ContactPhone == user.ContactPhone) .FirstOrDefault().Cookie = cookie; context.SaveChanges(); } } }
public IEnumerable <Product> GetProducts(SearchFilter sf) { using (StoreDBcontext context = new StoreDBcontext()) { var products = context.Products.Include("Town").AsNoTracking().AsQueryable(); if (sf.IncludeInactive == false) { products = products.Where(e => e.IsActive == true); } if (sf.UserId != 0) { products = products.Where(e => e.UserId == sf.UserId); } if (!String.IsNullOrEmpty(sf.FilterString)) { products = products.Where(e => e.Name.ToLower().Contains(sf.FilterString.ToLower())); } if (!String.IsNullOrEmpty(sf.TownName)) { products = products.Where(e => e.Town.TownName == sf.TownName); } else if (String.IsNullOrEmpty(sf.TownName) && sf.Regions != 1) { products = products.Where(e => e.Town.RegionId == sf.Regions); } if (sf.SearchOptions == Constants.Options[1] || String.IsNullOrEmpty(sf.SearchOptions)) //"Cамые новые" { products = products.OrderByDescending(e => e.AddingTime); } else if (sf.SearchOptions == Constants.Options[2]) // "Caмые дешевые" { products = products.OrderBy(e => e.Price); } if (products.Count() == 0) { return(null); } List <Product> productModels = new List <Product>(); foreach (var p in products) { productModels.Add(p.ConvertToApplicationModel()); } return(productModels); } }
public void SetInactiveProduct(int productId) { using (StoreDBcontext context = new StoreDBcontext()) { var product = context.Products.Where(e => e.ProductId == productId).FirstOrDefault(); if (product != null) { product.IsActive = false; context.SaveChanges(); } } }
public void AddTown(Town town) { using (StoreDBcontext context = new StoreDBcontext()) { TownEntity townEntity = new TownEntity { RegionId = town.RegionId, TownName = town.TownName }; context.SaveChanges(); } }
public bool CheckIfRegistered(string phonenum) { if (phonenum == null) { return(false); } using (StoreDBcontext context = new StoreDBcontext()) { var p = context.UserEntities.Where(e => e.ContactPhone == phonenum).FirstOrDefault(); return((context.UserEntities.Where(e => e.ContactPhone == phonenum).FirstOrDefault() == null) ? false : true); } }
public List <Region> GetRegions() { using (StoreDBcontext context = new StoreDBcontext()) { List <Region> regions = new List <Region>(); var entityRegions = context.Regions.Include("Towns").AsNoTracking(); foreach (var r in entityRegions) { regions.Add(r.ConvertToApplicationModel()); } return(regions); } }
public User GetUserByCookies(string cookie) { if (cookie == null) { return(null); } using (StoreDBcontext context = new StoreDBcontext()) { var user = context.UserEntities.AsNoTracking(). Where(e => e.Cookie == cookie).FirstOrDefault(); return((user != null) ? user.ConvertToApplicationModel() : null); } }
public User GetUser(Login login) { if (login == null) { return(null); } using (StoreDBcontext context = new StoreDBcontext()) { var user = context.UserEntities.AsNoTracking(). Where(e => e.ContactPhone == login.LoginString && e.Password == login.Password). FirstOrDefault(); return((user != null) ? user.ConvertToApplicationModel() : null); } }
public void UpdateUser(User user) { if (user != null) { bool needUpdate = false; user.Password = Guid.NewGuid().ToString().Take(5).ToString(); using (StoreDBcontext context = new StoreDBcontext()) { UserEntity userEntity = context.UserEntities.Find(user.UserId); if (userEntity != null) { if (userEntity.FirstName != user.FirstName) { userEntity.FirstName = user.FirstName; needUpdate = true; } if (userEntity.LastName != user.LastName) { userEntity.LastName = user.LastName; needUpdate = true; } if (userEntity.Email != user.Email) { userEntity.Email = user.Email; needUpdate = true; } if (userEntity.ContactInfo != user.ContactInfo) { userEntity.ContactInfo = user.ContactInfo; needUpdate = true; } } if (needUpdate) { context.SaveChanges(); } } } }
public List <Town> GetTownsByRegion(int regionId) { using (StoreDBcontext context = new StoreDBcontext()) { var entityTowns = context.Towns.AsNoTracking().AsQueryable(); List <Town> towns = new List <Town>(); if (regionId != 1) { entityTowns = entityTowns.Where(e => e.RegionId == regionId); } foreach (var t in entityTowns) { towns.Add(t.ConvertToApplicationModel()); } return(towns); } }
public void AddProduct(Product product) { ProductEntity productEntity = (ProductEntity) new ProductEntity().ConvertFromApplicationModel(product); using (StoreDBcontext context = new StoreDBcontext()) { var town = context.Towns.Where(e => e.TownName == product.Town.TownName && e.RegionId == product.Town.RegionId).FirstOrDefault(); if (town != null) { productEntity.TownId = town.ID; } else { TownEntity townEntity = new TownEntity { TownName = product.Town.TownName, RegionId = product.Town.RegionId }; productEntity.Town = townEntity; } context.Products.Add(productEntity); context.SaveChanges(); } }
public void UpdateProduct(Product product) { using (StoreDBcontext context = new StoreDBcontext()) { bool HasChanged = false; var dbProduct = context.Products .Where(e => e.ProductId == product.ProductId).FirstOrDefault(); if (product.Name != dbProduct.Name) { dbProduct.Name = product.Name; HasChanged = true; } if (product.Price != dbProduct.Price) { dbProduct.Price = product.Price; HasChanged = true; } if (product.Description != dbProduct.Description) { dbProduct.Description = product.Description; HasChanged = true; } if (product.Contacts != dbProduct.Contacts) { dbProduct.Contacts = product.Contacts; } if (product.Town.RegionId != dbProduct.Town.RegionId) { dbProduct.Town.RegionId = product.Town.RegionId; HasChanged = true; } if (product.Town.TownName != dbProduct.Town.TownName) { var town = context.Towns.Where(e => e.TownName == product.Town.TownName && e.RegionId == product.Town.RegionId).FirstOrDefault(); if (town != null) { dbProduct.TownId = town.ID; } else { TownEntity townEntity = new TownEntity { TownName = product.Town.TownName, RegionId = product.Town.RegionId }; dbProduct.Town = townEntity; } HasChanged = true; } //Images comparison int currentImagesCount = product.Images.Value.Count; List <ImageEntity> dbImages = dbProduct.Images.OrderBy(e => e.ID).ToList(); if (currentImagesCount > 0) { HasChanged = true; for (int i = 0; i < currentImagesCount; i++) { if (product.Images.Value[i] != null) { if (dbImages.ElementAtOrDefault(i) != null && dbImages.ElementAtOrDefault(i).ImageUrl != product.Images.Value[i].ImageUrl) { int id = dbImages[i].ID; var img = context.Images.Where(e => e.ID == id).FirstOrDefault(); img.ImageUrl = product.Images.Value[i].ImageUrl; } else if (dbImages.ElementAtOrDefault(i) == null) { dbProduct.Images.Add(new ImageEntity().ConvertFromApplicationModel(product.Images.Value[i]) as ImageEntity); } } } } if (HasChanged) { context.SaveChanges(); } } }