Example #1
0
        public async Task <ApiResult <bool> > Create(OrderCreateRequest request)
        {
            Order order = new Order()
            {
                OrderDate    = DateTime.UtcNow,
                UserId       = request.UserId,
                OrderDetails = request.ListCart.Select(x => new OrderDetail()
                {
                    ProductId = x.ProductId,
                    Quantity  = x.Quantity,
                    Price     = x.Price
                }).ToList(),
                ShipAddress     = request.ShipAddress,
                ShipEmail       = request.ShipEmail,
                ShipName        = request.ShipName,
                ShipPhoneNumber = request.ShipPhoneNumber,
                Status          = OrderStatus.InProgress
            };

            _context.Orders.Add(order);
            if (await _context.SaveChangesAsync() != 0)
            {
                return(new ApiSuccessResult <bool>());
            }
            return(new ApiErrorResult <bool>());
        }
Example #2
0
        public async Task <ApiResult <bool> > Create(CategoryCreateRequest request)
        {
            var query = from c in _context.Categories
                        join ct in _context.CategoryTranslations on c.Id equals ct.CategoryId
                        where ct.LanguageId == request.LanguageId && ct.Name == request.Name

                        select new { c, ct };

            if (query.Count() != 0)
            {
                return(new ApiErrorResult <bool>("Tên danh mục bị trùng "));
            }

            Category category = new Category()
            {
                SortOrder            = request.SortOrder,
                IsShowOnHome         = request.IsShowOnHome,
                ParentId             = request.ParentId,
                Status               = request.Status,
                CategoryTranslations = new List <CategoryTranslation>()
                {
                    new CategoryTranslation()
                    {
                        LanguageId     = request.LanguageId,
                        Name           = request.Name,
                        SeoAlias       = request.SeoAlias,
                        SeoDescription = request.SeoDescription,
                        SeoTitle       = request.SeoTitle
                    }
                },
            };

            _context.Categories.Add(category);
            if (await _context.SaveChangesAsync() == 0)
            {
                return(new ApiErrorResult <bool>());
            }
            else
            {
                return(new ApiSuccessResult <bool>());
            }
        }
Example #3
0
        public async Task <ApiResult <bool> > Delete(int productId)
        {
            var product = await _context.Products.FindAsync(productId);

            if (product == null)
            {
                return(new ApiErrorResult <bool>(true));
            }

            if (_context.OrderDetails.Where(x => x.ProductId == productId) != null)
            {
                product.Status = Status.InActive;
                await _context.SaveChangesAsync();

                return(new ApiSuccessResult <bool>("Sản phẩm đã bị vô hóa", true));
            }

            product.ThumnailId = null;
            _context.Products.Update(product);

            var images = _context.ProductImages.Where(x => x.ProductId == product.Id);

            foreach (var image in images)
            {
                await _storageService.DeleteFileAsync(image.ImagePath);
            }
            _context.ProductImages.RemoveRange(images);
            _context.SaveChanges();


            _context.Products.Remove(product);
            if (_context.SaveChanges() == 0)
            {
                return(new ApiErrorResult <bool>("không thể xóa"));
            }
            return(new ApiSuccessResult <bool>("Xoá thành công", true));
        }
        public async Task <IActionResult> CreateAsync(CatalogItem item)
        {
            if (ModelState.IsValid)
            {
                await db.CatalogItems.AddAsync(item);

                await db.SaveChangesAsync();

                //return RedirectToAction("Index");
                return(RedirectToRoute("ListProducts"));
            }
            else
            {
                return(View(item));
            }
        }