mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
simplify SeedService with generics
per: https://stackoverflow.com/questions/48441884/refactor-shared-net-core-api-action-using-generics-and-automapper-to-reduce-dup
This commit is contained in:
parent
3326fe0edf
commit
45e470f831
2 changed files with 10 additions and 35 deletions
|
|
@ -1,5 +1,7 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using FilterLists.Data.Entities;
|
||||
using FilterLists.Services.Models.Seed;
|
||||
using FilterLists.Services.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
|
|
@ -30,37 +32,37 @@ private async Task<IActionResult> GetSeed(string controllerName)
|
|||
return await MemoryCache.GetOrCreateAsync(CacheKeys.Entry, async entry =>
|
||||
{
|
||||
entry.SlidingExpiration = TimeSpan.FromSeconds(3);
|
||||
return Json(await SeedService.GetAllFilterLists());
|
||||
return Json(await SeedService.GetAll<FilterList, FilterListSeedDto>());
|
||||
});
|
||||
case "languages":
|
||||
return await MemoryCache.GetOrCreateAsync(CacheKeys.Entry, async entry =>
|
||||
{
|
||||
entry.SlidingExpiration = TimeSpan.FromSeconds(3);
|
||||
return Json(await SeedService.GetAllLanguages());
|
||||
return Json(await SeedService.GetAll<Language, LanguageSeedDto>());
|
||||
});
|
||||
case "licenses":
|
||||
return await MemoryCache.GetOrCreateAsync(CacheKeys.Entry, async entry =>
|
||||
{
|
||||
entry.SlidingExpiration = TimeSpan.FromSeconds(3);
|
||||
return Json(await SeedService.GetAllLicenses());
|
||||
return Json(await SeedService.GetAll<License, LicenseSeedDto>());
|
||||
});
|
||||
case "maintainers":
|
||||
return await MemoryCache.GetOrCreateAsync(CacheKeys.Entry, async entry =>
|
||||
{
|
||||
entry.SlidingExpiration = TimeSpan.FromSeconds(3);
|
||||
return Json(await SeedService.GetAllMaintainers());
|
||||
return Json(await SeedService.GetAll<Maintainer, MaintainerSeedDto>());
|
||||
});
|
||||
case "software":
|
||||
return await MemoryCache.GetOrCreateAsync(CacheKeys.Entry, async entry =>
|
||||
{
|
||||
entry.SlidingExpiration = TimeSpan.FromSeconds(3);
|
||||
return Json(await SeedService.GetAllSoftware());
|
||||
return Json(await SeedService.GetAll<Software, SoftwareSeedDto>());
|
||||
});
|
||||
case "syntaxes":
|
||||
return await MemoryCache.GetOrCreateAsync(CacheKeys.Entry, async entry =>
|
||||
{
|
||||
entry.SlidingExpiration = TimeSpan.FromSeconds(3);
|
||||
return Json(await SeedService.GetAllSyntaxes());
|
||||
return Json(await SeedService.GetAll<Syntax, SyntaxSeedDto>());
|
||||
});
|
||||
default:
|
||||
return await Task.FromResult(NotFound());
|
||||
|
|
|
|||
|
|
@ -2,8 +2,6 @@
|
|||
using System.Threading.Tasks;
|
||||
using AutoMapper.QueryableExtensions;
|
||||
using FilterLists.Data;
|
||||
using FilterLists.Data.Entities;
|
||||
using FilterLists.Services.Models.Seed;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace FilterLists.Services.Services
|
||||
|
|
@ -17,34 +15,9 @@ public SeedService(FilterListsDbContext filterListsDbContext)
|
|||
this.filterListsDbContext = filterListsDbContext;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<FilterListSeedDto>> GetAllFilterLists()
|
||||
public async Task<IEnumerable<TMapped>> GetAll<TEntity, TMapped>() where TEntity : class
|
||||
{
|
||||
return await filterListsDbContext.Set<FilterList>().ProjectTo<FilterListSeedDto>().ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<LanguageSeedDto>> GetAllLanguages()
|
||||
{
|
||||
return await filterListsDbContext.Set<Language>().ProjectTo<LanguageSeedDto>().ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<LicenseSeedDto>> GetAllLicenses()
|
||||
{
|
||||
return await filterListsDbContext.Set<License>().ProjectTo<LicenseSeedDto>().ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<MaintainerSeedDto>> GetAllMaintainers()
|
||||
{
|
||||
return await filterListsDbContext.Set<Maintainer>().ProjectTo<MaintainerSeedDto>().ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<SoftwareSeedDto>> GetAllSoftware()
|
||||
{
|
||||
return await filterListsDbContext.Set<Software>().ProjectTo<SoftwareSeedDto>().ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<SyntaxSeedDto>> GetAllSyntaxes()
|
||||
{
|
||||
return await filterListsDbContext.Set<Syntax>().ProjectTo<SyntaxSeedDto>().ToListAsync();
|
||||
return await filterListsDbContext.Set<TEntity>().ProjectTo<TMapped>().ToListAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue