Skip to content

Commit

Permalink
Merge pull request #72 from davishoang96/delete-quiz
Browse files Browse the repository at this point in the history
Delete quiz
  • Loading branch information
davishoang96 authored Jan 5, 2025
2 parents 3ab6fcd + 3e0eb15 commit 9a59193
Show file tree
Hide file tree
Showing 12 changed files with 453 additions and 15 deletions.
94 changes: 94 additions & 0 deletions QuizApp.Api/QuizApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ public partial interface IQuizApiClient
/// <exception cref="ApiException">A server side error occurred.</exception>
System.Threading.Tasks.Task<int> CreateQuizEndpointAsync(QuizDTO quizDTO, System.Threading.CancellationToken cancellationToken);

/// <returns>Success</returns>
/// <exception cref="ApiException">A server side error occurred.</exception>
System.Threading.Tasks.Task<bool> DeleteQuizEndpointAsync(DeleteQuizRequest deleteQuizRequest);

/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <returns>Success</returns>
/// <exception cref="ApiException">A server side error occurred.</exception>
System.Threading.Tasks.Task<bool> DeleteQuizEndpointAsync(DeleteQuizRequest deleteQuizRequest, System.Threading.CancellationToken cancellationToken);

/// <returns>Success</returns>
/// <exception cref="ApiException">A server side error occurred.</exception>
System.Threading.Tasks.Task<System.Collections.Generic.ICollection<QuizDTO>> GetQuizzesEndpointAsync();
Expand Down Expand Up @@ -642,6 +651,91 @@ public virtual async System.Threading.Tasks.Task<int> CreateQuizEndpointAsync(Qu
}
}

/// <returns>Success</returns>
/// <exception cref="ApiException">A server side error occurred.</exception>
public virtual System.Threading.Tasks.Task<bool> DeleteQuizEndpointAsync(DeleteQuizRequest deleteQuizRequest)
{
return DeleteQuizEndpointAsync(deleteQuizRequest, System.Threading.CancellationToken.None);
}

/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <returns>Success</returns>
/// <exception cref="ApiException">A server side error occurred.</exception>
public virtual async System.Threading.Tasks.Task<bool> DeleteQuizEndpointAsync(DeleteQuizRequest deleteQuizRequest, System.Threading.CancellationToken cancellationToken)
{
if (deleteQuizRequest == null)
throw new System.ArgumentNullException("deleteQuizRequest");

var client_ = _httpClient;
var disposeClient_ = false;
try
{
using (var request_ = new System.Net.Http.HttpRequestMessage())
{
var json_ = Newtonsoft.Json.JsonConvert.SerializeObject(deleteQuizRequest, JsonSerializerSettings);
var content_ = new System.Net.Http.StringContent(json_);
content_.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/json");
request_.Content = content_;
request_.Method = new System.Net.Http.HttpMethod("DELETE");
request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/json"));

var urlBuilder_ = new System.Text.StringBuilder();
if (!string.IsNullOrEmpty(_baseUrl)) urlBuilder_.Append(_baseUrl);
// Operation Path: "quiz/delete"
urlBuilder_.Append("quiz/delete");

PrepareRequest(client_, request_, urlBuilder_);

var url_ = urlBuilder_.ToString();
request_.RequestUri = new System.Uri(url_, System.UriKind.RelativeOrAbsolute);

PrepareRequest(client_, request_, url_);

var response_ = await client_.SendAsync(request_, System.Net.Http.HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
var disposeResponse_ = true;
try
{
var headers_ = new System.Collections.Generic.Dictionary<string, System.Collections.Generic.IEnumerable<string>>();
foreach (var item_ in response_.Headers)
headers_[item_.Key] = item_.Value;
if (response_.Content != null && response_.Content.Headers != null)
{
foreach (var item_ in response_.Content.Headers)
headers_[item_.Key] = item_.Value;
}

ProcessResponse(client_, response_);

var status_ = (int)response_.StatusCode;
if (status_ == 200)
{
var objectResponse_ = await ReadObjectResponseAsync<bool>(response_, headers_, cancellationToken).ConfigureAwait(false);
if (objectResponse_.Object == null)
{
throw new ApiException("Response was null which was not expected.", status_, objectResponse_.Text, headers_, null);
}
return objectResponse_.Object;
}
else
{
var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
throw new ApiException("The HTTP status code of the response was not expected (" + status_ + ").", status_, responseData_, headers_, null);
}
}
finally
{
if (disposeResponse_)
response_.Dispose();
}
}
}
finally
{
if (disposeClient_)
client_.Dispose();
}
}

/// <returns>Success</returns>
/// <exception cref="ApiException">A server side error occurred.</exception>
public virtual System.Threading.Tasks.Task<System.Collections.Generic.ICollection<QuizDTO>> GetQuizzesEndpointAsync()
Expand Down
33 changes: 21 additions & 12 deletions QuizApp.Client/Pages/QuizPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
@using System.Collections.ObjectModel
@inject IQuizApiClient apiClient
@inject DialogService DialogService
@inject NotificationService NotificationService

<h3>Quiz Manager</h3>

<RadzenButton Click="AddNewQuiz">Add Quiz</RadzenButton>
<RadzenButton ButtonStyle="ButtonStyle.Secondary" Style="margin-bottom: 10px" Click="AddNewQuiz">Add Quiz</RadzenButton>

@if (Quizzes == null)
{
Expand All @@ -22,10 +23,10 @@ else
<RadzenDataGrid ColumnWidth="200px" AllowFiltering="true" AllowPaging="true" AllowSorting="true" Data="@Quizzes" TItem="QuizDTO">
<Columns>
<RadzenDataGridColumn Property="@(nameof(QuizDTO.Name))" Title="Name" Width="140px" />
<RadzenDataGridColumn Property="@(nameof(QuizDTO.Description))" Title="Description" />
<RadzenDataGridColumn Width="100px" Title="Actions">
<RadzenDataGridColumn Property="@(nameof(QuizDTO.Description))" Title="Description" Filterable="false" />
<RadzenDataGridColumn Filterable="false" TextAlign="TextAlign.Center">
<Template Context="data">
<RadzenButton Icon="delete" Class="rz-button rz-danger" Style="margin-left: 5px" />
<RadzenButton Click="@(() => DeleteQuiz(data.Id.Value))" Icon="delete" ButtonStyle="ButtonStyle.Danger" Style="margin-left: 5px" />
</Template>
</RadzenDataGridColumn>
</Columns>
Expand All @@ -51,8 +52,11 @@ else
private void AddNewQuizClosed(dynamic obj)
{
var newQuiz = obj as QuizDTO;
Quizzes.Add(newQuiz);
StateHasChanged();
if(newQuiz is not null)
{
Quizzes.Add(newQuiz);
StateHasChanged();
}
}

private void EditQuiz(int quizId)
Expand All @@ -66,11 +70,16 @@ else

private async Task DeleteQuiz(int quizId)
{
//var confirm = await JSRuntime.InvokeAsync<bool>("confirm", $"Are you sure you want to delete Quiz #{quizId}?");
// if (confirm)
// {
// //await QuizService.DeleteQuizAsync(quizId);
// await ReloadQuizzes();
// }
var result = await apiClient.DeleteQuizEndpointAsync(new Common.Request.DeleteQuizRequest{
QuizId = quizId
});

if(result)
{
var quizToRemove = Quizzes.First(s => s.Id == quizId);
Quizzes.Remove(quizToRemove);
StateHasChanged();
NotificationService.Notify(NotificationSeverity.Success, "Delete quiz successfuly");
}
}
}
6 changes: 6 additions & 0 deletions QuizApp.Common/Request/DeleteQuizRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace QuizApp.Common.Request;

public class DeleteQuizRequest
{
public int QuizId { get; set; }
}
202 changes: 202 additions & 0 deletions QuizApp.Database/Migrations/20250105041952_quizconfig.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9a59193

Please sign in to comment.