A set of technologies in .NET for building web applications and web services. Miscellaneous topics that do not fit into specific categories.
The reason why I harded coded it is because i wanted to see if it would invoke that get action at all and it did not do anyhting
the same as with your code that i tried to invoke trough the url didnt do anything either
I am just trying to create a simple product filter and it taking longer than a week to figure this out
I have absoluty no qlue anymore, anything that i have tried on blog, youtubes videos, forums doesnt work.
it is really frustrating me.
My Full code:
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ReactWebshop.Models;
using ReactWebshop.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Hosting;
using System.IO;
namespace ReactWebshop.Controllers
{
[Route("api/Upload")]
[ApiController]
public class ProductsController : ControllerBase
{
private readonly DbProductContext _context;
private readonly IWebHostEnvironment _env;
public ProductsController(DbProductContext context, IWebHostEnvironment webHostEnvironment)
{
_context = context;
_env = webHostEnvironment;
}
[HttpPost]
public async Task<ActionResult<Product>> PostProduct([FromForm] ProductFile productFile)
{
if (productFile.File.Length > 0)
{
string root = _env.WebRootPath + "/img/";
string fileName = Guid.NewGuid().ToString() + "_" + productFile.File.FileName;
string Filepath = Path.Combine(root, fileName);
using (var stream = System.IO.File.Create(Filepath))
{
await productFile.File.CopyToAsync(stream);
}
Product newproduct = new Product()
{
Title = productFile.Title,
Description = productFile.Description,
Price = productFile.Price,
Quantity = productFile.Quantity,
FilePath = fileName
};
_context.products.Add(newproduct);
await _context.SaveChangesAsync();
return Ok("complete");
}
return BadRequest();
}
[HttpDelete("{id}")]
public async Task<IActionResult> deleteproduct(int id)
{
var getproduct = await _context.products.FindAsync(id);
if (getproduct == null)
return NotFound();
_context.products.Remove(getproduct);
await _context.SaveChangesAsync();
return NoContent();
}
[Route("{Title?}/{Description?}/{Pricemin?}/{Pricemax?}")]
public async Task<List<Product>> GetFilter(
[FromQuery] string Title,
string Description,
int Pricemin,
int Pricemax
)
{
IQueryable<Product> products = _context.products;
if (!string.IsNullOrEmpty(Title))
products = products.Where(x => x.Title.ToLower().Contains(Title.ToLower()));
if (!string.IsNullOrEmpty(Description))
products = products.Where(x => x.Description.ToLower().Contains(Description.ToLower()));
if (Pricemin > 0)
products = products.Where(x => x.Price >= Pricemin);
if (Pricemax > 0)
products = products.Where(x => x.Price <= Pricemax);
return await products.ToListAsync();
}
[HttpGet]
public async Task<IEnumerable<Product>> GetProducts()
{
var allproduct = from m in _context.products
select m;
return await allproduct.ToListAsync();
}
}
}
Reactjs
async Getfilter1() {
const TitleFIlter = document.getElementById("TitleFIlter").value;
const descriptionFilter = document.getElementById("DescriptionFilter").value;
const priceminFIlter = document.getElementById("PriceMin").value;
const pricemaxFIlter = document.getElementById("PriceMax").value;
const url = `api/Upload/?Title=${TitleFIlter}&Description=${descriptionFilter}&Pricemin=${priceminFIlter}&Pricemax=${pricemaxFIlter}`;
const response = await fetch(url);
const data = await response.json();
this.setState({
allproducts: data,
tableLoading: false
});
}
<div id="filtersection" style={this.state.filters ? FilterTrue : Filterfalse}>
<div className="FilterBloks" style={{width: "20%"}} >
<label>Title:</label>
<input type="text" id="TitleFIlter" />
</div>
<div className="FilterBloks" style={{ width: "15%" }}>
<label>Description:</label>
<input type="text" id="DescriptionFilter" />
</div>
<div className="FilterBloks" style={{ width: "10%" }}>
<label>Price min:</label>
<input type="text" id="PriceMin" />
</div>
<div className="FilterBloks" style={{ width: "10%" }}>
<label>Price max:</label>
<input type="text" id="PriceMax" />
</div>
<div className="FilterBloks" style={{ width: "10%" }}>
<label>Quantity:</label>
<input type="text" id="QuantityFilter" />
</div>
<div className="FilterBloks" style={{ width: "20%" }}>
<button className="searchButton" onClick={() => this.Getfilter1() }>Search</button>
</div>
</div>