async all the things, add seed endpoints for entities

This commit is contained in:
Collin M. Barrett 2018-01-24 19:30:01 -06:00
parent fd81aacd37
commit dfe98c893f
16 changed files with 204 additions and 17 deletions

View file

@ -1,5 +1,5 @@
using System;
using FilterLists.Services.Models;
using System.Threading.Tasks;
using FilterLists.Services.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
@ -16,15 +16,55 @@ protected EntityController(IMemoryCache memoryCache, SeedService seedService) :
}
[HttpGet]
public IActionResult Seed()
public async Task<IActionResult> Seed()
{
var controllerName = ControllerContext.RouteData.Values["controller"].ToString();
return MemoryCache.GetOrCreate(CacheKeys.Entry, entry =>
return await GetSeed(controllerName);
}
private async Task<IActionResult> GetSeed(string controllerName)
{
switch (controllerName)
{
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(86400);
//TODO: GetAll<T>() by seed type of request controller
return Json(SeedService.GetAll<FilterListSeedDto>());
});
case "lists":
return await MemoryCache.GetOrCreateAsync(CacheKeys.Entry, entry =>
{
entry.SlidingExpiration = TimeSpan.FromSeconds(3);
return Task.FromResult(Json(SeedService.GetAllFilterLists()));
});
case "languages":
return await MemoryCache.GetOrCreateAsync(CacheKeys.Entry, entry =>
{
entry.SlidingExpiration = TimeSpan.FromSeconds(3);
return Task.FromResult(Json(SeedService.GetAllLanguages()));
});
case "licenses":
return await MemoryCache.GetOrCreateAsync(CacheKeys.Entry, entry =>
{
entry.SlidingExpiration = TimeSpan.FromSeconds(3);
return Task.FromResult(Json(SeedService.GetAllLicenses()));
});
case "maintainers":
return await MemoryCache.GetOrCreateAsync(CacheKeys.Entry, entry =>
{
entry.SlidingExpiration = TimeSpan.FromSeconds(3);
return Task.FromResult(Json(SeedService.GetAllMaintainers()));
});
case "software":
return await MemoryCache.GetOrCreateAsync(CacheKeys.Entry, entry =>
{
entry.SlidingExpiration = TimeSpan.FromSeconds(3);
return Task.FromResult(Json(SeedService.GetAllSoftware()));
});
case "syntaxes":
return await MemoryCache.GetOrCreateAsync(CacheKeys.Entry, entry =>
{
entry.SlidingExpiration = TimeSpan.FromSeconds(3);
return Task.FromResult(Json(SeedService.GetAllSyntaxes()));
});
default:
return await Task.FromResult(NotFound());
}
}
}
}

View file

@ -0,0 +1,12 @@
using FilterLists.Services.Services;
using Microsoft.Extensions.Caching.Memory;
namespace FilterLists.Api.V1.Controllers
{
public class LanguagesController : EntityController
{
public LanguagesController(IMemoryCache memoryCache, SeedService seedService) : base(memoryCache, seedService)
{
}
}
}

View file

@ -0,0 +1,12 @@
using FilterLists.Services.Services;
using Microsoft.Extensions.Caching.Memory;
namespace FilterLists.Api.V1.Controllers
{
public class LicensesController : EntityController
{
public LicensesController(IMemoryCache memoryCache, SeedService seedService) : base(memoryCache, seedService)
{
}
}
}

View file

@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using FilterLists.Services.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
@ -16,12 +17,12 @@ public ListsController(IMemoryCache memoryCache, SeedService seedService, Filter
}
[HttpGet]
public IActionResult Index()
public async Task<IActionResult> Index()
{
return MemoryCache.GetOrCreate(CacheKeys.Entry, entry =>
return await MemoryCache.GetOrCreateAsync(CacheKeys.Entry, entry =>
{
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(86400);
return Json(filterListService.GetAllSummaries());
entry.SlidingExpiration = TimeSpan.FromSeconds(3);
return Task.FromResult(Json(filterListService.GetAllSummaries()));
});
}
}

View file

@ -0,0 +1,12 @@
using FilterLists.Services.Services;
using Microsoft.Extensions.Caching.Memory;
namespace FilterLists.Api.V1.Controllers
{
public class MaintainersController : EntityController
{
public MaintainersController(IMemoryCache memoryCache, SeedService seedService) : base(memoryCache, seedService)
{
}
}
}

View file

@ -0,0 +1,12 @@
using FilterLists.Services.Services;
using Microsoft.Extensions.Caching.Memory;
namespace FilterLists.Api.V1.Controllers
{
public class SoftwareController : EntityController
{
public SoftwareController(IMemoryCache memoryCache, SeedService seedService) : base(memoryCache, seedService)
{
}
}
}

View file

@ -0,0 +1,12 @@
using FilterLists.Services.Services;
using Microsoft.Extensions.Caching.Memory;
namespace FilterLists.Api.V1.Controllers
{
public class SyntaxesController : EntityController
{
public SyntaxesController(IMemoryCache memoryCache, SeedService seedService) : base(memoryCache, seedService)
{
}
}
}

View file

@ -1,6 +1,7 @@
using AutoMapper.Configuration;
using FilterLists.Data.Entities;
using FilterLists.Services.Models;
using FilterLists.Services.Models.Seed;
namespace FilterLists.Services
{
@ -9,7 +10,17 @@ public class MappingProfile : MapperConfigurationExpression
public MappingProfile()
{
CreateMap<FilterList, FilterListSummaryDto>();
MapSeedDtos();
}
private void MapSeedDtos()
{
CreateMap<FilterList, FilterListSeedDto>();
CreateMap<Language, LanguageSeedDto>();
CreateMap<License, LicenseSeedDto>();
CreateMap<Maintainer, MaintainerSeedDto>();
CreateMap<Software, SoftwareSeedDto>();
CreateMap<Syntax, SyntaxSeedDto>();
}
}
}

View file

@ -1,6 +1,6 @@
using System;
namespace FilterLists.Services.Models
namespace FilterLists.Services.Models.Seed
{
public class FilterListSeedDto
{

View file

@ -1,4 +1,4 @@
namespace FilterLists.Services.Models
namespace FilterLists.Services.Models.Seed
{
public class LanguageSeedDto
{

View file

@ -0,0 +1,11 @@
namespace FilterLists.Services.Models.Seed
{
public class LicenseSeedDto
{
public int Id { get; set; }
public string DescriptionUrl { get; set; }
public string Name { get; set; }
public bool PermissiveAdaptation { get; set; }
public bool PermissiveCommercial { get; set; }
}
}

View file

@ -0,0 +1,11 @@
namespace FilterLists.Services.Models.Seed
{
public class MaintainerSeedDto
{
public int Id { get; set; }
public string EmailAddress { get; set; }
public string HomeUrl { get; set; }
public string Name { get; set; }
public string TwitterHandle { get; set; }
}
}

View file

@ -0,0 +1,10 @@
namespace FilterLists.Services.Models.Seed
{
public class SoftwareSeedDto
{
public int Id { get; set; }
public string DownloadUrl { get; set; }
public string HomeUrl { get; set; }
public string Name { get; set; }
}
}

View file

@ -0,0 +1,9 @@
namespace FilterLists.Services.Models.Seed
{
public class SyntaxSeedDto
{
public int Id { get; set; }
public string DefinitionUrl { get; set; }
public string Name { get; set; }
}
}

View file

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using AutoMapper;
using FilterLists.Data;
using FilterLists.Services.Models;
@ -16,9 +17,10 @@ public FilterListService(FilterListsDbContext filterListsDbContext, IMapper mapp
this.mapper = mapper;
}
public IEnumerable<FilterListSummaryDto> GetAllSummaries()
public async Task<IEnumerable<FilterListSummaryDto>> GetAllSummaries()
{
return mapper.Map<IEnumerable<FilterListSummaryDto>>(filterListsDbContext.FilterLists);
return await Task.FromResult(
mapper.Map<IEnumerable<FilterListSummaryDto>>(filterListsDbContext.FilterLists));
}
}
}

View file

@ -1,6 +1,9 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using AutoMapper;
using FilterLists.Data;
using FilterLists.Data.Entities;
using FilterLists.Services.Models.Seed;
namespace FilterLists.Services.Services
{
@ -15,9 +18,38 @@ public SeedService(FilterListsDbContext filterListsDbContext, IMapper mapper)
this.mapper = mapper;
}
public IEnumerable<T> GetAll<T>() where T : class
public async Task<IEnumerable<FilterListSeedDto>> GetAllFilterLists()
{
return mapper.Map<IEnumerable<T>>(filterListsDbContext.Set<T>());
return await Task.FromResult(
mapper.Map<IEnumerable<FilterListSeedDto>>(filterListsDbContext.Set<FilterList>()));
}
public async Task<IEnumerable<LanguageSeedDto>> GetAllLanguages()
{
return await Task.FromResult(
mapper.Map<IEnumerable<LanguageSeedDto>>(filterListsDbContext.Set<Language>()));
}
public async Task<IEnumerable<LicenseSeedDto>> GetAllLicenses()
{
return await Task.FromResult(mapper.Map<IEnumerable<LicenseSeedDto>>(filterListsDbContext.Set<License>()));
}
public async Task<IEnumerable<MaintainerSeedDto>> GetAllMaintainers()
{
return await Task.FromResult(
mapper.Map<IEnumerable<MaintainerSeedDto>>(filterListsDbContext.Set<Maintainer>()));
}
public async Task<IEnumerable<SoftwareSeedDto>> GetAllSoftware()
{
return await Task.FromResult(
mapper.Map<IEnumerable<SoftwareSeedDto>>(filterListsDbContext.Set<Software>()));
}
public async Task<IEnumerable<SyntaxSeedDto>> GetAllSyntaxes()
{
return await Task.FromResult(mapper.Map<IEnumerable<SyntaxSeedDto>>(filterListsDbContext.Set<Syntax>()));
}
}
}