mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
abstract API action details
This commit is contained in:
parent
4f4394e693
commit
cefdd4453c
17 changed files with 55 additions and 121 deletions
|
|
@ -1,4 +1,6 @@
|
|||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading.Tasks;
|
||||
using FilterLists.Services.Seed;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
|
|
@ -11,23 +13,35 @@ namespace FilterLists.Api.V1.Controllers
|
|||
[ResponseCache(Duration = 14400)]
|
||||
public class BaseController : Controller
|
||||
{
|
||||
protected static readonly TimeSpan MemoryCacheExpirationDefault = TimeSpan.FromHours(4);
|
||||
protected readonly IMemoryCache MemoryCache;
|
||||
private static readonly TimeSpan MemoryCacheExpirationDefault = TimeSpan.FromHours(4);
|
||||
protected readonly SeedService SeedService;
|
||||
private readonly IMemoryCache memoryCache;
|
||||
|
||||
public BaseController()
|
||||
{
|
||||
}
|
||||
|
||||
protected BaseController(IMemoryCache memoryCache) => MemoryCache = memoryCache;
|
||||
protected BaseController(IMemoryCache memoryCache) => this.memoryCache = memoryCache;
|
||||
|
||||
protected BaseController(IMemoryCache memoryCache, SeedService seedService)
|
||||
{
|
||||
MemoryCache = memoryCache;
|
||||
this.memoryCache = memoryCache;
|
||||
SeedService = seedService;
|
||||
}
|
||||
|
||||
protected IActionResult CoalesceNotFound(JsonResult result) =>
|
||||
protected async Task<IActionResult> Get<T>(Func<Task<T>> createAction, int? actionParam = null,
|
||||
[CallerMemberName] string actionName = null)
|
||||
{
|
||||
var cacheKey = GetType().Name + "_" + actionName + "_" + actionParam;
|
||||
var result = await memoryCache.GetOrCreateAsync(cacheKey, entry =>
|
||||
{
|
||||
entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault;
|
||||
return createAction();
|
||||
});
|
||||
return CoalesceNotFound(Json(result));
|
||||
}
|
||||
|
||||
private IActionResult CoalesceNotFound(JsonResult result) =>
|
||||
result.Value is null ? NotFound() : (IActionResult)result;
|
||||
}
|
||||
}
|
||||
|
|
@ -15,11 +15,8 @@ public DependentsController(IMemoryCache memoryCache, SeedService seedService) :
|
|||
|
||||
[HttpGet("seed")]
|
||||
public async Task<IActionResult> Seed() =>
|
||||
CoalesceNotFound(Json(await MemoryCache.GetOrCreate("DependentsController_Seed", entry =>
|
||||
{
|
||||
entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault;
|
||||
return SeedService.GetAllAsync<Dependent, DependentSeedDto>(typeof(Dependent).GetProperty("DependentFilterListId"),
|
||||
typeof(Dependent).GetProperty("DependencyFilterListId"));
|
||||
})));
|
||||
await Get(() => SeedService.GetAllAsync<Dependent, DependentSeedDto>(
|
||||
typeof(Dependent).GetProperty("DependentFilterListId"),
|
||||
typeof(Dependent).GetProperty("DependencyFilterListId")));
|
||||
}
|
||||
}
|
||||
|
|
@ -15,11 +15,8 @@ public ForksController(IMemoryCache memoryCache, SeedService seedService) : base
|
|||
|
||||
[HttpGet("seed")]
|
||||
public async Task<IActionResult> Seed() =>
|
||||
CoalesceNotFound(Json(await MemoryCache.GetOrCreate("ForksController_Seed", entry =>
|
||||
{
|
||||
entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault;
|
||||
return SeedService.GetAllAsync<Fork, ForkSeedDto>(typeof(Fork).GetProperty("UpstreamFilterListId"),
|
||||
typeof(Fork).GetProperty("ForkFilterListId"));
|
||||
})));
|
||||
await Get(() => SeedService.GetAllAsync<Fork, ForkSeedDto>(
|
||||
typeof(Fork).GetProperty("UpstreamFilterListId"),
|
||||
typeof(Fork).GetProperty("ForkFilterListId")));
|
||||
}
|
||||
}
|
||||
|
|
@ -17,27 +17,15 @@ public LanguagesController(IMemoryCache memoryCache, SeedService seedService, La
|
|||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> Index() =>
|
||||
CoalesceNotFound(Json(await MemoryCache.GetOrCreate("LanguagesController_Index", entry =>
|
||||
{
|
||||
entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault;
|
||||
return languageService.GetAllTargetedAsync();
|
||||
})));
|
||||
await Get(() => languageService.GetAllTargetedAsync());
|
||||
|
||||
[HttpGet]
|
||||
[Route("{id}")]
|
||||
public async Task<IActionResult> GetById(int id) =>
|
||||
CoalesceNotFound(Json(await MemoryCache.GetOrCreate("LanguagesController_GetById_" + id, entry =>
|
||||
{
|
||||
entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault;
|
||||
return languageService.GetTargetedByIdAsync(id);
|
||||
})));
|
||||
await Get(() => languageService.GetTargetedByIdAsync(id), id);
|
||||
|
||||
[HttpGet("seed")]
|
||||
public async Task<IActionResult> Seed() =>
|
||||
CoalesceNotFound(Json(await MemoryCache.GetOrCreate("LanguagesController_Seed", entry =>
|
||||
{
|
||||
entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault;
|
||||
return SeedService.GetAllAsync<Language, LanguageSeedDto>();
|
||||
})));
|
||||
await Get(() => SeedService.GetAllAsync<Language, LanguageSeedDto>());
|
||||
}
|
||||
}
|
||||
|
|
@ -15,10 +15,6 @@ public LicensesController(IMemoryCache memoryCache, SeedService seedService) : b
|
|||
|
||||
[HttpGet("seed")]
|
||||
public async Task<IActionResult> Seed() =>
|
||||
CoalesceNotFound(Json(await MemoryCache.GetOrCreate("LicensesController_Seed", entry =>
|
||||
{
|
||||
entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault;
|
||||
return SeedService.GetAllAsync<License, LicenseSeedDto>();
|
||||
})));
|
||||
await Get(() => SeedService.GetAllAsync<License, LicenseSeedDto>());
|
||||
}
|
||||
}
|
||||
|
|
@ -17,27 +17,15 @@ public ListsController(IMemoryCache memoryCache, SeedService seedService, Filter
|
|||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> Index() =>
|
||||
CoalesceNotFound(Json(await MemoryCache.GetOrCreate("ListsController_Index", entry =>
|
||||
{
|
||||
entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault;
|
||||
return filterListService.GetAllAsync();
|
||||
})));
|
||||
await Get(() => filterListService.GetAllAsync());
|
||||
|
||||
[HttpGet]
|
||||
[Route("{id}")]
|
||||
public async Task<IActionResult> GetById(int id) =>
|
||||
CoalesceNotFound(Json(await MemoryCache.GetOrCreate("ListsController_GetById_" + id, entry =>
|
||||
{
|
||||
entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault;
|
||||
return filterListService.GetDetailsAsync((uint)id);
|
||||
})));
|
||||
await Get(() => filterListService.GetDetailsAsync(id), id);
|
||||
|
||||
[HttpGet("seed")]
|
||||
public async Task<IActionResult> Seed() =>
|
||||
CoalesceNotFound(Json(await MemoryCache.GetOrCreate("ListsController_Seed", entry =>
|
||||
{
|
||||
entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault;
|
||||
return SeedService.GetAllAsync<FilterList, FilterListSeedDto>();
|
||||
})));
|
||||
await Get(() => SeedService.GetAllAsync<FilterList, FilterListSeedDto>());
|
||||
}
|
||||
}
|
||||
|
|
@ -16,12 +16,8 @@ public ListsLanguagesController(IMemoryCache memoryCache, SeedService seedServic
|
|||
|
||||
[HttpGet("seed")]
|
||||
public async Task<IActionResult> Seed() =>
|
||||
CoalesceNotFound(Json(await MemoryCache.GetOrCreate("ListsLanguagesController_Seed", entry =>
|
||||
{
|
||||
entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault;
|
||||
return SeedService.GetAllAsync<FilterListLanguage, FilterListLanguageSeedDto>(
|
||||
typeof(FilterListLanguage).GetProperty("FilterListId"),
|
||||
typeof(FilterListLanguage).GetProperty("LanguageId"));
|
||||
})));
|
||||
await Get(() => SeedService.GetAllAsync<FilterListLanguage, FilterListLanguageSeedDto>(
|
||||
typeof(FilterListLanguage).GetProperty("FilterListId"),
|
||||
typeof(FilterListLanguage).GetProperty("LanguageId")));
|
||||
}
|
||||
}
|
||||
|
|
@ -16,12 +16,8 @@ public ListsMaintainersController(IMemoryCache memoryCache, SeedService seedServ
|
|||
|
||||
[HttpGet("seed")]
|
||||
public async Task<IActionResult> Seed() =>
|
||||
CoalesceNotFound(Json(await MemoryCache.GetOrCreate("ListsMaintainersController_Seed", entry =>
|
||||
{
|
||||
entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault;
|
||||
return SeedService.GetAllAsync<FilterListMaintainer, FilterListMaintainerSeedDto>(
|
||||
typeof(FilterListMaintainer).GetProperty("MaintainerId"),
|
||||
typeof(FilterListMaintainer).GetProperty("FilterListId"));
|
||||
})));
|
||||
await Get(() => SeedService.GetAllAsync<FilterListMaintainer, FilterListMaintainerSeedDto>(
|
||||
typeof(FilterListMaintainer).GetProperty("MaintainerId"),
|
||||
typeof(FilterListMaintainer).GetProperty("FilterListId")));
|
||||
}
|
||||
}
|
||||
|
|
@ -16,12 +16,8 @@ public ListsTagsController(IMemoryCache memoryCache, SeedService seedService)
|
|||
|
||||
[HttpGet("seed")]
|
||||
public async Task<IActionResult> Seed() =>
|
||||
CoalesceNotFound(Json(await MemoryCache.GetOrCreate("ListsTagsController_Seed", entry =>
|
||||
{
|
||||
entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault;
|
||||
return SeedService.GetAllAsync<FilterListTag, FilterListTagSeedDto>(
|
||||
typeof(FilterListTag).GetProperty("FilterListId"),
|
||||
typeof(FilterListTag).GetProperty("TagId"));
|
||||
})));
|
||||
await Get(() => SeedService.GetAllAsync<FilterListTag, FilterListTagSeedDto>(
|
||||
typeof(FilterListTag).GetProperty("FilterListId"),
|
||||
typeof(FilterListTag).GetProperty("TagId")));
|
||||
}
|
||||
}
|
||||
|
|
@ -15,10 +15,6 @@ public MaintainersController(IMemoryCache memoryCache, SeedService seedService)
|
|||
|
||||
[HttpGet("seed")]
|
||||
public async Task<IActionResult> Seed() =>
|
||||
CoalesceNotFound(Json(await MemoryCache.GetOrCreate("MaintainersController_Seed", entry =>
|
||||
{
|
||||
entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault;
|
||||
return SeedService.GetAllAsync<Maintainer, MaintainerSeedDto>();
|
||||
})));
|
||||
await Get(() => SeedService.GetAllAsync<Maintainer, MaintainerSeedDto>());
|
||||
}
|
||||
}
|
||||
|
|
@ -15,11 +15,8 @@ public MergesController(IMemoryCache memoryCache, SeedService seedService) : bas
|
|||
|
||||
[HttpGet("seed")]
|
||||
public async Task<IActionResult> Seed() =>
|
||||
CoalesceNotFound(Json(await MemoryCache.GetOrCreate("MergesController_Seed", entry =>
|
||||
{
|
||||
entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault;
|
||||
return SeedService.GetAllAsync<Merge, MergeSeedDto>(typeof(Merge).GetProperty("MergeFilterListId"),
|
||||
typeof(Merge).GetProperty("UpstreamFilterListId"));
|
||||
})));
|
||||
await Get(() => SeedService.GetAllAsync<Merge, MergeSeedDto>(
|
||||
typeof(Merge).GetProperty("MergeFilterListId"),
|
||||
typeof(Merge).GetProperty("UpstreamFilterListId")));
|
||||
}
|
||||
}
|
||||
|
|
@ -14,10 +14,6 @@ public RulesController(IMemoryCache memoryCache, RuleService ruleService) : base
|
|||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> Index() =>
|
||||
CoalesceNotFound(Json(await MemoryCache.GetOrCreate("RulesController_Index", entry =>
|
||||
{
|
||||
entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault;
|
||||
return ruleService.GetCountAll();
|
||||
})));
|
||||
await Get(() => ruleService.GetCountAll());
|
||||
}
|
||||
}
|
||||
|
|
@ -17,18 +17,10 @@ public SoftwareController(IMemoryCache memoryCache, SeedService seedService, Sof
|
|||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> Index() =>
|
||||
CoalesceNotFound(Json(await MemoryCache.GetOrCreate("SoftwareController_Index", entry =>
|
||||
{
|
||||
entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault;
|
||||
return softwareService.GetAll();
|
||||
})));
|
||||
await Get(() => softwareService.GetAll());
|
||||
|
||||
[HttpGet("seed")]
|
||||
public async Task<IActionResult> Seed() =>
|
||||
CoalesceNotFound(Json(await MemoryCache.GetOrCreate("SoftwareController_Seed", entry =>
|
||||
{
|
||||
entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault;
|
||||
return SeedService.GetAllAsync<Software, SoftwareSeedDto>();
|
||||
})));
|
||||
await Get(() => SeedService.GetAllAsync<Software, SoftwareSeedDto>());
|
||||
}
|
||||
}
|
||||
|
|
@ -16,11 +16,8 @@ public SoftwareSyntaxesController(IMemoryCache memoryCache, SeedService seedServ
|
|||
|
||||
[HttpGet("seed")]
|
||||
public async Task<IActionResult> Seed() =>
|
||||
CoalesceNotFound(Json(await MemoryCache.GetOrCreate("SoftwareSyntaxesController_Seed", entry =>
|
||||
{
|
||||
entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault;
|
||||
return SeedService.GetAllAsync<SoftwareSyntax, SoftwareSyntaxSeedDto>(
|
||||
typeof(SoftwareSyntax).GetProperty("SyntaxId"), typeof(SoftwareSyntax).GetProperty("SoftwareId"));
|
||||
})));
|
||||
await Get(() => SeedService.GetAllAsync<SoftwareSyntax, SoftwareSyntaxSeedDto>(
|
||||
typeof(SoftwareSyntax).GetProperty("SyntaxId"),
|
||||
typeof(SoftwareSyntax).GetProperty("SoftwareId")));
|
||||
}
|
||||
}
|
||||
|
|
@ -15,10 +15,6 @@ public SyntaxesController(IMemoryCache memoryCache, SeedService seedService) : b
|
|||
|
||||
[HttpGet("seed")]
|
||||
public async Task<IActionResult> Seed() =>
|
||||
CoalesceNotFound(Json(await MemoryCache.GetOrCreate("SyntaxesController_Seed", entry =>
|
||||
{
|
||||
entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault;
|
||||
return SeedService.GetAllAsync<Syntax, SyntaxSeedDto>();
|
||||
})));
|
||||
await Get(() => SeedService.GetAllAsync<Syntax, SyntaxSeedDto>());
|
||||
}
|
||||
}
|
||||
|
|
@ -17,18 +17,10 @@ public TagsController(IMemoryCache memoryCache, SeedService seedService, TagServ
|
|||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> Index() =>
|
||||
CoalesceNotFound(Json(await MemoryCache.GetOrCreate("TagsController_Index", entry =>
|
||||
{
|
||||
entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault;
|
||||
return tagService.GetAll();
|
||||
})));
|
||||
await Get(() => tagService.GetAll());
|
||||
|
||||
[HttpGet("seed")]
|
||||
public async Task<IActionResult> Seed() =>
|
||||
CoalesceNotFound(Json(await MemoryCache.GetOrCreate("TagsController_Seed", entry =>
|
||||
{
|
||||
entry.AbsoluteExpirationRelativeToNow = MemoryCacheExpirationDefault;
|
||||
return SeedService.GetAllAsync<Tag, TagSeedDto>();
|
||||
})));
|
||||
await Get(() => SeedService.GetAllAsync<Tag, TagSeedDto>());
|
||||
}
|
||||
}
|
||||
|
|
@ -22,7 +22,7 @@ await DbContext.FilterLists
|
|||
.ProjectTo<List>(MapConfig)
|
||||
.ToListAsync();
|
||||
|
||||
public async Task<ListDetails> GetDetailsAsync(uint id) =>
|
||||
public async Task<ListDetails> GetDetailsAsync(int id) =>
|
||||
await DbContext.FilterLists
|
||||
.ProjectTo<ListDetails>(MapConfig)
|
||||
.FirstOrDefaultAsync(x => x.Id == id)
|
||||
|
|
|
|||
Loading…
Reference in a new issue