示例#1
0
        public async Task <IActionResult> AddProductComponent([FromBody] ProductComponentModel model)
        {
            try
            {
                var component = mapper.Map <ProductComponent>(model);
                component.SetCreatedBy(model.User);
                var items = mapper.Map <List <ProductComponentItem> >(model.Items);
                foreach (var item in items)
                {
                    item.SetCreatedBy(model.User);
                }
                component.Items = items;
                await service.AddProductComponent(component);

                return(Ok(new Response {
                    Status = "Success", Message = "Product Component created successfully"
                }));
            }
            catch (System.Exception ex)
            {
                return(BadRequest(new Response
                {
                    Status = "Error",
                    Message = ex.Message
                }));
            }
        }
        public async Task <IViewComponentResult> InvokeAsync(string sorting)
        {
            try
            {
                var products = _productContext.GetAll();
                var model    = new ProductComponentModel()
                {
                    Products = products.Take(10).ToList()
                };

                if (sorting == "toprated")
                {
                    model.Products    = products.OrderBy(c => c.Sold);
                    model.WidgetTitle = "Top Rated Products";
                }
                else if (sorting == "popular")
                {
                    model.Products    = products.OrderBy(c => c.Views);
                    model.WidgetTitle = "Propular Products";
                }
                else if (sorting == "featured")
                {
                    model.Products    = products.Where(c => c.Featured);
                    model.WidgetTitle = "Featured Products";
                }
                else if (sorting == "sale")
                {
                    model.Products    = products.Where(c => c.DiscountPercentage > 0);
                    model.WidgetTitle = "Products On Sale";
                }
                else if (sorting == "new")
                {
                    model.Products    = products.Where(c => c.Created > DateTime.Now.AddMonths(2));
                    model.WidgetTitle = "New Arrivals";
                }

                return(View(model));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
示例#3
0
        public async Task <IActionResult> DeleteProductComponent([FromBody] ProductComponentModel model)
        {
            try
            {
                var component = await service.GetProductComponentById(model.Id);

                component.SetModifyByAndModifyDate(model.User);
                await service.DeleteProductComponent(component);

                return(Ok(new Response {
                    Status = "Success", Message = "Product Component deleted successfully"
                }));
            }
            catch (System.Exception ex)
            {
                return(BadRequest(new Response
                {
                    Status = "Error",
                    Message = ex.Message
                }));
            }
        }
示例#4
0
        public async Task <IActionResult> UpdateProductComponent([FromBody] ProductComponentModel model)
        {
            try
            {
                var productComponent = await service.GetProductComponentById(model.Id);

                productComponent.Type  = model.Type;
                productComponent.Items = mapper.Map <List <ProductComponentItem> >(model.Items);
                productComponent.SetModifyByAndModifyDate(model.User);
                await service.UpdateProductComponent(productComponent);

                return(Ok(new Response {
                    Status = "Success", Message = "Product Component updated successfully"
                }));
            }
            catch (System.Exception ex)
            {
                return(BadRequest(new Response
                {
                    Status = "Error",
                    Message = ex.Message
                }));
            }
        }