public IActionResult SaveProduct(ProductRequestDTO productRequest) { if (productRequest.ProductId > 0) { _product.UpdateProduct(productRequest); } else { _product.CreateProduct(productRequest); } return(RedirectToAction("Index")); }
protected Task <DynamicParameters> SetParameterAsync(string command, ProductRequestDTO productRequest) { return(Task.Run(() => { DynamicParameters dynamicParameters = new(); dynamicParameters.Add("@Command", command, DbType.String, direction: ParameterDirection.Input); dynamicParameters.Add("@ProductIdentity", productRequest?.ProductIdentity, DbType.Guid, direction: ParameterDirection.Input); dynamicParameters.Add("@ProductName", productRequest?.ProductName, DbType.String, direction: ParameterDirection.Input); dynamicParameters.Add("@UnitPrice", productRequest?.UnitPrice, DbType.String, direction: ParameterDirection.Input); return dynamicParameters; })); }
public object Get(ProductRequestDTO request) { Random rand = new Random(); var client = new MongoClient(connectionString); var database = client.GetDatabase("products"); var collection = database.GetCollection <Product>("products"); var products = collection.AsQueryable <Product>(); //Check if the Note Id was passed in the Request. if (!request.Id.HasValue) { //Note Id is not present in the Request, hence return all Notes. var productsResponse = from p in products select new ProductResponseDTO { _Id = p._id, Id = p.id, Name = p.name, Category = p.category, Description = p.description, Price = p.price, }; return(productsResponse.ToList()); } //Note Id is present in the Request. Check if there is a corresponding Note. var product = products.Where(p => p._id == request.Id.Value).FirstOrDefault(); if (product != null) { //Return the corresponding Note. return(new ProductResponseDTO { _Id = product._id, Name = product.name, Category = product.category, Description = product.description, Price = product.price, }); } else { //Return Empty string as there is no corresponding Note. return(string.Empty); } }
public async Task <IActionResult> Create([Bind("Id,Name,Quantity")] ProductRequestProductsDTO productRequest) { var product = _productRequestsService.GetProductRequestProducts();//Pull data from api again ProductRequest request = new ProductRequest { Confirmed = false, Quantity = productRequest.Quantity, ProductName = productRequest.Name, Price = product.Result.First(p => p.Name == productRequest.Name).Price }; ProductRequestDTO requestAPI = new ProductRequestDTO { Confirmed = false, Quantity = productRequest.Quantity, ProductName = productRequest.Name, Price = product.Result.First(p => p.Name == productRequest.Name).Price }; if (ModelState.IsValid) { _context.Add(request); await _context.SaveChangesAsync(); } try { var response = _productRequestsService.PushProductRequest(requestAPI); } catch (Exception e) { //_logger.LogWarning(e); } return(RedirectToAction(nameof(Index))); }
public async Task <string> PostProductsPaginated([FromBody] string productRequestJson) { //Create response object ProductsResponse productsResponse = new ProductsResponse(); try { //Deserialize request information from client data transfer object ProductRequestDTO productRequest = JsonConvert.DeserializeObject <ProductRequestDTO>(productRequestJson); //Get list of all product categories and add to response List <CategoryModel> categories = _context.ProductCategory .Select(c => new CategoryModel() { CategoryId = c.ProductCategoryId, CategoryName = c.CategoryName }) .ToList(); productsResponse.Categories = categories; //Create a queryable collection and filter using the ids (if any) provided in the request IQueryable <Product> productQuery = FilterProducts(productRequest.FilterCategoryId, productRequest.FilterVehicleId); //Sort products using the requested sort parameters switch (productRequest.SortBy) { case ProductRequestDTO.SortType.ProductName: if (productRequest.Descending) { productQuery = productQuery.OrderByDescending(p => p.ProductName); } else { productQuery = productQuery.OrderBy(p => p.ProductName); } break; case ProductRequestDTO.SortType.ProductPrice: if (productRequest.Descending) { productQuery = productQuery.OrderByDescending(p => p.CurrentPrice); } else { productQuery = productQuery.OrderBy(p => p.CurrentPrice); } break; default: if (productRequest.Descending) { productQuery = productQuery.OrderByDescending(p => p.ProductId); } else { productQuery = productQuery.OrderBy(p => p.ProductId); } break; } //Create a list of sorted and filtered products that are paginated, //this uses my Paginate extension method that can be found in the Utilities folder List <Product> products = productQuery .Paginate(productRequest.PageNumber, productRequest.QuantityPerPage) .ToList(); //Calculate the total number of pages double count = await productQuery.CountAsync(); int totalPages = (int)Math.Ceiling(count / productRequest.QuantityPerPage); //Add to the response productsResponse.TotalPages = totalPages; productsResponse.Products = products; productsResponse.Success = products != null; } catch (Exception e) { productsResponse.Success = false; productsResponse.ErrorMessage = e.Message; } //Serialize and return return(JsonConvert.SerializeObject(productsResponse, Formatting.Indented, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore })); }