Added functionality to create contact queue
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
using TextSender_API.Models;
|
||||||
|
|
||||||
namespace TextSender_API.Controllers;
|
namespace TextSender_API.Controllers;
|
||||||
|
|
||||||
public class ContactManagement
|
public class ContactManagement
|
||||||
@@ -18,14 +20,22 @@ public class ContactManagement
|
|||||||
#region Methods
|
#region Methods
|
||||||
// TODO: Need to implement
|
// TODO: Need to implement
|
||||||
// Queues the contact file to be processed latter
|
// Queues the contact file to be processed latter
|
||||||
public void QueueContact(IFormFile file)
|
public ContactCreationQueue QueueContact(IFormFile file)
|
||||||
{
|
{
|
||||||
|
var queue = new ContactCreationQueue();
|
||||||
|
queue.Processed = false;
|
||||||
var directory = this._config["Paths:ContactQueueDirectory"];
|
var directory = this._config["Paths:ContactQueueDirectory"];
|
||||||
|
|
||||||
if (!System.IO.File.Exists(directory))
|
if (directory!.ElementAt(directory!.Length - 1) != '/')
|
||||||
{
|
{
|
||||||
|
directory.Append('/');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!System.IO.Directory.Exists(directory))
|
||||||
|
{
|
||||||
|
queue.Status = "Missing directory";
|
||||||
// Directory does not exist
|
// Directory does not exist
|
||||||
return;
|
return queue;
|
||||||
}
|
}
|
||||||
|
|
||||||
var length = 16;
|
var length = 16;
|
||||||
@@ -35,18 +45,24 @@ public class ContactManagement
|
|||||||
s[random.Next(s.Length)]).ToArray());
|
s[random.Next(s.Length)]).ToArray());
|
||||||
var extension = ".txt";
|
var extension = ".txt";
|
||||||
|
|
||||||
var contactPath = $"{filename}{extension}";
|
var contactPath = $"{directory}/{filename}{extension}";
|
||||||
|
queue.Filepath = contactPath;
|
||||||
|
|
||||||
if (System.IO.File.Exists(contactPath))
|
if (System.IO.File.Exists(contactPath))
|
||||||
{
|
{
|
||||||
// Already exists
|
// Already exists
|
||||||
return;
|
queue.Status = "File exists";
|
||||||
|
return queue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
queue.Status = "Created";
|
||||||
|
|
||||||
using (var filestream = new FileStream(contactPath, FileMode.Create))
|
using (var filestream = new FileStream(contactPath, FileMode.Create))
|
||||||
{
|
{
|
||||||
file.CopyTo(filestream);
|
file.CopyTo(filestream);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return queue;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,9 @@
|
|||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
using TextSender_API.Models;
|
||||||
|
using TextSender_API.Repositories;
|
||||||
|
|
||||||
namespace TextSender_API.Controllers;
|
namespace TextSender_API.Controllers;
|
||||||
|
|
||||||
@@ -30,21 +34,55 @@ public class ContactQueueCreationController : ControllerBase
|
|||||||
{
|
{
|
||||||
// TODO: Save the file on the filesystem to be processed by a separate
|
// TODO: Save the file on the filesystem to be processed by a separate
|
||||||
// system
|
// system
|
||||||
|
var response = new CreateQueueResponse();
|
||||||
|
var connString = this._config.GetConnectionString("MongoDBURI");
|
||||||
|
var userRepo = new UsersRepository(connString!);
|
||||||
|
var queueRepo = new ContactCreationQueueepository(connString!);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var ctMgr = new ContactManagement(this._config);
|
var ctMgr = new ContactManagement(this._config);
|
||||||
|
var user = userRepo.Exists(userID);
|
||||||
|
|
||||||
|
if (!user.Item1)
|
||||||
|
{
|
||||||
|
return NotFound(response);
|
||||||
|
}
|
||||||
|
|
||||||
foreach (var file in contactFile)
|
foreach (var file in contactFile)
|
||||||
{
|
{
|
||||||
ctMgr.QueueContact(file);
|
var queue = ctMgr.QueueContact(file);
|
||||||
|
queue.UserId = user.Item2.Id;
|
||||||
|
queue.DateCreated = DateTime.Now;
|
||||||
|
queueRepo.Create(queue);
|
||||||
|
|
||||||
|
response.Data.Add(queue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
this._logger.LogError($"An error occurred: {ex.Message}");
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok();
|
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
|
#endregion
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
using MongoDB.Driver;
|
||||||
|
|
||||||
|
using TextSender_API.Models;
|
||||||
|
|
||||||
|
namespace TextSender_API.Repositories;
|
||||||
|
|
||||||
|
|
||||||
|
public class ContactCreationQueueepository : BaseRepository
|
||||||
|
{
|
||||||
|
#region Fields
|
||||||
|
private readonly IMongoCollection<ContactCreationQueue> _bookCollection;
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
#region Constructors
|
||||||
|
public ContactCreationQueueepository(string connectionString)
|
||||||
|
{
|
||||||
|
this._connectionString = connectionString;
|
||||||
|
this._tableName = "contactCreationQueue";
|
||||||
|
var client = this.InitializeClient();
|
||||||
|
this._bookCollection = client.GetDatabase(this._databaseName).GetCollection<ContactCreationQueue>(this._tableName);
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Metods
|
||||||
|
public Tuple<bool, ContactCreationQueue> Exists(string id)
|
||||||
|
{
|
||||||
|
var allQueue = this.RetrieveAll();
|
||||||
|
var result = allQueue.FirstOrDefault(q => q.Id!.Equals(id));
|
||||||
|
|
||||||
|
if (result != null)
|
||||||
|
{
|
||||||
|
return new Tuple<bool, ContactCreationQueue>(true, result!);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return new Tuple<bool, ContactCreationQueue>(false, new ContactCreationQueue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Create(ContactCreationQueue queue)
|
||||||
|
{
|
||||||
|
this._bookCollection.InsertOne(queue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<ContactCreationQueue> RetrieveAll()
|
||||||
|
{
|
||||||
|
return this._bookCollection.Find(_ => true).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Update(ContactCreationQueue queue)
|
||||||
|
{
|
||||||
|
var filter = Builders<ContactCreationQueue>.Filter.Eq(q => q.Id, queue.Id);
|
||||||
|
this._bookCollection.ReplaceOne(filter, queue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Delete(ContactCreationQueue queue)
|
||||||
|
{
|
||||||
|
var filter = Builders<ContactCreationQueue>.Filter.Eq(q => q.Id, queue.Id);
|
||||||
|
this._bookCollection.DeleteOne(filter);
|
||||||
|
}
|
||||||
|
|
||||||
|
private MongoClient InitializeClient()
|
||||||
|
{
|
||||||
|
return new MongoClient(this._connectionString);
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
@@ -37,6 +37,22 @@ public class UsersRepository : BaseRepository
|
|||||||
return new Tuple<bool, User>(false, new User());
|
return new Tuple<bool, User>(false, new User());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Tuple<bool, User> Exists(string userId)
|
||||||
|
{
|
||||||
|
var allUsers = this.RetrieveAllUsers();
|
||||||
|
var result = allUsers.FirstOrDefault(ea => ea.Id!.Equals(userId));
|
||||||
|
|
||||||
|
if (result != null)
|
||||||
|
{
|
||||||
|
return new Tuple<bool, User>(true, result!);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return new Tuple<bool, User>(false, new User());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void CreateUser(User user)
|
public void CreateUser(User user)
|
||||||
{
|
{
|
||||||
this._bookCollection.InsertOne(user);
|
this._bookCollection.InsertOne(user);
|
||||||
|
|||||||
Reference in New Issue
Block a user