lots of changes, successfully talking to db now

This commit is contained in:
Collin M. Barrett 2017-04-08 16:25:48 -05:00
parent dc4d02a1ac
commit a1e65eb93d
14 changed files with 115 additions and 117 deletions

View file

@ -1,4 +1,5 @@
using System.Collections.Generic;
using FilterLists.Models;
using FilterLists.Services;
using Microsoft.AspNetCore.Mvc;
@ -16,10 +17,10 @@ public ListsController(IListService listService)
// GET api/lists
[HttpGet]
public IEnumerable<string> Get()
public IEnumerable<List> Get()
{
var lists = _listService.Lists;
return (IEnumerable<string>) lists;
var result = _listService.GetAll();
return result;
}
// GET api/lists/5

View file

@ -0,0 +1,13 @@
using Microsoft.Extensions.DependencyInjection;
namespace FilterLists.Api.DependencyInjection.Extensions
{
public static class ConfigureServicesCollection
{
public static IServiceCollection RegisterFilterListsApi(this IServiceCollection services)
{
services.AddMvc();
return services;
}
}
}

View file

@ -21,5 +21,10 @@
<ProjectReference Include="..\FilterLists.Data\FilterLists.Data.csproj" />
<ProjectReference Include="..\FilterLists.Services\FilterLists.Services.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.Configuration">
<HintPath>..\..\..\..\..\..\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.2\System.Configuration.dll</HintPath>
</Reference>
</ItemGroup>
</Project>

View file

@ -1,13 +1,11 @@
using FilterLists.Data.Contexts;
using FilterLists.Services;
using FilterLists.Api.DependencyInjection.Extensions;
using FilterLists.Data.DependencyInjection.Extensions;
using FilterLists.Services.DependencyInjection.Extensions;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using MySQL.Data.EntityFrameworkCore;
using MySQL.Data.EntityFrameworkCore.Extensions;
namespace FilterLists.Api
{
@ -25,24 +23,17 @@ public Startup(IHostingEnvironment env)
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddEntityFramework()
.AddEntityFrameworkMySQL()
.AddDbContext<FilterListsDbContext>(options =>
options.UseMySQL(Configuration.GetConnectionString("FilterListsConnection")));
services.AddMvc();
services.AddTransient<IListService, ListService>();
services.RegisterFilterListsRepositories(Configuration);
services.RegisterFilterListsServices();
services.RegisterFilterListsApi();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseMvc();
}
}

View file

@ -1,6 +1,10 @@
namespace FilterLists.Data.Contracts.Repositories
using System.Collections.Generic;
using FilterLists.Models;
namespace FilterLists.Data.Contracts.Repositories
{
public interface IListRepository
{
IEnumerable<List> GetAll();
}
}

View file

@ -11,10 +11,6 @@ public FilterListsDbContext(DbContextOptions options)
{
}
public FilterListsDbContext()
{
}
public DbSet<List> Lists { get; set; }
public DbSet<List> List { get; set; }
}
}

View file

@ -0,0 +1,27 @@
using FilterLists.Data.Contexts;
using FilterLists.Data.Contracts.Repositories;
using FilterLists.Data.Repositories;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using MySQL.Data.EntityFrameworkCore;
using MySQL.Data.EntityFrameworkCore.Extensions;
namespace FilterLists.Data.DependencyInjection.Extensions
{
public static class ConfigureServicesCollection
{
public static IServiceCollection RegisterFilterListsRepositories(this IServiceCollection services,
IConfiguration configuration)
{
services.AddSingleton(c => configuration);
services.AddEntityFramework()
.AddEntityFrameworkMySQL()
.AddDbContext<FilterListsDbContext>(options =>
options.UseMySQL(configuration.GetConnectionString("FilterListsConnection")));
services.TryAddScoped<IListRepository, ListRepository>();
return services;
}
}
}

View file

@ -1,16 +0,0 @@
//https://code.msdn.microsoft.com/Generic-Repository-Pattern-f133bca4/sourcecode?fileId=164016&pathId=804969589
using System.Collections.Generic;
using FilterLists.Models;
namespace FilterLists.Data.Repositories
{
public interface IRepository<T> where T : BaseEntity
{
IEnumerable<T> GetAll();
T Get(long id);
void Insert(T entity);
void Update(T entity);
void Delete(T entity);
}
}

View file

@ -1,13 +1,28 @@
using FilterLists.Data.Contexts;
using System.Collections.Generic;
using System.Linq;
using FilterLists.Data.Contexts;
using FilterLists.Data.Contracts.Repositories;
using FilterLists.Models;
using Microsoft.EntityFrameworkCore;
namespace FilterLists.Data.Repositories
{
public class ListRepository : Repository<List>, IListRepository
public class ListRepository : IListRepository
{
public ListRepository(FilterListsDbContext context) : base(context)
private readonly DbContextOptions _options;
public ListRepository(DbContextOptions options)
{
_options = options;
}
public IEnumerable<List> GetAll()
{
using (var context = new FilterListsDbContext(_options))
{
context.Database.OpenConnection();
return context.List.AsEnumerable();
}
}
}
}

View file

@ -1,56 +0,0 @@
//https://code.msdn.microsoft.com/Generic-Repository-Pattern-f133bca4/sourcecode?fileId=164016&pathId=902500354
using System;
using System.Collections.Generic;
using System.Linq;
using FilterLists.Data.Contexts;
using FilterLists.Models;
using Microsoft.EntityFrameworkCore;
namespace FilterLists.Data.Repositories
{
public class Repository<T> : IRepository<T> where T : BaseEntity
{
private readonly FilterListsDbContext _context;
private readonly DbSet<T> _entities;
public Repository(FilterListsDbContext context)
{
_context = context;
_entities = context.Set<T>();
}
public IEnumerable<T> GetAll()
{
return _entities.AsEnumerable();
}
public T Get(long id)
{
return _entities.SingleOrDefault(s => s.Id == id);
}
public void Insert(T entity)
{
if (entity == null)
throw new ArgumentNullException(nameof(entity));
_entities.Add(entity);
_context.SaveChanges();
}
public void Update(T entity)
{
if (entity == null)
throw new ArgumentNullException(nameof(entity));
_context.SaveChanges();
}
public void Delete(T entity)
{
if (entity == null)
throw new ArgumentNullException(nameof(entity));
_entities.Remove(entity);
_context.SaveChanges();
}
}
}

View file

@ -1,19 +1,20 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace FilterLists.Models
{
public class List : BaseEntity
{
[StringLength(1024)] [Column("description")] public string Description;
[StringLength(512)] [Column("description_source_url")] public string DescriptionSourceUrl;
[StringLength(512)] [Column("donate_url")] public string DonateUrl;
[StringLength(128)] [Column("email")] public string Email;
[StringLength(512)] [Column("forum_url")] public string ForumUrl;
[StringLength(512)] [Column("home_url")] public string HomeUrl;
[Key] [Column("id")] public new int Id;
[StringLength(512)] [Column("issues_url")] public string IssuesUrl;
[StringLength(256)] [Column("name")] public string Name;
[StringLength(512)] [Column("view_url")] public string ViewUrl;
[Key]
public new int Id { get; set; }
public string Description { get; set; }
public string DescriptionSourceUrl { get; set; }
public string DonateUrl { get; set; }
public string Email { get; set; }
public string ForumUrl { get; set; }
public string HomeUrl { get; set; }
public string IssuesUrl { get; set; }
public string Name { get; set; }
public string ViewUrl { get; set; }
}
}

View file

@ -0,0 +1,14 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
namespace FilterLists.Services.DependencyInjection.Extensions
{
public static class ConfigureServicesCollection
{
public static IServiceCollection RegisterFilterListsServices(this IServiceCollection services)
{
services.TryAddScoped<IListService, ListService>();
return services;
}
}
}

View file

@ -1,10 +1,10 @@
using System.Linq;
using System.Collections.Generic;
using FilterLists.Models;
namespace FilterLists.Services
{
public interface IListService
{
IQueryable<List> Lists { get; }
IEnumerable<List> GetAll();
}
}

View file

@ -1,18 +1,21 @@
using System.Linq;
using FilterLists.Data.Repositories;
using System.Collections.Generic;
using FilterLists.Data.Contracts.Repositories;
using FilterLists.Models;
namespace FilterLists.Services
{
public class ListService : IListService
{
private readonly IRepository<List> _listRepository;
private readonly IListRepository _listRepository;
public ListService(IRepository<List> listRepository)
public ListService(IListRepository listRepository)
{
_listRepository = listRepository;
}
public IQueryable<List> Lists => (IQueryable<List>) _listRepository.GetAll();
public IEnumerable<List> GetAll()
{
return _listRepository.GetAll();
}
}
}