Restructured query repositries to move ef logic from handlers
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
using System.Threading.Tasks;
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using BrightGlimmer.Service.Commands;
|
using BrightGlimmer.Service.Commands;
|
||||||
using BrightGlimmer.Services.Queries;
|
using BrightGlimmer.Service.Queries;
|
||||||
using MediatR;
|
using MediatR;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
@@ -18,12 +19,20 @@ namespace BrightGlimmer.Api.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<ActionResult> GetAll()
|
public async Task<ActionResult> Get()
|
||||||
{
|
{
|
||||||
var students = await mediator.Send(new GetAllStudentsQuery());
|
var students = await mediator.Send(new GetStudentsQuery());
|
||||||
return new JsonResult(students);
|
return new JsonResult(students);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpGet]
|
||||||
|
[Route("{id}")]
|
||||||
|
public async Task<ActionResult> Get(Guid id)
|
||||||
|
{
|
||||||
|
var student = await mediator.Send(new GetStudentQuery(id));
|
||||||
|
return new JsonResult(student);
|
||||||
|
}
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task<ActionResult> Create([FromBody]CreateStudentCommand command)
|
public async Task<ActionResult> Create([FromBody]CreateStudentCommand command)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using BrightGlimmer.Data;
|
using BrightGlimmer.Data;
|
||||||
using BrightGlimmer.Data.Interfaces;
|
using BrightGlimmer.Data.Interfaces;
|
||||||
using BrightGlimmer.Data.Repositories;
|
using BrightGlimmer.Data.Repositories;
|
||||||
|
using BrightGlimmer.Domain;
|
||||||
using MediatR;
|
using MediatR;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
@@ -31,6 +32,8 @@ namespace BrightGlimmer.Api
|
|||||||
services.AddTransient(typeof(IQueryRepository<>), typeof(QueryRepository<>));
|
services.AddTransient(typeof(IQueryRepository<>), typeof(QueryRepository<>));
|
||||||
services.AddTransient(typeof(ICommandRepository<>), typeof(CommandRepository<>));
|
services.AddTransient(typeof(ICommandRepository<>), typeof(CommandRepository<>));
|
||||||
|
|
||||||
|
services.AddTransient(typeof(IQueryRepository<Student>), typeof(StudentQueryRepository));
|
||||||
|
|
||||||
services.AddDbContext<BgContext>(options => options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));
|
services.AddDbContext<BgContext>(options => options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));
|
||||||
services.AddTransient<BgContext, BgContext>();
|
services.AddTransient<BgContext, BgContext>();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace BrightGlimmer.Data.Interfaces
|
namespace BrightGlimmer.Data.Interfaces
|
||||||
{
|
{
|
||||||
@@ -12,5 +13,7 @@ namespace BrightGlimmer.Data.Interfaces
|
|||||||
IQueryable<T> Get();
|
IQueryable<T> Get();
|
||||||
|
|
||||||
T Get(Guid id);
|
T Get(Guid id);
|
||||||
|
|
||||||
|
Task<T> GetAsync(Guid id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
using BrightGlimmer.Data.Interfaces;
|
using BrightGlimmer.Data.Interfaces;
|
||||||
using BrightGlimmer.Domain;
|
using BrightGlimmer.Domain;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace BrightGlimmer.Data.Repositories
|
namespace BrightGlimmer.Data.Repositories
|
||||||
{
|
{
|
||||||
@@ -27,5 +29,10 @@ namespace BrightGlimmer.Data.Repositories
|
|||||||
{
|
{
|
||||||
return context.Set<T>().Find(id);
|
return context.Set<T>().Find(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public virtual async Task<T> GetAsync(Guid id)
|
||||||
|
{
|
||||||
|
return await context.Set<T>().SingleAsync(x => x.Id == id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,43 @@
|
|||||||
using BrightGlimmer.Data.Interfaces;
|
using BrightGlimmer.Data.Interfaces;
|
||||||
using BrightGlimmer.Domain;
|
using BrightGlimmer.Domain;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace BrightGlimmer.Data.Repositories
|
namespace BrightGlimmer.Data.Repositories
|
||||||
{
|
{
|
||||||
public class StudentQueryRepository : QueryRepository<Student>
|
public class StudentQueryRepository : QueryRepository<Student>
|
||||||
{
|
{
|
||||||
public StudentQueryRepository(BgContext context) : base(context) { }
|
public StudentQueryRepository(BgContext context) : base(context) { }
|
||||||
|
|
||||||
|
public override IQueryable<Student> Get()
|
||||||
|
{
|
||||||
|
return context.Students
|
||||||
|
.Include(x => x.Phones)
|
||||||
|
.Include(x => x.Address)
|
||||||
|
.Include(x => x.AssignedCourses);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override Student Get(Guid id)
|
||||||
|
{
|
||||||
|
return context.Students
|
||||||
|
.Include(x => x.Phones)
|
||||||
|
.Include(x => x.Address)
|
||||||
|
.Include(x => x.AssignedCourses)
|
||||||
|
.Single(x => x.Id == id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async Task<Student> GetAsync(Guid id)
|
||||||
|
{
|
||||||
|
return await context.Students
|
||||||
|
.Include(x => x.Phones)
|
||||||
|
.Include(x => x.Address)
|
||||||
|
.Include(x => x.AssignedCourses)
|
||||||
|
.SingleAsync(x => x.Id == id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,5 +36,30 @@ namespace BrightGlimmer.Domain
|
|||||||
County = county;
|
County = county;
|
||||||
ZipCode = zipCode;
|
ZipCode = zipCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Address(string streetAddress1,
|
||||||
|
string streetAddress2,
|
||||||
|
string city,
|
||||||
|
string stateCode,
|
||||||
|
string county,
|
||||||
|
string zipCode,
|
||||||
|
decimal latitude,
|
||||||
|
decimal longitude)
|
||||||
|
{
|
||||||
|
StreetAddress1 = streetAddress1;
|
||||||
|
StreetAddress2 = streetAddress2;
|
||||||
|
City = city;
|
||||||
|
StateCode = stateCode;
|
||||||
|
County = county;
|
||||||
|
ZipCode = zipCode;
|
||||||
|
Latitude = latitude;
|
||||||
|
Longitude = longitude;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetLatitudeAndLongitude(decimal latitude, decimal longitude)
|
||||||
|
{
|
||||||
|
Latitude = latitude;
|
||||||
|
Longitude = longitude;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,15 +8,21 @@ namespace BrightGlimmer.Domain
|
|||||||
[Table("AssignedCourses")]
|
[Table("AssignedCourses")]
|
||||||
public class AssignedCourse : Entity
|
public class AssignedCourse : Entity
|
||||||
{
|
{
|
||||||
public decimal Grade { get; set; }
|
public decimal? Grade { get; set; }
|
||||||
public bool IsActive { get; set; }
|
public bool IsActive { get; set; }
|
||||||
public string Term { get; set; }
|
public string Term { get; set; }
|
||||||
|
|
||||||
public Student Student { get; set; }
|
public Student Student { get; private set; }
|
||||||
public Course Course { get; set; }
|
public Course Course { get; private set; }
|
||||||
|
|
||||||
private AssignedCourse() { }
|
private AssignedCourse() { }
|
||||||
|
|
||||||
|
public AssignedCourse(bool isActive, string term)
|
||||||
|
{
|
||||||
|
IsActive = isActive;
|
||||||
|
Term = term;
|
||||||
|
}
|
||||||
|
|
||||||
public AssignedCourse(decimal grade, bool isActive, string term)
|
public AssignedCourse(decimal grade, bool isActive, string term)
|
||||||
{
|
{
|
||||||
Grade = grade;
|
Grade = grade;
|
||||||
|
|||||||
@@ -8,9 +8,9 @@ namespace BrightGlimmer.Domain
|
|||||||
[Table("Phones")]
|
[Table("Phones")]
|
||||||
public class Phone : Entity
|
public class Phone : Entity
|
||||||
{
|
{
|
||||||
public PhoneType Type { get; private set; }
|
public PhoneType Type { get; set; }
|
||||||
public int AreaCode { get; private set; }
|
public int AreaCode { get; set; }
|
||||||
public int Number { get; private set; }
|
public int Number { get; set; }
|
||||||
|
|
||||||
private Phone() { }
|
private Phone() { }
|
||||||
|
|
||||||
|
|||||||
@@ -15,8 +15,8 @@ namespace BrightGlimmer.Domain
|
|||||||
public string FirstName { get; set; }
|
public string FirstName { get; set; }
|
||||||
public string MiddleName { get; set; }
|
public string MiddleName { get; set; }
|
||||||
public string LastName { get; set; }
|
public string LastName { get; set; }
|
||||||
public string Email { get; private set; }
|
public string Email { get; set; }
|
||||||
public Address Address { get; set; }
|
public Address Address { get; private set; }
|
||||||
public string ProfilePictureUrl { get; set; }
|
public string ProfilePictureUrl { get; set; }
|
||||||
|
|
||||||
private readonly List<Phone> phones = new List<Phone>();
|
private readonly List<Phone> phones = new List<Phone>();
|
||||||
|
|||||||
@@ -6,8 +6,6 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="Interfaces\" />
|
<Folder Include="Interfaces\" />
|
||||||
<Folder Include="Handlers\QueryHandlers\" />
|
|
||||||
<Folder Include="Queries\" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
using BrightGlimmer.Domain;
|
using BrightGlimmer.Domain;
|
||||||
using MediatR;
|
using MediatR;
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace BrightGlimmer.Service.Commands
|
namespace BrightGlimmer.Service.Commands
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -2,9 +2,6 @@
|
|||||||
using BrightGlimmer.Domain;
|
using BrightGlimmer.Domain;
|
||||||
using BrightGlimmer.Service.Commands;
|
using BrightGlimmer.Service.Commands;
|
||||||
using MediatR;
|
using MediatR;
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
|||||||
@@ -1,30 +0,0 @@
|
|||||||
using BrightGlimmer.Data.Interfaces;
|
|
||||||
using BrightGlimmer.Domain;
|
|
||||||
using BrightGlimmer.Services.Queries;
|
|
||||||
using MediatR;
|
|
||||||
using Microsoft.EntityFrameworkCore; /* TODO */
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Threading;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace BrightGlimmer.Services.Handlers.QueryHandlers
|
|
||||||
{
|
|
||||||
public class GetAllStudentsQueryHandler : IRequestHandler<GetAllStudentsQuery, IEnumerable<Student>>
|
|
||||||
{
|
|
||||||
private readonly IQueryRepository<Student> repository;
|
|
||||||
|
|
||||||
public GetAllStudentsQueryHandler(IQueryRepository<Student> repository)
|
|
||||||
{
|
|
||||||
this.repository = repository;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<IEnumerable<Student>> Handle(GetAllStudentsQuery request, CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
return await repository.Get()
|
|
||||||
.Include(x => x.Phones)
|
|
||||||
.Include(x => x.Address)
|
|
||||||
.Include(x => x.AssignedCourses)
|
|
||||||
.ToListAsync();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
using BrightGlimmer.Data.Interfaces;
|
||||||
|
using BrightGlimmer.Domain;
|
||||||
|
using BrightGlimmer.Service.Queries;
|
||||||
|
using MediatR;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace BrightGlimmer.Service.Handlers.QueryHandlers
|
||||||
|
{
|
||||||
|
public class GetStudentQueryHandler : IRequestHandler<GetStudentQuery, Student>
|
||||||
|
{
|
||||||
|
private readonly IQueryRepository<Student> repository;
|
||||||
|
|
||||||
|
public GetStudentQueryHandler(IQueryRepository<Student> repository)
|
||||||
|
{
|
||||||
|
this.repository = repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Student> Handle(GetStudentQuery request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return await repository.GetAsync(request.Id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
using BrightGlimmer.Data.Interfaces;
|
||||||
|
using BrightGlimmer.Domain;
|
||||||
|
using BrightGlimmer.Service.Queries;
|
||||||
|
using MediatR;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace BrightGlimmer.Service.Handlers.QueryHandlers
|
||||||
|
{
|
||||||
|
public class GetStudentsQueryHandler : IRequestHandler<GetStudentsQuery, IEnumerable<Student>>
|
||||||
|
{
|
||||||
|
private readonly IQueryRepository<Student> repository;
|
||||||
|
|
||||||
|
public GetStudentsQueryHandler(IQueryRepository<Student> repository)
|
||||||
|
{
|
||||||
|
this.repository = repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IEnumerable<Student>> Handle(GetStudentsQuery request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return await repository.Get().ToListAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
using BrightGlimmer.Domain;
|
|
||||||
using MediatR;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace BrightGlimmer.Services.Queries
|
|
||||||
{
|
|
||||||
public class GetAllStudentsQuery : IRequest<IEnumerable<Student>>
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
16
BrightGlimmer.Service/Queries/GetStudentQuery.cs
Normal file
16
BrightGlimmer.Service/Queries/GetStudentQuery.cs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
using BrightGlimmer.Domain;
|
||||||
|
using MediatR;
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace BrightGlimmer.Service.Queries
|
||||||
|
{
|
||||||
|
public class GetStudentQuery : IRequest<Student>
|
||||||
|
{
|
||||||
|
public Guid Id { get; set; }
|
||||||
|
|
||||||
|
public GetStudentQuery(Guid id)
|
||||||
|
{
|
||||||
|
Id = id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
10
BrightGlimmer.Service/Queries/GetStudentsQuery.cs
Normal file
10
BrightGlimmer.Service/Queries/GetStudentsQuery.cs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
using BrightGlimmer.Domain;
|
||||||
|
using MediatR;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace BrightGlimmer.Service.Queries
|
||||||
|
{
|
||||||
|
public class GetStudentsQuery : IRequest<IEnumerable<Student>>
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user