162 lines
4.4 KiB
C#
162 lines
4.4 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")]
|
|
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();
|
|
var users = userRepo.RetrieveAllUsers();
|
|
|
|
if (users.Count() == 0)
|
|
{
|
|
return BadRequest(response);
|
|
}
|
|
|
|
if (contacts.Count() > 0)
|
|
{
|
|
if (contacts.Exists(c => c.PhoneNumber!.Equals(contact.PhoneNumber)) ||
|
|
string.IsNullOrEmpty(contact.UserID))
|
|
{
|
|
// The Contact already exists
|
|
return BadRequest(response);
|
|
}
|
|
}
|
|
|
|
if (!userRepo.Exists(contact.UserID!).Item1)
|
|
{
|
|
return BadRequest(response);
|
|
}
|
|
|
|
contact.DateCreated = DateTime.Now;
|
|
contactsRepo.Create(contact);
|
|
response.Data.Add(contact);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this._logger.LogError($"An error occurred: {ex.Message}");
|
|
}
|
|
|
|
return Ok(response);
|
|
}
|
|
|
|
[HttpPatch("update"), DisableRequestSizeLimit]
|
|
public IActionResult UpdateContact([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();
|
|
var users = userRepo.RetrieveAllUsers();
|
|
|
|
if (users.Count() == 0 || contacts.Count() == 0)
|
|
{
|
|
return BadRequest(response);
|
|
}
|
|
|
|
var c = contacts.FirstOrDefault(c => c.Id!.Equals(contact.Id));
|
|
|
|
if (c == null ||
|
|
string.IsNullOrEmpty(contact.UserID))
|
|
{
|
|
// The Contact does not exists or no user id provided
|
|
return BadRequest(response);
|
|
}
|
|
|
|
if (!userRepo.Exists(contact.UserID!).Item1)
|
|
{
|
|
return BadRequest(response);
|
|
}
|
|
|
|
if (!c.UserID!.Equals(contact.UserID))
|
|
{
|
|
// The provided user id does is not the same that created the contact
|
|
return BadRequest(response);
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(contact.Firstname))
|
|
{
|
|
c.Firstname = contact.Firstname;
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(contact.Lastname))
|
|
{
|
|
c.Lastname = contact.Lastname;
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(contact.PhoneNumber))
|
|
{
|
|
if (!contacts.Exists(cc => cc.PhoneNumber!.Equals(contact.PhoneNumber)))
|
|
{
|
|
c.PhoneNumber = contact.PhoneNumber;
|
|
}
|
|
}
|
|
|
|
contactsRepo.Update(c);
|
|
response.Data.Add(c);
|
|
}
|
|
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
|
|
}
|