the rest of the code:
[Route("api/[controller]")]
[ApiController]
public class AdminAuthController : ControllerBase
[HttpPut("editProduct")]
public async Task<IActionResult> EditProduct([FromForm]ProductDto dto)
{
var find = await _dbContext.Products.FirstOrDefaultAsync(x => x.Id == dto.Id);
if (find == null)
return BadRequest("Could not find product");
if (dto.File != null)
{
if (dto.File.Length > 0)
{
string root = _env.WebRootPath + "/client/src/images/ProductImages/";
string oldFileName = find.ImagePath;
if (System.IO.File.Exists(Path.Combine(root, oldFileName)))
System.IO.File.Delete(Path.Combine(root, oldFileName));
string filename = Guid.NewGuid().ToString() + "_" + dto.File.FileName;
string filepath = Path.Combine(root, filename);
using (var stream = System.IO.File.Create(filepath))
{
await dto.File.CopyToAsync(stream);
}
find.ImagePath = filename;
}
}
if (!string.IsNullOrWhiteSpace(dto.Name))
find.Name = dto.Name;
if (!string.IsNullOrWhiteSpace(dto.Description))
find.Description = dto.Description;
if (dto.Price > 0)
find.Price = dto.Price;
if (!string.IsNullOrWhiteSpace(dto.Category))
find.Category = dto.Category;
await _dbContext.SaveChangesAsync();
return Ok();
}
the rest of the front end is just a button that starts the "edit" function
and there are some inputs that have a onChange handler function