db test
This commit is contained in:
@@ -1,11 +1,64 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace file_finder__test.DataBaseModules{
|
||||
|
||||
//Alloufi Yazan
|
||||
public class DataBase
|
||||
//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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
37
file finder test/Modules/DataBaseModules/mediaUpdate.cs
Normal file
37
file finder test/Modules/DataBaseModules/mediaUpdate.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using System.Text.Json;
|
||||
|
||||
namespace file_finder__test.DataBaseModules
|
||||
{
|
||||
public class mediaUpdate
|
||||
{
|
||||
public int Uid { get; set; }
|
||||
public string MovieName { get; set; }
|
||||
|
||||
DataBase tmp = new DataBase("http//:localhost//file_finder/mediaUpdate");
|
||||
public string Json { get; private set; }
|
||||
public mediaUpdate(int uid, string movieName)
|
||||
{
|
||||
Uid = uid;
|
||||
MovieName = movieName;
|
||||
|
||||
// Serialize to JSON
|
||||
Json = JsonSerializer.Serialize(new
|
||||
{
|
||||
uid = Uid,
|
||||
movieName = MovieName
|
||||
});
|
||||
Console.WriteLine(Json);
|
||||
using var db = new DataBase("https://yourserver.com/api");
|
||||
string response = db.MovieWatchIncrement(Json).Result;
|
||||
|
||||
if(response.Contains("302"))
|
||||
Console.WriteLine("db not found");
|
||||
else
|
||||
{
|
||||
Console.WriteLine(response);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user