Refactoring and adding new endpoint
This commit is contained in:
@@ -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
|
||||
{
|
||||
|
||||
@@ -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
|
||||
}
|
||||
+10
-2
@@ -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
|
||||
}
|
||||
|
||||
|
||||
+5
-2
@@ -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())
|
||||
|
||||
@@ -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<ContactCreationQueue> _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<Contact> _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<Contact>(this._tableName);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Metods
|
||||
public Tuple<bool, Contact> Exists(string id)
|
||||
{
|
||||
var allContacts= this.RetrieveAll();
|
||||
var result = allContacts.FirstOrDefault(c => c.Id!.Equals(id));
|
||||
|
||||
if (result != null)
|
||||
{
|
||||
return new Tuple<bool, Contact>(true, result!);
|
||||
}
|
||||
else
|
||||
{
|
||||
return new Tuple<bool, Contact>(false, new Contact());
|
||||
}
|
||||
}
|
||||
|
||||
public void Create(Contact contact)
|
||||
{
|
||||
this._bookCollection.InsertOne(contact);
|
||||
}
|
||||
|
||||
public List<Contact> RetrieveAll()
|
||||
{
|
||||
return this._bookCollection.Find(_ => true).ToList();
|
||||
}
|
||||
|
||||
public void Update(Contact contact)
|
||||
{
|
||||
var filter = Builders<Contact>.Filter.Eq(c => c.Id, contact.Id);
|
||||
this._bookCollection.ReplaceOne(filter, contact);
|
||||
}
|
||||
|
||||
public void Delete(Contact contact)
|
||||
{
|
||||
var filter = Builders<Contact>.Filter.Eq(c => c.Id, contact.Id);
|
||||
this._bookCollection.DeleteOne(filter);
|
||||
}
|
||||
|
||||
private MongoClient InitializeClient()
|
||||
{
|
||||
return new MongoClient(this._connectionString);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
Reference in New Issue
Block a user