mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
work towards seed API action for all entities
This commit is contained in:
parent
deff680952
commit
8bba881c84
6 changed files with 80 additions and 25 deletions
35
src/FilterLists.Api/V1/Controllers/BaseController.cs
Normal file
35
src/FilterLists.Api/V1/Controllers/BaseController.cs
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
using System;
|
||||
using FilterLists.Services.Models;
|
||||
using FilterLists.Services.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
|
||||
namespace FilterLists.Api.V1.Controllers
|
||||
{
|
||||
[ApiVersion("1.0")]
|
||||
[Produces("application/json")]
|
||||
[ResponseCache(CacheProfileName = "Long-Lived")]
|
||||
public class BaseController : Controller
|
||||
{
|
||||
protected readonly IMemoryCache MemoryCache;
|
||||
protected readonly SeedService SeedService;
|
||||
|
||||
public BaseController(IMemoryCache memoryCache, SeedService seedService)
|
||||
{
|
||||
MemoryCache = memoryCache;
|
||||
SeedService = seedService;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult Seed()
|
||||
{
|
||||
var controllerName = ControllerContext.RouteData.Values["controller"].ToString();
|
||||
return MemoryCache.GetOrCreate(CacheKeys.Entry, entry =>
|
||||
{
|
||||
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(86400);
|
||||
//TODO: GetAll<T>() by seed type of request controller
|
||||
return Json(SeedService.GetAll<FilterListSeedDto>());
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,42 +1,28 @@
|
|||
using System;
|
||||
using FilterLists.Services;
|
||||
using FilterLists.Services.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
|
||||
namespace FilterLists.Api.V1.Controllers
|
||||
{
|
||||
[ApiVersion("1.0")]
|
||||
[Produces("application/json")]
|
||||
[ResponseCache(CacheProfileName = "Long-Lived")]
|
||||
public class ListsController : Controller
|
||||
public class ListsController : BaseController
|
||||
{
|
||||
private readonly FilterListService filterListService;
|
||||
private readonly IMemoryCache memoryCache;
|
||||
|
||||
public ListsController(IMemoryCache memoryCache, FilterListService filterListService)
|
||||
public ListsController(IMemoryCache memoryCache, SeedService seedService, FilterListService filterListService) :
|
||||
base(memoryCache, seedService)
|
||||
{
|
||||
this.memoryCache = memoryCache;
|
||||
this.filterListService = filterListService;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult Index()
|
||||
{
|
||||
return memoryCache.GetOrCreate(CacheKeys.Entry, entry =>
|
||||
return MemoryCache.GetOrCreate(CacheKeys.Entry, entry =>
|
||||
{
|
||||
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(86400);
|
||||
return Json(filterListService.GetAllSummaries());
|
||||
});
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult Seed()
|
||||
{
|
||||
return memoryCache.GetOrCreate(CacheKeys.Entry, entry =>
|
||||
{
|
||||
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(86400);
|
||||
return Json(filterListService.GetAllSeeds());
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
using AutoMapper;
|
||||
using FilterLists.Data;
|
||||
using FilterLists.Services.Services;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
|
@ -15,6 +16,7 @@ public static void AddFilterListsServices(this IServiceCollection services, ICon
|
|||
services.AddEntityFrameworkMySql().AddDbContextPool<FilterListsDbContext>(options =>
|
||||
options.UseMySql(configuration.GetConnectionString("FilterListsConnection"),
|
||||
b => b.MigrationsAssembly("FilterLists.Api")));
|
||||
services.TryAddScoped<SeedService>();
|
||||
services.TryAddScoped<FilterListService>();
|
||||
services.AddAutoMapper();
|
||||
}
|
||||
|
|
|
|||
14
src/FilterLists.Services/Models/LanguageSeedDto.cs
Normal file
14
src/FilterLists.Services/Models/LanguageSeedDto.cs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
namespace FilterLists.Services.Models
|
||||
{
|
||||
public class LanguageSeedDto
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Iso6391 { get; set; }
|
||||
public string Iso6392 { get; set; }
|
||||
public string Iso6392B { get; set; }
|
||||
public string Iso6392T { get; set; }
|
||||
public string Iso6393 { get; set; }
|
||||
public string LocalName { get; set; }
|
||||
public string Name { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -3,7 +3,7 @@
|
|||
using FilterLists.Data;
|
||||
using FilterLists.Services.Models;
|
||||
|
||||
namespace FilterLists.Services
|
||||
namespace FilterLists.Services.Services
|
||||
{
|
||||
public class FilterListService
|
||||
{
|
||||
|
|
@ -20,10 +20,5 @@ public IEnumerable<FilterListSummaryDto> GetAllSummaries()
|
|||
{
|
||||
return mapper.Map<IEnumerable<FilterListSummaryDto>>(filterListsDbContext.FilterLists);
|
||||
}
|
||||
|
||||
public IEnumerable<FilterListSeedDto> GetAllSeeds()
|
||||
{
|
||||
return mapper.Map<IEnumerable<FilterListSeedDto>>(filterListsDbContext.FilterLists);
|
||||
}
|
||||
}
|
||||
}
|
||||
23
src/FilterLists.Services/Services/SeedService.cs
Normal file
23
src/FilterLists.Services/Services/SeedService.cs
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
using System.Collections.Generic;
|
||||
using AutoMapper;
|
||||
using FilterLists.Data;
|
||||
|
||||
namespace FilterLists.Services.Services
|
||||
{
|
||||
public class SeedService
|
||||
{
|
||||
private readonly FilterListsDbContext filterListsDbContext;
|
||||
private readonly IMapper mapper;
|
||||
|
||||
public SeedService(FilterListsDbContext filterListsDbContext, IMapper mapper)
|
||||
{
|
||||
this.filterListsDbContext = filterListsDbContext;
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
||||
public IEnumerable<T> GetAll<T>() where T : class
|
||||
{
|
||||
return mapper.Map<IEnumerable<T>>(filterListsDbContext.Set<T>());
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue