88 lines
2.3 KiB
C#
88 lines
2.3 KiB
C#
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/queue")]
|
|
public class ContactQueueCreationController : ControllerBase
|
|
{
|
|
#region Fields
|
|
private IConfiguration _config;
|
|
private readonly ILogger<ContactQueueCreationController> _logger;
|
|
#endregion
|
|
|
|
|
|
#region Constructors
|
|
|
|
public ContactQueueCreationController(ILogger<ContactQueueCreationController> logger, IConfiguration config)
|
|
{
|
|
this._logger = logger;
|
|
this._config = config;
|
|
}
|
|
#endregion
|
|
|
|
#region Methods
|
|
[HttpPost("upload"), DisableRequestSizeLimit]
|
|
public IActionResult Upload([FromForm(Name = "file")] List<IFormFile> contactFile,
|
|
[FromForm(Name = "user_id")] string userID)
|
|
{
|
|
// 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 ContactCreationQueueRepository(connString!);
|
|
|
|
try
|
|
{
|
|
var ctMgr = new ContactManagement(this._config);
|
|
var user = userRepo.Exists(userID);
|
|
|
|
if (!user.Item1)
|
|
{
|
|
return NotFound(response);
|
|
}
|
|
|
|
foreach (var file in contactFile)
|
|
{
|
|
var queue = ctMgr.QueueContact(file);
|
|
queue.UserId = user.Item2.Id;
|
|
queue.DateCreated = DateTime.Now;
|
|
queueRepo.Create(queue);
|
|
|
|
response.Data.Add(queue);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this._logger.LogError($"An error occurred: {ex.Message}");
|
|
}
|
|
|
|
return Ok(response);
|
|
}
|
|
#endregion
|
|
|
|
|
|
#region Responses
|
|
public class CreateQueueResponse
|
|
{
|
|
#region Properties
|
|
[JsonProperty("data")]
|
|
public List<ContactCreationQueue> Data { get; set; }
|
|
#endregion
|
|
|
|
#region Constructors
|
|
public CreateQueueResponse()
|
|
{
|
|
this.Data = new List<ContactCreationQueue>();
|
|
}
|
|
#endregion
|
|
}
|
|
#endregion
|
|
} |