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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -28,6 +28,19 @@ namespace weddingapi.Converters
|
|||||||
Name = person.Name,
|
Name = person.Name,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static ContactDto AsDto(this Contact contact)
|
||||||
|
{
|
||||||
|
return new ContactDto()
|
||||||
|
{
|
||||||
|
CreatedDate = contact.CreatedDate,
|
||||||
|
Email = contact.Email,
|
||||||
|
From = contact.From,
|
||||||
|
Id = contact.Id,
|
||||||
|
Message = contact.Message,
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
14
Dtos/ContactDto.cs
Normal file
14
Dtos/ContactDto.cs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace weddingapi.Dtos
|
||||||
|
{
|
||||||
|
public record ContactDto
|
||||||
|
{
|
||||||
|
public Guid Id { get; init; }
|
||||||
|
public string From { get; init; }
|
||||||
|
public string Email { get; init; }
|
||||||
|
public string Message { get; init; }
|
||||||
|
public DateTimeOffset CreatedDate { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
15
Dtos/CreateContactDto.cs
Normal file
15
Dtos/CreateContactDto.cs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace weddingapi.Dtos
|
||||||
|
{
|
||||||
|
public record CreateContactDto
|
||||||
|
{
|
||||||
|
[Required]
|
||||||
|
public string From { get; init; }
|
||||||
|
[Required]
|
||||||
|
public string Email { get; init; }
|
||||||
|
[Required]
|
||||||
|
public string Message { get; init; }
|
||||||
|
}
|
||||||
|
}
|
||||||
13
Models/Contact.cs
Normal file
13
Models/Contact.cs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace weddingapi
|
||||||
|
{
|
||||||
|
public record Contact
|
||||||
|
{
|
||||||
|
public Guid Id { get; init; }
|
||||||
|
public string From { get; init; }
|
||||||
|
public string Email { get; init; }
|
||||||
|
public string Message { get; init; }
|
||||||
|
public DateTimeOffset CreatedDate { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
20
Repositories/IContactRepository.cs
Normal file
20
Repositories/IContactRepository.cs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using weddingapi.Models;
|
||||||
|
|
||||||
|
namespace weddingapi.Repositories
|
||||||
|
{
|
||||||
|
public interface IContactRepository
|
||||||
|
{
|
||||||
|
Task<Contact> GetContactAsync(Guid id);
|
||||||
|
|
||||||
|
Task<IEnumerable<Contact>> GetContactsAsync();
|
||||||
|
|
||||||
|
Task CreateContactAsync(Contact family);
|
||||||
|
|
||||||
|
Task UpdateContactAsync(Contact family);
|
||||||
|
|
||||||
|
Task DeleteContactAsync(Guid id);
|
||||||
|
}
|
||||||
|
}
|
||||||
53
Repositories/MongoDBContactUsRepository.cs
Normal file
53
Repositories/MongoDBContactUsRepository.cs
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using MongoDB.Bson;
|
||||||
|
using MongoDB.Driver;
|
||||||
|
using weddingapi.Models;
|
||||||
|
|
||||||
|
namespace weddingapi.Repositories
|
||||||
|
{
|
||||||
|
public class MongoDBContactUsRepository : IContactRepository
|
||||||
|
{
|
||||||
|
private const string databaseName = "wedding";
|
||||||
|
private const string collectionName = "contacts";
|
||||||
|
private readonly IMongoCollection<Contact> familyCollection;
|
||||||
|
private readonly FilterDefinitionBuilder<Contact> filterBuilder = Builders<Contact>.Filter;
|
||||||
|
|
||||||
|
public MongoDBContactUsRepository(IMongoClient mongoClient)
|
||||||
|
{
|
||||||
|
IMongoDatabase database = mongoClient.GetDatabase(databaseName);
|
||||||
|
familyCollection = database.GetCollection<Contact>(collectionName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task CreateContactAsync(Contact contact)
|
||||||
|
{
|
||||||
|
await familyCollection.InsertOneAsync(contact);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task DeleteContactAsync(Guid id)
|
||||||
|
{
|
||||||
|
var filter = filterBuilder.Eq(contact => contact.Id, id);
|
||||||
|
await familyCollection.DeleteOneAsync(filter);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IEnumerable<Contact>> GetContactsAsync()
|
||||||
|
{
|
||||||
|
var res = await familyCollection.FindAsync(new BsonDocument());
|
||||||
|
return await res.ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Contact> GetContactAsync(Guid id)
|
||||||
|
{
|
||||||
|
var filter = filterBuilder.Eq(contact => contact.Id, id);
|
||||||
|
var res = await familyCollection.FindAsync(filter);
|
||||||
|
return await res.SingleOrDefaultAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task UpdateContactAsync(Contact contact)
|
||||||
|
{
|
||||||
|
var filter = filterBuilder.Eq(exisitingContact => exisitingContact.Id, contact.Id);
|
||||||
|
await familyCollection.ReplaceOneAsync(filter, contact);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -45,6 +45,7 @@ namespace weddingapi
|
|||||||
});
|
});
|
||||||
|
|
||||||
services.AddSingleton<IFamilyRepository, MongoDBFamilyRepository>();
|
services.AddSingleton<IFamilyRepository, MongoDBFamilyRepository>();
|
||||||
|
services.AddSingleton<IContactRepository, MongoDBContactUsRepository>();
|
||||||
services.AddControllers(options => {
|
services.AddControllers(options => {
|
||||||
options.SuppressAsyncSuffixInActionNames = false;
|
options.SuppressAsyncSuffixInActionNames = false;
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,15 +0,0 @@
|
|||||||
using System;
|
|
||||||
|
|
||||||
namespace weddingapi
|
|
||||||
{
|
|
||||||
public class WeatherForecast
|
|
||||||
{
|
|
||||||
public DateTime Date { get; set; }
|
|
||||||
|
|
||||||
public int TemperatureC { get; set; }
|
|
||||||
|
|
||||||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
|
||||||
|
|
||||||
public string Summary { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -8,7 +8,7 @@
|
|||||||
},
|
},
|
||||||
"AllowedHosts": "*",
|
"AllowedHosts": "*",
|
||||||
"MongoDbConfig": {
|
"MongoDbConfig": {
|
||||||
"Host": "truenas",
|
"Host": "192.168.2.107",
|
||||||
"Port": "27017"
|
"Port": "27017"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user