From 1a95acac0125a143eb7a3c835a87fe98d3eb7f35 Mon Sep 17 00:00:00 2001 From: Giovani Date: Sat, 27 Apr 2019 00:02:55 -0400 Subject: [PATCH] Restructured query repositries to move ef logic from handlers --- .../Controllers/StudentController.cs | 17 ++++++++--- BrightGlimmer.Api/Startup.cs | 3 ++ .../Interfaces/IQueryRepository.cs | 3 ++ .../Repositories/QueryRepository.cs | 7 +++++ .../Repositories/StudentQueryRepository.cs | 28 +++++++++++++++++ BrightGlimmer.Domain/Address.cs | 25 ++++++++++++++++ BrightGlimmer.Domain/AssignedCourse.cs | 12 ++++++-- BrightGlimmer.Domain/Phone.cs | 6 ++-- BrightGlimmer.Domain/Student.cs | 4 +-- .../BrightGlimmer.Service.csproj | 2 -- .../Commands/CreateStudentCommand.cs | 2 -- .../CreateStudentCommandHandler.cs | 3 -- .../GetAllStudentsQueryHandler.cs | 30 ------------------- .../QueryHandlers/GetStudentQueryHandler.cs | 24 +++++++++++++++ .../QueryHandlers/GetStudentsQueryHandler.cs | 26 ++++++++++++++++ .../Queries/GetAllStudentsQuery.cs | 10 ------- .../Queries/GetStudentQuery.cs | 16 ++++++++++ .../Queries/GetStudentsQuery.cs | 10 +++++++ 18 files changed, 169 insertions(+), 59 deletions(-) delete mode 100644 BrightGlimmer.Service/Handlers/QueryHandlers/GetAllStudentsQueryHandler.cs create mode 100644 BrightGlimmer.Service/Handlers/QueryHandlers/GetStudentQueryHandler.cs create mode 100644 BrightGlimmer.Service/Handlers/QueryHandlers/GetStudentsQueryHandler.cs delete mode 100644 BrightGlimmer.Service/Queries/GetAllStudentsQuery.cs create mode 100644 BrightGlimmer.Service/Queries/GetStudentQuery.cs create mode 100644 BrightGlimmer.Service/Queries/GetStudentsQuery.cs diff --git a/BrightGlimmer.Api/Controllers/StudentController.cs b/BrightGlimmer.Api/Controllers/StudentController.cs index 1e633e3..b4eef9a 100644 --- a/BrightGlimmer.Api/Controllers/StudentController.cs +++ b/BrightGlimmer.Api/Controllers/StudentController.cs @@ -1,6 +1,7 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; using BrightGlimmer.Service.Commands; -using BrightGlimmer.Services.Queries; +using BrightGlimmer.Service.Queries; using MediatR; using Microsoft.AspNetCore.Mvc; @@ -18,12 +19,20 @@ namespace BrightGlimmer.Api.Controllers } [HttpGet] - public async Task GetAll() + public async Task Get() { - var students = await mediator.Send(new GetAllStudentsQuery()); + var students = await mediator.Send(new GetStudentsQuery()); return new JsonResult(students); } + [HttpGet] + [Route("{id}")] + public async Task Get(Guid id) + { + var student = await mediator.Send(new GetStudentQuery(id)); + return new JsonResult(student); + } + [HttpPost] public async Task Create([FromBody]CreateStudentCommand command) { diff --git a/BrightGlimmer.Api/Startup.cs b/BrightGlimmer.Api/Startup.cs index a339566..a64ca33 100644 --- a/BrightGlimmer.Api/Startup.cs +++ b/BrightGlimmer.Api/Startup.cs @@ -1,6 +1,7 @@ using BrightGlimmer.Data; using BrightGlimmer.Data.Interfaces; using BrightGlimmer.Data.Repositories; +using BrightGlimmer.Domain; using MediatR; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; @@ -31,6 +32,8 @@ namespace BrightGlimmer.Api services.AddTransient(typeof(IQueryRepository<>), typeof(QueryRepository<>)); services.AddTransient(typeof(ICommandRepository<>), typeof(CommandRepository<>)); + services.AddTransient(typeof(IQueryRepository), typeof(StudentQueryRepository)); + services.AddDbContext(options => options.UseSqlite(Configuration.GetConnectionString("DefaultConnection"))); services.AddTransient(); } diff --git a/BrightGlimmer.Data/Interfaces/IQueryRepository.cs b/BrightGlimmer.Data/Interfaces/IQueryRepository.cs index 58bb819..2732aec 100644 --- a/BrightGlimmer.Data/Interfaces/IQueryRepository.cs +++ b/BrightGlimmer.Data/Interfaces/IQueryRepository.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.Threading.Tasks; namespace BrightGlimmer.Data.Interfaces { @@ -12,5 +13,7 @@ namespace BrightGlimmer.Data.Interfaces IQueryable Get(); T Get(Guid id); + + Task GetAsync(Guid id); } } diff --git a/BrightGlimmer.Data/Repositories/QueryRepository.cs b/BrightGlimmer.Data/Repositories/QueryRepository.cs index 512ca09..c1705b0 100644 --- a/BrightGlimmer.Data/Repositories/QueryRepository.cs +++ b/BrightGlimmer.Data/Repositories/QueryRepository.cs @@ -1,9 +1,11 @@ using BrightGlimmer.Data.Interfaces; using BrightGlimmer.Domain; +using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Threading.Tasks; namespace BrightGlimmer.Data.Repositories { @@ -27,5 +29,10 @@ namespace BrightGlimmer.Data.Repositories { return context.Set().Find(id); } + + public virtual async Task GetAsync(Guid id) + { + return await context.Set().SingleAsync(x => x.Id == id); + } } } diff --git a/BrightGlimmer.Data/Repositories/StudentQueryRepository.cs b/BrightGlimmer.Data/Repositories/StudentQueryRepository.cs index 8fd2dea..88cf9b0 100644 --- a/BrightGlimmer.Data/Repositories/StudentQueryRepository.cs +++ b/BrightGlimmer.Data/Repositories/StudentQueryRepository.cs @@ -1,15 +1,43 @@ using BrightGlimmer.Data.Interfaces; using BrightGlimmer.Domain; +using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Threading.Tasks; namespace BrightGlimmer.Data.Repositories { public class StudentQueryRepository : QueryRepository { public StudentQueryRepository(BgContext context) : base(context) { } + + public override IQueryable 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 GetAsync(Guid id) + { + return await context.Students + .Include(x => x.Phones) + .Include(x => x.Address) + .Include(x => x.AssignedCourses) + .SingleAsync(x => x.Id == id); + } } } diff --git a/BrightGlimmer.Domain/Address.cs b/BrightGlimmer.Domain/Address.cs index fe741df..154efea 100644 --- a/BrightGlimmer.Domain/Address.cs +++ b/BrightGlimmer.Domain/Address.cs @@ -36,5 +36,30 @@ namespace BrightGlimmer.Domain County = county; 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; + } } } diff --git a/BrightGlimmer.Domain/AssignedCourse.cs b/BrightGlimmer.Domain/AssignedCourse.cs index b11a135..bb147c2 100644 --- a/BrightGlimmer.Domain/AssignedCourse.cs +++ b/BrightGlimmer.Domain/AssignedCourse.cs @@ -8,15 +8,21 @@ namespace BrightGlimmer.Domain [Table("AssignedCourses")] public class AssignedCourse : Entity { - public decimal Grade { get; set; } + public decimal? Grade { get; set; } public bool IsActive { get; set; } public string Term { get; set; } - public Student Student { get; set; } - public Course Course { get; set; } + public Student Student { get; private set; } + public Course Course { get; private set; } private AssignedCourse() { } + public AssignedCourse(bool isActive, string term) + { + IsActive = isActive; + Term = term; + } + public AssignedCourse(decimal grade, bool isActive, string term) { Grade = grade; diff --git a/BrightGlimmer.Domain/Phone.cs b/BrightGlimmer.Domain/Phone.cs index 1cda3ed..5abf0ca 100644 --- a/BrightGlimmer.Domain/Phone.cs +++ b/BrightGlimmer.Domain/Phone.cs @@ -8,9 +8,9 @@ namespace BrightGlimmer.Domain [Table("Phones")] public class Phone : Entity { - public PhoneType Type { get; private set; } - public int AreaCode { get; private set; } - public int Number { get; private set; } + public PhoneType Type { get; set; } + public int AreaCode { get; set; } + public int Number { get; set; } private Phone() { } diff --git a/BrightGlimmer.Domain/Student.cs b/BrightGlimmer.Domain/Student.cs index cde5a05..68e3772 100644 --- a/BrightGlimmer.Domain/Student.cs +++ b/BrightGlimmer.Domain/Student.cs @@ -15,8 +15,8 @@ namespace BrightGlimmer.Domain public string FirstName { get; set; } public string MiddleName { get; set; } public string LastName { get; set; } - public string Email { get; private set; } - public Address Address { get; set; } + public string Email { get; set; } + public Address Address { get; private set; } public string ProfilePictureUrl { get; set; } private readonly List phones = new List(); diff --git a/BrightGlimmer.Service/BrightGlimmer.Service.csproj b/BrightGlimmer.Service/BrightGlimmer.Service.csproj index b40d112..d66e379 100644 --- a/BrightGlimmer.Service/BrightGlimmer.Service.csproj +++ b/BrightGlimmer.Service/BrightGlimmer.Service.csproj @@ -6,8 +6,6 @@ - - diff --git a/BrightGlimmer.Service/Commands/CreateStudentCommand.cs b/BrightGlimmer.Service/Commands/CreateStudentCommand.cs index a620853..e0204c1 100644 --- a/BrightGlimmer.Service/Commands/CreateStudentCommand.cs +++ b/BrightGlimmer.Service/Commands/CreateStudentCommand.cs @@ -1,8 +1,6 @@ using BrightGlimmer.Domain; using MediatR; -using System; using System.Collections.Generic; -using System.Text; namespace BrightGlimmer.Service.Commands { diff --git a/BrightGlimmer.Service/Handlers/CommandHandlers/CreateStudentCommandHandler.cs b/BrightGlimmer.Service/Handlers/CommandHandlers/CreateStudentCommandHandler.cs index 4a94b66..6623bdd 100644 --- a/BrightGlimmer.Service/Handlers/CommandHandlers/CreateStudentCommandHandler.cs +++ b/BrightGlimmer.Service/Handlers/CommandHandlers/CreateStudentCommandHandler.cs @@ -2,9 +2,6 @@ using BrightGlimmer.Domain; using BrightGlimmer.Service.Commands; using MediatR; -using System; -using System.Collections.Generic; -using System.Text; using System.Threading; using System.Threading.Tasks; diff --git a/BrightGlimmer.Service/Handlers/QueryHandlers/GetAllStudentsQueryHandler.cs b/BrightGlimmer.Service/Handlers/QueryHandlers/GetAllStudentsQueryHandler.cs deleted file mode 100644 index a829f88..0000000 --- a/BrightGlimmer.Service/Handlers/QueryHandlers/GetAllStudentsQueryHandler.cs +++ /dev/null @@ -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> - { - private readonly IQueryRepository repository; - - public GetAllStudentsQueryHandler(IQueryRepository repository) - { - this.repository = repository; - } - - public async Task> Handle(GetAllStudentsQuery request, CancellationToken cancellationToken) - { - return await repository.Get() - .Include(x => x.Phones) - .Include(x => x.Address) - .Include(x => x.AssignedCourses) - .ToListAsync(); - } - } -} diff --git a/BrightGlimmer.Service/Handlers/QueryHandlers/GetStudentQueryHandler.cs b/BrightGlimmer.Service/Handlers/QueryHandlers/GetStudentQueryHandler.cs new file mode 100644 index 0000000..5e8c69e --- /dev/null +++ b/BrightGlimmer.Service/Handlers/QueryHandlers/GetStudentQueryHandler.cs @@ -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 + { + private readonly IQueryRepository repository; + + public GetStudentQueryHandler(IQueryRepository repository) + { + this.repository = repository; + } + + public async Task Handle(GetStudentQuery request, CancellationToken cancellationToken) + { + return await repository.GetAsync(request.Id); + } + } +} diff --git a/BrightGlimmer.Service/Handlers/QueryHandlers/GetStudentsQueryHandler.cs b/BrightGlimmer.Service/Handlers/QueryHandlers/GetStudentsQueryHandler.cs new file mode 100644 index 0000000..0fd9281 --- /dev/null +++ b/BrightGlimmer.Service/Handlers/QueryHandlers/GetStudentsQueryHandler.cs @@ -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> + { + private readonly IQueryRepository repository; + + public GetStudentsQueryHandler(IQueryRepository repository) + { + this.repository = repository; + } + + public async Task> Handle(GetStudentsQuery request, CancellationToken cancellationToken) + { + return await repository.Get().ToListAsync(); + } + } +} diff --git a/BrightGlimmer.Service/Queries/GetAllStudentsQuery.cs b/BrightGlimmer.Service/Queries/GetAllStudentsQuery.cs deleted file mode 100644 index 8f341f8..0000000 --- a/BrightGlimmer.Service/Queries/GetAllStudentsQuery.cs +++ /dev/null @@ -1,10 +0,0 @@ -using BrightGlimmer.Domain; -using MediatR; -using System.Collections.Generic; - -namespace BrightGlimmer.Services.Queries -{ - public class GetAllStudentsQuery : IRequest> - { - } -} diff --git a/BrightGlimmer.Service/Queries/GetStudentQuery.cs b/BrightGlimmer.Service/Queries/GetStudentQuery.cs new file mode 100644 index 0000000..0d8642e --- /dev/null +++ b/BrightGlimmer.Service/Queries/GetStudentQuery.cs @@ -0,0 +1,16 @@ +using BrightGlimmer.Domain; +using MediatR; +using System; + +namespace BrightGlimmer.Service.Queries +{ + public class GetStudentQuery : IRequest + { + public Guid Id { get; set; } + + public GetStudentQuery(Guid id) + { + Id = id; + } + } +} diff --git a/BrightGlimmer.Service/Queries/GetStudentsQuery.cs b/BrightGlimmer.Service/Queries/GetStudentsQuery.cs new file mode 100644 index 0000000..e91181c --- /dev/null +++ b/BrightGlimmer.Service/Queries/GetStudentsQuery.cs @@ -0,0 +1,10 @@ +using BrightGlimmer.Domain; +using MediatR; +using System.Collections.Generic; + +namespace BrightGlimmer.Service.Queries +{ + public class GetStudentsQuery : IRequest> + { + } +}