当前位置: 首页>>代码示例>>C#>>正文


C# ProductViewModel.Create方法代码示例

本文整理汇总了C#中ProductViewModel.Create方法的典型用法代码示例。如果您正苦于以下问题:C# ProductViewModel.Create方法的具体用法?C# ProductViewModel.Create怎么用?C# ProductViewModel.Create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ProductViewModel的用法示例。


在下文中一共展示了ProductViewModel.Create方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetAll

        public async Task<IHttpActionResult> GetAll()
        {
            var user = await _authRepository.FindUser(HttpContext.Current.User as ClaimsPrincipal);
            var isAdmin = await _authRepository.IsAdmin(HttpContext.Current.User as ClaimsPrincipal);

            if (!isAdmin)
            {
                var userOrders = await _unitOfWork.Orders.FindAsync(o => o.UserId == user.Id);

                var orderedProducts = new List<ProductViewModel>();
                foreach (var order in userOrders)
                {
                    var viewModel = new ProductViewModel();
                    viewModel.Create(order.Product);
                    orderedProducts.Add(viewModel);
                }

                return Ok(orderedProducts);
            }
            var allOrders = await _unitOfWork.Orders.GetAllAsync();

            var allProducts = new List<ProductViewModel>();
            foreach (var order in allOrders)
            {
                var viewModel = new ProductViewModel();
                viewModel.Create(order.Product);
                viewModel.UserName = order.User.UserName;
                allProducts.Add(viewModel);
            }

            return Ok(allProducts);
        }
开发者ID:smalpik,项目名称:Store,代码行数:32,代码来源:OrderController.cs

示例2: Get

        public async Task<IHttpActionResult> Get(long id)
        {
            var entity = await _productRepository.GetAsync(id);
            if (entity == null)
            {
                return NotFound();
            }
            var viewModel = new ProductViewModel();
            viewModel.Create(entity);

            return Ok(viewModel);
        }
开发者ID:smalpik,项目名称:Store,代码行数:12,代码来源:ProductsController.cs

示例3: GetAll

        public async Task<IHttpActionResult> GetAll()
        {
            var all = await _productRepository.GetAllAsync();
            var allViewModels = new List<ProductViewModel>();

            foreach (var entity in all)
            {
                var viewModel = new ProductViewModel();
                viewModel.Create(entity);
                allViewModels.Add(viewModel);
            }
            return Ok(allViewModels);
        }
开发者ID:smalpik,项目名称:Store,代码行数:13,代码来源:ProductsController.cs

示例4: GetAll

        public async Task<IHttpActionResult> GetAll()
        {
            var user = await _authRepository.FindUser(HttpContext.Current.User as ClaimsPrincipal);

            var all = await _cartRepository.GetProductsInCartByUser(user.Id);

            var allViewModels = new List<ProductViewModel>();

            foreach (var entity in all)
            {
                var viewModel = new ProductViewModel();
                viewModel.Create(entity);
                allViewModels.Add(viewModel);
            }
            return Ok(allViewModels);
        }
开发者ID:smalpik,项目名称:Store,代码行数:16,代码来源:CartController.cs

示例5: Put

        public async Task<IHttpActionResult> Put()
        {
            if (!await IsAdmin())
            {
                return StatusCode(HttpStatusCode.Forbidden);
            }

            var image = HttpContext.Current.Request.Files["file"];
            if (image != null)
            {
                if (!image.ContentType.Contains("image"))
                    return BadRequest();
            }
            var id = HttpContext.Current.Request.Form["newProduct[id]"];
            var name = HttpContext.Current.Request.Form["newProduct[name]"];
            var description = HttpContext.Current.Request.Form["newProduct[description]"];
            var cat = HttpContext.Current.Request.Form["newProduct[categoryId]"];
            var reqCost = HttpContext.Current.Request.Form["newProduct[cost]"];
            long categoryId;
            long prodId;
            int cost;

            if (long.TryParse(id, out prodId) == false ||
                String.IsNullOrWhiteSpace(name) ||
                Int32.TryParse(reqCost, out cost) == false ||
                long.TryParse(cat, out categoryId) == false)
            {
                return BadRequest();
            }

            var category = await _categoryRepository.GetAsync(categoryId);
            if (category == null)
            {
                return BadRequest();
            }
            string path = "";
            if (image != null)
            {
                path = Path.GetRandomFileName() + Path.GetExtension(image.FileName);
                image.SaveAs(System.Web.Hosting.HostingEnvironment.MapPath("~/Img/") + path);
                path = "img/" + path;
            }
            else
            {
                path = HttpContext.Current.Request.Form["newProduct[imagePath]"];
            }

            var product = await _productRepository.GetAsync(prodId);
            product.Name = name;
            product.Description = description;
            product.Cost = cost;
            product.Category = category;
            product.CategoryId = category.Id;
            product.ImagePath = path;


            await _productRepository.Update(product);
            await _unitOfWork.CompleteAsync();
            ProductViewModel vm = new ProductViewModel();
            vm.Create(product);

            return CreatedAtRoute("DefaultApi", new { id = product.Id }, vm);
        }
开发者ID:smalpik,项目名称:Store,代码行数:63,代码来源:ProductsController.cs

示例6: GetProducts

 public async Task<IEnumerable<ProductViewModel>> GetProducts(long categoryId, string findValue, int page)
 {
     const int productsPerPage = 8;
     var category = await _categoryRepository.GetAsync(categoryId);
     var productsPage = await _productRepository.FindProductsInCategoryAsync(findValue, category, page, productsPerPage);
     var viewModelsList = new List<ProductViewModel>();
     foreach (var product in productsPage)
     {
         var viewModel = new ProductViewModel();
         viewModel.Create(product);
         viewModelsList.Add(viewModel);
     }
     return viewModelsList;
 }
开发者ID:smalpik,项目名称:Store,代码行数:14,代码来源:ProductsController.cs

示例7: Delete

        public async Task<IHttpActionResult> Delete(long id)
        {
            if (!await IsAdmin())
            {
                return StatusCode(HttpStatusCode.Forbidden);
            }

            var entity = await _productRepository.GetAsync(id);
            if (entity == null)
            {
                return NotFound();
            }

            var ordereditems = await _unitOfWork.Orders.FindAsync(o => o.ProductId == id);
            var cartsItems = await _unitOfWork.Carts.FindAsync(o => o.ProductId == id);
            _unitOfWork.Orders.RemoveRange(ordereditems);
            _unitOfWork.Carts.RemoveRange(cartsItems);

            _productRepository.Remove(entity);
            await _unitOfWork.CompleteAsync();
            var viewModel = new ProductViewModel();
            viewModel.Create(entity);

            return Ok(viewModel);

        }
开发者ID:smalpik,项目名称:Store,代码行数:26,代码来源:ProductsController.cs


注:本文中的ProductViewModel.Create方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。