Browse Source
* replace NewtonSoft with System.Text.Json * not use auth for brande and types and use GetFromJsonAsync * fix * fix * Auth HttpGet more simple. * Put, Delete and Post more simple. * Fixed Edit for remove image and keep image and change other fields. Added description required in Blazor Admin. * Removed using not used * Refactor AuthService and introduce HttpService. add validation for price. * return null in HttpService if not success. * Limt Price to 1000 mximum * DI for Blazor Services * one blazor service. * fixmain
committed by
GitHub
24 changed files with 164 additions and 190 deletions
@ -1,31 +1,19 @@ |
|||||
using System.Net; |
using System.Threading.Tasks; |
||||
using System.Threading.Tasks; |
|
||||
using Newtonsoft.Json; |
|
||||
|
|
||||
namespace BlazorAdmin.Services.CatalogItemServices |
namespace BlazorAdmin.Services.CatalogItemServices |
||||
{ |
{ |
||||
public class Create |
public class Create |
||||
{ |
{ |
||||
private readonly AuthService _authService; |
private readonly HttpService _httpService; |
||||
|
|
||||
public Create(AuthService authService) |
public Create(AuthService authService) |
||||
{ |
{ |
||||
_authService = authService; |
_httpService = new HttpService(authService.GetHttpClient(), authService.ApiUrl); |
||||
} |
} |
||||
|
|
||||
public async Task<CatalogItem> HandleAsync(CreateCatalogItemRequest catalogItem) |
public async Task<CatalogItem> HandleAsync(CreateCatalogItemRequest catalogItem) |
||||
{ |
{ |
||||
var catalogItemResult = new CatalogItem(); |
return (await _httpService.HttpPost<CreateCatalogItemResult>("catalog-items", catalogItem)).CatalogItem; |
||||
|
|
||||
var result = await _authService.HttpPost("catalog-items", catalogItem); |
|
||||
if (result.StatusCode != HttpStatusCode.OK) |
|
||||
{ |
|
||||
return catalogItemResult; |
|
||||
} |
|
||||
|
|
||||
catalogItemResult = JsonConvert.DeserializeObject<CreateCatalogItemResult>(await result.Content.ReadAsStringAsync()).CatalogItem; |
|
||||
|
|
||||
return catalogItemResult; |
|
||||
} |
} |
||||
} |
} |
||||
} |
} |
||||
|
|||||
@ -1,31 +1,19 @@ |
|||||
using System.Net; |
using System.Threading.Tasks; |
||||
using System.Threading.Tasks; |
|
||||
using Newtonsoft.Json; |
|
||||
|
|
||||
namespace BlazorAdmin.Services.CatalogItemServices |
namespace BlazorAdmin.Services.CatalogItemServices |
||||
{ |
{ |
||||
public class Delete |
public class Delete |
||||
{ |
{ |
||||
private readonly AuthService _authService; |
private readonly HttpService _httpService; |
||||
|
|
||||
public Delete(AuthService authService) |
public Delete(AuthService authService) |
||||
{ |
{ |
||||
_authService = authService; |
_httpService = new HttpService(authService.GetHttpClient(), authService.ApiUrl); |
||||
} |
} |
||||
|
|
||||
public async Task<string> HandleAsync(int catalogItemId) |
public async Task<string> HandleAsync(int catalogItemId) |
||||
{ |
{ |
||||
var catalogItemResult = string.Empty; |
return (await _httpService.HttpDelete<DeleteCatalogItemResult>("catalog-items", catalogItemId)).Status; |
||||
|
|
||||
var result = await _authService.HttpDelete("catalog-items", catalogItemId); |
|
||||
if (result.StatusCode != HttpStatusCode.OK) |
|
||||
{ |
|
||||
return catalogItemResult; |
|
||||
} |
|
||||
|
|
||||
catalogItemResult = JsonConvert.DeserializeObject<DeleteCatalogItemResult>(await result.Content.ReadAsStringAsync()).Status; |
|
||||
|
|
||||
return catalogItemResult; |
|
||||
} |
} |
||||
} |
} |
||||
} |
} |
||||
|
|||||
@ -1,31 +1,19 @@ |
|||||
using System.Net; |
using System.Threading.Tasks; |
||||
using System.Threading.Tasks; |
|
||||
using Newtonsoft.Json; |
|
||||
|
|
||||
namespace BlazorAdmin.Services.CatalogItemServices |
namespace BlazorAdmin.Services.CatalogItemServices |
||||
{ |
{ |
||||
public class Edit |
public class Edit |
||||
{ |
{ |
||||
private readonly AuthService _authService; |
private readonly HttpService _httpService; |
||||
|
|
||||
public Edit(AuthService authService) |
public Edit(AuthService authService) |
||||
{ |
{ |
||||
_authService = authService; |
_httpService = new HttpService(authService.GetHttpClient(), authService.ApiUrl); |
||||
} |
} |
||||
|
|
||||
public async Task<CatalogItem> HandleAsync(CatalogItem catalogItem) |
public async Task<CatalogItem> HandleAsync(CatalogItem catalogItem) |
||||
{ |
{ |
||||
var catalogItemResult = new CatalogItem(); |
return (await _httpService.HttpPut<EditCatalogItemResult>("catalog-items", catalogItem)).CatalogItem; |
||||
|
|
||||
var result = await _authService.HttpPut("catalog-items", catalogItem); |
|
||||
if (result.StatusCode != HttpStatusCode.OK) |
|
||||
{ |
|
||||
return catalogItemResult; |
|
||||
} |
|
||||
|
|
||||
catalogItemResult = JsonConvert.DeserializeObject<EditCatalogItemResult>(await result.Content.ReadAsStringAsync()).CatalogItem; |
|
||||
|
|
||||
return catalogItemResult; |
|
||||
} |
} |
||||
} |
} |
||||
} |
} |
||||
|
|||||
@ -1,31 +1,19 @@ |
|||||
using System.Net; |
using System.Threading.Tasks; |
||||
using System.Threading.Tasks; |
|
||||
using Newtonsoft.Json; |
|
||||
|
|
||||
namespace BlazorAdmin.Services.CatalogItemServices |
namespace BlazorAdmin.Services.CatalogItemServices |
||||
{ |
{ |
||||
public class GetById |
public class GetById |
||||
{ |
{ |
||||
private readonly AuthService _authService; |
private readonly HttpService _httpService; |
||||
|
|
||||
public GetById(AuthService authService) |
public GetById(AuthService authService) |
||||
{ |
{ |
||||
_authService = authService; |
_httpService = new HttpService(authService.GetHttpClient(), authService.ApiUrl); |
||||
} |
} |
||||
|
|
||||
public async Task<CatalogItem> HandleAsync(int catalogItemId) |
public async Task<CatalogItem> HandleAsync(int catalogItemId) |
||||
{ |
{ |
||||
var catalogItemResult = new CatalogItem(); |
return (await _httpService.HttpGet<EditCatalogItemResult>($"catalog-items/{catalogItemId}")).CatalogItem; |
||||
|
|
||||
var result = await _authService.HttpGet($"catalog-items/{catalogItemId}"); |
|
||||
if (result.StatusCode != HttpStatusCode.OK) |
|
||||
{ |
|
||||
return catalogItemResult; |
|
||||
} |
|
||||
|
|
||||
catalogItemResult = JsonConvert.DeserializeObject<EditCatalogItemResult>(await result.Content.ReadAsStringAsync()).CatalogItem; |
|
||||
|
|
||||
return catalogItemResult; |
|
||||
} |
} |
||||
} |
} |
||||
} |
} |
||||
|
|||||
@ -1,32 +1,20 @@ |
|||||
using System.Collections.Generic; |
using System.Collections.Generic; |
||||
using System.Net; |
|
||||
using System.Threading.Tasks; |
using System.Threading.Tasks; |
||||
using Newtonsoft.Json; |
|
||||
|
|
||||
namespace BlazorAdmin.Services.CatalogItemServices |
namespace BlazorAdmin.Services.CatalogItemServices |
||||
{ |
{ |
||||
public class ListPaged |
public class ListPaged |
||||
{ |
{ |
||||
private readonly AuthService _authService; |
private readonly HttpService _httpService; |
||||
|
|
||||
public ListPaged(AuthService authService) |
public ListPaged(AuthService authService) |
||||
{ |
{ |
||||
_authService = authService; |
_httpService = new HttpService(authService.GetHttpClient(), authService.ApiUrl); |
||||
} |
} |
||||
|
|
||||
public async Task<List<CatalogItem>> HandleAsync(int pageSize) |
public async Task<List<CatalogItem>> HandleAsync(int pageSize) |
||||
{ |
{ |
||||
var catalogItems = new List<CatalogItem>(); |
return (await _httpService.HttpGet<PagedCatalogItemResult>($"catalog-items?PageSize={pageSize}")).CatalogItems; |
||||
|
|
||||
var result = await _authService.HttpGet($"catalog-items?PageSize={pageSize}"); |
|
||||
if (result.StatusCode != HttpStatusCode.OK) |
|
||||
{ |
|
||||
return catalogItems; |
|
||||
} |
|
||||
|
|
||||
catalogItems = JsonConvert.DeserializeObject<PagedCatalogItemResult>(await result.Content.ReadAsStringAsync()).CatalogItems; |
|
||||
|
|
||||
return catalogItems; |
|
||||
} |
} |
||||
|
|
||||
} |
} |
||||
|
|||||
@ -0,0 +1,85 @@ |
|||||
|
using System.Net.Http; |
||||
|
using System.Text; |
||||
|
using System.Text.Json; |
||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace BlazorAdmin.Services |
||||
|
{ |
||||
|
public class HttpService |
||||
|
{ |
||||
|
private readonly HttpClient _httpClient; |
||||
|
private readonly string _apiUrl; |
||||
|
|
||||
|
public HttpService(HttpClient httpClient, string apiUrl) |
||||
|
{ |
||||
|
_httpClient = httpClient; |
||||
|
_apiUrl = apiUrl; |
||||
|
} |
||||
|
|
||||
|
public async Task<T> HttpGet<T>(string uri) |
||||
|
where T : class |
||||
|
{ |
||||
|
var result = await _httpClient.GetAsync($"{_apiUrl}{uri}"); |
||||
|
if (!result.IsSuccessStatusCode) |
||||
|
{ |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
return await FromHttpResponseMessage<T>(result); |
||||
|
} |
||||
|
|
||||
|
public async Task<T> HttpDelete<T>(string uri, int id) |
||||
|
where T : class |
||||
|
{ |
||||
|
var result = await _httpClient.DeleteAsync($"{_apiUrl}{uri}/{id}"); |
||||
|
if (!result.IsSuccessStatusCode) |
||||
|
{ |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
return await FromHttpResponseMessage<T>(result); |
||||
|
} |
||||
|
|
||||
|
public async Task<T> HttpPost<T>(string uri, object dataToSend) |
||||
|
where T : class |
||||
|
{ |
||||
|
var content = ToJson(dataToSend); |
||||
|
|
||||
|
var result = await _httpClient.PostAsync($"{_apiUrl}{uri}", content); |
||||
|
if (!result.IsSuccessStatusCode) |
||||
|
{ |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
return await FromHttpResponseMessage<T>(result); |
||||
|
} |
||||
|
|
||||
|
public async Task<T> HttpPut<T>(string uri, object dataToSend) |
||||
|
where T : class |
||||
|
{ |
||||
|
var content = ToJson(dataToSend); |
||||
|
|
||||
|
var result = await _httpClient.PutAsync($"{_apiUrl}{uri}", content); |
||||
|
if (!result.IsSuccessStatusCode) |
||||
|
{ |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
return await FromHttpResponseMessage<T>(result); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
private StringContent ToJson(object obj) |
||||
|
{ |
||||
|
return new StringContent(JsonSerializer.Serialize(obj), Encoding.UTF8, "application/json"); |
||||
|
} |
||||
|
|
||||
|
private async Task<T> FromHttpResponseMessage<T>(HttpResponseMessage result) |
||||
|
{ |
||||
|
return JsonSerializer.Deserialize<T>(await result.Content.ReadAsStringAsync(), new JsonSerializerOptions |
||||
|
{ |
||||
|
PropertyNameCaseInsensitive = true |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,22 @@ |
|||||
|
using BlazorAdmin.Services.CatalogItemServices; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
|
||||
|
namespace BlazorAdmin |
||||
|
{ |
||||
|
public static class ServicesConfiguration |
||||
|
{ |
||||
|
public static IServiceCollection AddBlazorServices(this IServiceCollection service) |
||||
|
{ |
||||
|
service.AddScoped<Create>(); |
||||
|
service.AddScoped<ListPaged>(); |
||||
|
service.AddScoped<Delete>(); |
||||
|
service.AddScoped<Edit>(); |
||||
|
service.AddScoped<GetById>(); |
||||
|
|
||||
|
service.AddScoped<BlazorAdmin.Services.CatalogBrandServices.List>(); |
||||
|
service.AddScoped<BlazorAdmin.Services.CatalogTypeServices.List>(); |
||||
|
|
||||
|
return service; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue