Skip to content

Instantly share code, notes, and snippets.

@andrebaltieri
Created May 3, 2024 15:01
Show Gist options
  • Save andrebaltieri/5cf0d70aa52b6624811c3cff03c62774 to your computer and use it in GitHub Desktop.
Save andrebaltieri/5cf0d70aa52b6624811c3cff03c62774 to your computer and use it in GitHub Desktop.
using System.Net.Http.Json;
using FinaFlow.Core.Entities;
namespace FinaFlow.Services;
public class CategoryService(
IHttpClientFactory httpClientFactory)
{
public async Task CreateAsync(
Category category)
{
var client = httpClientFactory.CreateClient(Configuration.HttpClientName);
await client.PostAsJsonAsync($"v1/categories/", category);
}
public async Task UpdateAsync(uint id, Category category)
{
var client = httpClientFactory.CreateClient(Configuration.HttpClientName);
await client.PutAsJsonAsync($"v1/categories/{id}", category);
}
public async Task<List<Category>> GetAsync()
{
var client = httpClientFactory.CreateClient(Configuration.HttpClientName);
return await client.GetFromJsonAsync<List<Category>>("v1/categories") ?? [];
}
public async Task<Category?> GetByIdAsync(uint id)
{
var client = httpClientFactory.CreateClient(Configuration.HttpClientName);
return await client.GetFromJsonAsync<Category>($"v1/categories/{id}") ?? null;
}
public async Task DeleteAsync(uint id)
{
var client = httpClientFactory.CreateClient(Configuration.HttpClientName);
await client.DeleteAsync($"v1/categories/{id}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment