From b6c199da03ba73f82a6924bbfbc2b8a2b1b70e51 Mon Sep 17 00:00:00 2001 From: "Collin M. Barrett" Date: Mon, 3 Sep 2018 15:58:00 -0500 Subject: [PATCH] add software endpoint ref #268 --- .../V1/Controllers/SoftwareController.cs | 16 ++++++++++--- .../Extensions/ConfigureServicesCollection.cs | 2 ++ .../Software/Models/SoftwareDto.cs | 11 +++++++++ .../Software/SoftwareService.cs | 23 +++++++++++++++++++ 4 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 src/FilterLists.Services/Software/Models/SoftwareDto.cs create mode 100644 src/FilterLists.Services/Software/SoftwareService.cs diff --git a/src/FilterLists.Api/V1/Controllers/SoftwareController.cs b/src/FilterLists.Api/V1/Controllers/SoftwareController.cs index 0940a56d3..dc231eb57 100644 --- a/src/FilterLists.Api/V1/Controllers/SoftwareController.cs +++ b/src/FilterLists.Api/V1/Controllers/SoftwareController.cs @@ -2,6 +2,7 @@ using FilterLists.Data.Entities; using FilterLists.Services.Seed; using FilterLists.Services.Seed.Models; +using FilterLists.Services.Software; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Caching.Memory; @@ -9,9 +10,18 @@ namespace FilterLists.Api.V1.Controllers { public class SoftwareController : BaseController { - public SoftwareController(IMemoryCache memoryCache, SeedService seedService) : base(memoryCache, seedService) - { - } + private readonly SoftwareService softwareService; + + public SoftwareController(IMemoryCache memoryCache, SeedService seedService, SoftwareService softwareService) : + base(memoryCache, seedService) => this.softwareService = softwareService; + + [HttpGet] + public async Task Index() => + Json(await MemoryCache.GetOrCreate("SoftwareController_Index", entry => + { + entry.AbsoluteExpirationRelativeToNow = FourHoursFromNow; + return softwareService.GetAll(); + })); [HttpGet("seed")] public async Task Seed() => diff --git a/src/FilterLists.Services/DependencyInjection/Extensions/ConfigureServicesCollection.cs b/src/FilterLists.Services/DependencyInjection/Extensions/ConfigureServicesCollection.cs index e42244baf..5968c6b52 100644 --- a/src/FilterLists.Services/DependencyInjection/Extensions/ConfigureServicesCollection.cs +++ b/src/FilterLists.Services/DependencyInjection/Extensions/ConfigureServicesCollection.cs @@ -3,6 +3,7 @@ using FilterLists.Services.FilterList; using FilterLists.Services.Seed; using FilterLists.Services.Snapshot; +using FilterLists.Services.Software; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -21,6 +22,7 @@ public static void AddFilterListsApiServices(this IServiceCollection services, I m => m.MigrationsAssembly("FilterLists.Api"))); services.TryAddScoped(); services.TryAddScoped(); + services.TryAddScoped(); services.TryAddScoped(); services.AddAutoMapper(); } diff --git a/src/FilterLists.Services/Software/Models/SoftwareDto.cs b/src/FilterLists.Services/Software/Models/SoftwareDto.cs new file mode 100644 index 000000000..fe3247dec --- /dev/null +++ b/src/FilterLists.Services/Software/Models/SoftwareDto.cs @@ -0,0 +1,11 @@ +using JetBrains.Annotations; + +namespace FilterLists.Services.Software.Models +{ + [UsedImplicitly] + public class SoftwareDto + { + public uint Id { get; set; } + public string Name { get; set; } + } +} \ No newline at end of file diff --git a/src/FilterLists.Services/Software/SoftwareService.cs b/src/FilterLists.Services/Software/SoftwareService.cs new file mode 100644 index 000000000..548012d54 --- /dev/null +++ b/src/FilterLists.Services/Software/SoftwareService.cs @@ -0,0 +1,23 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using AutoMapper; +using AutoMapper.QueryableExtensions; +using FilterLists.Data; +using FilterLists.Services.Software.Models; +using JetBrains.Annotations; +using Microsoft.EntityFrameworkCore; + +namespace FilterLists.Services.Software +{ + [UsedImplicitly] + public class SoftwareService : Service + { + public SoftwareService(FilterListsDbContext dbContext, IConfigurationProvider mapConfig) + : base(dbContext, mapConfig) + { + } + + public async Task> GetAll() => + await DbContext.Software.ProjectTo(MapConfig).ToListAsync(); + } +} \ No newline at end of file