namespace sopro.Controllers { public class BookingController : Controller { private static IMemoryCache _cache; private List<Booking> bookingsList; private List<Guid> cacheKeys; public BookingController(IMemoryCache memoryCache) { _cache = memoryCache; bookingsList = new List<Booking>(); cacheKeys = Booking.guids; } public IActionResult Index() { if (_cache != null) { foreach (Guid guid in cacheKeys) { var cacheKey = guid.ToString(); bookingsList.Add(_cache.Get<Booking>(cacheKey)); } } ViewData["bookings"] = bookingsList; return View(); } [HttpGet] public IActionResult Create() { return View(); } [HttpPost] public IActionResult Create(Booking newBooking) { if (ModelState.IsValid) { var cacheEntryOptions = new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(100)); _cache.Set(newBooking.InstanceID.ToString(), newBooking, cacheEntryOptions); ViewBag.Success = "Booking was successfully created!"; return View(); } else { cacheKeys.Remove(newBooking.InstanceID); ModelState.AddModelError(string.Empty, "Booking was not created! Please enter correct booking details."); return View(newBooking); //return Content("Failed to create the product. Please try again"); } } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)