Skip to content

Instantly share code, notes, and snippets.

@andrebaltieri
Created May 3, 2024 15:06
Show Gist options
  • Save andrebaltieri/4fef28c046bea6ef5251834d56b99fc4 to your computer and use it in GitHub Desktop.
Save andrebaltieri/4fef28c046bea6ef5251834d56b99fc4 to your computer and use it in GitHub Desktop.
using RestSharp;
using FinaFlow.Core.Entities;
namespace FinaFlow.Services;
public class CategoryService
{
private readonly RestClient _client;
public CategoryService(IHttpClientFactory httpClientFactory)
{
var httpClient = httpClientFactory.CreateClient(Configuration.HttpClientName);
_client = new RestClient(httpClient);
}
public async Task CreateAsync(Category category)
{
var request = new RestRequest("v1/categories/", Method.Post).AddJsonBody(category);
await _client.ExecuteAsync(request);
}
public async Task UpdateAsync(uint id, Category category)
{
var request = new RestRequest($"v1/categories/{id}", Method.Put).AddJsonBody(category);
await _client.ExecuteAsync(request);
}
public async Task<List<Category>> GetAsync()
{
var request = new RestRequest("v1/categories", Method.Get);
var response = await _client.ExecuteAsync<List<Category>>(request);
return response.Data ?? new List<Category>();
}
public async Task<Category?> GetByIdAsync(uint id)
{
var request = new RestRequest($"v1/categories/{id}", Method.Get);
var response = await _client.ExecuteAsync<Category>(request);
return response.Data;
}
public async Task DeleteAsync(uint id)
{
var request = new RestRequest($"v1/categories/{id}", Method.Delete);
await _client.ExecuteAsync(request);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment