Пример #1
0
        public ActionResult <ProductDto> CreateProduct([FromForm] ProductDto productCreateDto)
        {
            var file = HttpContext.Request.Form.Files[0];

            if (file.Length == 0)
            {
                return(BadRequest());
            }

            var path = _environment.WebRootPath + "\\Uploads\\";

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            string fileExtention = (Path.GetExtension(file.FileName)).ToLower();
            string fileName      = Guid.NewGuid().ToString();

            fileName = fileName + fileExtention;
            using (var stream = System.IO.File.Create(path + fileName))
            {
                file.CopyTo(stream);
                stream.Flush();
            }
            productCreateDto.Image = Path.Combine(fileName);
            var productModel = _mapper.Map <Product>(productCreateDto);

            _repository.CreateProduct(productModel);
            _repository.SaveChanges();
            var productReadDto = _mapper.Map <ProductDto>(productModel);

            return(CreatedAtRoute(nameof(GetProductById), new { productReadDto.Id }, productReadDto));
        }
Пример #2
0
 public Product AddProduct(Product product)
 {
     if (string.IsNullOrEmpty(product.Name))
     {
         throw new Exception("Name of the product is required.");
     }
     else if (string.IsNullOrEmpty(product.Description))
     {
         throw new Exception("Description of the product is required.");
     }
     else if (string.IsNullOrEmpty(product.Type))
     {
         throw new Exception("Type of the product is required.");
     }
     else if (product.Price <= 0)
     {
         throw new Exception("Price of the product is wrong.");
     }
     else if (product.DiscountPrice < 0)
     {
         throw new Exception("Discount price of the product is wrong.");
     }
     else
     {
         return(_productRepo.CreateProduct(product));
     }
 }
Пример #3
0
        public IHttpActionResult CreateProduct([FromBody] Product product)
        {
            if (product == null)
            {
                return(BadRequest());
            }

            _productRepo.CreateProduct(product);
            return(StatusCode(HttpStatusCode.Created));
        }
Пример #4
0
        public ActionResult <ProductReadDto> CreateProduct(ProductCreateDto productCreateDto)
        {
            var productModel = _mapper.Map <Product>(productCreateDto);

            _repository.CreateProduct(productModel);
            _repository.SaveChanges();


            var productReadDto = _mapper.Map <ProductReadDto>(productModel);

            return(CreatedAtRoute(nameof(GetProductById), new { Id = productReadDto.Id }, productReadDto));
        }
Пример #5
0
        public async Task <Product> Handle(CreateProductCommand request, CancellationToken cancellationToken)
        {
            Product exists = _productRepo.GetProductByDetails(request.product.Name, request.product.Description, request.product.Image, request.product.CategoryID);

            if (exists != null)
            {
                return(await Task.Run(() => { return new Product(); }));
            }
            else
            {
                return(await _productRepo.CreateProduct(request.product));
            }
        }
Пример #6
0
        public async Task <ActionResult <ProductCreateDto> > CreateProductAsync([FromForm] ProductCreateDto productCreateDto)
        {
            productCreateDto.ImageName = await SaveImage(productCreateDto.ImageFile);

            var productModel = _mapper.Map <Products>(productCreateDto);

            _repository.CreateProduct(productModel);
            _repository.SaveChanges();

            var productReadDto = _mapper.Map <ProductReadDto>(productModel);

            return(Ok(productReadDto));
        }
Пример #7
0
        public ActionResult <ProductReadDto> CreateProduct(ProductCreateDto productCreateDto)
        {
            if (_categoryRepo.GetCategoryById((int)productCreateDto.ProductCategoryId) == null)
            {
                return(NoContent());
            }
            var productModel = _mapper.Map <Product>(productCreateDto);

            _repository.CreateProduct(productModel);
            _repository.SaveChanges();

            var productReadDto = _mapper.Map <ProductReadDto>(productModel);

            return(CreatedAtRoute(nameof(GetProductById), new { Id = productReadDto }, productReadDto));
        }
Пример #8
0
 public void CreateProduct(double price, string title, string categoryTitle)
 {
     if (!CategoryService.CategoryExists(categoryTitle))
     {
         throw new Exception($"Category with title: {categoryTitle} does not exists");
     }
     if (price <= 0)
     {
         throw new Exception($"Price: {price} should be positive");
     }
     if (ProductExists(title))
     {
         throw new Exception($"Product with title: {title} already exists");
     }
     ProductRepo.CreateProduct(new Product(price, title, categoryTitle));
 }
Пример #9
0
 public IActionResult Create(Product product)
 {
     repo.CreateProduct(product);
     return(RedirectToAction(nameof(Index)));
 }
Пример #10
0
        public async Task <IActionResult> CreateProduct(CreateProductRequest request)
        {
            var data = await _repo.CreateProduct(request);

            return(Ok(data));
        }
Пример #11
0
        public async Task <ActionResult <Product> > CreateProduct([FromBody] Product product)
        {
            await _repo.CreateProduct(product);

            return(CreatedAtRoute("GetProduct", new { id = product.Id }, product));
        }
Пример #12
0
 public ActionResult <Product> CreateProduct(Product product)
 {
     _repository.CreateProduct(product);
     _repository.SaveChanges();
     return(CreatedAtRoute("GetProductById", new { Id = product.Id }, product));
 }