Spaces:
Sleeping
Sleeping
File size: 4,664 Bytes
f85b485 ac85ccd f85b485 19d90a0 f85b485 19d90a0 f85b485 19d90a0 f85b485 19d90a0 335a394 f85b485 19d90a0 f85b485 19d90a0 f85b485 19d90a0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | using Microsoft.AspNetCore.Mvc;
using System.Text;
namespace RankingApp.Controllers
{
[ApiController]
[Route("api/tierlist")]
public class TierlistController : ControllerBase
{
private readonly HttpClient _httpClient;
private const string PHP_API_BASE = "http://127.0.0.1:8000";
public TierlistController(HttpClient httpClient)
{
_httpClient = httpClient;
}
[HttpGet("read")]
public async Task<IActionResult> Read()
{
try
{
// Updated to point to tierapp instead of api
var url = $"{PHP_API_BASE}/tierapp/tierlist/read.php";
Console.WriteLine($"Attempting to call PHP API: {url}");
var response = await _httpClient.GetAsync(url);
if (!response.IsSuccessStatusCode)
{
var errorContent = await response.Content.ReadAsStringAsync();
Console.WriteLine($"PHP API error response: {errorContent}");
return StatusCode((int)response.StatusCode,
new { success = false, message = $"PHP API returned status code {response.StatusCode}: {errorContent}" });
}
var content = await response.Content.ReadAsStringAsync();
return Content(content, "application/json");
}
catch (Exception ex)
{
Console.WriteLine($"Exception calling PHP API: {ex.Message}");
return StatusCode(500, new { success = false, message = ex.Message });
}
}
[HttpPost("create")]
public async Task<IActionResult> Create([FromBody] object data)
{
try
{
var content = new StringContent(
System.Text.Json.JsonSerializer.Serialize(data),
Encoding.UTF8,
"application/json");
// Updated to point to tierapp instead of api
var url = $"{PHP_API_BASE}/tierapp/tierlist/create.php";
Console.WriteLine($"Attempting to call PHP API: {url}");
var response = await _httpClient.PostAsync(url, content);
if (!response.IsSuccessStatusCode)
{
var errorContent = await response.Content.ReadAsStringAsync();
Console.WriteLine($"PHP API error response: {errorContent}");
return StatusCode((int)response.StatusCode,
new { success = false, message = $"PHP API returned status code {response.StatusCode}: {errorContent}" });
}
var responseContent = await response.Content.ReadAsStringAsync();
return Content(responseContent, "application/json");
}
catch (Exception ex)
{
Console.WriteLine($"Exception calling PHP API: {ex.Message}");
return StatusCode(500, new { success = false, message = ex.Message });
}
}
[HttpPost("delete")]
public async Task<IActionResult> Delete([FromBody] object data)
{
try
{
var content = new StringContent(
System.Text.Json.JsonSerializer.Serialize(data),
Encoding.UTF8,
"application/json");
// Updated to point to tierapp instead of api
var url = $"{PHP_API_BASE}/tierapp/tierlist/delete.php";
Console.WriteLine($"Attempting to call PHP API: {url}");
var response = await _httpClient.PostAsync(url, content);
if (!response.IsSuccessStatusCode)
{
var errorContent = await response.Content.ReadAsStringAsync();
Console.WriteLine($"PHP API error response: {errorContent}");
return StatusCode((int)response.StatusCode,
new { success = false, message = $"PHP API returned status code {response.StatusCode}: {errorContent}" });
}
var responseContent = await response.Content.ReadAsStringAsync();
return Content(responseContent, "application/json");
}
catch (Exception ex)
{
Console.WriteLine($"Exception calling PHP API: {ex.Message}");
return StatusCode(500, new { success = false, message = ex.Message });
}
}
}
} |