From 9f2b9bcff948c2fd79116eb4b6dd831dc77b5e91 Mon Sep 17 00:00:00 2001 From: Giovani Date: Mon, 29 Apr 2019 13:54:10 -0400 Subject: [PATCH] Continued to work on the update student handler --- BrightGlimmer.Api/BrightGlimmer.Api.csproj | 4 +++ .../Controllers/StudentController.cs | 3 ++- BrightGlimmer.Api/Startup.cs | 7 ++++- BrightGlimmer.Domain/Address.cs | 21 +++++++++------ BrightGlimmer.Domain/AssignedCourse.cs | 14 ++++++++++ BrightGlimmer.Domain/Course.cs | 13 +++++++--- BrightGlimmer.Domain/Entity.cs | 2 +- BrightGlimmer.Domain/Phone.cs | 26 ++++++++++++++++--- BrightGlimmer.Domain/Student.cs | 22 +++++++++++++--- .../UpdateStudentCommandHandler.cs | 26 ++++++++++++++++--- 10 files changed, 114 insertions(+), 24 deletions(-) diff --git a/BrightGlimmer.Api/BrightGlimmer.Api.csproj b/BrightGlimmer.Api/BrightGlimmer.Api.csproj index 48ad64a..40c5f3e 100644 --- a/BrightGlimmer.Api/BrightGlimmer.Api.csproj +++ b/BrightGlimmer.Api/BrightGlimmer.Api.csproj @@ -9,6 +9,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/BrightGlimmer.Api/Controllers/StudentController.cs b/BrightGlimmer.Api/Controllers/StudentController.cs index 9fecdec..c9f8ea5 100644 --- a/BrightGlimmer.Api/Controllers/StudentController.cs +++ b/BrightGlimmer.Api/Controllers/StudentController.cs @@ -43,7 +43,8 @@ namespace BrightGlimmer.Api.Controllers [HttpPut] public async Task Update([FromBody]UpdateStudentCommand command) { - + var student = await mediator.Send(command); + return new JsonResult(student); } } } \ No newline at end of file diff --git a/BrightGlimmer.Api/Startup.cs b/BrightGlimmer.Api/Startup.cs index a64ca33..16ea2aa 100644 --- a/BrightGlimmer.Api/Startup.cs +++ b/BrightGlimmer.Api/Startup.cs @@ -24,7 +24,12 @@ namespace BrightGlimmer.Api // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { - services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); + services.AddMvc() + .SetCompatibilityVersion(CompatibilityVersion.Version_2_1) + .AddJsonOptions(options => + { + options.SerializerSettings.ContractResolver = new PrivateSetterContractResolver() + }); services.AddMediatR(); services.AddMediatR(typeof(Cqrs.Cqrs).Assembly); // Registers handlers in services project diff --git a/BrightGlimmer.Domain/Address.cs b/BrightGlimmer.Domain/Address.cs index 4944daa..927fa31 100644 --- a/BrightGlimmer.Domain/Address.cs +++ b/BrightGlimmer.Domain/Address.cs @@ -9,12 +9,12 @@ namespace BrightGlimmer.Domain [Table("Addresses")] public class Address : Entity { - public string StreetAddress1 { get; set; } - public string StreetAddress2 { get; set; } - public string City { get; set; } - public string StateCode { get; set; } - public string County { get; set; } - public string ZipCode { get; set; } + public string StreetAddress1 { get; private set; } + public string StreetAddress2 { get; private set; } + public string City { get; private set; } + public string StateCode { get; private set; } + public string County { get; private set; } + public string ZipCode { get; private set; } [JsonIgnore] public decimal Latitude { get; private set; } [JsonIgnore] @@ -56,9 +56,14 @@ namespace BrightGlimmer.Domain Longitude = longitude; } - public void Update(Address address) + internal void Update(Address address) { - + StreetAddress1 = address.StreetAddress1; + StreetAddress2 = address.StreetAddress2; + City = address.City; + StateCode = address.StateCode; + County = address.County; + ZipCode = address.ZipCode; } public void SetLatitudeAndLongitude(decimal latitude, decimal longitude) diff --git a/BrightGlimmer.Domain/AssignedCourse.cs b/BrightGlimmer.Domain/AssignedCourse.cs index bb147c2..e5ea3a1 100644 --- a/BrightGlimmer.Domain/AssignedCourse.cs +++ b/BrightGlimmer.Domain/AssignedCourse.cs @@ -29,5 +29,19 @@ namespace BrightGlimmer.Domain IsActive = isActive; Term = term; } + + public AssignedCourse(AssignedCourse assignedCourse) + { + Grade = assignedCourse.Grade; + IsActive = assignedCourse.IsActive; + Term = assignedCourse.Term; + } + + public void Update(AssignedCourse assignedCourse) + { + Grade = assignedCourse.Grade; + IsActive = assignedCourse.IsActive; + Term = assignedCourse.Term; + } } } diff --git a/BrightGlimmer.Domain/Course.cs b/BrightGlimmer.Domain/Course.cs index 7c27bf9..0201cb2 100644 --- a/BrightGlimmer.Domain/Course.cs +++ b/BrightGlimmer.Domain/Course.cs @@ -8,9 +8,9 @@ namespace BrightGlimmer.Domain [Table("Courses")] public class Course : Entity { - public string Name { get; private set; } - public string Description { get; private set; } - public string Code { get; private set; } + public string Name { get; set; } + public string Description { get; set; } + public string Code { get; set; } private Course() { } @@ -20,5 +20,12 @@ namespace BrightGlimmer.Domain Description = description; Code = code; } + + public void Update(Course course) + { + Name = course.Name; + Description = course.Description; + Code = course.Code; + } } } diff --git a/BrightGlimmer.Domain/Entity.cs b/BrightGlimmer.Domain/Entity.cs index 858ac97..d38898e 100644 --- a/BrightGlimmer.Domain/Entity.cs +++ b/BrightGlimmer.Domain/Entity.cs @@ -6,7 +6,7 @@ using System.Text; namespace BrightGlimmer.Domain { - public class Entity + public abstract class Entity { [Key] public Guid Id { get; private set; } diff --git a/BrightGlimmer.Domain/Phone.cs b/BrightGlimmer.Domain/Phone.cs index 5abf0ca..1401132 100644 --- a/BrightGlimmer.Domain/Phone.cs +++ b/BrightGlimmer.Domain/Phone.cs @@ -1,4 +1,5 @@ -using System; +using Newtonsoft.Json; +using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; using System.Text; @@ -8,9 +9,12 @@ namespace BrightGlimmer.Domain [Table("Phones")] public class Phone : Entity { - public PhoneType Type { get; set; } - public int AreaCode { get; set; } - public int Number { get; set; } + [JsonProperty] + public PhoneType Type { get; private set; } + [JsonProperty] + public int AreaCode { get; private set; } + [JsonProperty] + public int Number { get; private set; } private Phone() { } @@ -20,5 +24,19 @@ namespace BrightGlimmer.Domain AreaCode = areaCode; Number = number; } + + public Phone(Phone phone) + { + Type = phone.Type; + AreaCode = phone.AreaCode; + Number = phone.Number; + } + + internal void Update(Phone phone) + { + Type = phone.Type; + AreaCode = phone.AreaCode; + Number = Number; + } } } diff --git a/BrightGlimmer.Domain/Student.cs b/BrightGlimmer.Domain/Student.cs index 0e5f380..5db02bf 100644 --- a/BrightGlimmer.Domain/Student.cs +++ b/BrightGlimmer.Domain/Student.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; using System.Text; namespace BrightGlimmer.Domain @@ -47,18 +48,33 @@ namespace BrightGlimmer.Domain MiddleName = student.MiddleName; LastName = student.LastName; Email = student.Email; + } - if () + public void UpdateAddress(Address address) + { + Address.Update(address); + } + + public bool UpdatePhone(Phone phone) + { + var existingPhone = Phones.SingleOrDefault(x => x.Id == phone.Id); + if (existingPhone == null) + { + return false; + } + + existingPhone.Update(phone); + return true; } public void AddPhone(Phone phone) { - phones.Add(phone); + phones.Add(new Phone(phone)); } public void AddAssignedCourse(AssignedCourse assignedCourse) { - assignedCourses.Add(assignedCourse); + assignedCourses.Add(new AssignedCourse(assignedCourse)); } } } diff --git a/BrightGlimmer.Service/Handlers/CommandHandlers/UpdateStudentCommandHandler.cs b/BrightGlimmer.Service/Handlers/CommandHandlers/UpdateStudentCommandHandler.cs index 4896e6e..98395bb 100644 --- a/BrightGlimmer.Service/Handlers/CommandHandlers/UpdateStudentCommandHandler.cs +++ b/BrightGlimmer.Service/Handlers/CommandHandlers/UpdateStudentCommandHandler.cs @@ -4,6 +4,7 @@ using BrightGlimmer.Service.Commands; using MediatR; using System; using System.Collections.Generic; +using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -27,12 +28,31 @@ namespace BrightGlimmer.Service.Handlers.CommandHandlers student.MiddleName = request.MiddleName; student.LastName = request.LastName; student.Email = request.Email; - - if (student.Address != null) - { + if (request.Address != null) + { + /* TODO: CALL TO SET LAT LONG */ + student.UpdateAddress(request.Address); } + if (student.Phones != null) + { + var newPhones = new List(); + foreach (var phone in request.Phones) + { + if (!student.UpdatePhone(phone)) + { + newPhones.Add(phone); + } + } + + foreach (var newPhone in newPhones) + { + student.AddPhone(newPhone); + } + } + + await repository.UnitOfWork.SaveChangesAsync(); return student; } }