Skip to content

Commit

Permalink
Merge pull request #774 from bounswe/mobile/comment-controller-endpoints
Browse files Browse the repository at this point in the history
Modified CommentController & ForumPostComments
  • Loading branch information
said-yolcu authored Nov 21, 2023
2 parents 01d4428 + cfb9c3a commit 8c6f6b2
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 37 deletions.
56 changes: 26 additions & 30 deletions app/mobile/Assets/Scripts/Controllers/CommentController.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -46,6 +44,4 @@ public class Comment
public string lastEditedAt;
public int overallVote;
public int voteCount;
}


}
91 changes: 84 additions & 7 deletions app/mobile/Assets/Scripts/PostRequests/ForumPostComments.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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" +
Expand All @@ -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();
}
}

0 comments on commit 8c6f6b2

Please sign in to comment.