Image preview and images for items (wip)
This commit is contained in:
@@ -20,21 +20,23 @@ using GrossesMitainesAPI.Services;
|
||||
/// qui sera effectuée dans les 10 secondes après
|
||||
/// l'éxécution d'une modification de la BD.
|
||||
/// </summary>
|
||||
[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]"),
|
||||
[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]"),
|
||||
Authorize(AuthenticationSchemes = "Identity.Application")]
|
||||
public class ProductController : ControllerBase {
|
||||
#region DI Fields
|
||||
private readonly ILogger<ProductController> _logger;
|
||||
private readonly InventoryContext _context;
|
||||
private readonly DatabaseCacheService _cache;
|
||||
private readonly IWebHostEnvironment _hostEnvironment;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Ctor
|
||||
public ProductController(ILogger<ProductController> logger, InventoryContext context, DatabaseCacheService cache) {
|
||||
public ProductController(ILogger<ProductController> logger, InventoryContext context, DatabaseCacheService cache, IWebHostEnvironment hostEnvironment) {
|
||||
_logger = logger;
|
||||
_context = context;
|
||||
_cache = cache;
|
||||
_hostEnvironment = hostEnvironment;
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -55,10 +57,13 @@ public class ProductController : ControllerBase {
|
||||
}
|
||||
|
||||
[EnableCors("_myAllowSpecificOrigins"), HttpPost(Name = "Product")]
|
||||
public ActionResult<Product> Post(Product prod) {
|
||||
public async Task<ActionResult<Product>> Post(Product prod) {
|
||||
if (prod.Price <= prod.PromoPrice)
|
||||
prod.PromoPrice = prod.Price - 0.01M;
|
||||
try {
|
||||
if (prod.ImageFile != null)
|
||||
prod.ImageName = await SaveImage(prod.ImageFile);
|
||||
|
||||
_context.Products.Add(prod);
|
||||
_context.SaveChanges();
|
||||
}
|
||||
@@ -99,4 +104,16 @@ public class ProductController : ControllerBase {
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region UtilityMethods
|
||||
private async Task<string> SaveImage(IFormFile imageFile) {
|
||||
string imageName = new String(Path.GetFileNameWithoutExtension(imageFile.FileName).Take(10).ToArray()).Replace(' ', '-');
|
||||
imageName = imageName + DateTime.Now.ToString("yymmssfff") + Path.GetExtension(imageFile.FileName);
|
||||
var imagePath = Path.Combine(_hostEnvironment.ContentRootPath, "Images", imageName);
|
||||
using (var fileStream = new FileStream(imagePath, FileMode.Create)) {
|
||||
await imageFile.CopyToAsync(fileStream);
|
||||
}
|
||||
return imageName;
|
||||
}
|
||||
#endregion
|
||||
}
|
@@ -1,5 +1,6 @@
|
||||
using Microsoft.Data.SqlClient.Server;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace GrossesMitainesAPI.Models;
|
||||
|
||||
@@ -31,4 +32,7 @@ public class Product {
|
||||
public DateTime? LastSale { get; set; }
|
||||
public DateTime? LastHit { get; set; }
|
||||
public string? ImageName { get; set; } // Base pour sortir les images ({ImageName}.jpg , {ImageName}_thumbnail.jpg, etc...)
|
||||
|
||||
[NotMapped]
|
||||
public IFormFile? ImageFile { get; set; }
|
||||
}
|
Reference in New Issue
Block a user