Skip to content

Commit

Permalink
Shady nagy/remove newton soft (dotnet-architecture#436)
Browse files Browse the repository at this point in the history
* 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.

* fix
  • Loading branch information
ShadyNagy authored Jul 29, 2020
1 parent 24cf9be commit b640926
Show file tree
Hide file tree
Showing 24 changed files with 164 additions and 190 deletions.
1 change: 0 additions & 1 deletion src/BlazorAdmin/BlazorAdmin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Build" Version="3.2.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="3.2.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.Extensions.Identity.Core" Version="3.1.5" />
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
<PackageReference Include="System.Net.Http.Json" Version="3.2.0" />
</ItemGroup>

Expand Down
4 changes: 1 addition & 3 deletions src/BlazorAdmin/Helpers/BlazorLayoutComponent.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Threading.Tasks;
using BlazorAdmin.Services;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components;

namespace BlazorAdmin.Helpers
{
Expand Down
7 changes: 1 addition & 6 deletions src/BlazorAdmin/JavaScript/JSInteropConstants.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BlazorAdmin.JavaScript
namespace BlazorAdmin.JavaScript
{
public static class JSInteropConstants
{
Expand Down
3 changes: 3 additions & 0 deletions src/BlazorAdmin/Pages/CatalogItemPage/List.razor
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
@page "/admin"
@attribute [Authorize(Roles = BlazorShared.Authorization.Constants.Roles.ADMINISTRATORS)]
@inject AuthService Auth
@inject BlazorAdmin.Services.CatalogItemServices.ListPaged CatalogItemListPaged
@inject BlazorAdmin.Services.CatalogTypeServices.List TypeList
@inject BlazorAdmin.Services.CatalogBrandServices.List BrandList
@inherits BlazorAdmin.Helpers.BlazorComponent
@namespace BlazorAdmin.Pages.CatalogItemPage

Expand Down
10 changes: 5 additions & 5 deletions src/BlazorAdmin/Pages/CatalogItemPage/List.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
catalogItems = await new BlazorAdmin.Services.CatalogItemServices.ListPaged(Auth).HandleAsync(50);
catalogTypes = await new BlazorAdmin.Services.CatalogTypeServices.List(Auth).HandleAsync();
catalogBrands = await new BlazorAdmin.Services.CatalogBrandServices.List(Auth).HandleAsync();
catalogItems = await CatalogItemListPaged.HandleAsync(50);
catalogTypes = await TypeList.HandleAsync();
catalogBrands = await BrandList.HandleAsync();

CallRequestRefresh();
}
Expand All @@ -37,9 +37,9 @@ private async void DetailsClick(int id)
await DetailsComponent.Open(id);
}

private void CreateClick()
private async Task CreateClick()
{
CreateComponent.Open();
await CreateComponent.Open();
}

private async Task EditClick(int id)
Expand Down
2 changes: 2 additions & 0 deletions src/BlazorAdmin/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public static async Task Main(string[] args)
builder.Services.AddSingleton<AuthenticationStateProvider, CustomAuthStateProvider>();
builder.Services.AddSingleton(sp => (CustomAuthStateProvider)sp.GetRequiredService<AuthenticationStateProvider>());

builder.Services.AddBlazorServices();

await builder.Build().RunAsync();
}
}
Expand Down
34 changes: 3 additions & 31 deletions src/BlazorAdmin/Services/AuthService.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text;
using System.Threading.Tasks;
using BlazorAdmin.JavaScript;
using Blazored.LocalStorage;
using Microsoft.JSInterop;
using Newtonsoft.Json;
using BlazorShared.Authorization;

namespace BlazorAdmin.Services
Expand All @@ -16,12 +14,10 @@ public class AuthService
private readonly HttpClient _httpClient;
private readonly ILocalStorageService _localStorage;
private readonly IJSRuntime _jSRuntime;
private static bool InDocker { get; set; }

public string ApiUrl => Constants.GetApiUrl(InDocker);
public string WebUrl => Constants.GetWebUrl(InDocker);

private static bool InDocker { get; set; }

public bool IsLoggedIn { get; set; }
public string UserName { get; set; }

Expand All @@ -32,28 +28,9 @@ public AuthService(HttpClient httpClient, ILocalStorageService localStorage, IJS
_jSRuntime = jSRuntime;
}

public async Task<HttpResponseMessage> HttpGet(string uri)
{
return await _httpClient.GetAsync($"{ApiUrl}{uri}");
}

public async Task<HttpResponseMessage> HttpDelete(string uri, int id)
{
return await _httpClient.DeleteAsync($"{ApiUrl}{uri}/{id}");
}

public async Task<HttpResponseMessage> HttpPost(string uri, object dataToSend)
public HttpClient GetHttpClient()
{
var content = ToJson(dataToSend);

return await _httpClient.PostAsync($"{ApiUrl}{uri}", content);
}

public async Task<HttpResponseMessage> HttpPut(string uri, object dataToSend)
{
var content = ToJson(dataToSend);

return await _httpClient.PutAsync($"{ApiUrl}{uri}", content);
return _httpClient;
}

public async Task Logout()
Expand Down Expand Up @@ -108,11 +85,6 @@ public async Task<bool> GetInDocker()
return (await _localStorage.GetItemAsync<string>("inDocker")).ToLower() == "true";
}

private StringContent ToJson(object obj)
{
return new StringContent(JsonConvert.SerializeObject(obj), Encoding.UTF8, "application/json");
}

private async Task LogoutIdentityManager()
{
await _httpClient.PostAsync("Identity/Account/Logout", null);
Expand Down
32 changes: 6 additions & 26 deletions src/BlazorAdmin/Services/CatalogBrandServices/List.cs
Original file line number Diff line number Diff line change
@@ -1,45 +1,25 @@
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
using Newtonsoft.Json;

namespace BlazorAdmin.Services.CatalogBrandServices
{
public class List
{
private readonly AuthService _authService;
private readonly HttpClient _httpClient;

public List(AuthService authService)
public List(AuthService authService, HttpClient httpClient)
{
_authService = authService;
_httpClient = httpClient;
}

public async Task<List<CatalogBrand>> HandleAsync()
{
var brands = new List<CatalogBrand>();
if (!_authService.IsLoggedIn)
{
return brands;
}

try
{
var result = await _authService.HttpGet("catalog-brands");
if (result.StatusCode != HttpStatusCode.OK)
{
return brands;
}

brands = JsonConvert.DeserializeObject<CatalogBrandResult>(await result.Content.ReadAsStringAsync()).CatalogBrands;
}
catch (AccessTokenNotAvailableException)
{
return brands;
}

return brands;
return (await _httpClient.GetFromJsonAsync<CatalogBrandResult>($"{_authService.ApiUrl}catalog-brands"))?.CatalogBrands;
}

public static string GetBrandName(IEnumerable<CatalogBrand> brands, int brandId)
Expand Down
3 changes: 2 additions & 1 deletion src/BlazorAdmin/Services/CatalogItemServices/CatalogItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ public class CatalogItem
[Required(ErrorMessage = "The Name field is required")]
public string Name { get; set; }

[Required(ErrorMessage = "The Description field is required")]
public string Description { get; set; }

// decimal(18,2)
[RegularExpression(@"^\d+(\.\d{0,2})*$", ErrorMessage = "The field Price must be a positive number with maximum two decimals.")]
[Range(0, 9999999999999999.99)]
[Range(0.01, 1000)]
[DataType(DataType.Currency)]
public decimal Price { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ public class CreateCatalogItemRequest
[Required(ErrorMessage = "The Name field is required")]
public string Name { get; set; } = string.Empty;

[Required(ErrorMessage = "The Description field is required")]
public string Description { get; set; } = string.Empty;

// decimal(18,2)
[RegularExpression(@"^\d+(\.\d{0,2})*$", ErrorMessage = "The field Price must be a positive number with maximum two decimals.")]
[Range(0, 9999999999999999.99)]
[Range(0.01, 1000)]
[DataType(DataType.Currency)]
public decimal Price { get; set; } = 0;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BlazorAdmin.Services.CatalogItemServices
namespace BlazorAdmin.Services.CatalogItemServices
{
public class CreateCatalogItemResult
{
Expand Down
20 changes: 4 additions & 16 deletions src/BlazorAdmin/Services/CatalogItemServices/Create.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,19 @@
using System.Net;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Threading.Tasks;

namespace BlazorAdmin.Services.CatalogItemServices
{
public class Create
{
private readonly AuthService _authService;
private readonly HttpService _httpService;

public Create(AuthService authService)
{
_authService = authService;
_httpService = new HttpService(authService.GetHttpClient(), authService.ApiUrl);
}

public async Task<CatalogItem> HandleAsync(CreateCatalogItemRequest catalogItem)
{
var catalogItemResult = new 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;
return (await _httpService.HttpPost<CreateCatalogItemResult>("catalog-items", catalogItem)).CatalogItem;
}
}
}
20 changes: 4 additions & 16 deletions src/BlazorAdmin/Services/CatalogItemServices/Delete.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,19 @@
using System.Net;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Threading.Tasks;

namespace BlazorAdmin.Services.CatalogItemServices
{
public class Delete
{
private readonly AuthService _authService;
private readonly HttpService _httpService;

public Delete(AuthService authService)
{
_authService = authService;
_httpService = new HttpService(authService.GetHttpClient(), authService.ApiUrl);
}

public async Task<string> HandleAsync(int catalogItemId)
{
var catalogItemResult = string.Empty;

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;
return (await _httpService.HttpDelete<DeleteCatalogItemResult>("catalog-items", catalogItemId)).Status;
}
}
}
20 changes: 4 additions & 16 deletions src/BlazorAdmin/Services/CatalogItemServices/Edit.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,19 @@
using System.Net;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Threading.Tasks;

namespace BlazorAdmin.Services.CatalogItemServices
{
public class Edit
{
private readonly AuthService _authService;
private readonly HttpService _httpService;

public Edit(AuthService authService)
{
_authService = authService;
_httpService = new HttpService(authService.GetHttpClient(), authService.ApiUrl);
}

public async Task<CatalogItem> HandleAsync(CatalogItem catalogItem)
{
var catalogItemResult = new 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;
return (await _httpService.HttpPut<EditCatalogItemResult>("catalog-items", catalogItem)).CatalogItem;
}
}
}
20 changes: 4 additions & 16 deletions src/BlazorAdmin/Services/CatalogItemServices/GetById.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,19 @@
using System.Net;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Threading.Tasks;

namespace BlazorAdmin.Services.CatalogItemServices
{
public class GetById
{
private readonly AuthService _authService;
private readonly HttpService _httpService;

public GetById(AuthService authService)
{
_authService = authService;
_httpService = new HttpService(authService.GetHttpClient(), authService.ApiUrl);
}

public async Task<CatalogItem> HandleAsync(int catalogItemId)
{
var catalogItemResult = new 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;
return (await _httpService.HttpGet<EditCatalogItemResult>($"catalog-items/{catalogItemId}")).CatalogItem;
}
}
}
Loading

0 comments on commit b640926

Please sign in to comment.