initial commit
This commit is contained in:
107
Controllers/FamilyController.cs
Normal file
107
Controllers/FamilyController.cs
Normal file
@@ -0,0 +1,107 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
using weddingapi.Models;
|
||||
using weddingapi.Repositories;
|
||||
using weddingapi.Dtos;
|
||||
using weddingapi.Converters;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace weddingapi.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("family")]
|
||||
public class FamilyController : ControllerBase
|
||||
{
|
||||
private readonly IFamilyRepository repository;
|
||||
|
||||
public FamilyController(IFamilyRepository repository)
|
||||
{
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
// GET /families
|
||||
[HttpGet]
|
||||
public async Task<IEnumerable<FamilyDto>> GetFamiliesAsync()
|
||||
{
|
||||
return (await repository.GetFamiliesAsync()).Select(family => family.AsDto());
|
||||
}
|
||||
|
||||
// GET /family/{id}
|
||||
[HttpGet("{id}")]
|
||||
public async Task<ActionResult<FamilyDto>> GetFamilyAsync(Guid id)
|
||||
{
|
||||
var family = await repository.GetFamilyAsync(id);
|
||||
|
||||
if(family is null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
return family.AsDto();
|
||||
}
|
||||
|
||||
// POST /family
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<FamilyDto>> CreateFamilyAsync(CreateFamilyDto familyDto)
|
||||
{
|
||||
Family fam = new Family()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
CreatedDate = DateTimeOffset.UtcNow,
|
||||
Name = familyDto.Name,
|
||||
Members = familyDto.Members.Select(person => new Person() {
|
||||
IsChild = person.IsChild,
|
||||
Name = person.Name,
|
||||
}).ToList(),
|
||||
};
|
||||
|
||||
await repository.CreateFamilyAsync(fam);
|
||||
|
||||
return CreatedAtAction(nameof(GetFamilyAsync), new { id = fam.Id }, fam.AsDto());
|
||||
}
|
||||
|
||||
// PUT /family/{id}
|
||||
[HttpPut("{id}")]
|
||||
public async Task<ActionResult> UpdateFamilyAsync(Guid id, UpdateFamilyDto familyDto)
|
||||
{
|
||||
var exisitingFamily = await repository.GetFamilyAsync(id);
|
||||
|
||||
if(exisitingFamily is null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
Family updatedFamily = exisitingFamily with
|
||||
{
|
||||
Members = familyDto.Members.Select(person => new Person() {
|
||||
Name = person.Name,
|
||||
IsChild = person.IsChild,
|
||||
}).ToList(),
|
||||
Name = familyDto.Name,
|
||||
};
|
||||
|
||||
await repository.UpdateFamilyAsync(updatedFamily);
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
// Delete /family/{id}
|
||||
[HttpDelete("{id}")]
|
||||
public async Task<ActionResult> DeleteFamilyAsync(Guid id)
|
||||
{
|
||||
var exisitingFamily = repository.GetFamilyAsync(id);
|
||||
|
||||
if(exisitingFamily is null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
await repository.DeleteFamilyAsync(id);
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user