Example #1
0
        public async Task <IActionResult> PutMessage(int id, Message message)
        {
            if (id != message.MemberID)
            {
                return(BadRequest());
            }

            _context.Entry(message).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!MessageExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> UpdateCart(int id, CartDetail cartDetail)
        {
            if (id != cartDetail.MemberID)
            {
                return(BadRequest());
            }

            _context.Entry(cartDetail).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CartDetailExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            _context.Entry(cartDetail)
            .Reference(cd => cd.Variant)
            .Query()
            .Include(cd => cd.Product)
            .Load();

            return(Ok(new { cartDetail = cartDetail, totalPrice = CalculateCart(cartDetail) }));
        }
Example #3
0
        public async Task <IActionResult> PutOrderProduct(int id, OrderProduct orderProduct)
        {
            /*
             * if (id != orderProduct.OrderId)
             * {
             *  return BadRequest();
             * }
             */

            _context.Entry(orderProduct).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!OrderProductExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Example #4
0
        public async Task <IActionResult> PutOrderDetail(int id, OrderDetail orderDetail)
        {
            if (id != orderDetail.OrderID)
            {
                return(BadRequest());
            }

            _context.Entry(orderDetail).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!OrderDetailExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Example #5
0
        public async Task <IActionResult> PutCategory(int id, Category category)
        {
            if (id != category.ID)
            {
                return(BadRequest());
            }

            _context.Entry(category).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CategoryExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <ActionResult> PostAsync(Product product)
        {
            product.CreationDate = DateTime.Now;
            _dbContext.Product.Add(product);
            await _dbContext.SaveChangesAsync();


            return(Ok(product));
        }
Example #7
0
        public async Task <IActionResult> Create([Bind("UserItemId,UserId,ItemId")] UserItems userItems)
        {
            if (ModelState.IsValid)
            {
                _context.Add(userItems);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(userItems));
        }
        public async Task <IActionResult> Create([Bind("Id,CategoryName")] Category category)
        {
            if (ModelState.IsValid)
            {
                _context.Add(category);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(category));
        }
        public async Task <IActionResult> Create([Bind("Id,CategoryId,ProductName,ProductPrice,InsertedDate,UpdatedDate")] Product product)
        {
            if (ModelState.IsValid)
            {
                _context.Add(product);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CategoryId"] = new SelectList(_context.Categories, "Id", "CategoryName", product.CategoryId);
            return(View(product));
        }
Example #10
0
 public async Task CreateAsync(Product p)
 {
     try
     {
         p.CreationDate = DateTime.Now;
         _ShopDBContext.Product.Add(p);
         await _ShopDBContext.SaveChangesAsync();
     }
     catch (Exception)
     {
         throw;
     }
 }
        /// <summary>
        /// Создание заказа в базе данных
        /// </summary>
        /// <param name="products">Список продуктов в корзине</param>
        /// <returns></returns>
        public async Task <Order_DB> CreateOrderInDBAsync(IEnumerable <CartItem> products)
        {
            if (products == null || products.Count() == 0)
            {
                throw new ArgumentException("Products count should be more than thero.");
            }

            var order = new Order_DB
            {
                User_Id       = 2,
                Creation_Date = DateTime.Now,
                Status_Id     = 1
            };

            _shopDBContext.Shop_Orders.Add(order);

            for (int i = 0; i < products.Count(); i++)
            {
                order.Order_Composition.Add(new OrderComposition_DB
                {
                    Order_Id     = order.Id,
                    Product_Id   = products.ElementAt(i).ProductId,
                    ProductCount = products.ElementAt(i).Count
                });
            }

            await _shopDBContext.SaveChangesAsync();

            order.Status = await _shopDBContext.OrderStatus.Where(s => s.Id == order.Status_Id).SingleAsync();

            order.User = await _shopDBContext.Users.Where(u => u.Id == order.User_Id).SingleAsync();

            return(order);
        }
        public async Task <int> CreateAsync(ProductCreateRequest request)
        {
            var product = new Product()
            {
                Price               = request.Price,
                OriginalPrice       = request.OriginalPrice,
                Stock               = request.Stock,
                ViewCount           = 0,
                DateCreated         = DateTime.Now,
                ProductTranslations = new List <ProductTranslation>()
                {
                    new ProductTranslation()
                    {
                        Name           = request.Name,
                        Description    = request.Description,
                        Details        = request.Details,
                        SeoDescription = request.SeoDescription,
                        SeoAlias       = request.SeoAlias,
                        SeoTitle       = request.SeoTitle,
                        LanguageId     = request.LanguageId
                    }
                }
            };

            if (request.ThumbnailImage != null)
            {
                var imagePath = await fileHandlers.SaveFileAsync(request.ThumbnailImage);

                product.ProductImages = new List <ProductImage>()
                {
                    new ProductImage()
                    {
                        Caption     = "Thumbnail image",
                        DateCreated = DateTime.Now,
                        FileSize    = request.ThumbnailImage.Length,
                        ImagePath   = imagePath,
                        IsDefault   = true,
                        SortOrder   = 1
                    }
                };
            }

            shopDBContext.Products.Add(product);
            await shopDBContext.SaveChangesAsync();

            return(product.ID);
        }
        public async Task <ActionResult> Post(Product product)
        {
            var dbProduct = await _shopDbContext.Product.FindAsync(product.Id);

            if (dbProduct != null)
            {
                return(Conflict());
            }

            product.CreationDate = DateTime.Now;

            await _shopDbContext.Product.AddAsync(product);

            await _shopDbContext.SaveChangesAsync();

            return(Created(new Uri($"https://localhost:44328/products/{product.Id}"), product));
        }
Example #14
0
        public async Task <IActionResult> Buy()
        {
            /*In case I decide to go back to using TempData.
             *
             * Users user = _context.Users.FirstOrDefault(e => e.Id == (int)TempData["userId"]);
             * Items item = _context.Items.FirstOrDefault(e => e.Id == (int)TempData["itemId"]);*/

            UserItems thisPurchase = (UserItems)ByteArrayToObject(HttpContext.Session.Get("thisPurchase"));

            thisPurchase.PurchaseDate = DateTime.Now;
            Users user = _context.Users.FirstOrDefault(e => e.Id == thisPurchase.UserId);
            Items item = _context.Items.FirstOrDefault(e => e.Id == thisPurchase.ItemId);

            user.Balance -= item.Price;
            user.Points++;
            _context.Update(user);
            _context.UserItems.Add(thisPurchase);
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
Example #15
0
        public async Task <IActionResult> PutProduct(int id, Product product)
        {
            product.Id = id;

            _context.Entry(product).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Example #16
0
        public async Task <IActionResult> PutUser(int id, User user)
        {
            user.Id = id;

            _context.Entry(user).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UserExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
 public async Task Save()
 {
     await _context.SaveChangesAsync();
 }