using System.Net.Http.Json; using Tasks.Module.Models; namespace Tasks.Module.Services; public interface ITodoService { Task> GetAllTodosAsync(TodoFilterDto? filter = null); Task GetTodoByIdAsync(string id); Task CreateTodoAsync(CreateTodoItemDto createDto); Task UpdateTodoAsync(UpdateTodoItemDto updateDto); Task DeleteTodoAsync(string id); Task ToggleCompleteAsync(string id); } public class TodoService : ITodoService { private readonly HttpClient _httpClient; public TodoService(HttpClient httpClient) { _httpClient = httpClient; } public async Task> GetAllTodosAsync(TodoFilterDto? filter = null) { var url = $"/api/v1/todos"; var queryParams = new List(); if (filter?.IsCompleted != null) queryParams.Add($"isCompleted={filter.IsCompleted}"); if (filter?.Priority != null) queryParams.Add($"priority={filter.Priority}"); if (filter?.DueDate != null) queryParams.Add($"dueDate={filter.DueDate:yyyy-MM-ddTHH:mm:ssZ}"); if (!string.IsNullOrEmpty(filter?.ParentId)) queryParams.Add($"parentId={filter.ParentId}"); if (queryParams.Any()) url += "?" + string.Join("&", queryParams); return await _httpClient.GetFromJsonAsync>(url) ?? new List(); } public async Task GetTodoByIdAsync(string id) { return await _httpClient.GetFromJsonAsync($"/api/v1/todos/{id}"); } public async Task CreateTodoAsync(CreateTodoItemDto createDto) { var response = await _httpClient.PostAsJsonAsync("/api/v1/todos", createDto); response.EnsureSuccessStatusCode(); return await response.Content.ReadFromJsonAsync() ?? throw new Exception("Failed to create todo"); } public async Task UpdateTodoAsync(UpdateTodoItemDto updateDto) { var response = await _httpClient.PutAsJsonAsync("/api/v1/todos", updateDto); if (!response.IsSuccessStatusCode) return null; return await response.Content.ReadFromJsonAsync(); } public async Task DeleteTodoAsync(string id) { var response = await _httpClient.DeleteAsync($"/api/v1/todos/{id}"); return response.IsSuccessStatusCode; } public async Task ToggleCompleteAsync(string id) { var response = await _httpClient.PatchAsync($"/api/v1/todos/{id}/toggle", null); if (!response.IsSuccessStatusCode) return null; return await response.Content.ReadFromJsonAsync(); } }