Spaces:
Sleeping
Sleeping
File size: 7,199 Bytes
6f7ef5c aa120c6 6f7ef5c f85b485 6f7ef5c aa120c6 ac85ccd 6f7ef5c f85b485 6f7ef5c aa120c6 6f7ef5c 19d90a0 66906f8 aa120c6 66906f8 19d90a0 79899b2 aa120c6 79899b2 aa120c6 79899b2 aa120c6 79899b2 aa120c6 6f7ef5c 19d90a0 f85b485 19d90a0 f85b485 19d90a0 f85b485 6f7ef5c aa120c6 19d90a0 79899b2 6f7ef5c aa120c6 79899b2 aa120c6 79899b2 aa120c6 6f7ef5c aa120c6 79899b2 aa120c6 6f7ef5c 19d90a0 aa120c6 19d90a0 f85b485 aa120c6 f85b485 aa120c6 f85b485 aa120c6 f85b485 aa120c6 6f7ef5c 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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | using Microsoft.AspNetCore.Mvc;
using System.Text;
namespace RankingApp.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class ItemController : ControllerBase
{
private readonly HttpClient _httpClient;
private const string PHP_API_BASE = "http://127.0.0.1:8000";
public ItemController(HttpClient httpClient)
{
_httpClient = httpClient;
}
// Removed .php from decorator
[HttpGet("read")]
public async Task<IActionResult> Read([FromQuery] int? item_type, [FromQuery] int? tier_list_id)
{
try
{
var queryParts = new List<string>();
if (item_type.HasValue)
{
queryParts.Add($"item_type={item_type.Value}");
}
if (tier_list_id.HasValue)
{
queryParts.Add($"tier_list_id={tier_list_id.Value}");
}
var queryParams = queryParts.Count > 0 ? $"?{string.Join("&", queryParts)}" : "";
// Pointed to tierapp
var url = $"{PHP_API_BASE}/tierapp/item/read.php{queryParams}";
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 });
}
}
// Removed .php from decorator
[HttpPost("create")]
public async Task<IActionResult> Create()
{
try
{
var requestContent = new MultipartFormDataContent();
foreach (var formKey in Request.Form.Keys)
{
requestContent.Add(new StringContent(Request.Form[formKey].ToString()), formKey);
}
foreach (var file in Request.Form.Files)
{
var streamContent = new StreamContent(file.OpenReadStream());
streamContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(file.ContentType);
requestContent.Add(streamContent, "image", file.FileName);
}
// Pointed to tierapp
var url = $"{PHP_API_BASE}/tierapp/item/create.php";
Console.WriteLine($"Attempting to call PHP API: {url}");
var response = await _httpClient.PostAsync(url, requestContent);
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 });
}
}
// Removed .php from decorator
[HttpPost("update")]
public async Task<IActionResult> Update([FromBody] object data)
{
try
{
var content = new StringContent(
System.Text.Json.JsonSerializer.Serialize(data),
Encoding.UTF8,
"application/json");
// Pointed to tierapp
var url = $"{PHP_API_BASE}/tierapp/item/update.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 });
}
}
// Removed .php from decorator
[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");
// Pointed to tierapp
var url = $"{PHP_API_BASE}/tierapp/item/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 });
}
}
}
} |