Untitled

 avatar
unknown
plain_text
2 years ago
3.7 kB
4
Indexable
{
    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.ReadAsStreamAsync(), 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
            {
                throw;
            }
        }

        public async Task EditAsync<TDto>(string uri, TDto dto)
        {
            try
            {
                using StringContent jsonContent = new(
                    JsonSerializer.Serialize(dto),
                    Encoding.UTF8,
                    "application/json");

                using HttpResponseMessage response = await _http.Client.PutAsync(uri, jsonContent); //"films", jsonContent);

                response.EnsureSuccessStatusCode();
            }
            catch
            {
                throw;
            }
        }

        public async Task DeleteAsync<TDto>(string uri)
        {
            try
            {
                using StringContent jsonContent = new(
                    JsonSerializer.Serialize(uri),
                    Encoding.UTF8,
                    "application/json");

                using HttpResponseMessage response = await _http.Client.DeleteAsync(uri); //"films", jsonContent);

                response.EnsureSuccessStatusCode();
            }
            catch
            {
                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...