feat : delete

pull/41/head
Yvan CALATAYUD 1 year ago
parent e9fbf7c15a
commit cf83d8d840

@ -2,22 +2,13 @@
public class QuestionModel
{
public int Id { get; private set; }
public int Id { get; set; }
public string Content { get; set; }
public int IdChapter { get; private set; }
public int IdChapter { get; set; }
public int? IdAnswerGood { get; set; }
public int Difficulty { get; set; }
public int NbFails { get; private set; }
public int NbFails { get; set; }
public QuestionModel(int id, string content, int idChapter, int difficulty, int nbFails, int? idAnswerGood = null)
{
Id = id;
Content = content;
IdChapter = idChapter;
Difficulty = difficulty;
NbFails = nbFails;
IdAnswerGood = idAnswerGood;
}
public void addFails(int nb) { NbFails += nb; }
public void removeFails(int nb) { NbFails -= nb; }

@ -85,14 +85,29 @@ public partial class Administrators
parameters.Add(nameof(Administrator.Id), id);
var modal = Modal.Show<DeleteConfirmation>("Delete Confirmation", parameters);
var result = modal.Result;
var result = await modal.Result;
if (result.IsCanceled)
if (result.Cancelled)
{
return;
}
await DataService.Delete(id);
string apiUri = "https://trusting-panini.87-106-126-109.plesk.page/api/delete/administrator/" + id;
using (var httpClient = new HttpClient())
{
var response = await httpClient.DeleteAsync(apiUri);
if (response.IsSuccessStatusCode)
{
var responseBody = await response.Content.ReadAsStringAsync();
}
else
{
var errorResponse = await response.Content.ReadAsStringAsync();
}
}
//await DataService.Delete(id);
// Reload the page
NavigationManager.NavigateTo("administrators", true);

@ -10,6 +10,8 @@ using ChoETL;
using Microsoft.AspNetCore.Components.Forms;
using Blazor.Modals;
using Blazored.Modal;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Blazor.Models;
namespace Blazor.Pages.Chapters;
public partial class Chapters
@ -44,14 +46,31 @@ public partial class Chapters
parameters.Add(nameof(Chapter.Id), id);
var modal = Modal.Show<DeleteConfirmation>("Delete Confirmation", parameters);
var result = modal.Result;
var result = await modal.Result;
if (result.IsCanceled)
if (result.Cancelled)
{
return;
}
await DataService.Delete(id);
string apiUri = "https://trusting-panini.87-106-126-109.plesk.page/api/delete/chapter/" + id;
using (var httpClient = new HttpClient())
{
var response = await httpClient.DeleteAsync(apiUri);
if (response.IsSuccessStatusCode)
{
var responseBody = await response.Content.ReadAsStringAsync();
}
else
{
var errorResponse = await response.Content.ReadAsStringAsync();
}
}
// /api/delete/player/ +chapterid
//await DataService.Delete(id);
// Reload the page
NavigationManager.NavigateTo("chapters", true);

@ -46,14 +46,30 @@ public partial class Players
parameters.Add(nameof(Player.Id), id);
var modal = Modal.Show<DeleteConfirmation>("Delete Confirmation", parameters);
var result = modal.Result;
var result = await modal.Result;
if (result.IsCanceled)
if (result.Cancelled)
{
return;
}
await DataService.Delete(id);
string apiUri = "https://trusting-panini.87-106-126-109.plesk.page/api/delete/player/" + id;
using (var httpClient = new HttpClient())
{
var response = await httpClient.DeleteAsync(apiUri);
if (response.IsSuccessStatusCode)
{
var responseBody = await response.Content.ReadAsStringAsync();
}
else
{
var errorResponse = await response.Content.ReadAsStringAsync();
}
}
//await DataService.Delete(id);
// Reload the page
NavigationManager.NavigateTo("Players", true);

@ -1,4 +1,4 @@
@page "/addAdministrator"
@page "/addQuestion"
@using Blazor.Models
<h3>AddQuestion</h3>

@ -1,5 +1,39 @@
<h3>EditQuestion</h3>
@page "/editQuestion/{Id:int}"
@code {
<h3>EditQuestion</h3>
<EditForm Model="@questionModel" OnValidSubmit="@HandleValidSubmit">
<DataAnnotationsValidator />
<ValidationSummary />
<p>
<label for="content">
Content:
<InputText id="content" @bind-Value="questionModel.Content" />
</label>
<label for="chapter">
Chapter:
<InputText id="chapter"/>
</label>
<label for="reponse1">
Reponse n°1:
<InputText id="reponse1" />
</label>
<label for="reponse2">
Reponse n°2:
<InputText id="reponse2" />
</label>
<label for="reponse3">
Reponse n°3:
<InputText id="reponse3" />
</label>
<label for="reponse4">
Reponse n°4:
<InputText id="reponse4" />
</label>
</p>
<button type="submit">Submit</button>
</EditForm>
}

@ -1,4 +1,5 @@
using Blazor.Models;
using Blazor.Pages.Admins;
using Blazor.Services;
using Microsoft.AspNetCore.Components;
@ -6,5 +7,50 @@ namespace Blazor.Pages.Questions
{
public partial class EditQuestion
{
[Parameter]
public int Id { get; set; }
private QuestionModel questionModel = new();
[Inject]
public IDataService DataService { get; set; }
[Inject]
public NavigationManager NavigationManager { get; set; }
[Inject]
public IWebHostEnvironment WebHostEnvironment { get; set; }
[Inject]
public ILogger<EditQuestion> Logger { get; set; }
private async void HandleValidSubmit()
{
await DataService.Update(Id, questionModel);
var formData = new List<KeyValuePair<string, string>>();
formData.Add(new KeyValuePair<string, string>("content", questionModel.Content));
var formContent = new FormUrlEncodedContent(formData);
string apiUri = "https://trusting-panini.87-106-126-109.plesk.page/api/update/questions/" + questionModel.Id;
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();
}
}
}
}
}

@ -45,14 +45,30 @@ public partial class Questions
parameters.Add(nameof(Question.Id), id);
var modal = Modal.Show<DeleteConfirmation>("Delete Confirmation", parameters);
var result = modal.Result;
var result = await modal.Result;
if (result.IsCanceled)
if (result.Cancelled)
{
return;
}
await DataService.Delete(id);
string apiUri = "https://trusting-panini.87-106-126-109.plesk.page/api/delete/question/" + id;
using (var httpClient = new HttpClient())
{
var response = await httpClient.DeleteAsync(apiUri);
if (response.IsSuccessStatusCode)
{
var responseBody = await response.Content.ReadAsStringAsync();
}
else
{
var errorResponse = await response.Content.ReadAsStringAsync();
}
}
//await DataService.Delete(id);
// Reload the page
NavigationManager.NavigateTo("questions", true);

@ -2,6 +2,7 @@
using Blazor.ViewClasses;
using Blazored.LocalStorage;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using static System.Net.WebRequestMethods;
namespace Blazor.Services
@ -242,30 +243,29 @@ namespace Blazor.Services
return question;
}
//public async Task Update(int id, QuestionsModel model)
//{
// // Get the current data
// var currentData = await _localStorage.GetItemAsync<List<Question>>("data");
public async Task Update(int id, QuestionModel 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);
// 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}");
// }
// 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;
// Modify the content of the adminnistrator
question.Content = model.Content;
// // Save the data
// await _localStorage.SetItemAsync("data", currentData);
//}
// Save the data
await _localStorage.SetItemAsync("data", currentData);
}
//public async Task Add(QuestionsModel model)
//{
public async Task Add(QuestionModel model)
{
// // Get the current data
// var currentData = await _localStorage.GetItemAsync<List<Question>>("data");
@ -283,7 +283,7 @@ namespace Blazor.Services
// // Save the data
// await _localStorage.SetItemAsync("data", currentData);
//}
}
public async Task<int> CountQuestion()
{

@ -24,9 +24,9 @@ namespace Blazor.Services
Task<int> CountAdmin();
Task<List<Administrator>> ListAdmin(int currentPage, int pageSize);
//Task Add(QuestionsModel model);
Task Add(QuestionModel model);
//Task Update(int id, QuestionsModel model);
Task Update(int id, QuestionModel model);
Task<Question> GetQuestionById(int id);

Loading…
Cancel
Save