public async Task SaveProduct(ProductPostModel model) { using (var db = new SupplierbaseContext()) { try { var entity = await db.Products.FirstOrDefaultAsync(p => p.Id == model.Id); if (entity == null) { entity = new Product { Name = model.Name, }; await db.Products.AddAsync(entity); } else { entity.Name = model.Name; db.Products.Update(entity); } await db.SaveChangesAsync(); model.Id = entity.Id; await AddProductLimits(model); } catch (Exception ex) { throw ex; } } }
public async Task <ProductModel> CreateAsync(ProductPostModel product) { var isCodeExist = await IsCodeExist(product.Code); if (isCodeExist) { throw new BadRequestException("Code is not unique please enter a diffrent code"); } if (product.Price <= 0) { throw new BadRequestException("Price should be greater then 0"); } var entity = mapper.Map <ProductEntity>(product); if (product.Price < 999) { entity.IsConfirmed = true; } await repository.CreateAsync(entity); var model = mapper.Map <ProductModel>(entity); return(model); }
public ActionResult Post([FromBody] ProductPostModel content) { switch (content.section) { case "Search": // Example: https://gyazo.com/f6e11a26e75e07d8c32e3607ef6ee63c var products = Products(null, null, null); var Matches = products.Where(x => x.title.Contains(content.input.search, StringComparison.CurrentCultureIgnoreCase) || x.description.Contains(content.input.search, StringComparison.CurrentCultureIgnoreCase) || x.tags.Contains(content.input.search, StringComparer.OrdinalIgnoreCase)).ToList(); if (content.AdvancedSearch != null) { Matches = AdvancedSearch(Matches, content.AdvancedSearch); } return(Ok(Matches)); case "createOffer": ObjectId sellerId = GetAllUsers(content.createProduct.username, "users").Result[0]._id; createOffer(content.createProduct, client, sellerId); break; case "Purchase": break; } return(Ok()); }
public ProductViewModel Create(ProductPostModel model) { var product = _mapper.Map <ProductModel>(model); var createdProduct = _productsService.Create(product); return(_mapper.Map <ProductViewModel>(createdProduct)); }
public Products Create(ProductPostModel product, User employee) { Products productToAdd = ProductPostModel.ToProduct(product); Category category = categoryService.GetById(productToAdd.CategoryId, employee); productToAdd.CategoryId = category.Id; context.Products.Add(productToAdd); context.SaveChanges(); return(productToAdd); }
public async Task <IActionResult> SaveProduct([FromBody] ProductPostModel model) { var validator = new ProductValidator(); var result = await validator.ValidateAsync(model); if (!result.IsValid) { var error = string.Join(';', result.Errors); throw new Exception(error); } await _service.SaveProduct(model); return(Ok()); }
public async Task <Guid> Create(ProductPostModel postModel) { if (postModel == null) { throw new InvalidArgumentException($"{typeof(ProductPostModel).Name} was null!"); } //await UploadImage(postModel.Image); var entity = _mapper.Map <Product>(postModel); await _fileService.UploadImage(entity.Image); await _writeOnly.SaveChangesAsync(entity); return(entity.Id); }
private async Task AddProductLimits(ProductPostModel model) { using (var db = new SupplierbaseContext()) { var productLimits = await db.Limits.Where(c => c.ProductId == model.Id).ToListAsync(); db.RemoveRange(productLimits); var limits = model.Limits.CalculateRangeCapacity(); foreach (var limit in limits) { var entity = new Limit { ProductId = model.Id.Value, LimitValue = limit.LimitValue, RangeCapacity = limit.RangeCapacity, }; db.Limits.Add(entity); } await db.SaveChangesAsync(); } }
public async Task <IActionResult> Create(ProductPostModel product) { var serviceRepsonse = await productService.CreateAsync(product); return(Ok(serviceRepsonse)); }
public void Post([FromBody] ProductPostModel product) { User employee = this.userService.GetCurrentUser(HttpContext); productService.Create(product, employee); }
public async Task <ActionResult <Guid> > Create([FromBody] ProductPostModel model) { var newGuid = await _productService.Create(model); return(StatusCode((int)HttpStatusCode.Created, newGuid)); }