Refactoring and adding new endpoint

This commit is contained in:
kdeng00
2023-10-06 22:18:23 -04:00
parent 4accff050c
commit 421488c120
6 changed files with 169 additions and 10 deletions
-1
View File
@@ -18,7 +18,6 @@ public class ContactManagement
#region Methods
// TODO: Need to implement
// Queues the contact file to be processed latter
public ContactCreationQueue QueueContact(IFormFile file)
{
@@ -32,12 +32,12 @@ public class ContactQueueCreationController : ControllerBase
public IActionResult Upload([FromForm(Name = "file")] List<IFormFile> contactFile,
[FromForm(Name = "user_id")] string userID)
{
// TODO: Save the file on the filesystem to be processed by a separate
// Save the file on the filesystem to be processed by a separate
// system
var response = new CreateQueueResponse();
var connString = this._config.GetConnectionString("MongoDBURI");
var userRepo = new UsersRepository(connString!);
var queueRepo = new ContactCreationQueueepository(connString!);
var userRepo = new UsersRepository(connString!);
var queueRepo = new ContactCreationQueueRepository(connString!);
try
{
+86
View File
@@ -0,0 +1,86 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using TextSender_API.Models;
using TextSender_API.Repositories;
namespace TextSender_API.Controllers;
[Authorize]
[ApiController]
[Route("api/v1/contact")]
public class ContactsController : ControllerBase
{
#region Fields
private IConfiguration _config;
private readonly ILogger<ContactsController> _logger;
#endregion
#region Constructors
public ContactsController(ILogger<ContactsController> logger, IConfiguration config)
{
this._logger = logger;
this._config = config;
}
#endregion
#region Methods
[HttpPost("new"), DisableRequestSizeLimit]
public IActionResult CreateContact([FromBody] Contact contact)
{
var response = new CreateContactResponse();
var connString = this._config.GetConnectionString("MongoDBURI");
var userRepo = new UsersRepository(connString!);
var contactsRepo = new ContactsRepository(connString!);
try
{
var contacts = contactsRepo.RetrieveAll();
if (contacts.Exists(c => c.PhoneNumber!.Equals(contact.PhoneNumber)) ||
string.IsNullOrEmpty(contact.UserID))
{
// The Contact already exists
return BadRequest(response);
}
var user = new User { Id = contact.UserID };
if (!userRepo.Exists(user).Item1)
{
return BadRequest(response);
}
contactsRepo.Create(contact);
response.Data.Add(contact);
}
catch (Exception ex)
{
this._logger.LogError($"An error occurred: {ex.Message}");
}
return Ok(response);
}
#endregion
#region Responses
public class CreateContactResponse
{
#region Properties
[JsonProperty("data")]
public List<Contact> Data { get; set; }
#endregion
#region Constructors
public CreateContactResponse()
{
this.Data = new List<Contact>();
}
#endregion
}
#endregion
}