diff --git a/Controllers/ContactsController.cs b/Controllers/ContactsController.cs index ccb3e00..9d492d8 100644 --- a/Controllers/ContactsController.cs +++ b/Controllers/ContactsController.cs @@ -39,21 +39,29 @@ public class ContactsController : ControllerBase try { var contacts = contactsRepo.RetrieveAll(); + var users = userRepo.RetrieveAllUsers(); - 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) + 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); } diff --git a/Models/Contact.cs b/Models/Contact.cs index a658a26..72da9a0 100644 --- a/Models/Contact.cs +++ b/Models/Contact.cs @@ -2,6 +2,7 @@ using System; using MongoDB.Bson; using MongoDB.Bson.Serialization.Attributes; +using Newtonsoft.Json; namespace TextSender_API.Models; @@ -10,17 +11,23 @@ public class Contact #region Properties [BsonId] [BsonRepresentation(BsonType.ObjectId)] + [JsonProperty("id")] public string? Id { get; set; } [BsonElement("firstname")] + [JsonProperty("firstname")] public string? Firstname { get; set; } [BsonElement("lastname")] + [JsonProperty("lastname")] public string? Lastname { get; set; } [BsonElement("phonenumber")] + [JsonProperty("phonenumber")] public string? PhoneNumber { get; set; } [BsonElement("date_created")] + [JsonProperty("date_created")] public DateTime DateCreated { get; set; } // [BsonRepresentation(BsonType.ObjectId)] [BsonElement("user_id")] + [JsonProperty("user_id")] public string? UserID { get; set; } #endregion }