Merge branch 'master' of https://codefirst.iut.uca.fr/git/jade.van_brabandt/3.01-QCM_MuscuMaths
continuous-integration/drone/push Build is passing Details

pull/38/head
Yvan CALATAYUD 1 year ago
commit 86cb4700d9

@ -2,7 +2,7 @@
using Blazor.Services;
using Microsoft.AspNetCore.Components;
namespace Blazor.Pages
namespace Blazor.Pages.Admins
{
public partial class EditAdministrator
{

@ -90,8 +90,9 @@ public partial class Chapters
if (!e.CancellationToken.IsCancellationRequested)
{
totalChapter = (await LocalStorage.GetItemAsync<List<Chapter>>("data")).Count;
chapters = new List<Chapter>(response); // an actual data for the current page
totalChapter = chapters.Count;
}
}

@ -1,47 +1,29 @@
@page "/questions"
@using Blazor.Data
@inject WeatherForecastService ForecastService
@using Blazor.ViewClasses;
@using Blazorise.DataGrid
@using Blazored.Modal;
<h3>Chapters</h3>
<PageTitle>Weather forecast</PageTitle>
<div>
<NavLink class="btn btn-primary" href="addChapter" Match="NavLinkMatch.All">
<i class="fa fa-plus"></i> Ajouter
</NavLink>
</div>
<h1>Weather forecast</h1>
<DataGrid TItem="Question"
Data="@questions"
ReadData="@OnReadData"
TotalItems="@totalQuestion"
PageSize="10"
ShowPager
Responsive>
<DataGridColumn TItem="Question" Field="@nameof(Question.Id)" Caption="#" />
<DataGridColumn TItem="Question" Field="@nameof(Question.Content)" Caption="Display content" />
<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(DateOnly.FromDateTime(DateTime.Now));
}
}
<DataGridColumn TItem="Question" Field="@nameof(Question.Id)" Caption="Action">
<DisplayTemplate>
<a href="editQuestion/@(context.Id)" class="btn btn-primary"><i class="fa fa-edit"></i> Editer</a>
<button type="button" class="btn btn-primary" @onclick="() => OnDelete(context.Id)"><i class="fa fa-trash"></i> Supprimer</button>
</DisplayTemplate>
</DataGridColumn>
</DataGrid>

@ -1,6 +1,179 @@
namespace Blazor.Pages.Questions;
using Blazored.LocalStorage;
using Blazor.Services;
using Blazored.Modal.Services;
using Blazor.ViewClasses;
using System.Text;
using Microsoft.JSInterop;
using Microsoft.AspNetCore.Components;
using Blazorise.DataGrid;
using ChoETL;
using Microsoft.AspNetCore.Components.Forms;
using Blazor.Modals;
using Blazored.Modal;
using Blazor.Pages.Admins;
namespace Blazor.Pages.Questions;
public partial class Questions
{
public List<Question> questions;
private int totalQuestion;
[Inject]
public NavigationManager NavigationManager { get; set; }
[CascadingParameter]
public IModalService Modal { get; set; }
[Inject]
public IDataService DataService { get; set; }
public IWebHostEnvironment WebHostEnvironment { get; set; }
[Inject]
public HttpClient Http { get; set; }
[Inject]
public ILocalStorageService LocalStorage { get; set; }
[Inject]
public IJSRuntime IJSRuntime { get; set; }
private async void OnDelete(int id)
{
var parameters = new ModalParameters();
parameters.Add(nameof(Question.Id), id);
var modal = Modal.Show<DeleteConfirmation>("Delete Confirmation", parameters);
var result = modal.Result;
if (result.IsCanceled)
{
return;
}
await DataService.Delete(id);
// Reload the page
NavigationManager.NavigateTo("questions", true);
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
// Do not treat this action if is not the first render
if (!firstRender)
{
return;
}
var currentData = await LocalStorage.GetItemAsync<Question[]>("data");
// Check if data exist in the local storage
if (currentData == null)
{
// this code add in the local storage the fake data (we load the data sync for initialize the data before load the OnReadData method)
var originalData = Http.GetFromJsonAsync<Question[]>($"https://trusting-panini.87-106-126-109.plesk.page/api/chapters").Result;
await LocalStorage.SetItemAsync("data", originalData);
}
}
private async Task OnReadData(DataGridReadDataEventArgs<Question> e)
{
if (e.CancellationToken.IsCancellationRequested)
{
return;
}
// When you use a real API, we use this follow code
//var response = await Http.GetJsonAsync<Item[]>( $"http://my-api/api/data?page={e.Page}&pageSize={e.PageSize}" );
var response = (await Http.GetFromJsonAsync<Question[]>($"{NavigationManager.BaseUri}fake-question.json")).Skip((e.Page - 1) * e.PageSize).Take(e.PageSize).ToList();
if (!e.CancellationToken.IsCancellationRequested)
{
totalQuestion = (await Http.GetFromJsonAsync<List<Question>>($"{NavigationManager.BaseUri}fake-question.json")).Count;
questions = new List<Question>(response); // an actual data for the current page
}
}
private async void Export()
{
StringBuilder sb = new StringBuilder();
HttpResponseMessage response = await Http.GetAsync("https://trusting-panini.87-106-126-109.plesk.page/api/chapters");
var json = await response.Content.ReadAsStringAsync();
using (var jsonFile = ChoJSONReader.LoadText(json))
{
using (var csvFile = new ChoCSVWriter(sb).WithFirstLineHeader())
{
csvFile.Write(jsonFile);
}
}
var sentFile = new MemoryStream(Encoding.UTF32.GetBytes(sb.ToString()));
using (var streamRef = new DotNetStreamReference(stream: sentFile))
{
await IJSRuntime.InvokeVoidAsync("downloadFileFromStream", "data.csv", streamRef);
}
}
private async Task SingleUpload(InputFileChangeEventArgs e)
{
using (MemoryStream ms = new MemoryStream())
{
await e.File.OpenReadStream().CopyToAsync(ms);
var bytes = ms.ToArray();
string s = Encoding.UTF8.GetString(bytes);
char[] invalidChars = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '\r', '\n', ',', ' ' };
List<string> filteredStrings = new List<string>();
StringBuilder filteredString = new StringBuilder();
foreach (var c in s)
{
if (!invalidChars.Contains(c))
{
filteredString.Append(c);
}
else
{
if (filteredString.Length > 0)
{
filteredStrings.Add(filteredString.ToString());
filteredString.Clear();
}
}
}
if (filteredString.Length > 0)
{
filteredStrings.Add(filteredString.ToString());
}
foreach (var filteredStr in filteredStrings)
{
var formData = new List<KeyValuePair<string, string>>();
formData.Add(new KeyValuePair<string, string>("name", filteredStr));
var formContent = new FormUrlEncodedContent(formData);
string apiUri = "https://trusting-panini.87-106-126-109.plesk.page/api/add/chapters";
using (var httpClient = new HttpClient())
{
var response = await httpClient.PostAsync(apiUri, formContent);
if (response.IsSuccessStatusCode)
{
var responseBody = await response.Content.ReadAsStringAsync();
}
else
{
var errorResponse = await response.Content.ReadAsStringAsync();
}
}
}
}
}
}

@ -100,8 +100,6 @@ namespace Blazor.Services
await _localStorage.SetItemAsync("data", currentData);
}
public async Task<int> Count()
{
// Load data from the local storage
@ -139,10 +137,10 @@ namespace Blazor.Services
// Get the current data
var currentData = await _localStorage.GetItemAsync<List<Administrator>>("data");
// Get the chapter int the list
// Get the admin int the list
var admin = currentData.FirstOrDefault(w => w.Id == id);
// Check if chapter exist
// Check if admin exist
if (admin == null)
{
throw new Exception($"Unable to found the item with ID: {id}");
@ -173,7 +171,6 @@ namespace Blazor.Services
await _localStorage.SetItemAsync("data", currentData);
}
public async Task Add(AdministratorsModel model)
{
// Get the current data
@ -182,7 +179,7 @@ namespace Blazor.Services
// Simulate the Id
model.Id = currentData.Max(s => s.Id) + 1;
// Add the chapter to the current data
// Add the admin to the current data
currentData.Add(new Administrator
{
Id = model.Id,
@ -226,5 +223,98 @@ namespace Blazor.Services
return (await _localStorage.GetItemAsync<Administrator[]>("data")).Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
}
public async Task<Question> GetQuestionById(int id)
{
// Get the current data
var currentData = await _localStorage.GetItemAsync<List<Question>>("data");
// Get the question int the list
var question = currentData.FirstOrDefault(w => w.Id == id);
// Check if question exist
if (question == null)
{
throw new Exception($"Unable to found the item with ID: {id}");
}
return question;
}
//public async Task Update(int id, QuestionsModel model)
//{
// // Get the current data
// var currentData = await _localStorage.GetItemAsync<List<Question>>("data");
// // Get the admin int the list
// var question = currentData.FirstOrDefault(w => w.Id == id);
// // Check if admin exist
// if (question == null)
// {
// throw new Exception($"Unable to found the item with ID: {id}");
// }
// // Modify the content of the adminnistrator
// question.Username = model.Username;
// question.hashedPassword = model.hashedPassword;
// // Save the data
// await _localStorage.SetItemAsync("data", currentData);
//}
//public async Task Add(QuestionsModel model)
//{
// // Get the current data
// var currentData = await _localStorage.GetItemAsync<List<Question>>("data");
// // Simulate the Id
// model.Id = currentData.Max(s => s.Id) + 1;
// // Add the admin to the current data
// currentData.Add(new Question
// {
// Id = model.Id,
// Username = model.Username,
// hashedPassword = model.hashedPassword
// });
// // Save the data
// await _localStorage.SetItemAsync("data", currentData);
//}
public async Task<int> CountQuestion()
{
// Load data from the local storage
var currentData = await _localStorage.GetItemAsync<Question[]>("data");
// Check if data exist in the local storage
if (currentData == null)
{
// this code add in the local storage the fake data
var originalData = await _http.GetFromJsonAsync<Question[]>($"{_navigationManager.BaseUri}fake-question.json");
await _localStorage.SetItemAsync("data", originalData);
}
return (await _localStorage.GetItemAsync<Question[]>("data")).Length;
}
public async Task<List<Question>> ListQuestion(int currentPage, int pageSize)
{
// Load data from the local storage
var currentData = await _localStorage.GetItemAsync<Question[]>("data");
// Check if data exist in the local storage
if (currentData == null)
{
// this code add in the local storage the fake data
var originalData = await _http.GetFromJsonAsync<Question[]>($"{_navigationManager.BaseUri}fake-question.json");
await _localStorage.SetItemAsync("data", originalData);
}
return (await _localStorage.GetItemAsync<Question[]>("data")).Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
}
}
}

@ -24,6 +24,15 @@ namespace Blazor.Services
Task<int> CountAdmin();
Task<List<Administrator>> ListAdmin(int currentPage, int pageSize);
//Task Add(QuestionsModel model);
//Task Update(int id, QuestionsModel model);
Task<Question> GetQuestionById(int id);
Task<int> CountQuestion();
Task<List<Question>> ListQuestion(int currentPage, int pageSize);
Task Delete(int id);
}

@ -1,98 +0,0 @@
[
{
"id": 1,
"name": "Zilla"
},
{
"id": 2,
"name": "Silodyne"
},
{
"id": 3,
"name": "Zolarity"
},
{
"id": 4,
"name": "Straloy"
},
{
"id": 5,
"name": "Extro"
},
{
"id": 6,
"name": "Prosely"
},
{
"id": 7,
"name": "Orbalix"
},
{
"id": 8,
"name": "Hatology"
},
{
"id": 9,
"name": "Exodoc"
},
{
"id": 10,
"name": "Cinaster"
},
{
"id": 11,
"name": "Toyletry"
},
{
"id": 12,
"name": "Combogene"
},
{
"id": 13,
"name": "Olympix"
},
{
"id": 14,
"name": "Emoltra"
},
{
"id": 15,
"name": "Macronaut"
},
{
"id": 16,
"name": "Genekom"
},
{
"id": 17,
"name": "Zaya"
},
{
"id": 18,
"name": "Elentrix"
},
{
"id": 19,
"name": "Comvex"
},
{
"id": 20,
"name": "Exozent"
},
{
"id": 21,
"name": "Fuelworks"
},
{
"id": 22,
"name": "Splinx"
},
{
"id": 23,
"name": "Greeker"
},
{
"id": 24,
"name": "Martgo"
}
]
Loading…
Cancel
Save