diff --git a/web-api/Controllers/ContactsController.cs b/web-api/Controllers/ContactsController.cs index 76c8debe..b476a3c3 100644 --- a/web-api/Controllers/ContactsController.cs +++ b/web-api/Controllers/ContactsController.cs @@ -12,6 +12,12 @@ public class ContactsController : ControllerBase { private readonly IUsersService _usersService; private readonly IChatsService _chatsService; + private readonly HttpClient _httpClient = new HttpClient(); + + private readonly JsonSerializerOptions _jsonSerializerOptions = new JsonSerializerOptions + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase + }; public ContactsController(IUsersService usersService, IChatsService chatsService) { @@ -73,10 +79,10 @@ public IActionResult SignUp([FromBody] JsonElement body) } // Create new user - User newUser = new User(username, name, "localhost", password, profilePicture); + var newUser = new User(username, name, "localhost", password, profilePicture); _usersService.Add(newUser); - return Ok(); + return Created("", null); } @@ -91,8 +97,8 @@ public IActionResult Get() return NotFound(); } - List contacsIds = currentUser.Chats.Keys.ToList(); - return Ok(_usersService.Get(contacsIds)); + List contactsIds = currentUser.Chats.Keys.ToList(); + return Ok(_usersService.Get(contactsIds)); } [HttpPost] @@ -165,10 +171,14 @@ public IActionResult Post([FromBody] JsonElement body) } else { - // TODO: add new chat to contact on a remote server + // Send an invitation to the contact on the remote server + var invitation = new Invitation(currentUser.Username, contact.Username, "localhost"); + var json = JsonSerializer.Serialize(invitation, _jsonSerializerOptions); + var stringContent = new StringContent(json, System.Text.Encoding.UTF8, "application/json"); + _httpClient.PostAsync("https://" + contact.Server + "/api/invitations", stringContent).Wait(); } - return Ok(); + return Created("", null); } [HttpGet("{id}")] @@ -264,7 +274,7 @@ public IActionResult Put(string id, [FromBody] JsonElement body) return NotFound(); } - return Ok(); + return NoContent(); } [HttpDelete("{id}")] @@ -303,7 +313,7 @@ public IActionResult Delete(string id) // Delete the currentUser from the contact contact.Chats.Remove(currentUser.Username); _usersService.Update(contact); - return Ok(); + return NoContent(); } [HttpGet("{id}/messages")] @@ -383,11 +393,14 @@ public IActionResult PostNewMessage(string id, [FromBody] string content) // Add the new message to the contact if on a remote server if (contact.Server != "localhost") { - // TODO: add new message to contact on a remote server // Send transfer request + var transfer = new Transfer(currentUser.Username, contact.Username, content); + var json = JsonSerializer.Serialize(transfer, _jsonSerializerOptions); + var stringContent = new StringContent(json, System.Text.Encoding.UTF8, "application/json"); + _httpClient.PostAsync("https://" + contact.Server + "/api/transfer", stringContent).Wait(); } - return Ok(); + return Created("", null); } [HttpGet("{id}/messages/{id2}")] @@ -468,13 +481,9 @@ public IActionResult PutMessage(string id, int id2, [FromBody] string content) // Update the chat Chat chat = currentUser.Chats[id]; _chatsService.Update(chat); - // Update the contact if on a remote server - if (contact.Server != "localhost") - { - // TODO: update message on a remote server - } + // No API exists for updating the contact if on a remote server - return Ok(); + return NoContent(); } [HttpDelete("{id}/messages/{id2}")] @@ -516,12 +525,7 @@ public IActionResult DeleteMessage(string id, int id2) Chat chat = currentUser.Chats[id]; chat.Messages.Remove(message); _chatsService.Update(chat); - // Delete the message from the contact if on a remote server - if (contact.Server != "localhost") - { - // TODO: delete message from contact on a remote server - } - - return Ok(); + // No API exists for deleting a message from the contact if on a remote server + return NoContent(); } } \ No newline at end of file diff --git a/web-api/Controllers/InvitationController.cs b/web-api/Controllers/InvitationController.cs index 67f3acc8..4276dab0 100644 --- a/web-api/Controllers/InvitationController.cs +++ b/web-api/Controllers/InvitationController.cs @@ -53,6 +53,6 @@ public IActionResult Post([FromBody] Invitation invitation) _usersService.Update(remoteUser); } - return Ok(); + return Created("", null); } } \ No newline at end of file diff --git a/web-api/Controllers/TransferController.cs b/web-api/Controllers/TransferController.cs index 40f70597..23bc0839 100644 --- a/web-api/Controllers/TransferController.cs +++ b/web-api/Controllers/TransferController.cs @@ -41,6 +41,6 @@ public IActionResult Post([FromBody] Transfer transfer) chat.Messages.Add(message); _chatsService.Update(chat); - return Ok(); + return Created("", null); } } \ No newline at end of file