From 125a9d4bb6519b5554a9ebe499103877662e6c7e Mon Sep 17 00:00:00 2001 From: Giovani Date: Sat, 27 Apr 2019 15:23:12 -0400 Subject: [PATCH] Creating endpoint that allows for student update --- .../Controllers/StudentController.cs | 6 +++ .../Interfaces/ICommandRepository.cs | 3 ++ .../Repositories/CommandRepository.cs | 6 +++ BrightGlimmer.Domain/Address.cs | 5 +++ BrightGlimmer.Domain/Student.cs | 10 +++++ .../Commands/UpdateStudentCommand.cs | 19 +++++++++ .../UpdateStudentCommandHandler.cs | 39 +++++++++++++++++++ 7 files changed, 88 insertions(+) create mode 100644 BrightGlimmer.Service/Commands/UpdateStudentCommand.cs create mode 100644 BrightGlimmer.Service/Handlers/CommandHandlers/UpdateStudentCommandHandler.cs diff --git a/BrightGlimmer.Api/Controllers/StudentController.cs b/BrightGlimmer.Api/Controllers/StudentController.cs index b4eef9a..9fecdec 100644 --- a/BrightGlimmer.Api/Controllers/StudentController.cs +++ b/BrightGlimmer.Api/Controllers/StudentController.cs @@ -39,5 +39,11 @@ namespace BrightGlimmer.Api.Controllers var student = await mediator.Send(command); return new JsonResult(student); } + + [HttpPut] + public async Task Update([FromBody]UpdateStudentCommand command) + { + + } } } \ No newline at end of file diff --git a/BrightGlimmer.Data/Interfaces/ICommandRepository.cs b/BrightGlimmer.Data/Interfaces/ICommandRepository.cs index 4877cc9..98cbaab 100644 --- a/BrightGlimmer.Data/Interfaces/ICommandRepository.cs +++ b/BrightGlimmer.Data/Interfaces/ICommandRepository.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.Threading.Tasks; namespace BrightGlimmer.Data.Interfaces { @@ -18,5 +19,7 @@ namespace BrightGlimmer.Data.Interfaces IQueryable Get(); T Get(Guid id); + + Task GetAsync(Guid id); } } diff --git a/BrightGlimmer.Data/Repositories/CommandRepository.cs b/BrightGlimmer.Data/Repositories/CommandRepository.cs index 94c6d57..a217ed3 100644 --- a/BrightGlimmer.Data/Repositories/CommandRepository.cs +++ b/BrightGlimmer.Data/Repositories/CommandRepository.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Threading.Tasks; namespace BrightGlimmer.Data.Repositories { @@ -43,5 +44,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.Domain/Address.cs b/BrightGlimmer.Domain/Address.cs index 154efea..4944daa 100644 --- a/BrightGlimmer.Domain/Address.cs +++ b/BrightGlimmer.Domain/Address.cs @@ -56,6 +56,11 @@ namespace BrightGlimmer.Domain Longitude = longitude; } + public void Update(Address address) + { + + } + public void SetLatitudeAndLongitude(decimal latitude, decimal longitude) { Latitude = latitude; diff --git a/BrightGlimmer.Domain/Student.cs b/BrightGlimmer.Domain/Student.cs index 68e3772..0e5f380 100644 --- a/BrightGlimmer.Domain/Student.cs +++ b/BrightGlimmer.Domain/Student.cs @@ -41,6 +41,16 @@ namespace BrightGlimmer.Domain this.phones = phones; } + public void Update(Student student) + { + FirstName = student.FirstName; + MiddleName = student.MiddleName; + LastName = student.LastName; + Email = student.Email; + + if () + } + public void AddPhone(Phone phone) { phones.Add(phone); diff --git a/BrightGlimmer.Service/Commands/UpdateStudentCommand.cs b/BrightGlimmer.Service/Commands/UpdateStudentCommand.cs new file mode 100644 index 0000000..464d110 --- /dev/null +++ b/BrightGlimmer.Service/Commands/UpdateStudentCommand.cs @@ -0,0 +1,19 @@ +using BrightGlimmer.Domain; +using MediatR; +using System; +using System.Collections.Generic; +using System.Text; + +namespace BrightGlimmer.Service.Commands +{ + public class UpdateStudentCommand : IRequest + { + public Guid Id { get; set; } + public string FirstName { get; set; } + public string MiddleName { get; set; } + public string LastName { get; set; } + public string Email { get; set; } + public List Phones { get; set; } + public Address Address { get; set; } + } +} diff --git a/BrightGlimmer.Service/Handlers/CommandHandlers/UpdateStudentCommandHandler.cs b/BrightGlimmer.Service/Handlers/CommandHandlers/UpdateStudentCommandHandler.cs new file mode 100644 index 0000000..4896e6e --- /dev/null +++ b/BrightGlimmer.Service/Handlers/CommandHandlers/UpdateStudentCommandHandler.cs @@ -0,0 +1,39 @@ +using BrightGlimmer.Data.Interfaces; +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; + +namespace BrightGlimmer.Service.Handlers.CommandHandlers +{ + public class UpdateStudentCommandHandler : IRequestHandler + { + private readonly ICommandRepository repository; + + public UpdateStudentCommandHandler(ICommandRepository repository) + { + this.repository = repository; + } + + public async Task Handle(UpdateStudentCommand request, CancellationToken cancellationToken) + { + var student = await repository.GetAsync(request.Id); + + student.FirstName = request.FirstName; + student.MiddleName = request.MiddleName; + student.LastName = request.LastName; + student.Email = request.Email; + + if (student.Address != null) + { + + } + + return student; + } + } +}