Sergey Karmanov 0fbeb6160a
All checks were successful
Create and publish a Docker image / build-and-push-image (push) Successful in 29s
Добавил глобальный поиск
2023-07-30 14:21:27 +03:00

68 lines
2.3 KiB
C#

using System.Text.Json;
using PaydayFrontend.Models;
namespace PaydayFrontend.Services;
public interface IUniversityService
{
public Task<List<University>> GetAllUniversity();
public Task<List<Direction>> GetDirectionsByUniversityId(long universityId);
public Task<University> GetUniversityById(long id);
public Task<Direction> GetDirectionsById(long directionId);
}
public class UniversityService : IUniversityService
{
private readonly HttpClient _httpClient;
public UniversityService(HttpClient httpClient)
{
_httpClient = httpClient;
_httpClient.BaseAddress = new Uri("https://payday.zetcraft.ru");
}
public async Task<List<University>> GetAllUniversity()
{
var response = await _httpClient.GetAsync("v1/public/university");
var result = await response.Content.ReadAsStringAsync();
var university = JsonSerializer.Deserialize<List<University>>(result, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
return university;
}
public async Task<List<Direction>> GetDirectionsByUniversityId(long universityId)
{
var response = await _httpClient.GetAsync($"v1/public/university/{universityId}/directions");
var result = await response.Content.ReadAsStringAsync();
var directions = JsonSerializer.Deserialize<List<Direction>>(result, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
return directions;
}
public async Task<Direction> GetDirectionsById(long directionId)
{
var response = await _httpClient.GetAsync("v1/public/direction/" + directionId);
var result = await response.Content.ReadAsStringAsync();
var direction = JsonSerializer.Deserialize<Direction>(result, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
return direction;
}
public async Task<University> GetUniversityById(long id)
{
var response = await _httpClient.GetAsync("v1/public/university/" + id);
var result = await response.Content.ReadAsStringAsync();
var university = JsonSerializer.Deserialize<University>(result, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
return university;
}
}