Untitled
unknown
plain_text
3 years ago
3.7 kB
14
Indexable
//AdminService
using System.Net.Http.Json;
using System.Text;
namespace FlFi.Common.Services
{
public class AdminService : IAdminService
{
private readonly MembershipHttpClient _http;
public AdminService(MembershipHttpClient http)
{
_http = http;
}
public async Task<List<TDto>> GetAsync<TDto>(string uri)
{
try
{
using HttpResponseMessage filmsresponse = await _http.Client.GetAsync(uri);
filmsresponse.EnsureSuccessStatusCode();
var result = JsonSerializer.Deserialize<List<TDto>>(await
filmsresponse.Content.ReadAsStringAsync(), new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
return result ?? new List<TDto>();
}
catch
{
throw;
}
}
public async Task<TDto> SingleAsync<TDto>(string uri)
{
try
{
using HttpResponseMessage filmsresponse = await _http.Client.GetAsync(uri);
filmsresponse.EnsureSuccessStatusCode();
var result = JsonSerializer.Deserialize<TDto>(await
filmsresponse.Content.ReadAsStringAsync(), new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
return result ?? default;
}
catch
{
throw;
}
}
public async Task CreateAsync<TDto>(string uri, TDto dto)
{
try
{
using StringContent jsonContent = new(
JsonSerializer.Serialize(dto),
Encoding.UTF8,
"application/json");
using HttpResponseMessage response = await _http.Client.PostAsync(uri, jsonContent); //"films", jsonContent);
response.EnsureSuccessStatusCode();
}
catch (Exception ex)
{
throw;
}
}
public async Task EditAsync<TDto>(string uri, TDto dto)
{
try
{
using StringContent jsonContent = new( //ska kanske vara som deleteasync (nedan)
JsonSerializer.Serialize(dto),
Encoding.UTF8,
"application/json");
using HttpResponseMessage response = await _http.Client.PutAsync(uri, jsonContent); //"films", jsonContent);
response.EnsureSuccessStatusCode();
}
catch (Exception ex)
{
throw;
}
}
public async Task DeleteAsync<TDto>(string uri)
{
try
{
using HttpResponseMessage response = await _http.Client.DeleteAsync(uri); //"films", jsonContent);
response.EnsureSuccessStatusCode();
}
catch (Exception ex)
{
throw;
}
}
public async Task DeleteReferenceAsync<TDto>(string uri, TDto dto)
{
try
{
var requestMessage = new HttpRequestMessage(HttpMethod.Delete, uri);
requestMessage.Content = JsonContent.Create(dto);
using var response = await _http.Client.SendAsync(requestMessage);
response.EnsureSuccessStatusCode();
requestMessage.Dispose();
}
catch (Exception ex)
{
throw;
}
}
}
}
Editor is loading...