64 lines
2.0 KiB
C#
64 lines
2.0 KiB
C#
using System;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace file_finder__test.DataBaseModules{
|
|
|
|
//Quinn
|
|
public class DataBase:IDisposable
|
|
{
|
|
private readonly HttpClient _client;
|
|
private readonly string _baseUrl;
|
|
|
|
public DataBase(string baseUrl)
|
|
{
|
|
_baseUrl = baseUrl.TrimEnd('/');
|
|
_client = new HttpClient();
|
|
}
|
|
|
|
// Core method to send JSON requests
|
|
private async Task<string> SendRequestAsync(string endpoint, object data)
|
|
{
|
|
string url = $"{_baseUrl}/{endpoint}";
|
|
string jsonData = System.Text.Json.JsonSerializer.Serialize(data);
|
|
|
|
var content = new StringContent(jsonData, Encoding.UTF8, "application/json");
|
|
HttpResponseMessage response = await _client.PostAsync(url, content);
|
|
|
|
response.EnsureSuccessStatusCode(); // throw if erro
|
|
return await response.Content.ReadAsStringAsync();
|
|
}
|
|
|
|
// User methods
|
|
public Task<string> UserSet(object userData)
|
|
=> SendRequestAsync("user/set.php", userData);
|
|
|
|
public Task<string> UserGet(object requestData)
|
|
=> SendRequestAsync("user/get.php", requestData);
|
|
|
|
public Task<string> UserCreate(object userData)
|
|
=> SendRequestAsync("user/create.php", userData);
|
|
|
|
// Media methods
|
|
public Task<string> MediaGet(object requestData)
|
|
=> SendRequestAsync("media/get.php", requestData);
|
|
|
|
public Task<string> MediaAdd(object mediaData)
|
|
=> SendRequestAsync("media/add.php", mediaData);
|
|
|
|
public Task<string> MediaRemove(object mediaData)
|
|
=> SendRequestAsync("media/remove.php", mediaData);
|
|
|
|
// update muvie watch counter
|
|
public Task<string> MovieWatchIncrement(object requestData)
|
|
=> SendRequestAsync("movie/watch.php", requestData);
|
|
|
|
|
|
public void Dispose()
|
|
{
|
|
_client.Dispose();
|
|
}
|
|
}
|
|
|
|
} |