Creating endpoint that allows for student update
This commit is contained in:
@@ -39,5 +39,11 @@ namespace BrightGlimmer.Api.Controllers
|
|||||||
var student = await mediator.Send(command);
|
var student = await mediator.Send(command);
|
||||||
return new JsonResult(student);
|
return new JsonResult(student);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpPut]
|
||||||
|
public async Task<ActionResult> Update([FromBody]UpdateStudentCommand command)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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
|
||||||
{
|
{
|
||||||
@@ -18,5 +19,7 @@ namespace BrightGlimmer.Data.Interfaces
|
|||||||
IQueryable<T> Get();
|
IQueryable<T> Get();
|
||||||
|
|
||||||
T Get(Guid id);
|
T Get(Guid id);
|
||||||
|
|
||||||
|
Task<T> GetAsync(Guid id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ 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
|
||||||
{
|
{
|
||||||
@@ -43,5 +44,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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,6 +56,11 @@ namespace BrightGlimmer.Domain
|
|||||||
Longitude = longitude;
|
Longitude = longitude;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Update(Address address)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public void SetLatitudeAndLongitude(decimal latitude, decimal longitude)
|
public void SetLatitudeAndLongitude(decimal latitude, decimal longitude)
|
||||||
{
|
{
|
||||||
Latitude = latitude;
|
Latitude = latitude;
|
||||||
|
|||||||
@@ -41,6 +41,16 @@ namespace BrightGlimmer.Domain
|
|||||||
this.phones = phones;
|
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)
|
public void AddPhone(Phone phone)
|
||||||
{
|
{
|
||||||
phones.Add(phone);
|
phones.Add(phone);
|
||||||
|
|||||||
19
BrightGlimmer.Service/Commands/UpdateStudentCommand.cs
Normal file
19
BrightGlimmer.Service/Commands/UpdateStudentCommand.cs
Normal file
@@ -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<Student>
|
||||||
|
{
|
||||||
|
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<Phone> Phones { get; set; }
|
||||||
|
public Address Address { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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<UpdateStudentCommand, Student>
|
||||||
|
{
|
||||||
|
private readonly ICommandRepository<Student> repository;
|
||||||
|
|
||||||
|
public UpdateStudentCommandHandler(ICommandRepository<Student> repository)
|
||||||
|
{
|
||||||
|
this.repository = repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Student> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user