コード例 #1
0
        public async Task <IActionResult> AddProduct(ProductAddDto model, IFormFile Image)
        {
            if (ModelState.IsValid)
            {
                if (Image != null)
                {
                    var    images = Path.GetExtension(Image.FileName);
                    string guid   = Guid.NewGuid() + images;

                    var addimages = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/image/" + guid);

                    using (var stream = new FileStream(addimages, FileMode.Create))
                    {
                        await Image.CopyToAsync(stream);
                    }


                    _productService.Add(_mapper.Map <Product>(model));

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

            return(View(model));
        }
コード例 #2
0
        public async Task <IActionResult> AddAsync([FromBody] ProductAddDto model)
        {
            var validator = new ProductAddDtoValidator();
            var result    = await validator.ValidateAsync(model);

            if (result.IsValid)
            {
                await _productRepository.AddAsync(new Product
                {
                    Description    = model.Description,
                    Name           = model.Name,
                    ProductBrandId = model.ProductBrandId,
                    ProductTypeId  = model.ProductTypeId,
                });

                var columnCount = await _productRepository.SavechangesAsync();

                columnCount = 0;
                if (columnCount == 0)
                {
                    ModelState.AddModelError("", "Bir problem ile karşılaştık");
                    return(BadRequest(ModelState.Values));
                }
                return(NoContent());
            }
            return(BadRequest(result.Errors));
        }
コード例 #3
0
        public async Task <IActionResult> AddNewProductAsync([FromBody] ProductAddDto newProduct)
        {
            var product = await context.Products.FirstOrDefaultAsync(p => p.Name == newProduct.Name);

            if (product == null)
            {
                product            = mapper.Map <Product>(newProduct);
                product.UploadDate = DateTime.Now;
                if (product.NumberOfProducts == 0)
                {
                    product.InStock = false;
                }
                else
                {
                    product.InStock = true;
                }
                await adminService.AddProductAsync(product);

                await adminService.SaveAllAsync();

                return(Ok());
            }
            product.NumberOfProducts = product.NumberOfProducts + newProduct.NumberOfProducts;
            await adminService.SaveAllAsync();

            return(Ok());
        }
コード例 #4
0
ファイル: ProductService.cs プロジェクト: mofoj0e/ShipIt
        public async Task <ProductDto> AddProduct(ProductAddDto productAddDto)
        {
            var product = _mapper.Map <Product>(productAddDto);
            await _dbContext.Products.InsertOneAsync(product);

            return(await GetProduct(product.Id, DateTime.UtcNow));
        }
コード例 #5
0
        public async Task <IActionResult> Add(ProductAddDto productAddDto)
        {
            // await _productService.Add(new Product { Name = productAddDto.Name });
            await _productService.Add(_mapper.Map <Product>(productAddDto));

            return(Created("", productAddDto));
        }
コード例 #6
0
        public async Task <IActionResult> AddNewProduct([FromBody] ProductAddDto productAddDto)
        {
            var product = _mapper.Map <Product>(productAddDto);
            await _productService.Add(product);

            return(Created("", productAddDto));
        }
コード例 #7
0
 public async Task <IActionResult> AddProduct(ProductAddDto productAddDto)
 {
     if (ModelState.IsValid)
     {
         await productService.AddAsync(mapper.Map <Product>(productAddDto));
     }
     return(View(productAddDto));
 }
コード例 #8
0
        public async Task <IActionResult> UpdateProduct(int id, ProductAddDto productToUpdate)
        {
            var productFromRepo = await _repo.GetProduct(id);

            _mapper.Map(productToUpdate, productFromRepo);
            if (await _repo.SaveAll())
            {
                return(NoContent());
            }
            throw new Exception($"Updating product {id} failed on save");
        }
コード例 #9
0
        public async Task <ActionResult> Post([FromBody] ProductAddDto productDto)
        {
            var result = await _productService.AddAsync(productDto);

            if (result.IsError)
            {
                return(BadRequest(result));
            }

            return(Ok(result));
        }
コード例 #10
0
        public async Task <IActionResult> Add([FromBody] ProductAddDto model)
        {
            var result = await _productService.Add(model);

            if (result.Message != ApiResultMessages.Ok)
            {
                return(BadRequest(result));
            }

            return(Ok(result));
        }
コード例 #11
0
        public async Task <IActionResult> AddProduct(ProductAddDto newProduct)
        {
            ServiceResponse <List <ProductGetDto> > response = await _productService.AddProduct(newProduct);

            if (response.Data == null)
            {
                return(NotFound(response));
            }

            return(Ok(response));
        }
コード例 #12
0
        public IActionResult AddProduct(ProductAddDto productAddDto)
        {
            var addingProduct = _mapper.Map <ProductAddDto, Product>(productAddDto);

            addingProduct.ProductAddDate = DateTime.Now;
            if (_productService.isProductExistsByBarkod(addingProduct.Barkod))
            {
                return(BadRequest(Messages.ProductExists));
            }
            _productService.Add(addingProduct);
            return(Ok(Messages.ProductAdded));
        }
コード例 #13
0
        public async Task <ActionResult <ProductAddDto> > AddProduct([FromBody] ProductAddDto model)
        {
            if (UserResolverService.IsUserAdmin())
            {
                var product = await ProductService.AddProduct(model);

                if (product != null)
                {
                    return(Ok(product));
                }
            }
            return(Forbid());
        }
コード例 #14
0
        public async Task <ActionResult <ApiResponse <Product> > > Add(ProductAddDto productAddDto)
        {
            try
            {
                var product        = _mapper.Map <Product>(productAddDto);
                var createdProduct = await _productService.AddProduct(product);

                return(createdProduct.CreateSuccessResponse("Product created successfully!"));
            }
            catch (Exception exception)
            {
                return(BadRequest(exception.CreateErrorResponse()));
            }
        }
コード例 #15
0
        public async Task <IActionResult> AddProduct(ProductAddDto productAddDto)
        {
            Product addProduct  = _mapper.Map <Product>(productAddDto);
            string  webRootPath = "assets/img/" + addProduct.ProductImageUrl;

            addProduct.ProductImageUrl = webRootPath;
            if (await _ProductRepository.AddAsync(addProduct))
            {
                return(Ok(addProduct));
            }
            else
            {
                return(BadRequest("Product Can't Added"));
            }
        }
コード例 #16
0
        public string Add(ProductAddDto model)
        {
            Product entity = new Product
            {
                Id        = Guid.NewGuid(),
                CreatedAt = DateTime.UtcNow
            };

            entity.Name  = model.Name;
            entity.Desc  = model.Desc;
            entity.Price = model.Price ?? 0;

            data.Add(entity);

            return($"{model.Name} eklendi");
        }
コード例 #17
0
        public async Task <IActionResult> AddProduct([FromBody] ProductAddDto newP)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Səhv özəlliklər daxil edilib." + ModelState));
            }

            var result = await _repo.AddProduct(newP);

            if (result.IsSucces)
            {
                return(Ok(result.Content));
            }

            return(StatusCode(520, result.Message));
        }
コード例 #18
0
        public async Task <IDataResult <ProductDto> > Add(ProductAddDto productAddDto)
        {
            var product = _mapper.Map <Product>(productAddDto);

            product.IsActive = true;
            var addedCategory = await _unitOfWork.Products.AddAsync(product);

            await _unitOfWork.SaveAsync();

            return(new DataResult <ProductDto>(ResultStatus.Success, $"{productAddDto.Name} adlı kategori başarı ile eklenmiştir.", new ProductDto
            {
                Product = addedCategory,
                ResultStatus = ResultStatus.Success,
                Message = $"{productAddDto.Name} adlı kategori başarı ile eklenmiştir."
            }));
        }
コード例 #19
0
        public async Task <IActionResult> ProductAdd(ProductAddDto model)
        {
            if (ModelState.IsValid)
            {
                Product product = mapper.Map <Product>(model);
                product.CreateUserId = appUserSessionService.Get().Id;
                await productService.AddAsync(product);

                return(RedirectToAction("ProductList"));
            }
            else
            {
                ModelState.AddModelError("", "Lütfen gerekli tüm alanları doldurun.");
                return(View(model));
            }
        }
コード例 #20
0
        public async Task <IActionResult> Add(ProductAddDto productAddDto)
        {
            if (!ModelState.IsValid)
            {
                return(View(productAddDto));
            }

            //if (await _productService.ProductExistsAsync(productAddDto.ProductName, (int)productAddDto.CategoryId))
            //    return RedirectToAction("Index").ShowMessage(Status.Error, "Hata", "Eklenmek istenen ürün aynı kategoride zaten mevcut!");

            var product = _mapper.Map <Product>(productAddDto);

            await _productService.AddAsync(product);

            return(RedirectToAction("Index").ShowMessage(Status.Ok, "Başarılı", $"{product.ProductName} adlı ürün başarıyla eklendi."));
        }
コード例 #21
0
        public async Task <IActionResult> Create([FromBody] ProductAddDto productAddDto)
        {
            var product = _mapper.Map <Product>(productAddDto);

            var result = await _productService.AddAsync(product);

            if (result.ResultStatus == ResultStatus.Error)
            {
                return(BadRequest(new ErrorDto
                {
                    Message = ApiResultMessages.ResultMessage(false),
                    ResultStatus = result.ResultStatus
                }));
            }

            return(Created(string.Empty, productAddDto));
        }
コード例 #22
0
        public ActionResult <Product> AddProduct(ProductAddDto productAddDto)
        {
            var product = _mapper.Map <Product>(productAddDto);

            try
            {
                _repository.AddProduct(product);
                if (_repository.SaveChanges())
                {
                    return(CreatedAtAction(nameof(GetProductById), new { Id = product.Id }, product));
                }
            }
            catch
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Database Failure"));
            }
            return(BadRequest());
        }
コード例 #23
0
        public async Task <IResult> Insert(ProductAddDto productAddDto)
        {
            try
            {
                ProductEntity product = Mapper.Map <ProductEntity>(productAddDto);
                await UnitOfWork.Product.AddAsync(product);

                await UnitOfWork.SaveAsync();

                ProductAddDto result = Mapper.Map <ProductEntity, ProductAddDto>(product);
                return(new Result(ResultStatus.Success, $"{result.ProductName} eklendi"));
            }
            catch (Exception ex)
            {
                //Logger.Log(LogLevel.Error, ex, ex.Message);
                throw;
            }
        }
コード例 #24
0
        public async Task <IActionResult> AddProduct(ProductAddDto productAddDtoDto)
        {
            var productToCreate = new Product
            {
                Description      = productAddDtoDto.Description,
                SuggestedUse     = productAddDtoDto.SuggestedUse,
                OtherIngredients = productAddDtoDto.OtherIngredients,
                Warnings         = productAddDtoDto.Warnings,
                Price            = productAddDtoDto.Price,
                Quantity         = productAddDtoDto.Quantity,
                Name             = productAddDtoDto.Name.ToLower(),
                Brand            = productAddDtoDto.Brand.ToLower(),
                Category         = productAddDtoDto.Category.ToLower()
            };
            var CreatedProduct = await _repo.AddProduct(productToCreate);

            return(Ok(CreatedProduct));
        }
コード例 #25
0
        public async Task <ProductAddDto> AddProduct(ProductAddDto model)
        {
            var category = await _productCategoryRepository.GetAll()
                           .Include(x => x.Products)
                           .SingleOrDefaultAsync(c => c.Id == model.CategoryId);

            if (category != null)
            {
                var product = _mapper.Map <Product>(model);
                category.Products.Add(product);
                await _productCategoryRepository.SaveChangesAsync();

                return(_mapper.Map <ProductAddDto>(product));
            }
            else
            {
                throw new Exception($"Product with name = {model.Name} in database");
            }
        }
コード例 #26
0
        public async Task <ServiceResponse <List <ProductGetDto> > > AddProduct(ProductAddDto newProduct)
        {
            ServiceResponse <List <ProductGetDto> > serviceResponse = new ServiceResponse <List <ProductGetDto> >();
            Product product = _mapper.Map <Product>(newProduct);

            try
            {
                await _context.Products.AddAsync(product);

                await _context.SaveChangesAsync();

                serviceResponse.Data = (_context.Products.Select(u => _mapper.Map <ProductGetDto>(u))).ToList();
            }
            catch (Exception ex)
            {
                serviceResponse.Success = false;
                serviceResponse.Message = ex.Message;
            }
            return(serviceResponse);
        }
コード例 #27
0
        public async Task <IActionResult> AddProduct(ProductAddDto productAddDto)
        {
            Product addProduct  = _mapper.Map <Product>(productAddDto);
            string  webRootPath = "assets/img/" + addProduct.ProductImageUrl;
            string  extension   = Path.GetExtension(webRootPath);

            if (extension == ".jpg" || extension == ".jpeg" || extension == ".png")
            {
                addProduct.ProductImageUrl = webRootPath;
                if (await _ProductRepository.AddAsync(addProduct))
                {
                    return(Ok(addProduct));
                }
                else
                {
                    return(BadRequest("Product Can't Added"));
                }
            }
            return(BadRequest("Product Image Type is wrong"));
        }
コード例 #28
0
ファイル: ProductService.cs プロジェクト: qnill/bootcamp-b1
        public async Task <ApiResult> Add(ProductAddDto model)
        {
            Product entity = new Product
            {
                Id        = Guid.NewGuid(),
                CreatedAt = DateTime.UtcNow
            };

            entity.Name              = model.Name;
            entity.Desc              = model.Desc;
            entity.Price             = model.Price ?? 0;
            entity.ProductCategoryId = model.ProductCategoryId;

            await _context.Product.AddAsync(entity);

            await _context.SaveChangesAsync();

            return(new ApiResult {
                Data = entity.Id, Message = ApiResultMessages.Ok
            });
        }
コード例 #29
0
        public IActionResult CreateProduct(ProductAddDto model)
        {
            if (ModelState.IsValid)
            {
                var entity = new Product()
                {
                    Name          = model.Name,
                    Price         = model.Price,
                    StockQuantity = model.StockQuantity,
                };

                if (_productService.Create(entity))
                {
                    return(RedirectToAction("ProductList"));
                }
                ViewBag.ErrorMessage = _productService.ErrorMessage;
                return(View(model));
            }

            return(View(model));
        }
コード例 #30
0
ファイル: ProductsController.cs プロジェクト: chrisK00/ordexo
        public async Task <IActionResult> AddProduct(ProductAddDto productAddDto)
        {
            //should only be reachable by moderators and admin
            var productToAdd = new Product
            {
                Name  = productAddDto.Name,
                Brand = productAddDto.Brand,
                Price = productAddDto.Price,
                Stock = productAddDto.Stock
            };

            await _productRepository.AddAsync(productToAdd);

            if (!await _unitOfWOrk.SaveAsync())
            {
                return(BadRequest("Failed to add product"));
            }

            _logger.LogInformation($"Created a new product with the id: {productToAdd.Id}");
            return(Created("Products", new { id = productToAdd.Id }));
        }