コード例 #1
0
ファイル: ProductsController.cs プロジェクト: wwwK/Comet
        public async Task <IActionResult> Add([FromForm] ProductsAddInput model)
        {
            var fileName = "";

            if (model?.Image != null)
            {
                var formFile = model?.Image;
                fileName = ContentDispositionHeaderValue.Parse(formFile.ContentDisposition).FileName.Trim('"');
                string filePath = _environment.WebRootPath + $@"\Files\Pictures\";
                if (!Directory.Exists(filePath))
                {
                    Directory.CreateDirectory(filePath);
                }
                fileName = string.Format("{0}.{1}", Guid.NewGuid(), fileName.Split('.')[1]);
                string fileFullName = filePath + fileName;
                using (FileStream fs = new FileStream(fileFullName, FileMode.Create))
                {
                    await formFile.CopyToAsync(fs);

                    fs.Flush();
                }
            }
            string imgpath    = $"/src/Pictures/{fileName}";
            var    adminmodel = _mapper.Map <Products>(model);

            adminmodel.ImageUrl = imgpath;

            var optresult = await _service.AddAsync(adminmodel);

            var result = ApiResultBase.GetInstance(optresult ? ResultCode.Access : ResultCode.Fail, result: optresult);

            return(Ok(result));
        }
コード例 #2
0
        public async Task <IActionResult> Add([FromBody] ProductRequest productRequest)
        {
            //var newProduct = new Product()
            //{
            //    Name = productRequest.Name,
            //    OwnerUserId = userId,
            //    CategoryId = productRequest.CategoryId
            //};
            var newProduct = mapper.Map <Product>(productRequest);


            var product = await productsService.AddAsync(newProduct);



            //var response = new ProductResponse
            //{
            //    Id = product.Id,
            //    Name = product.Name,
            //    CategoryId = product.CategoryId,
            //    CategoryName = product.Category.Name
            //};
            var response = mapper.Map <ProductResponse>(product);

            return(CreatedAtAction(nameof(Get), new { id = response.Id }, response));
        }
コード例 #3
0
      public async Task <IActionResult> PostProductAsync([FromBody] Product product)
      {
          if (product == null)
          {
              return(BadRequest());
          }
          await _productService.AddAsync(product);

          return(CreatedAtRoute(nameof(GetProductByIdAsync), new { id = product.ProductId }, product));
      }
コード例 #4
0
        public async Task <IActionResult> Add([FromBody] ProductRequest productRequest)
        {
            var product = await productsService.AddAsync(productRequest.Name);

            var response = new ProductResponse {
                Id = product.Id, Name = product.Name
            };

            return(CreatedAtAction(nameof(Get), new { id = response.Id }, response));
        }
コード例 #5
0
        public async Task <IActionResult> Add([FromBody] ProductRequest productRequest)
        {
            var newProduct = mapper.Map <Product>(productRequest);

            var product = await productsService.AddAsync(newProduct);

            var response = mapper.Map <ProductResponse>(product);

            return(CreatedAtAction(nameof(Get), new { id = response.Id }, response));
        }
コード例 #6
0
        public async Task <IHttpActionResult> PostProduct(ProductResource resoure)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            ProductResource result = await _productsService.AddAsync(resoure);

            return(CreatedAtRoute("DefaultApi", new { id = result.Id }, result));
        }
コード例 #7
0
        public async Task <IActionResult> Create(CreateProductViewModel createProductViewModel)
        {
            var success = await productsService.AddAsync(createProductViewModel);

            if (success)
            {
                return(RedirectToAction(nameof(Index)));
            }

            return(View(createProductViewModel));
        }
コード例 #8
0
        public async Task <IActionResult> PostAsync([FromBody] DtoCreateProduct model)
        {
            try
            {
                Guid productId = await service.AddAsync(model);

                return(Ok(productId));
            }
            catch
            {
                return(BadRequest());
            }
        }
コード例 #9
0
        public async Task <IActionResult> Add([FromBody] ProductRequest productRequest)
        {
            //if (productRequest.Price<1 || productRequest.Price > 1000)
            //{
            //    return BadRequest();
            //}
            var newProduct = Mapper.Map <Product>(productRequest);
            var product    = await productsService.AddAsync(newProduct);

            var response = Mapper.Map <ProductResponse>(product);

            return(CreatedAtAction(nameof(Get), new { id = response.Id }, response));
        }
コード例 #10
0
        public async Task <IActionResult> Post([FromBody] Products model)
        {
            try
            {
                var product = await _productsService.AddAsync(model);

                return(Created(Url.Link("ProductGet", new { id = product.ProductId }), product));
            }
            catch (Exception ex)
            {
                Log.Error(ex, ex.Message);
            }
            return(BadRequest());
        }
コード例 #11
0
ファイル: ProductsController.cs プロジェクト: MaxDemb/WebApi
        public HttpResponseMessage AddProduct([FromBody] ProductsView productView)
        {
            HttpResponseMessage responseMessage;
            var product = mapper.Map <ProductsDTM>(productView);

            try
            {
                service.AddAsync(product);
                responseMessage = new HttpResponseMessage(HttpStatusCode.OK);
            }
            catch
            {
                responseMessage         = new HttpResponseMessage(HttpStatusCode.NotAcceptable);
                responseMessage.Content = new StringContent("Name can`t be empty!");
            }
            return(responseMessage);
        }
コード例 #12
0
        public async Task <IActionResult> Create([Bind("ProductId,ProductName,SupplierId,CategoryId,QuantityPerUnit,UnitPrice,UnitsInStock,UnitsOnOrder,ReorderLevel,Discontinued")] Products products)
        {
            if (ModelState.IsValid)
            {
                await _productsService.AddAsync(products);

                return(RedirectToAction(nameof(Index)));
            }

            var categories = await _categoriesService.GetAllAsync();

            var suppliersesTemp = await _suppliersService.GetAllAsync();

            ViewData["CategoryId"] = new SelectList(categories, "CategoryId", "CategoryName", products.CategoryId);
            ViewData["SupplierId"] = new SelectList(suppliersesTemp, "SupplierId", "CompanyName", products.SupplierId);
            return(View(products));
        }
コード例 #13
0
        public async Task <IActionResult> Create(ProductsEditModel model)
        {
            if (model == null)
            {
                return(BadRequest(ModelState));
            }
            if (ModelState.IsValid)
            {
                var products = ToProducts(model);
                products = await _productsServiceService.AddAsync(products);

                return(RedirectToAction(nameof(Details), new { id = products.ProductId }));
            }
            ViewData["Message"] = "Creating the product";
            model.Categories    = await _productsServiceService.GetCategoriesAsync();

            model.Suppliers = await _productsServiceService.GetSuppliersAsync();

            return(View(nameof(Edit), model));
        }
コード例 #14
0
        public async Task <IActionResult> Post([FromBody] Product product)
        {
            product = await _productsService.AddAsync(product);

            return(CreatedAtAction(nameof(Get), new { id = product.Id }, product));
        }
コード例 #15
0
        public async Task <ActionResult> Add([FromForm] Product product)
        {
            await productsService.AddAsync(product);

            return(View(product));
        }
コード例 #16
0
        public async Task <ActionResult <Products> > PostProducts(Products products)
        {
            await _productsService.AddAsync(products);

            return(CreatedAtAction("GetProducts", new { id = products.ProductId }, products));
        }
コード例 #17
0
        public async Task <IActionResult> Create(ProductCreateViewModel product)
        {
            await productsService.AddAsync(product);

            return(RedirectToAction("Index", "Products"));
        }