Skip to content

Commit

Permalink
Merge pull request #773 from bounswe/mobile/user-controller-endpoints
Browse files Browse the repository at this point in the history
Created UserActions & Modified UserController & DeleteAccount
  • Loading branch information
said-yolcu authored Nov 21, 2023
2 parents 488b306 + ad0da75 commit 01d4428
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 47 deletions.
58 changes: 58 additions & 0 deletions app/mobile/Assets/Scripts/Controllers/UserActions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System.Collections;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
using Newtonsoft.Json;

public class UserActions : MonoBehaviour
{

public void ChangeUserRole(UserChangeRoleRequest request)
{
string url = AppVariables.HttpServerUrl + "/user/change-role";
string bodyJsonString = JsonConvert.SerializeObject(request);
StartCoroutine(Post(url, bodyJsonString));
}

public void GetAllUsers(UserGetAllRequest request)
{
string url = AppVariables.HttpServerUrl + "/user/get-all" +
ListToQueryParameters.ListToQueryParams(new[] {"id", "username", "isDeleted"},
new[] {request.id, request.username, request.isDeleted.ToString()});
StartCoroutine(Get(url));
}

public void DeleteUser(UserDeleteRequest request)
{
string url = AppVariables.HttpServerUrl + "/user/delete?id=" + request.id;
StartCoroutine(Delete(url));
}

IEnumerator Get(string url)
{
var request = new UnityWebRequest(url, "GET");
request.downloadHandler = new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
yield return request.SendWebRequest();
}

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");
yield return request.SendWebRequest();
// Handle the response
}

IEnumerator Delete(string url)
{
var request = new UnityWebRequest(url, "DELETE");
request.downloadHandler = new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
yield return request.SendWebRequest();
}

}
3 changes: 3 additions & 0 deletions app/mobile/Assets/Scripts/Controllers/UserActions.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 15 additions & 13 deletions app/mobile/Assets/Scripts/Controllers/UserController.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
public class UserController
{

}

// With the body send the parameters below:
// Query parameters:
// string id
public class UserChangeRoleRequest
{
public string id;
public string userRole;
}

// For UserGetAllRequest, do not sent any body but send the
// parameters below:
// Query parameters:
// string id, string username, bool isDeleted;
// The response is an array of User's
public class UserGetAllRequest
{
public string id;
public string username;
public bool isDeleted;
}

// For UserDeleteRequest, do not sent any body but send the
// parameters below:
// Query parameters:
// string id
public class UserGetAllResponse
{
public User[] users;
}

public class UserDeleteRequest
{
public string id;
}

public class User
{
Expand Down
53 changes: 19 additions & 34 deletions app/mobile/Assets/Scripts/DeleteAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ public class DeleteAccount : MonoBehaviour
[SerializeField] private Toggle checkBox;
[SerializeField] private TMP_Text infoText;


private void Awake()
{
deleteButton.onClick.AddListener(OnClickedDelete);
backButton.onClick.AddListener(OnClickedBack);
canvasManager = FindObjectOfType(typeof(CanvasManager)) as CanvasManager;
canvasManager = FindObjectOfType<CanvasManager>() as CanvasManager;
}

private void OnClickedDelete()
Expand All @@ -43,58 +42,44 @@ private void OnClickedDelete()
return;
}

string url = AppVariables.HttpServerUrl + "/user/delete" +
ListToQueryParameters.ListToQueryParams(
new[]{"id"}, new []{PersistenceManager.id});
/*
var deleteData = new UserDeleteRequest();
deleteData.id = PersistenceManager.id;
string bodyJsonString = JsonConvert.SerializeObject(deleteData);
*/

StartCoroutine(Delete(url));

var deleteRequest = new UserDeleteRequest { id = PersistenceManager.id };
StartCoroutine(DeleteUser(deleteRequest));
}
IEnumerator Delete(string url)

IEnumerator DeleteUser(UserDeleteRequest request)
{
// url += "?id=" + PersistenceManager.id;
var request = new UnityWebRequest(url, "DELETE");
request.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
yield return request.SendWebRequest();
var response = request.downloadHandler.text;

if (request.responseCode != 200 || response == null)
{
infoText.text = "Error: " + request.responseCode;
infoText.color = Color.red;
}
else if (response == "false")
string url = AppVariables.HttpServerUrl + "/user/delete?id=" + request.id;
var unityWebRequest = new UnityWebRequest(url, "DELETE");
unityWebRequest.downloadHandler = new DownloadHandlerBuffer();
unityWebRequest.SetRequestHeader("Content-Type", "application/json");
unityWebRequest.SetRequestHeader("Authorization", PersistenceManager.UserToken);

yield return unityWebRequest.SendWebRequest();

if (unityWebRequest.result != UnityWebRequest.Result.Success)
{
infoText.text = "Error: " + response;
infoText.text = "Error: " + unityWebRequest.error;
infoText.color = Color.red;
}
else
{
infoText.text = "Successfully Deleted";
infoText.color = Color.green;

DOVirtual.DelayedCall(2f, () =>
{
canvasManager.HideDeleteAccountPage();
canvasManager.ShowSignUpPage();
});

}
request.downloadHandler.Dispose();
request.uploadHandler.Dispose();

unityWebRequest.Dispose();
}

private void OnClickedBack()
{
canvasManager.HideDeleteAccountPage();
canvasManager.ShowHomePage();
}
}


}

0 comments on commit 01d4428

Please sign in to comment.