public async Task <IActionResult> Post([FromBody] NewProductDto newProduct) { var createdProduct = await _productsService.Create(newProduct); var productUri = CreateResourceUri(createdProduct.Id); return(Created(productUri, createdProduct)); }
public async Task <IActionResult> Save([FromBody] ProductsModel products) { if (!User.IsInRole("Admin")) { return(BadRequest("Not Authorized")); } await _productsService.Create(products); return(Ok()); }
public IActionResult Create(CreateProductInputView input) { _productsService.Create(input.Name, input.ProductCode, input.Viscosity, input.PackageCapacity, input.PackageWeight); return(this.Redirect("/Products/Add")); }
public void CreateSavesNewProduct() { var prod1 = new Mock <IProduct>(MockBehavior.Strict); prod1.SetupGet(p => p.Id).Returns(_id1); IProduct prod2 = null; _repo.Setup(r => r.Get(_id1)).Returns <IProduct>(null); _repo.Setup(r => r.Save(prod1.Object, true)).Verifiable(); _repo.Setup(r => r.Save(prod1.Object, false)).Callback <IProduct, bool>((p, _) => prod2 = p); _service.Create(prod1.Object); _repo.Verify(r => r.Get(_id1), Times.Once); _repo.Verify(r => r.Save(prod1.Object, true), Times.Never); _repo.Verify(r => r.Save(prod1.Object, false), Times.Once); prod2.Should().Be(prod1.Object); }
public IActionResult Create(ProductCreateInputModel model) { if (!ModelState.IsValid) { return(Redirect("/Products/Create")); } productsService.Create(model.MapTo <Product>()); return(this.Redirect("/Products/All")); }
public IActionResult OnPost() { _productsService.Create(new ProductCreateInputModel() { Name = Name, Type = Type, Price = Price, Duration = Duration }); return(RedirectToPage("./Index")); }
public async Task <IActionResult> Post([FromBody] Products products) { try { await _service.Create(products); return(Created("Criado com Sucesso", await _service.GetId(products.code))); } catch (Exception ex) { return(StatusCode((int)HttpStatusCode.InternalServerError, ex.Message)); } }
public void Create_works_Properly() { string errorMessagePrefix = "ProductsService Create() method does not work properly."; var context = OilsProDbContextInMemoryFactory.InitializeContext(); this.productsService = new ProductsService(context); var result = productsService.Create("Product1", "01010101", "10/10", 110, 1800); var expected = context.Products.First(); Assert.AreEqual(expected, result, errorMessagePrefix); }
public async Task <IActionResult> Create([FromForm] Products products) { if (!ModelState.IsValid) { _logger.LogError("Invalid owner object sent from client."); return(BadRequest("Invalid model object")); } else { await _productsService.Create(products); } return(NoContent()); }
public Product Create([FromBody] Product p) { try { Product res = _ProductsService.Create(p); return(res); } catch (Exception ex) { _logger.LogError(ex.Message); throw ex; } }
public async Task <IActionResult> Create([Bind("ProductId,ProductName,SupplierId,CategoryId,QuantityPerUnit,UnitPrice,UnitsInStock,UnitsOnOrder,ReorderLevel,Discontinued")] Products products) { if (ModelState.IsValid) { await _productsService.Create(products); return(RedirectToAction(nameof(Index))); } ViewData["CategoryId"] = await _categoriesService.GetCategoriesSelectListAsync(products.CategoryId); ViewData["SupplierId"] = await _suppliersService.GetSuppliersSelectListAsync(products.SupplierId); return(View(products)); }
public IActionResult Create(CreateProductInputView input) { if (!ModelState.IsValid) { return(View()); } _productsService.Create(input.Name, input.ProductCode, input.Viscosity, input.PackageCapacity, input.PackageWeight); return(this.Redirect("/Nomenclatures/Products")); }
public ActionResult Post([FromBody] Products request, [FromHeader] string Token) { try { _authService.Authorization(Token); Products response = _productService.Create(request); return(Ok(response)); } catch (Exception ex) { Response.StatusCode = 403; return(BadRequest(StatusCode(403, ex.Message))); } }
public async Task <IActionResult> Create(string name, string description, int price, int stock, List <IFormFile> images) { if (!(await _usersService.IsAdmin())) { return(StatusCodeAndDtoWrapper.BuildUnauthorized("Only admin user can create prodcuts")); } // If the user sends `images` POST param then the list<IFormFile> will be populated, if the user sends `images[]` instead, then it will be empty // this is why I populate that list with this little trick if (images?.Count == 0) { images = Request.Form.Files.GetFiles("images[]").ToList(); } List <Tag> tags = new List <Tag>(); List <Category> categories = new List <Category>(); foreach (string formKey in Request.Form.Keys) { Regex regex = new Regex("tags|categories\\[(?<name>\\w+)\\]"); Match match = regex.Match(formKey); if (match.Success && formKey.StartsWith("tag")) { var tagName = match.Groups["name"].Value; tags.Add(new Tag { Name = tagName, Description = Request.Form[key: formKey].ToString() }); } if (match.Success && formKey.StartsWith("cate")) { var categoryName = match.Groups["name"].Value; categories.Add(new Category { Name = categoryName, Description = Request.Form[key: formKey].ToString() }); } } Product product = await _productsService.Create(name, description, price, stock, tags, categories, images); return(StatusCodeAndDtoWrapper.BuildSuccess(ProductDetailsDto.Build(product))); }
public IActionResult Create(Product newProduct) { if (!ModelState.IsValid) { return(View(newProduct)); } try { newProduct = _productsService.Create(newProduct); return(RedirectToAction("Index", "Products", new { id = newProduct.Id })); } catch { return(View()); } }
public IActionResult Create([FromForm] ProductCreateRequest form) { ResultCode resultCode; int?productID; (resultCode, productID) = _service.Create(form); Result error; int statusCode; (statusCode, error) = ResultHandler.GetStatusCodeAndResult(resultCode); GeneralResponse response = new GeneralResponse { Result = productID, Error = error, }; return(StatusCode(statusCode, response)); }
//IActionResult é mais genérico e conseguimos retornar tanto o Unauthorized, quanto o Ok. public IActionResult Create(CreateProductRequest request) { StringValues userId; if (!Request.Headers.TryGetValue("UserId", out userId)) { return(Unauthorized()); } var user = _usersService.GetById(Guid.Parse(userId)); if (user == null) { return(Unauthorized()); } if (user.Profile != UserProfile.Admin) { return(Unauthorized()); } var response = _productsService.Create( request.Name, request.Description, request.Accessories, request.Brand, request.Model, request.Voltage, request.Frequency, request.PricePerDay, request.RentingPeriodLimit ); if (!response.IsValid) { return(BadRequest(response.Errors)); } return(NoContent()); }
public async Task <IActionResult> Post(ProductsViewModel model) { await productService.Create(model.Product.Name, model.Product.Description, model.Product.Value); return(RedirectToAction(nameof(Index))); }
public async Task <ActionResult> Post([FromBody] ProductDto productDto) { var id = await _productsService.Create(productDto); return(Created($"/{ApiConstants.BaseUri}/{ApiConstants.IdParamUri}/{id}", id)); }
public async Task <ActionResult <Product> > PostProduct(Product product) { await _productsService.Create(product); return(CreatedAtAction("GetProduct", new { id = product.ProductId }, product)); }
public IActionResult Create([FromBody] Product product) { var savedProduct = _productsService.Create(product); return(Ok(savedProduct)); }