public ProductFullModel GetProductById( [ValueProvider(typeof(HeaderValueProviderFactory <string>))] string sessionKey, int id) { var context = new StoreContext(); using (context) { var user = UsersController.GetUserBySessionKey(sessionKey); if (user == null) { throw new ArgumentException("There is no user with such sessionKey!"); } var productById = context.Products.FirstOrDefault(p => p.ProductId == id); var newProductModel = new ProductFullModel() { ProductId = productById.ProductId, Price = productById.Price, Category = productById.Category.CategoryName, DateOfCreation = productById.DateOfCreation, Description = productById.Description, Manufacturer = productById.Manufacturer, ProductName = productById.ProductName, Quantity = productById.Quantity }; return(newProductModel); } }
public HttpResponseMessage PutProductByAdmin( [ValueProvider(typeof(HeaderValueProviderFactory <string>))] string sessionKey, ProductFullModel product) { var responseMsg = this.PerformOperationAndHandleExceptions( () => { var context = new StoreContext(); using (context) { var user = UsersController.GetUserBySessionKey(sessionKey); if (user == null) { throw new ArgumentException("There is no user with such sessionKey!"); } if (!CheckUserIsAdmin(user.UserId)) { throw new ArgumentException("You are not admin"); } var productToUpdate = context.Products.FirstOrDefault(p => p.ProductId == product.ProductId); productToUpdate.DateOfCreation = product.DateOfCreation; productToUpdate.Description = product.Description; productToUpdate.Manufacturer = product.Manufacturer; productToUpdate.Price = product.Price; productToUpdate.Quantity = product.Quantity; productToUpdate.ProductName = product.ProductName; context.SaveChanges(); var productModel = new ProductFullModel() { ProductId = productToUpdate.ProductId, ProductName = productToUpdate.ProductName, Price = productToUpdate.Price, Category = productToUpdate.Category.CategoryName, DateOfCreation = productToUpdate.DateOfCreation, Description = productToUpdate.Description, Manufacturer = productToUpdate.Manufacturer, Quantity = productToUpdate.Quantity }; var response = this.Request.CreateResponse(HttpStatusCode.OK, productModel); return(response); } }); return(responseMsg); }
public HttpResponseMessage PutProduct( [ValueProvider(typeof(HeaderValueProviderFactory <string>))] string sessionKey, int id) { var responseMsg = this.PerformOperationAndHandleExceptions( () => { var context = new StoreContext(); using (context) { var user = context.Users.Where(usr => usr.SessionKey == sessionKey).FirstOrDefault(); if (user == null) { throw new ArgumentException("There is no user with such sessionKey!"); } var productToUpdate = context.Products.FirstOrDefault(p => p.ProductId == id); productToUpdate.Quantity--; user.Products.Add(productToUpdate); context.SaveChanges(); var productModel = new ProductFullModel() { ProductId = productToUpdate.ProductId, ProductName = productToUpdate.ProductName, Price = productToUpdate.Price, Category = productToUpdate.Category.CategoryName, DateOfCreation = productToUpdate.DateOfCreation, Description = productToUpdate.Description, Manufacturer = productToUpdate.Manufacturer, Quantity = productToUpdate.Quantity }; var response = this.Request.CreateResponse(HttpStatusCode.OK, productModel); return(response); } }); return(responseMsg); }
private List <ProductFullModel> Parse(IHtmlDocument document) { var productList = new List <ProductFullModel>(); var products = document.QuerySelectorAll(_storeParserProvider.GetTagCatalog()); foreach (var itemProd in products) { var product = new ProductFullModel(); var nameTag = itemProd.QuerySelectorAll("a").FirstOrDefault(x => x.ClassName != null && x.ClassName.Contains("product-name")); string name = nameTag?.TextContent.Trim(); if (name == null) { continue; } product.Name = name; var imageUrlParent = itemProd.QuerySelector(_storeParserProvider.GetTagParentImage()); string imageUrl = imageUrlParent?.Children.FirstOrDefault(u => u.TagName.Contains("IMG"))?.Attributes.FirstOrDefault(u => u.LocalName == "data-original")?.Value.Trim(); if (imageUrl == null) { continue; } product.ImageSource = imageUrl; var priceTag = itemProd.QuerySelector(_storeParserProvider.GetTagPrice()); if (priceTag == null) { priceTag = itemProd.QuerySelector(_storeParserProvider.GetTagNewPrice()); } string price = priceTag?.TextContent.Trim().Replace(" ", string.Empty); if (price == null) { continue; } decimal number = 0M; var priceWithoutSpaceStr = Regex.Replace(price, ReqularErxpressionConstants.SelectInvisibleSpace, ""); if (decimal.TryParse(priceWithoutSpaceStr, out number)) { product.Price = number; } var currencyTag = itemProd.QuerySelector(_storeParserProvider.GetTagCurrency()); if (currencyTag == null) { currencyTag = itemProd.QuerySelector(_storeParserProvider.GetTagNewCurrency()); } string currency = currencyTag?.TextContent.Trim(); if (name == null) { continue; } product.Currency = currency; var desctiptionTag = itemProd.QuerySelector(_storeParserProvider.GetTagDescription()); string description = desctiptionTag?.TextContent.Trim(); if (description == null) { continue; } product.Descriptions = description; productList.Add(product); } return(productList); }