diff --git a/Controllers/ContactManagement.cs b/Controllers/ContactManagement.cs index 59d1464..36f0630 100644 --- a/Controllers/ContactManagement.cs +++ b/Controllers/ContactManagement.cs @@ -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) { diff --git a/Controllers/ContactQueueCreationController.cs b/Controllers/ContactQueueCreationController.cs index 1711a93..4f9ae4a 100644 --- a/Controllers/ContactQueueCreationController.cs +++ b/Controllers/ContactQueueCreationController.cs @@ -32,12 +32,12 @@ public class ContactQueueCreationController : ControllerBase public IActionResult Upload([FromForm(Name = "file")] List 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 { diff --git a/Controllers/ContactsController.cs b/Controllers/ContactsController.cs new file mode 100644 index 0000000..ccb3e00 --- /dev/null +++ b/Controllers/ContactsController.cs @@ -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 _logger; + #endregion + + + #region Constructors + + public ContactsController(ILogger 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 Data { get; set; } + #endregion + + #region Constructors + public CreateContactResponse() + { + this.Data = new List(); + } + #endregion + } + #endregion +} diff --git a/Models/Contact.cs b/Models/Contact.cs index 23a9470..a658a26 100644 --- a/Models/Contact.cs +++ b/Models/Contact.cs @@ -8,12 +8,20 @@ namespace TextSender_API.Models; public class Contact { #region Properties - public int ContactID { get; set; } + [BsonId] + [BsonRepresentation(BsonType.ObjectId)] + public string? Id { get; set; } + [BsonElement("firstname")] public string? Firstname { get; set; } + [BsonElement("lastname")] public string? Lastname { get; set; } + [BsonElement("phonenumber")] public string? PhoneNumber { get; set; } + [BsonElement("date_created")] public DateTime DateCreated { get; set; } - public int UserID { get; set; } + // [BsonRepresentation(BsonType.ObjectId)] + [BsonElement("user_id")] + public string? UserID { get; set; } #endregion } diff --git a/Program.cs b/Program.cs index dcd5257..66aca82 100644 --- a/Program.cs +++ b/Program.cs @@ -6,6 +6,8 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.IdentityModel.Tokens; +using TextSender_API.Repositories; + var builder = WebApplication.CreateBuilder(args); // Add services to the container. @@ -33,9 +35,10 @@ builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJw }; }); -var app = builder.Build(); +// var connectionString = builder.Configuration.GetConnectionString("MongoDBURI"); -var connString = builder.Configuration.GetConnectionString("DefaultConnection"); + +var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) diff --git a/Repositories/ContactRepository.cs b/Repositories/ContactRepository.cs index 9d68112..b4d9ceb 100644 --- a/Repositories/ContactRepository.cs +++ b/Repositories/ContactRepository.cs @@ -5,7 +5,7 @@ using TextSender_API.Models; namespace TextSender_API.Repositories; -public class ContactCreationQueueepository : BaseRepository +public class ContactCreationQueueRepository : BaseRepository { #region Fields private readonly IMongoCollection _bookCollection; @@ -13,7 +13,7 @@ public class ContactCreationQueueepository : BaseRepository #region Constructors - public ContactCreationQueueepository(string connectionString) + public ContactCreationQueueRepository(string connectionString) { this._connectionString = connectionString; this._tableName = "contactCreationQueue"; @@ -66,3 +66,66 @@ public class ContactCreationQueueepository : BaseRepository } #endregion } + + +public class ContactsRepository : BaseRepository +{ + #region Fields + private readonly IMongoCollection _bookCollection; + #endregion + + + #region Constructors + public ContactsRepository(string connectionString) + { + this._connectionString = connectionString; + this._tableName = "contacts"; + var client = this.InitializeClient(); + this._bookCollection = client.GetDatabase(this._databaseName).GetCollection(this._tableName); + } + #endregion + + #region Metods + public Tuple Exists(string id) + { + var allContacts= this.RetrieveAll(); + var result = allContacts.FirstOrDefault(c => c.Id!.Equals(id)); + + if (result != null) + { + return new Tuple(true, result!); + } + else + { + return new Tuple(false, new Contact()); + } + } + + public void Create(Contact contact) + { + this._bookCollection.InsertOne(contact); + } + + public List RetrieveAll() + { + return this._bookCollection.Find(_ => true).ToList(); + } + + public void Update(Contact contact) + { + var filter = Builders.Filter.Eq(c => c.Id, contact.Id); + this._bookCollection.ReplaceOne(filter, contact); + } + + public void Delete(Contact contact) + { + var filter = Builders.Filter.Eq(c => c.Id, contact.Id); + this._bookCollection.DeleteOne(filter); + } + + private MongoClient InitializeClient() + { + return new MongoClient(this._connectionString); + } + #endregion +} \ No newline at end of file