diff --git a/app/mobile/Assets/Scripts/Controllers/CommentController.cs b/app/mobile/Assets/Scripts/Controllers/CommentController.cs index a9e43103..a3f932cd 100644 --- a/app/mobile/Assets/Scripts/Controllers/CommentController.cs +++ b/app/mobile/Assets/Scripts/Controllers/CommentController.cs @@ -1,38 +1,36 @@ public class CommentController { - -} + public class CommentReplyRequest + { + public string commentContent; + public string parentComment; + } -// Send Authorization as header parameter -// Response is Comment -public class CommentReplyRequest -{ - public string commentContent; - public string parentComment; -} + public class CommentReplyResponse : Comment { } + public class CommentEditRequest + { + public string id; + public string commentContent; + } -// Send id as query parameter and Authorization as -// header parameter -// Response is Comment -public class CommentEditRequest -{ - public string commentContent; -} + public class CommentEditResponse : Comment { } -// Send Authorization as header parameter -// Response is Comment -public class CommentCreateRequest -{ - public string commentContent; - public string post; -} + public class CommentCreateRequest + { + public string commentContent; + public string post; + } + + public class CommentCreateResponse : Comment { } + public class CommentDeleteRequest + { + public string id; + } -// For making a comment delete request, send no body, -// only specify id as a query parameter and Authorization -// as a header parameter -// Response is Comment + public class CommentDeleteResponse : Comment { } +} public class Comment { @@ -46,6 +44,4 @@ public class Comment public string lastEditedAt; public int overallVote; public int voteCount; -} - - +} \ No newline at end of file diff --git a/app/mobile/Assets/Scripts/PostRequests/ForumPostComments.cs b/app/mobile/Assets/Scripts/PostRequests/ForumPostComments.cs index 345578a1..2cd28aa7 100644 --- a/app/mobile/Assets/Scripts/PostRequests/ForumPostComments.cs +++ b/app/mobile/Assets/Scripts/PostRequests/ForumPostComments.cs @@ -1,9 +1,9 @@ -using System; using System.Collections; -using System.Collections.Generic; using System.Text; using UnityEngine; using UnityEngine.Networking; +using Newtonsoft.Json; +using static CommentController; public class ForumPostComments : MonoBehaviour { @@ -14,17 +14,16 @@ public class ForumPostComments : MonoBehaviour private void Start() { Init(new[] {"id"},new[] {"b73d132b-a3e3-4776-bbe6-01c2ce0d2d2c"}); - } public void Init(string[] pars, string[] vals) { - postID = ListToQueryParameters.GetValueOfParam( - pars, vals, "id" ); + postID = ListToQueryParameters.GetValueOfParam(pars, vals, "id"); - if (postID == "") + if (string.IsNullOrEmpty(postID)) { Debug.Log("Id must be specified"); + return; } string url = AppVariables.HttpServerUrl + "/post/get-post-comments" + @@ -51,5 +50,83 @@ IEnumerator Get(string url) } request.downloadHandler.Dispose(); } - + + + public void ReplyToComment(CommentReplyRequest commentReplyRequest) + { + string url = AppVariables.HttpServerUrl + "/comment/reply"; + string bodyJsonString = JsonConvert.SerializeObject(commentReplyRequest); + StartCoroutine(Post(url, bodyJsonString)); + } + + public void EditComment(CommentEditRequest commentEditRequest) + { + string url = AppVariables.HttpServerUrl + "/comment/edit"; + string bodyJsonString = JsonConvert.SerializeObject(commentEditRequest); + StartCoroutine(Put(url, bodyJsonString)); + } + + public void CreateComment(CommentCreateRequest commentCreateRequest) + { + string url = AppVariables.HttpServerUrl + "/comment/create"; + string bodyJsonString = JsonConvert.SerializeObject(commentCreateRequest); + StartCoroutine(Post(url, bodyJsonString)); + } + + public void DeleteComment(string commentId) + { + string url = AppVariables.HttpServerUrl + "/comment/delete?id=" + commentId; + StartCoroutine(Delete(url)); + } + + IEnumerator Post(string url, string bodyJsonString) + { + var request = new UnityWebRequest(url, "POST"); + byte[] bodyRaw = Encoding.UTF8.GetBytes(bodyJsonString); + request.uploadHandler = new UploadHandlerRaw(bodyRaw); + request.downloadHandler = new DownloadHandlerBuffer(); + request.SetRequestHeader("Content-Type", "application/json"); + request.SetRequestHeader("Authorization", PersistenceManager.UserToken); + + yield return request.SendWebRequest(); + HandleResponse(request); + } + + IEnumerator Put(string url, string bodyJsonString) + { + var request = new UnityWebRequest(url, "PUT"); + byte[] bodyRaw = Encoding.UTF8.GetBytes(bodyJsonString); + request.uploadHandler = new UploadHandlerRaw(bodyRaw); + request.downloadHandler = new DownloadHandlerBuffer(); + request.SetRequestHeader("Content-Type", "application/json"); + request.SetRequestHeader("Authorization", PersistenceManager.UserToken); + + yield return request.SendWebRequest(); + HandleResponse(request); + } + + IEnumerator Delete(string url) + { + var request = new UnityWebRequest(url, "DELETE"); + request.downloadHandler = new DownloadHandlerBuffer(); + request.SetRequestHeader("Content-Type", "application/json"); + request.SetRequestHeader("Authorization", PersistenceManager.UserToken); + + yield return request.SendWebRequest(); + HandleResponse(request); + } + + private void HandleResponse(UnityWebRequest request) + { + string response = request.downloadHandler.text; + if (request.responseCode == 200) + { + Debug.Log("Success: " + response); + } + else + { + Debug.LogError("Error: " + response); + } + request.downloadHandler.Dispose(); + } } \ No newline at end of file