Created
May 3, 2024 15:06
-
-
Save andrebaltieri/4fef28c046bea6ef5251834d56b99fc4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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