Service de cache de database fonctionnel

This commit is contained in:
MarcEricMartel
2022-10-21 14:52:25 -07:00
parent bd1b2edfea
commit 09786b0c50
5 changed files with 145 additions and 11 deletions

View File

@@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using GrossesMitainesAPI.Services;
namespace GrossesMitainesAPI.Controllers;
@@ -14,10 +15,12 @@ namespace GrossesMitainesAPI.Controllers;
public class ProductController : ControllerBase {
private readonly ILogger<ProductController> _logger;
private readonly InventoryContext _context;
private readonly DatabaseCacheService _cache;
public ProductController(ILogger<ProductController> logger, InventoryContext context) {
public ProductController(ILogger<ProductController> logger, InventoryContext context, DatabaseCacheService cache) {
_logger = logger;
_context = context;
_cache = cache;
}
[EnableCors("_myAllowSpecificOrigins")]
@@ -44,6 +47,7 @@ public class ProductController : ControllerBase {
try {
_context.Products.Add(prod);
_context.SaveChanges();
_cache.askForRefresh();
}
catch (Exception e) {
_logger.LogError(8, e.Message);
@@ -59,6 +63,7 @@ public class ProductController : ControllerBase {
try {
_context.Products.Update(prod);
_context.SaveChanges();
_cache.askForRefresh();
}
catch (Exception e) {
_logger.LogError(8, e.Message);
@@ -74,6 +79,7 @@ public class ProductController : ControllerBase {
try {
_context.Products.Remove(_context.Products.Where(x => x.Id == id).First());
_context.SaveChanges();
_cache.askForRefresh();
}
catch (Exception e) {
_logger.LogError(8, e.Message);