85 lines
2.3 KiB
C#
85 lines
2.3 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("contact")]
|
|
public class ContactUsController : ControllerBase
|
|
{
|
|
private readonly IContactRepository repository;
|
|
|
|
public ContactUsController(IContactRepository repository)
|
|
{
|
|
this.repository = repository;
|
|
}
|
|
|
|
// POST /contact
|
|
[HttpPost]
|
|
public async Task<ActionResult<ContactDto>> CreateContactAsync(CreateContactDto contactDto)
|
|
{
|
|
Contact contact = new Contact()
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
CreatedDate = DateTimeOffset.UtcNow,
|
|
From = contactDto.From,
|
|
Email = contactDto.Email,
|
|
Message = contactDto.Message,
|
|
};
|
|
|
|
await repository.CreateContactAsync(contact);
|
|
|
|
return CreatedAtAction(nameof(GetContactAsync), new { id = contact.Id }, contact.AsDto());
|
|
}
|
|
|
|
// GET /contact/{id}
|
|
[DevelopmentOnly]
|
|
[HttpGet("{id}")]
|
|
public async Task<ActionResult<ContactDto>> GetContactAsync(Guid id)
|
|
{
|
|
var contact = await repository.GetContactAsync(id);
|
|
|
|
if(contact is null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return contact.AsDto();
|
|
}
|
|
|
|
// Delete /contact/{id}
|
|
[DevelopmentOnly]
|
|
[HttpDelete("{id}")]
|
|
public async Task<ActionResult> DeleteContactAsync(Guid id)
|
|
{
|
|
var exisitingFamily = repository.GetContactAsync(id);
|
|
|
|
if(exisitingFamily is null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
await repository.DeleteContactAsync(id);
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
// GET /contacts
|
|
[DevelopmentOnly]
|
|
[HttpGet]
|
|
public async Task<IEnumerable<ContactDto>> GetContactsAsync()
|
|
{
|
|
var contacts = (await repository.GetContactsAsync()).Select(contact => contact.AsDto());
|
|
return contacts;
|
|
}
|
|
}
|
|
} |