68 lines
2.1 KiB
C#
68 lines
2.1 KiB
C#
using GradeBookApp.Dtos.Group;
|
|
using GradeBookApp.Dtos.Student;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Json;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace GradeBookApp.Services
|
|
{
|
|
public class GroupApiService
|
|
{
|
|
private readonly HttpClient _httpClient;
|
|
|
|
public GroupApiService(IHttpClientFactory factory)
|
|
{
|
|
_httpClient = factory.CreateClient("AuthorizedClient");
|
|
}
|
|
|
|
public async Task<List<GroupNameDto>?> GetAllAsync()
|
|
{
|
|
var response = await _httpClient.GetAsync("api/groups");
|
|
if (!response.IsSuccessStatusCode)
|
|
return null;
|
|
|
|
return await response.Content.ReadFromJsonAsync<List<GroupNameDto>>();
|
|
}
|
|
|
|
public async Task<GroupReadDto?> GetByIdAsync(int id)
|
|
{
|
|
var response = await _httpClient.GetAsync($"api/groups/{id}");
|
|
if (!response.IsSuccessStatusCode)
|
|
return null;
|
|
|
|
return await response.Content.ReadFromJsonAsync<GroupReadDto>();
|
|
}
|
|
|
|
public async Task<IEnumerable<GroupReadDto>?> GetByDirectionIdAsync(int directionId)
|
|
{
|
|
var response = await _httpClient.GetAsync($"api/groups/by-direction/{directionId}");
|
|
if (!response.IsSuccessStatusCode)
|
|
return null;
|
|
|
|
return await response.Content.ReadFromJsonAsync<IEnumerable<GroupReadDto>>();
|
|
}
|
|
|
|
public async Task<bool> CreateAsync(GroupCreateDto dto)
|
|
{
|
|
var response = await _httpClient.PostAsJsonAsync("api/groups", dto);
|
|
return response.IsSuccessStatusCode;
|
|
}
|
|
|
|
public async Task<bool> UpdateAsync(int id, GroupCreateDto dto)
|
|
{
|
|
var response = await _httpClient.PutAsJsonAsync($"api/groups/{id}", dto);
|
|
return response.IsSuccessStatusCode;
|
|
}
|
|
|
|
public async Task<bool> DeleteAsync(int id)
|
|
{
|
|
var response = await _httpClient.DeleteAsync($"api/groups/{id}");
|
|
return response.IsSuccessStatusCode;
|
|
}
|
|
}
|
|
}
|