Continued to work on the update student handler

This commit is contained in:
2019-04-29 13:54:10 -04:00
parent 8cacaa36ce
commit 9f2b9bcff9
10 changed files with 114 additions and 24 deletions

View File

@@ -9,6 +9,10 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="JsonNet.PrivateSettersContractResolvers.Source" Version="0.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="MediatR" Version="6.0.0" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="6.0.1" />
<PackageReference Include="Microsoft.AspNetCore.All" />

View File

@@ -43,7 +43,8 @@ namespace BrightGlimmer.Api.Controllers
[HttpPut]
public async Task<ActionResult> Update([FromBody]UpdateStudentCommand command)
{
var student = await mediator.Send(command);
return new JsonResult(student);
}
}
}

View File

@@ -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

View File

@@ -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)

View File

@@ -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;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -6,7 +6,7 @@ using System.Text;
namespace BrightGlimmer.Domain
{
public class Entity
public abstract class Entity
{
[Key]
public Guid Id { get; private set; }

View File

@@ -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;
}
}
}

View File

@@ -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));
}
}
}

View File

@@ -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<Phone>();
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;
}
}