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)); }
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)); } }
public IHttpActionResult CreateProduct([FromBody] Product product) { if (product == null) { return(BadRequest()); } _productRepo.CreateProduct(product); return(StatusCode(HttpStatusCode.Created)); }
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)); }
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)); } }
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)); }
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)); }
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)); }
public IActionResult Create(Product product) { repo.CreateProduct(product); return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> CreateProduct(CreateProductRequest request) { var data = await _repo.CreateProduct(request); return(Ok(data)); }
public async Task <ActionResult <Product> > CreateProduct([FromBody] Product product) { await _repo.CreateProduct(product); return(CreatedAtRoute("GetProduct", new { id = product.Id }, product)); }
public ActionResult <Product> CreateProduct(Product product) { _repository.CreateProduct(product); _repository.SaveChanges(); return(CreatedAtRoute("GetProductById", new { Id = product.Id }, product)); }