Created
December 22, 2019 16:44
-
-
Save acraven/a44154ab30382d61fe5f95715c9e1c7e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.ComponentModel.DataAnnotations; | |
using System.Globalization; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using Microservice.Controllers.Model; | |
using Microservice.Dependencies.ObjectStore; | |
using Microservice.Model; | |
using Microsoft.AspNetCore.Mvc; | |
namespace Microservice.Controllers | |
{ | |
[ApiController] | |
[Route("contacts")] | |
public class ContactsController : Controller | |
{ | |
private readonly IAggregateStore _aggregateStore; | |
public ContactsController(IAggregateStore aggregateStore) | |
{ | |
_aggregateStore = aggregateStore; | |
} | |
[HttpPost] | |
public async Task<ActionResult> PostAsync([FromBody, Required] ContactDto contactDto) | |
{ | |
var contact = new Contact | |
{ | |
FirstName = contactDto.FirstName, | |
LastName = contactDto.LastName, | |
DateOfBirth = DateTime.ParseExact(contactDto.DateOfBirth, "yyyy-MM-dd", CultureInfo.InvariantCulture) | |
}; | |
await _aggregateStore.AddAsync(contact); | |
return Created($"{Request.Scheme}://{Request.Host}/contacts/{contact.ID}", null); | |
} | |
[HttpGet("{contactId}")] | |
public async Task<ActionResult> GetAsync([FromRoute] Guid contactId) | |
{ | |
var contact = await _aggregateStore.GetAsync<Contact>(contactId); | |
return new JsonResult(MapContact(contact)); | |
} | |
[HttpGet] | |
public async Task<ActionResult> GetManyAsync() | |
{ | |
var contacts = await _aggregateStore.GetAllAsync<Contact>(); | |
return new JsonResult(contacts.Select(MapContact).ToArray()); | |
} | |
private static ContactDto MapContact(Contact contact) | |
{ | |
if (contact == null) | |
{ | |
return null; | |
} | |
return new ContactDto | |
{ | |
FirstName = contact.FirstName, | |
LastName = contact.LastName, | |
DateOfBirth = contact.DateOfBirth.ToString("yyyy-MM-dd") | |
}; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment