130 lines
3.8 KiB
C#
130 lines
3.8 KiB
C#
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;
|
|
using Microsoft.AspNetCore.Cors;
|
|
|
|
namespace weddingapi.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("family")]
|
|
public class FamilyController : ControllerBase
|
|
{
|
|
private readonly IFamilyRepository repository;
|
|
|
|
public FamilyController(IFamilyRepository repository)
|
|
{
|
|
this.repository = repository;
|
|
}
|
|
|
|
// GET /familyCount
|
|
[HttpGet("FamilyCount")]
|
|
public async Task<int> GetFamilyCountAsync()
|
|
{
|
|
int totalCount = 0;
|
|
(await repository.GetFamiliesAsync()).ToList().ForEach(family => totalCount += family.Members.Count());
|
|
return totalCount;
|
|
}
|
|
|
|
// GET /family
|
|
[DevelopmentOnly]
|
|
[HttpGet]
|
|
public async Task<IEnumerable<FamilyDto>> GetFamiliesAsync()
|
|
{
|
|
var fams = (await repository.GetFamiliesAsync()).Select(family => family.AsDto());
|
|
Response.Headers.Add("X-Total-Count", fams.Count().ToString());
|
|
return fams;
|
|
}
|
|
|
|
// GET /family/{id}
|
|
[DevelopmentOnly]
|
|
[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,
|
|
Overnight = familyDto.Overnight,
|
|
Email = familyDto.EMail,
|
|
Telephonenumber = familyDto.Telephonenumber,
|
|
CreatedByIp = Request.HttpContext.Connection.RemoteIpAddress.ToString(),
|
|
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}
|
|
[DevelopmentOnly]
|
|
[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(),
|
|
Overnight = familyDto.Overnight,
|
|
Email = familyDto.Email,
|
|
Telephonenumber = familyDto.Telephonenumber,
|
|
Name = familyDto.Name,
|
|
};
|
|
|
|
await repository.UpdateFamilyAsync(updatedFamily);
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
// Delete /family/{id}
|
|
[DevelopmentOnly]
|
|
[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();
|
|
}
|
|
}
|
|
} |