コード例 #1
0
        public void CreateNewSale(string userId, CreateSaleViewModel model)
        {
            using (var context = new HardwareShopContext())
            {
                var user = context.Users.FirstOrDefault(u => u.Id == userId);
                var cart = context.Carts.FirstOrDefault(c => c.UserId == userId && c.IsSold == false);
                var sale = Mapper.Instance.Map <CreateSaleViewModel, Sale>(model);

                sale.Cart       = cart;
                sale.SaleDate   = DateTime.Now;
                sale.TotalPrice = cart.Items.Sum(i => i.Price);

                cart.IsSold = true;

                foreach (var item in cart.Items)
                {
                    item.Quantity--;
                }

                user.Carts.Add(new Cart());
                context.Sales.Add(sale);
                context.SaveChanges();

                context.Items.Where(i => i.Quantity < 1).Update(i => new Item {
                    ItemStatus = ItemStatus.OutOfStock
                });
                context.SaveChanges();
            }
        }
コード例 #2
0
        public void EditItem(EditItemViewModel model)
        {
            using (var context = new HardwareShopContext())
            {
                var item = this.GetItemById(model.ItemId, context);

                item.SubCategoryId        = model.SubCategoryId;
                item.Description          = model.Description;
                item.Quantity             = model.Quantity;
                item.Price                = model.Price;
                item.NewPrice             = model.NewPrice;
                item.Model                = model.Model;
                item.ItemStatus           = model.ItemStatus;
                item.ManufacturerName     = model.ManufacturerName;
                item.WarrantyLengthMonths = model.WarrantyLengthMonths;

                if (model.PictureBase != null)
                {
                    MemoryStream stream = new MemoryStream();
                    model.PictureBase.InputStream.CopyTo(stream);
                    byte[] pictureInData = stream.ToArray();

                    item.Picture = pictureInData;
                }

                context.SaveChanges();
            }
        }
コード例 #3
0
        public void Delete(int categoryId)
        {
            using (var context = new HardwareShopContext())
            {
                var category = this.GetCategoryById(categoryId, context);

                category.IsDeleted = true;

                context.SubCategories.Where(sc => sc.CategoryId == category.CategoryId).Update(c => new SubCategory {
                    IsDeleted = true
                });
                context.Items.Where(i => i.SubCategory.CategoryId == category.CategoryId).Update(i => new Item {
                    IsDeleted = true
                });
                context.Reviews.Where(r => r.Item.SubCategory.CategoryId == category.CategoryId).Update(r => new Review {
                    IsDeleted = true
                });
                context.Comments.Where(c => c.Review.Item.SubCategory.CategoryId == category.CategoryId).Update(c => new Comment {
                    IsDeleted = true
                });

                context.Entry(category).State = EntityState.Modified;
                context.SaveChanges();
            }
        }
コード例 #4
0
 public void ClearItemsFromCart(string userId)
 {
     using (var context = new HardwareShopContext())
     {
         context.Carts.FirstOrDefault(s => s.UserId == userId && s.IsSold == false).Items.Clear();
         context.SaveChanges();
     }
 }
コード例 #5
0
        public void DeleteComment(int commentId)
        {
            using (var context = new HardwareShopContext())
            {
                var comment = GetCommentById(commentId, context);
                comment.IsDeleted = true;

                context.SaveChanges();
            }
        }
コード例 #6
0
        public void EditComment(CommentViewModel model)
        {
            using (var context = new HardwareShopContext())
            {
                var comment = this.GetCommentById(model.CommentId, context);
                comment.Content = model.Content;

                context.SaveChanges();
            }
        }
        public void Restore(int subCategoryId)
        {
            using (var context = new HardwareShopContext())
            {
                var subCategory = this.GetSubCategoryById(subCategoryId, context);

                subCategory.IsDeleted = false;
                context.SaveChanges();
            }
        }
コード例 #8
0
        public void AddItem(CreateItemViewModel model)
        {
            using (var context = new HardwareShopContext())
            {
                var newItem = Mapper.Map <Item>(model);

                context.Items.Add(newItem);
                context.SaveChanges();
            }
        }
        public void AddSubCategory(EditSubCategoryViewModel model)
        {
            using (var context = new HardwareShopContext())
            {
                var subCategory = Mapper.Map <SubCategory>(model);

                context.SubCategories.Add(subCategory);
                context.SaveChanges();
            }
        }
コード例 #10
0
        public void AddCategory(CategoryViewModel model)
        {
            using (var context = new HardwareShopContext())
            {
                var newCategory = Mapper.Map <Category>(model);

                context.Categories.Add(newCategory);
                context.SaveChanges();
            }
        }
コード例 #11
0
        public void EditReview(EditReviewViewModel model)
        {
            using (var context = new HardwareShopContext())
            {
                var review = this.GetReviewById(model.ReviewId, context);
                review.Content = model.Content;
                review.Score   = model.Score;

                context.SaveChanges();
            }
        }
コード例 #12
0
        public void EdiCategory(CategoryViewModel model)
        {
            using (var context = new HardwareShopContext())
            {
                var category = this.GetCategoryById(model.CategoryId, context);

                category.Name = model.Name;
                context.Entry(category).State = EntityState.Modified;
                context.SaveChanges();
            }
        }
コード例 #13
0
 public void AddItemToCart(Item item, string userId)
 {
     using (var context = new HardwareShopContext())
     {
         context.Items.Attach(item);
         Cart cart = context.Carts.FirstOrDefault(s => s.UserId == userId && s.IsSold == false);
         cart.Items.Add(item);
         cart.TotalPrice += item.Price;
         context.SaveChanges();
     }
 }
コード例 #14
0
 public void RemoveItemFromCart(Item item, string userId)
 {
     using (var context = new HardwareShopContext())
     {
         context.Items.Attach(item);
         Cart cart = context.Carts.FirstOrDefault(c => c.UserId == userId && c.IsSold == false);
         cart.Items.Remove(item);
         cart.TotalPrice -= item.Price;
         context.SaveChanges();
     }
 }
コード例 #15
0
        public void AddReview(CreateReviewViewModel model)
        {
            using (var context = new HardwareShopContext())
            {
                var review = Mapper.Map <Review>(model);
                review.ReviewDate = DateTime.Now;
                review.AuthorId   = HttpContext.Current.User.Identity.GetUserId();

                context.Reviews.Add(review);
                context.SaveChanges();
            }
        }
コード例 #16
0
        public void AddComment(CommentViewModel model)
        {
            using (var context = new HardwareShopContext())
            {
                var comment = Mapper.Map <Comment>(model);
                comment.DatePosted = DateTime.Now;
                comment.AuthorId   = HttpContext.Current.User.Identity.GetUserId();

                context.Comments.Add(comment);
                context.SaveChanges();
            }
        }
コード例 #17
0
        public void DeleteReview(int reviewId)
        {
            using (var context = new HardwareShopContext())
            {
                var review = GetReviewById(reviewId, context);
                review.IsDeleted = true;

                foreach (var comment in review.Comments)
                {
                    comment.IsDeleted = true;
                }

                context.SaveChanges();
            }
        }
コード例 #18
0
        public void EditUser(ManageUserViewModel model)
        {
            using (var context = new HardwareShopContext())
            {
                var user = this.GetUserByUsername(model.Id, context);

                user.FirstName = model.FirstName;
                user.LastName  = model.LastName;
                user.Address   = model.Address;
                user.Email     = model.Email;

                context.Entry(user).State = EntityState.Modified;
                context.SaveChanges();
            }
        }
コード例 #19
0
        public void Restore(int itemId)
        {
            using (var context = new HardwareShopContext())
            {
                var item = this.GetItemById(itemId, context);
                item.IsDeleted = false;

                context.Reviews.Where(r => r.ItemId == item.ItemId).Update(r => new Review {
                    IsDeleted = false
                });
                context.Comments.Where(c => c.Review.Item.ItemId == item.ItemId).Update(c => new Comment {
                    IsDeleted = false
                });

                context.SaveChanges();
            }
        }
コード例 #20
0
        public void EditUser(ApplicationUser user, EditUserViewModel model)
        {
            using (var context = new HardwareShopContext())
            {
                context.Users.Attach(user);

                if (!string.IsNullOrEmpty(model.Password))
                {
                    var hasher       = new PasswordHasher();
                    var passwordHash = hasher.HashPassword(model.Password);
                    user.PasswordHash = passwordHash;
                }

                user.UserName  = model.Username;
                user.FirstName = model.FirstName;
                user.LastName  = model.LastName;
                user.Address   = model.Address;
                user.Email     = model.Email;

                context.Entry(user).State = EntityState.Modified;
                context.SaveChanges();
            }
        }