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 b/src/CraftSharp/Pages/Connexion.razor index 43d4812..d7ed316 100644 --- a/src/CraftSharp/Pages/Connexion.razor +++ b/src/CraftSharp/Pages/Connexion.razor @@ -24,7 +24,7 @@
-
Creer un compte
+
Creer un compte
diff --git a/src/CraftSharp/Pages/Connexion.razor.cs b/src/CraftSharp/Pages/Connexion.razor.cs index 428c642..4552c57 100644 --- a/src/CraftSharp/Pages/Connexion.razor.cs +++ b/src/CraftSharp/Pages/Connexion.razor.cs @@ -24,6 +24,7 @@ namespace CraftSharp.Pages [Inject] public HttpClient httpClient { get; set; } + private string error { get; set; } private ConnexionModel loginRequest { get; set; } = new ConnexionModel(); 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 b/src/CraftSharp/Pages/Inscription.razor index 83af701..76ca222 100644 --- a/src/CraftSharp/Pages/Inscription.razor +++ b/src/CraftSharp/Pages/Inscription.razor @@ -28,7 +28,7 @@ -
Vous avez un compte ? Connectez vous
+
Vous avez un compte ? Connectez vous
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/Pages/_Host.cshtml b/src/CraftSharp/Pages/_Host.cshtml index 3451d14..6683441 100644 --- a/src/CraftSharp/Pages/_Host.cshtml +++ b/src/CraftSharp/Pages/_Host.cshtml @@ -10,7 +10,6 @@ @{ Layout = "_Layout"; - Console.WriteLine("==============START=============="); var response = await httpClient.GetAsync($"https://localhost:7139/User/GetUser"); string jsonUser = await response.Content.ReadAsStringAsync(); var user = new ConnexionModel(); 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 347854f..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 } @@ -41,17 +43,26 @@ namespace CraftSharp.Services public void Login(ConnexionModel loginRequest) { - Console.WriteLine("LOGIN : " + loginRequest.UserName); var user = CurrentUser.FirstOrDefault(w => w.UserName == loginRequest.UserName && w.Password == loginRequest.Password); if (user == null) { - Console.WriteLine("LOGINFAILED"); - throw new Exception("User name or password invalid !"); } } + 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 f1bbc58..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,19 +69,16 @@ 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) { - Console.WriteLine("GETUSER: " + _currentUser.UserName); return _currentUser; } - Console.WriteLine("GETUSER: FAIL"); - return new 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 280476b..7dbbe2a 100644 --- a/src/CraftSharp/Shared/HeaderLayout.razor.cs +++ b/src/CraftSharp/Shared/HeaderLayout.razor.cs @@ -1,4 +1,5 @@ using CraftSharp.Models; +using CraftSharp.Pages; using CraftSharp.Services; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Authorization; @@ -22,6 +23,7 @@ namespace CraftSharp.Shared [Inject] public HttpClient httpClient { get; set; } + [CascadingParameter] private Task AuthenticationState { get; set; } @@ -36,16 +38,6 @@ namespace CraftSharp.Shared isAdmin(); } - void goInscription() - { - NavigationManager.NavigateTo("inscription"); - } - - void goConnexion() - { - NavigationManager.NavigateTo("connexion"); - } - async public void isAdmin() { var roles = AuthStateProvider.GetCurrentUser().Roles;