using Asp.Versioning; using GerstITS.Examples.Api.Versioning; using GerstITS.System.Caching; using GerstITS.Validation; using GerstITS.Web.Api; using GerstITS.Web.Api.FluentApi; using Microsoft.AspNetCore.Mvc; namespace GerstITS.Examples.Api.Controllers._1._1; /// /// Is responsible to demonstrate the encrypted session based cache. /// [ApiController, ApiVersion(Versions._1_1), Route(ApplicationEnvironment.WebApi.ControllerRouteTemplate)] public class CacheController : FluentApiControllerBase { #region Fields private readonly ICache _cache; #endregion #region Constructors public CacheController(ICache cache, IValidator validator) : base(validator) { _cache = cache; } #endregion #region Methods /// /// Gets a cached value by specified key. /// /// Key of the cached value. /// Returns the cached value. [HttpGet, Route("{key}")] public IActionResult Get(string key) { return Api().Use(key) .Get(_cache.Get); } /// /// Checks whether a value is cached for the specified key. /// /// Key of the cached value. /// Returns true if a value is cached. [HttpGet, Route("{key}/Exists")] public IActionResult Exists(string key) { return Api().Use(key) .Get(_cache.ContainsKey); } /// /// Caches a value for the specified key. /// /// Key of the value to cache. /// Value to cache. /// Returns the cached value. [HttpPost, Route("{key}")] public IActionResult Set(string key, [FromBody] string value) { return Api().Use(key) .Create(x => { _cache.Set(x, value); return value; }); } /// /// Removes a cached value by specified key. /// /// Key of the cached value. /// Returns true if the value was removed. [HttpDelete, Route("{key}")] public IActionResult Remove(string key) { return Api().Use(key) .Delete(x => { _cache.Remove(x); return true; }); } #endregion }