diff --git a/Controllers/ContactManagement.cs b/Controllers/ContactManagement.cs index eed03b2..59d1464 100644 --- a/Controllers/ContactManagement.cs +++ b/Controllers/ContactManagement.cs @@ -1,3 +1,5 @@ +using TextSender_API.Models; + namespace TextSender_API.Controllers; public class ContactManagement @@ -18,14 +20,22 @@ public class ContactManagement #region Methods // TODO: Need to implement // 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"]; - 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 - return; + return queue; } var length = 16; @@ -35,18 +45,24 @@ public class ContactManagement s[random.Next(s.Length)]).ToArray()); var extension = ".txt"; - var contactPath = $"{filename}{extension}"; + var contactPath = $"{directory}/{filename}{extension}"; + queue.Filepath = contactPath; if (System.IO.File.Exists(contactPath)) { // Already exists - return; + queue.Status = "File exists"; + return queue; } + queue.Status = "Created"; + using (var filestream = new FileStream(contactPath, FileMode.Create)) { file.CopyTo(filestream); } + + return queue; } #endregion } \ No newline at end of file diff --git a/Controllers/ContactQueueCreationController.cs b/Controllers/ContactQueueCreationController.cs index 11d6a36..1711a93 100644 --- a/Controllers/ContactQueueCreationController.cs +++ b/Controllers/ContactQueueCreationController.cs @@ -1,5 +1,9 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using Newtonsoft.Json; + +using TextSender_API.Models; +using TextSender_API.Repositories; 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 // system + var response = new CreateQueueResponse(); + var connString = this._config.GetConnectionString("MongoDBURI"); + var userRepo = new UsersRepository(connString!); + var queueRepo = new ContactCreationQueueepository(connString!); try { var ctMgr = new ContactManagement(this._config); + var user = userRepo.Exists(userID); + + if (!user.Item1) + { + return NotFound(response); + } 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) { + 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 Data { get; set; } + #endregion + + #region Constructors + public CreateQueueResponse() + { + this.Data = new List(); + } + #endregion } #endregion } \ No newline at end of file diff --git a/Repositories/ContactRepository.cs b/Repositories/ContactRepository.cs new file mode 100644 index 0000000..9d68112 --- /dev/null +++ b/Repositories/ContactRepository.cs @@ -0,0 +1,68 @@ +using MongoDB.Driver; + +using TextSender_API.Models; + +namespace TextSender_API.Repositories; + + +public class ContactCreationQueueepository : BaseRepository +{ + #region Fields + private readonly IMongoCollection _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(this._tableName); + } + #endregion + + #region Metods + public Tuple Exists(string id) + { + var allQueue = this.RetrieveAll(); + var result = allQueue.FirstOrDefault(q => q.Id!.Equals(id)); + + if (result != null) + { + return new Tuple(true, result!); + } + else + { + return new Tuple(false, new ContactCreationQueue()); + } + } + + public void Create(ContactCreationQueue queue) + { + this._bookCollection.InsertOne(queue); + } + + public List RetrieveAll() + { + return this._bookCollection.Find(_ => true).ToList(); + } + + public void Update(ContactCreationQueue queue) + { + var filter = Builders.Filter.Eq(q => q.Id, queue.Id); + this._bookCollection.ReplaceOne(filter, queue); + } + + public void Delete(ContactCreationQueue queue) + { + var filter = Builders.Filter.Eq(q => q.Id, queue.Id); + this._bookCollection.DeleteOne(filter); + } + + private MongoClient InitializeClient() + { + return new MongoClient(this._connectionString); + } + #endregion +} diff --git a/Repositories/UsersRepository.cs b/Repositories/UsersRepository.cs index 9f47561..c9a86a1 100644 --- a/Repositories/UsersRepository.cs +++ b/Repositories/UsersRepository.cs @@ -37,6 +37,22 @@ public class UsersRepository : BaseRepository return new Tuple(false, new User()); } } + + public Tuple Exists(string userId) + { + var allUsers = this.RetrieveAllUsers(); + var result = allUsers.FirstOrDefault(ea => ea.Id!.Equals(userId)); + + if (result != null) + { + return new Tuple(true, result!); + } + else + { + return new Tuple(false, new User()); + } + } + public void CreateUser(User user) { this._bookCollection.InsertOne(user);