Add contact controller
This commit is contained in:
82
Controllers/ContactUsController.cs
Normal file
82
Controllers/ContactUsController.cs
Normal file
@@ -0,0 +1,82 @@
|
||||
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}
|
||||
[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}
|
||||
[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
|
||||
[HttpGet]
|
||||
public async Task<IEnumerable<ContactDto>> GetContactsAsync()
|
||||
{
|
||||
var contacts = (await repository.GetContactsAsync()).Select(contact => contact.AsDto());
|
||||
return contacts;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user