From d078305d4fbcc84f36bc1faa8f696a06bb2a3daf Mon Sep 17 00:00:00 2001 From: "arthur.valin" Date: Sat, 31 Dec 2022 15:38:06 +0100 Subject: [PATCH] Adding DataApi Log and Debug logs --- src/CraftSharp/App.razor | 2 +- src/CraftSharp/Controllers/UserController.cs | 2 + src/CraftSharp/Pages/Add.razor.cs | 2 +- src/CraftSharp/Pages/Connexion.razor.cs | 4 -- src/CraftSharp/Pages/Counter.razor | 18 ------- src/CraftSharp/Pages/Edit.razor.cs | 2 +- src/CraftSharp/Pages/FetchData.razor | 48 ------------------- src/CraftSharp/Pages/Inscription.razor.cs | 5 -- src/CraftSharp/Pages/List.razor.cs | 6 ++- src/CraftSharp/Pages/Opening.razor.cs | 9 ++-- src/CraftSharp/Pages/Shop.razor.cs | 4 ++ src/CraftSharp/Program.cs | 2 +- src/CraftSharp/Services/AuthService.cs | 14 ++++++ .../Services/CustomStateProvider.cs | 12 +++-- src/CraftSharp/Services/DataApiService.cs | 9 +++- src/CraftSharp/Services/IAuthService.cs | 1 + .../Shared/CultureSelector.razor.cs | 4 ++ src/CraftSharp/Shared/HeaderLayout.razor.cs | 3 -- 18 files changed, 54 insertions(+), 93 deletions(-) delete mode 100644 src/CraftSharp/Pages/Counter.razor delete mode 100644 src/CraftSharp/Pages/FetchData.razor diff --git a/src/CraftSharp/App.razor b/src/CraftSharp/App.razor index 4a40c50..a525e9c 100644 --- a/src/CraftSharp/App.razor +++ b/src/CraftSharp/App.razor @@ -1,4 +1,5 @@ @using CraftSharp.Models; + @@ -14,4 +15,3 @@ - diff --git a/src/CraftSharp/Controllers/UserController.cs b/src/CraftSharp/Controllers/UserController.cs index ac31e2d..ffe7e17 100644 --- a/src/CraftSharp/Controllers/UserController.cs +++ b/src/CraftSharp/Controllers/UserController.cs @@ -1,4 +1,5 @@ using CraftSharp.Models; +using CraftSharp.Pages; using CraftSharp.Services; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Localization; @@ -12,6 +13,7 @@ namespace CraftSharp.Controllers [Microsoft.AspNetCore.Mvc.Route("[controller]/[action]")] public class UserController : Controller { + [HttpPost] public IActionResult SetUser([FromBody] String user) { diff --git a/src/CraftSharp/Pages/Add.razor.cs b/src/CraftSharp/Pages/Add.razor.cs index 28d5d8e..1b210b8 100644 --- a/src/CraftSharp/Pages/Add.razor.cs +++ b/src/CraftSharp/Pages/Add.razor.cs @@ -8,6 +8,7 @@ namespace CraftSharp.Pages { public partial class Add { + /// /// The default enchant categories. /// @@ -36,7 +37,6 @@ namespace CraftSharp.Pages private async void HandleValidSubmit() { await DataService.Add(itemModel); - NavigationManager.NavigateTo("list"); } diff --git a/src/CraftSharp/Pages/Connexion.razor.cs b/src/CraftSharp/Pages/Connexion.razor.cs index 0202cf6..4552c57 100644 --- a/src/CraftSharp/Pages/Connexion.razor.cs +++ b/src/CraftSharp/Pages/Connexion.razor.cs @@ -24,8 +24,6 @@ namespace CraftSharp.Pages [Inject] public HttpClient httpClient { get; set; } - [Inject] - public ILogger Logger { get; set; } private string error { get; set; } private ConnexionModel loginRequest { get; set; } = new ConnexionModel(); @@ -44,7 +42,6 @@ namespace CraftSharp.Pages try { await AuthStateProvider.Login(loginRequest); - Logger.Log(LogLevel.Information, $"Login : {loginRequest.UserName}"); var stringified = JsonConvert.SerializeObject(loginRequest); var response = await httpClient.PostAsJsonAsync($"{NavigationManager.BaseUri}User/SetUser", stringified); NavigationManager.NavigateTo("index"); @@ -52,7 +49,6 @@ namespace CraftSharp.Pages } catch (Exception ex) { - Logger.Log(LogLevel.Error, $"Login Failure : {ex}"); error = ex.Message; } } diff --git a/src/CraftSharp/Pages/Counter.razor b/src/CraftSharp/Pages/Counter.razor deleted file mode 100644 index ef23cb3..0000000 --- a/src/CraftSharp/Pages/Counter.razor +++ /dev/null @@ -1,18 +0,0 @@ -@page "/counter" - -Counter - -

Counter

- -

Current count: @currentCount

- - - -@code { - private int currentCount = 0; - - private void IncrementCount() - { - currentCount++; - } -} diff --git a/src/CraftSharp/Pages/Edit.razor.cs b/src/CraftSharp/Pages/Edit.razor.cs index 8ce6c2e..f0bd1b6 100644 --- a/src/CraftSharp/Pages/Edit.razor.cs +++ b/src/CraftSharp/Pages/Edit.razor.cs @@ -39,6 +39,7 @@ namespace CraftSharp.Pages [Inject] public IWebHostEnvironment WebHostEnvironment { get; set; } + protected override async Task OnInitializedAsync() { var item = await DataService.GetById(Id); @@ -52,7 +53,6 @@ namespace CraftSharp.Pages private async void HandleValidSubmit() { await DataService.Update(Id, itemModel); - NavigationManager.NavigateTo("list"); } diff --git a/src/CraftSharp/Pages/FetchData.razor b/src/CraftSharp/Pages/FetchData.razor deleted file mode 100644 index 41256c3..0000000 --- a/src/CraftSharp/Pages/FetchData.razor +++ /dev/null @@ -1,48 +0,0 @@ -@page "/fetchdata" - -Weather forecast - -@using CraftSharp.Data -@inject WeatherForecastService ForecastService - -

Weather forecast

- -

This component demonstrates fetching data from a service.

- -@if (forecasts == null) -{ -

Loading...

-} -else -{ - - - - - - - - - - - @foreach (var forecast in forecasts) - { - - - - - - - } - -
DateTemp. (C)Temp. (F)Summary
@forecast.Date.ToShortDateString()@forecast.TemperatureC@forecast.TemperatureF@forecast.Summary
-} - -@code { - private WeatherForecast[]? forecasts; - - protected override async Task OnInitializedAsync() - { - forecasts = await ForecastService.GetForecastAsync(DateTime.Now); - } -} diff --git a/src/CraftSharp/Pages/Inscription.razor.cs b/src/CraftSharp/Pages/Inscription.razor.cs index 627ff82..a3c523f 100644 --- a/src/CraftSharp/Pages/Inscription.razor.cs +++ b/src/CraftSharp/Pages/Inscription.razor.cs @@ -21,9 +21,6 @@ namespace CraftSharp.Pages [Inject] public HttpClient httpClient { get; set; } - [Inject] - public ILogger Logger { get; set; } - private string error { get; set; } private InscriptionModel registerRequest { get; set; } = new InscriptionModel(); @@ -31,7 +28,6 @@ namespace CraftSharp.Pages { if (AuthStateProvider.GetCurrentUser() != null && AuthStateProvider.GetCurrentUser().IsAuthenticated) { - Logger.Log(LogLevel.Information, $"Automatic login : {AuthStateProvider.GetCurrentUser().UserName}"); NavigationManager.NavigateTo("index"); } } @@ -40,7 +36,6 @@ namespace CraftSharp.Pages { await AuthStateProvider.Register(registerRequest); - Logger.Log(LogLevel.Information, $"Register : {registerRequest.UserName}"); var stringified = JsonConvert.SerializeObject(new ConnexionModel() { Password=registerRequest.Password, UserName=registerRequest.UserName} diff --git a/src/CraftSharp/Pages/List.razor.cs b/src/CraftSharp/Pages/List.razor.cs index 39aa62c..5ee762c 100644 --- a/src/CraftSharp/Pages/List.razor.cs +++ b/src/CraftSharp/Pages/List.razor.cs @@ -5,6 +5,7 @@ using Blazorise.DataGrid; using CraftSharp.Modals; using CraftSharp.Models; using CraftSharp.Services; +using CraftSharp.Shared; using Microsoft.AspNetCore.Components; using Microsoft.Extensions.Localization; @@ -34,10 +35,14 @@ namespace CraftSharp.Pages [Inject] public IWebHostEnvironment WebHostEnvironment { get; set; } + [Inject] + public ILogger Logger { get; set; } + private async Task OnReadData(DataGridReadDataEventArgs e) { if (e.CancellationToken.IsCancellationRequested) { + Logger.Log(LogLevel.Critical, $"Failed to get api data ! - {e}"); return; } @@ -59,7 +64,6 @@ namespace CraftSharp.Pages { return; } - await DataService.Delete(id); // Reload the page diff --git a/src/CraftSharp/Pages/Opening.razor.cs b/src/CraftSharp/Pages/Opening.razor.cs index 8140806..22917f1 100644 --- a/src/CraftSharp/Pages/Opening.razor.cs +++ b/src/CraftSharp/Pages/Opening.razor.cs @@ -29,14 +29,16 @@ namespace CraftSharp.Pages [Inject] public IStringLocalizer Localizer { get; set; } + [Inject] + public ILogger Logger { get; set; } + int totalItem; List items; protected override async Task OnInitializedAsync() { - totalItem = await DataService.Count(); - - items = await DataService.List(0, totalItem); + totalItem = await DataService.Count(); + items = await DataService.List(0, totalItem); } bool canOpen() @@ -59,7 +61,6 @@ namespace CraftSharp.Pages AuthStateProvider.GetCurrentUser().addItem(randomItem); } } - Console.WriteLine(randomItem.Name); openingAnimation(); } else diff --git a/src/CraftSharp/Pages/Shop.razor.cs b/src/CraftSharp/Pages/Shop.razor.cs index 11c7cf6..ec12295 100644 --- a/src/CraftSharp/Pages/Shop.razor.cs +++ b/src/CraftSharp/Pages/Shop.razor.cs @@ -15,6 +15,9 @@ namespace CraftSharp.Pages [Inject] public IStringLocalizer Localizer { get; set; } + [Inject] + public ILogger Logger { get; set; } + List offers = new List() { new ShopOfferModel() @@ -54,6 +57,7 @@ namespace CraftSharp.Pages { if (offer.InputAmount <= AuthService.GetCurrentUser().numberOfEmeralds) { + Logger.Log(LogLevel.Information, $"User {AuthService.GetCurrentUser().UserName} bought {offer.OutputAmount} keys for {offer.InputAmount} emeralds."); AuthService.GetCurrentUser().numberOfEmeralds -= offer.InputAmount; AuthService.GetCurrentUser().NumberOfKeys += offer.OutputAmount; } diff --git a/src/CraftSharp/Program.cs b/src/CraftSharp/Program.cs index 9405507..3f1fb26 100644 --- a/src/CraftSharp/Program.cs +++ b/src/CraftSharp/Program.cs @@ -21,6 +21,7 @@ using Microsoft.JSInterop; using CraftSharp.Controllers; var builder = WebApplication.CreateBuilder(args); +builder.Logging.AddConfiguration(builder.Configuration.GetSection("Logging")); // Add services to the container. builder.Services.AddRazorPages(); @@ -41,7 +42,6 @@ builder.Services.AddScoped(); builder.Services.AddHttpClient(); builder.Services.AddBlazoredModal(); -builder.Logging.AddConfiguration(builder.Configuration.GetSection("Logging")); builder.Services .AddBlazorise() diff --git a/src/CraftSharp/Services/AuthService.cs b/src/CraftSharp/Services/AuthService.cs index 5652c56..8249e6f 100644 --- a/src/CraftSharp/Services/AuthService.cs +++ b/src/CraftSharp/Services/AuthService.cs @@ -8,8 +8,10 @@ namespace CraftSharp.Services { private static readonly List CurrentUser; + static AuthService() { + CurrentUser = new List { new AppUser { UserName = "Admin", Password = "123456", Roles = new List { UserRoles.Admin }, numberOfKeys=999 } @@ -49,6 +51,18 @@ namespace CraftSharp.Services } } + public void Logout(CurrentUser user) + { + var idx = CurrentUser.FindIndex(u => u.UserName == user.UserName); + if(idx != -1) + { + CurrentUser[idx].numberOfEmeralds = user.numberOfEmeralds; + CurrentUser[idx].numberOfKeys = user.NumberOfKeys; + CurrentUser[idx].inventory = user.Inventory; + + } + } + public void Register(InscriptionModel registerRequest) { CurrentUser.Add(new AppUser { UserName = registerRequest.UserName, Password = registerRequest.Password, Roles = new List { UserRoles.User } }); diff --git a/src/CraftSharp/Services/CustomStateProvider.cs b/src/CraftSharp/Services/CustomStateProvider.cs index 15327f2..99ee033 100644 --- a/src/CraftSharp/Services/CustomStateProvider.cs +++ b/src/CraftSharp/Services/CustomStateProvider.cs @@ -13,12 +13,14 @@ namespace CraftSharp.Services public class CustomStateProvider : AuthenticationStateProvider { private readonly IAuthService _authService; + private readonly ILogger _logger; private CurrentUser _currentUser { get; set; } - public CustomStateProvider(IAuthService authService) + public CustomStateProvider(IAuthService authService, ILogger logger) { this._authService = authService; + this._logger = logger; } public override async Task GetAuthenticationStateAsync() @@ -35,7 +37,7 @@ namespace CraftSharp.Services } catch (HttpRequestException ex) { - Console.WriteLine("Request failed:" + ex); + _logger.Log(LogLevel.Error, $"Auth Request failed : {ex}"); } return new AuthenticationState(new ClaimsPrincipal(identity)); @@ -48,12 +50,14 @@ namespace CraftSharp.Services CurrentUser user; user = _authService.GetUser(loginParameters.UserName); _currentUser = user; + _logger.Log(LogLevel.Information, $"Login : {_currentUser.UserName}"); NotifyAuthenticationStateChanged(GetAuthenticationStateAsync()); } public async Task Logout() { - + _logger.Log(LogLevel.Information, $"Logout : {_currentUser.UserName}"); + _authService.Logout(_currentUser); _currentUser = new CurrentUser(); NotifyAuthenticationStateChanged(GetAuthenticationStateAsync()); } @@ -65,12 +69,12 @@ namespace CraftSharp.Services // No error - Login the user var user = _authService.GetUser(registerParameters.UserName); _currentUser = user; + _logger.Log(LogLevel.Information, $"Register : {_currentUser.UserName}"); NotifyAuthenticationStateChanged(GetAuthenticationStateAsync()); } public CurrentUser GetCurrentUser() { - if (_currentUser != null && _currentUser.IsAuthenticated) { return _currentUser; diff --git a/src/CraftSharp/Services/DataApiService.cs b/src/CraftSharp/Services/DataApiService.cs index 0a8d477..b1d350e 100644 --- a/src/CraftSharp/Services/DataApiService.cs +++ b/src/CraftSharp/Services/DataApiService.cs @@ -1,17 +1,20 @@ using CraftSharp.Components; using CraftSharp.Factories; using CraftSharp.Models; +using Microsoft.Extensions.Logging; namespace CraftSharp.Services { public class DataApiService : IDataService { private readonly HttpClient _http; + private readonly ILogger _logger; public DataApiService( - HttpClient http) + HttpClient http, ILogger logger) { _http = http; + _logger = logger; } public async Task Add(ItemModel model) @@ -21,6 +24,7 @@ namespace CraftSharp.Services // Save the data await _http.PostAsJsonAsync("https://localhost:7234/api/Crafting/", item); + _logger.Log(LogLevel.Information, $"ADDITION OF ITEM : {model.Id} - {model.Name}"); } public async Task Count() @@ -42,13 +46,14 @@ namespace CraftSharp.Services { // Get the item var item = ItemFactory.Create(model); - await _http.PutAsJsonAsync($"https://localhost:7234/api/Crafting/{id}", item); + _logger.Log(LogLevel.Information, $"UPDATING ITEM : {model.Id} - {model.Name}"); } public async Task Delete(int id) { await _http.DeleteAsync($"https://localhost:7234/api/Crafting/{id}"); + _logger.Log(LogLevel.Information, $"DELETION ON ITEM : {id}"); } public async Task> GetRecipes() diff --git a/src/CraftSharp/Services/IAuthService.cs b/src/CraftSharp/Services/IAuthService.cs index 605a089..2293b3c 100644 --- a/src/CraftSharp/Services/IAuthService.cs +++ b/src/CraftSharp/Services/IAuthService.cs @@ -6,6 +6,7 @@ namespace CraftSharp.Services { CurrentUser GetUser(string userName); void Login(ConnexionModel loginRequest); + void Logout(CurrentUser user); void Register(InscriptionModel registerRequest); } } diff --git a/src/CraftSharp/Shared/CultureSelector.razor.cs b/src/CraftSharp/Shared/CultureSelector.razor.cs index 3437287..1265f3e 100644 --- a/src/CraftSharp/Shared/CultureSelector.razor.cs +++ b/src/CraftSharp/Shared/CultureSelector.razor.cs @@ -6,6 +6,9 @@ namespace CraftSharp.Shared { public partial class CultureSelector { + [Inject] + ILogger Logger { get; set; } + private CultureInfo[] supportedCultures = new[] { new CultureInfo("en-US"), @@ -28,6 +31,7 @@ namespace CraftSharp.Shared var uri = new Uri(this.NavigationManager.Uri).GetComponents(UriComponents.PathAndQuery, UriFormat.Unescaped); var query = $"?culture={Uri.EscapeDataString(culture)}&" + $"redirectUri={Uri.EscapeDataString(uri)}"; + Logger.Log(LogLevel.Debug, $"Culture change - {culture}"); // Redirect the user to the culture controller to set the cookie this.NavigationManager.NavigateTo("/Culture/SetCulture" + query, forceLoad: true); diff --git a/src/CraftSharp/Shared/HeaderLayout.razor.cs b/src/CraftSharp/Shared/HeaderLayout.razor.cs index a5d2227..7dbbe2a 100644 --- a/src/CraftSharp/Shared/HeaderLayout.razor.cs +++ b/src/CraftSharp/Shared/HeaderLayout.razor.cs @@ -23,8 +23,6 @@ namespace CraftSharp.Shared [Inject] public HttpClient httpClient { get; set; } - [Inject] - public ILogger Logger { get; set; } [CascadingParameter] private Task AuthenticationState { get; set; } @@ -48,7 +46,6 @@ namespace CraftSharp.Shared private async Task LogoutClick() { await AuthStateProvider.Logout(); - Logger.Log(LogLevel.Information, $"Logout : {AuthStateProvider.GetCurrentUser().UserName}"); await httpClient.DeleteAsync($"{NavigationManager.BaseUri}User/DeleteUser"); NavigationManager.NavigateTo("/inscription");