71 lines
2.6 KiB
C#
71 lines
2.6 KiB
C#
using System.Net.Http.Json;
|
|
using Tasks.Module.Models;
|
|
|
|
namespace Tasks.Module.Services;
|
|
|
|
public interface IViewService
|
|
{
|
|
Task<List<TaskViewDto>> GetAllViewsAsync();
|
|
Task<TaskViewDto?> GetViewByIdAsync(string id);
|
|
Task<TaskViewDto> CreateViewAsync(CreateTaskViewDto createDto);
|
|
Task<TaskViewDto?> UpdateViewAsync(UpdateTaskViewDto updateDto);
|
|
Task<bool> DeleteViewAsync(string id);
|
|
Task<List<TodoItemDto>> GetTasksByViewAsync(string viewId);
|
|
Task<TodoItemDto> CreateTodoWithViewCriteriaAsync(string viewId, CreateTodoItemDto createDto);
|
|
}
|
|
|
|
public class ViewService : IViewService
|
|
{
|
|
private readonly HttpClient _httpClient;
|
|
|
|
public ViewService(HttpClient httpClient)
|
|
{
|
|
_httpClient = httpClient;
|
|
}
|
|
|
|
public async Task<List<TaskViewDto>> GetAllViewsAsync()
|
|
{
|
|
var response = await _httpClient.GetFromJsonAsync<List<TaskViewDto>>("/api/v1/views");
|
|
return response ?? new List<TaskViewDto>();
|
|
}
|
|
|
|
public async Task<TaskViewDto?> GetViewByIdAsync(string id)
|
|
{
|
|
return await _httpClient.GetFromJsonAsync<TaskViewDto>($"/api/v1/views/{id}");
|
|
}
|
|
|
|
public async Task<TaskViewDto> CreateViewAsync(CreateTaskViewDto createDto)
|
|
{
|
|
var response = await _httpClient.PostAsJsonAsync("/api/v1/views", createDto);
|
|
response.EnsureSuccessStatusCode();
|
|
return await response.Content.ReadFromJsonAsync<TaskViewDto>() ?? throw new Exception("Failed to create view");
|
|
}
|
|
|
|
public async Task<TaskViewDto?> UpdateViewAsync(UpdateTaskViewDto updateDto)
|
|
{
|
|
var response = await _httpClient.PutAsJsonAsync("/api/v1/views", updateDto);
|
|
if (!response.IsSuccessStatusCode)
|
|
return null;
|
|
return await response.Content.ReadFromJsonAsync<TaskViewDto>();
|
|
}
|
|
|
|
public async Task<bool> DeleteViewAsync(string id)
|
|
{
|
|
var response = await _httpClient.DeleteAsync($"/api/v1/views/{id}");
|
|
return response.IsSuccessStatusCode;
|
|
}
|
|
|
|
public async Task<List<TodoItemDto>> GetTasksByViewAsync(string viewId)
|
|
{
|
|
var response = await _httpClient.GetFromJsonAsync<List<TodoItemDto>>($"/api/v1/views/{viewId}/tasks");
|
|
return response ?? new List<TodoItemDto>();
|
|
}
|
|
|
|
public async Task<TodoItemDto> CreateTodoWithViewCriteriaAsync(string viewId, CreateTodoItemDto createDto)
|
|
{
|
|
var response = await _httpClient.PostAsJsonAsync($"/api/v1/views/{viewId}/tasks", createDto);
|
|
response.EnsureSuccessStatusCode();
|
|
return await response.Content.ReadFromJsonAsync<TodoItemDto>() ?? throw new Exception("Failed to create task");
|
|
}
|
|
}
|