Merge pull request 'arthur_logs' (#20) from arthur_logs into master

Reviewed-on: #20
arthur_errorview
Arthur VALIN 2 years ago
commit ab45651328

@ -1,4 +1,5 @@
@using CraftSharp.Models;
<CascadingBlazoredModal>
<Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData">
@ -14,4 +15,3 @@
</NotFound>
</Router>
</CascadingBlazoredModal>

@ -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)
{

@ -8,6 +8,7 @@ namespace CraftSharp.Pages
{
public partial class Add
{
/// <summary>
/// The default enchant categories.
/// </summary>
@ -36,7 +37,6 @@ namespace CraftSharp.Pages
private async void HandleValidSubmit()
{
await DataService.Add(itemModel);
NavigationManager.NavigateTo("list");
}

@ -24,7 +24,7 @@
<br />
<label class="text-danger">@error</label>
<NavLink href="inscription">
<h6 class="font-weight-normal text-center">Creer un compte</h6>
<h6 class="font-weight-normal text-center" style="color: white">Creer un compte</h6>
</NavLink>
</EditForm>
</div>

@ -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();

@ -1,18 +0,0 @@
@page "/counter"
<PageTitle>Counter</PageTitle>
<h1>Counter</h1>
<p role="status">Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
private int currentCount = 0;
private void IncrementCount()
{
currentCount++;
}
}

@ -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");
}

@ -1,48 +0,0 @@
@page "/fetchdata"
<PageTitle>Weather forecast</PageTitle>
@using CraftSharp.Data
@inject WeatherForecastService ForecastService
<h1>Weather forecast</h1>
<p>This component demonstrates fetching data from a service.</p>
@if (forecasts == null)
{
<p><em>Loading...</em></p>
}
else
{
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
@foreach (var forecast in forecasts)
{
<tr>
<td>@forecast.Date.ToShortDateString()</td>
<td>@forecast.TemperatureC</td>
<td>@forecast.TemperatureF</td>
<td>@forecast.Summary</td>
</tr>
}
</tbody>
</table>
}
@code {
private WeatherForecast[]? forecasts;
protected override async Task OnInitializedAsync()
{
forecasts = await ForecastService.GetForecastAsync(DateTime.Now);
}
}

@ -28,7 +28,7 @@
<label class="text-danger">@error</label>
<NavLink href="connexion">
<h6 class="font-weight-normal text-center">Vous avez un compte ? Connectez vous</h6>
<h6 class="font-weight-normal text-center" style="color: white">Vous avez un compte ? Connectez vous</h6>
</NavLink>
</EditForm>

@ -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<List> Logger { get; set; }
private async Task OnReadData(DataGridReadDataEventArgs<Item> 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

@ -29,14 +29,16 @@ namespace CraftSharp.Pages
[Inject]
public IStringLocalizer<Opening> Localizer { get; set; }
[Inject]
public ILogger<Opening> Logger { get; set; }
int totalItem;
List<Item> 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

@ -15,6 +15,9 @@ namespace CraftSharp.Pages
[Inject]
public IStringLocalizer<Shop> Localizer { get; set; }
[Inject]
public ILogger<Shop> Logger { get; set; }
List<ShopOfferModel> offers = new List<ShopOfferModel>()
{
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;
}

@ -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();

@ -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<UserController>();
builder.Services.AddHttpClient();
builder.Services.AddBlazoredModal();
builder.Logging.AddConfiguration(builder.Configuration.GetSection("Logging"));
builder.Services
.AddBlazorise()

@ -8,8 +8,10 @@ namespace CraftSharp.Services
{
private static readonly List<AppUser> CurrentUser;
static AuthService()
{
CurrentUser = new List<AppUser>
{
new AppUser { UserName = "Admin", Password = "123456", Roles = new List<UserRoles> { 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> { UserRoles.User } });

@ -13,12 +13,14 @@ namespace CraftSharp.Services
public class CustomStateProvider : AuthenticationStateProvider
{
private readonly IAuthService _authService;
private readonly ILogger<CustomStateProvider> _logger;
private CurrentUser _currentUser { get; set; }
public CustomStateProvider(IAuthService authService)
public CustomStateProvider(IAuthService authService, ILogger<CustomStateProvider> logger)
{
this._authService = authService;
this._logger = logger;
}
public override async Task<AuthenticationState> 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();
}
}

@ -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<DataApiService> _logger;
public DataApiService(
HttpClient http)
HttpClient http, ILogger<DataApiService> 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<int> 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<List<CraftingRecipe>> GetRecipes()

@ -6,6 +6,7 @@ namespace CraftSharp.Services
{
CurrentUser GetUser(string userName);
void Login(ConnexionModel loginRequest);
void Logout(CurrentUser user);
void Register(InscriptionModel registerRequest);
}
}

@ -6,6 +6,9 @@ namespace CraftSharp.Shared
{
public partial class CultureSelector
{
[Inject]
ILogger<CultureSelector> 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);

@ -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> 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;

Loading…
Cancel
Save