From 1707cfe514d336a4b92fac8ff96671e87e680439 Mon Sep 17 00:00:00 2001 From: lebeaulato Date: Fri, 7 Feb 2025 16:39:26 +0100 Subject: [PATCH] Doc classe --- .../WF-WebAdmin/Service/QuizServiceStub.cs | 170 +++++++-- .../WF-WebAdmin/Service/QuoteServiceLocal.cs | 57 ++- .../WF-WebAdmin/Service/QuoteServiceStub.cs | 342 +++++++++++++----- .../WF-WebAdmin/Service/UserServiceStub.cs | 114 +++++- .../WF-WebAdmin/Shared/CultureSelector.razor | 11 + .../WF-WebAdmin/wwwroot/fake_data_quiz.json | 2 +- 6 files changed, 543 insertions(+), 153 deletions(-) diff --git a/WF-WebAdmin/WF-WebAdmin/Service/QuizServiceStub.cs b/WF-WebAdmin/WF-WebAdmin/Service/QuizServiceStub.cs index 7b6b662..f35612f 100644 --- a/WF-WebAdmin/WF-WebAdmin/Service/QuizServiceStub.cs +++ b/WF-WebAdmin/WF-WebAdmin/Service/QuizServiceStub.cs @@ -5,14 +5,37 @@ namespace WF_WebAdmin.Service; public class QuizServiceStub: IQuizService { - private readonly string _jsonFilePath = Path.Combine(Environment.CurrentDirectory, "wwwroot", "fake_data_quiz.json"); - - public async Task saveQuizJson(List quizzes) - { - var json = JsonSerializer.Serialize(quizzes, new JsonSerializerOptions { WriteIndented = true }); - await File.WriteAllTextAsync(_jsonFilePath, json); + private readonly string _jsonFilePath = Path.Combine(Environment.CurrentDirectory, "wwwroot", "fake_data_quiz.json"); + + + /// + /// Asynchronously saves a list of quiz objects to a JSON file. + /// + /// A list of objects to be serialized and saved. + /// A task representing the asynchronous operation. + /// + /// This method serializes the list of quizzes to a well-formatted JSON string and saves it + /// to a specified file path. The list is serialized using + /// with indented formatting to make the JSON human-readable. + /// + public async Task saveQuizJson(List quizzes) + { + var json = JsonSerializer.Serialize(quizzes, new JsonSerializerOptions { WriteIndented = true }); + await File.WriteAllTextAsync(_jsonFilePath, json); } + + /// + /// Asynchronously adds a new quiz to the list of quizzes and saves the updated list to a JSON file. + /// + /// The object to be added to the list of quizzes. + /// A task representing the asynchronous operation. + /// + /// This method retrieves the current list of quizzes using , assigns a unique ID to the + /// new quiz (based on the highest existing ID), and adds the new quiz to the list. Afterward, the updated list + /// of quizzes is saved back to the JSON file using . The new quiz will have an ID + /// that's one greater than the highest existing ID or 1 if no quizzes exist. + /// public async Task addQuiz(Quiz quiz) { var data = await getQuizzes(); @@ -21,32 +44,54 @@ public class QuizServiceStub: IQuizService await saveQuizJson(data); } + /// + /// Asynchronously updates an existing quiz in the list of quizzes and saves the updated list to a JSON file. + /// + /// The object containing the updated data. + /// A task representing the asynchronous operation. + /// + /// This method retrieves the current list of quizzes using , searches for the quiz + /// with the same ID as the one provided, and updates its properties with the new values from the given quiz object. + /// If the quiz is found, the updated list is saved back to the JSON file using . + /// If no quiz with the matching ID is found, no update is performed. + /// public async Task updateQuiz(Quiz quiz) { - var data = await getQuizzes(); - var existingQuiz = data.FirstOrDefault(q => q.Id == quiz.Id); - if (existingQuiz != null) - { - existingQuiz.Question = quiz.Question; - existingQuiz.AnswerA = quiz.AnswerA; - existingQuiz.AnswerB = quiz.AnswerB; - existingQuiz.AnswerC = quiz.AnswerC; - existingQuiz.AnswerD = quiz.AnswerD; - existingQuiz.CAnswer = quiz.CAnswer; - existingQuiz.IsValid = quiz.IsValid; - existingQuiz.UserProposition = quiz.UserProposition; - await saveQuizJson(data); + var data = await getQuizzes(); + var existingQuiz = data.FirstOrDefault(q => q.Id == quiz.Id); + if (existingQuiz != null) + { + existingQuiz.Question = quiz.Question; + existingQuiz.AnswerA = quiz.AnswerA; + existingQuiz.AnswerB = quiz.AnswerB; + existingQuiz.AnswerC = quiz.AnswerC; + existingQuiz.AnswerD = quiz.AnswerD; + existingQuiz.CAnswer = quiz.CAnswer; + existingQuiz.IsValid = quiz.IsValid; + existingQuiz.UserProposition = quiz.UserProposition; + await saveQuizJson(data); } } + /// + /// Asynchronously removes a quiz from the list of quizzes by its ID and saves the updated list to a JSON file. + /// + /// The ID of the to be removed. + /// A task representing the asynchronous operation. + /// + /// This method retrieves the current list of quizzes using , searches for the quiz + /// with the specified ID, and removes it from the list if found. After removal, the updated list of quizzes is + /// saved back to the JSON file using . If no quiz with the matching ID is found, + /// no changes are made. + /// public async Task removeQuiz(int id) { - var data = await getQuizzes(); - var quiz = data.FirstOrDefault(q => q.Id == id); - if (quiz != null) - { - data.Remove(quiz); - await saveQuizJson(data); + var data = await getQuizzes(); + var quiz = data.FirstOrDefault(q => q.Id == id); + if (quiz != null) + { + data.Remove(quiz); + await saveQuizJson(data); } } @@ -55,27 +100,56 @@ public class QuizServiceStub: IQuizService throw new NotImplementedException(); } + /// + /// Asynchronously retrieves the list of quizzes from a JSON file. + /// + /// A task representing the asynchronous operation, with a result containing the quizzes. + /// + /// This method checks if the JSON file exists at the specified file path. If the file does not exist, it logs a + /// message to the console and returns an empty list of quizzes. If the file exists, it reads the JSON content, + /// deserializes it into a list of objects, and returns the list. If the deserialization is + /// unsuccessful or the file is empty, it returns an empty list instead. + /// public async Task> getQuizzes() { - if (!File.Exists(_jsonFilePath)) - { - Console.Out.WriteLine($"{_jsonFilePath} not found"); - return new List(); - } - - var json = await File.ReadAllTextAsync(_jsonFilePath); + if (!File.Exists(_jsonFilePath)) + { + Console.Out.WriteLine($"{_jsonFilePath} not found"); + return new List(); + } + + var json = await File.ReadAllTextAsync(_jsonFilePath); return JsonSerializer.Deserialize>(json) ?? new List(); } + /// + /// Asynchronously retrieves the list of quizzes that are marked as invalid and need validation. + /// + /// A task representing the asynchronous operation, with a result containing quizzes that are not valid. + /// + /// This method retrieves the full list of quizzes using and filters it to return only those + /// quizzes where the property is set to false. The filtered list is then returned. + /// If no quizzes are invalid, an empty list will be returned. + /// public async Task> getQuizzesToValidate() { - var quizzes = await getQuizzes(); + var quizzes = await getQuizzes(); return quizzes.Where(quiz => quiz.IsValid == false).ToList(); } + + /// + /// Asynchronously retrieves a specific quiz by its ID from the list of quizzes. + /// + /// The ID of the to retrieve. + /// A task representing the asynchronous operation, with a result containing the matching quiz, or null if not found. + /// + /// This method retrieves the full list of quizzes using and searches for a quiz with + /// the specified ID. If a quiz with the matching ID is found, it is returned; otherwise, the method returns null. + /// public async Task getQuiz(int id) { - var data = await getQuizzes(); + var data = await getQuizzes(); var q = data.FirstOrDefault(p => p.Id == id); if (q != null) { @@ -84,23 +158,45 @@ public class QuizServiceStub: IQuizService return null; } + /// + /// Asynchronously retrieves a paginated list of quizzes, returning a specific number of quizzes for the given page. + /// + /// The number of quizzes to retrieve per page. + /// The page number to retrieve, where the first page is 1. + /// A task representing the asynchronous operation, with a result containing the quizzes for the specified page. + /// + /// This method retrieves the full list of quizzes using and returns a subset of quizzes based + /// on the specified page number and the number of quizzes per page. If the requested page exceeds the available quizzes, + /// the method returns the last page with the remaining quizzes. If the number of quizzes requested per page exceeds the + /// total number of quizzes, the method will return all quizzes available. + /// public async Task> getSommeQuiz(int nb, int page) { var data = await getQuizzes(); if ((page - 1) * nb + nb > data.Count()) { - if(nb > data.Count()) + if (nb > data.Count()) { - return data.GetRange(0, data.Count()-1); + return data.GetRange(0, data.Count() - 1); } return data.GetRange(data.Count() - nb, nb); } return data.GetRange((page - 1) * nb, nb); } + + /// + /// Asynchronously retrieves the total number of quizzes in the list. + /// + /// A task representing the asynchronous operation, with an result containing the total number of quizzes. + /// + /// This method retrieves the full list of quizzes using and returns the count of quizzes in the list. + /// It simply returns the number of quizzes available in the data source. + /// public async Task getNbQuiz() { var data = await getQuizzes(); return data.Count; - } + } + } diff --git a/WF-WebAdmin/WF-WebAdmin/Service/QuoteServiceLocal.cs b/WF-WebAdmin/WF-WebAdmin/Service/QuoteServiceLocal.cs index 4417c5b..4271681 100644 --- a/WF-WebAdmin/WF-WebAdmin/Service/QuoteServiceLocal.cs +++ b/WF-WebAdmin/WF-WebAdmin/Service/QuoteServiceLocal.cs @@ -10,7 +10,17 @@ namespace WF_WebAdmin.Service - + /// + /// Asynchronously adds a new quote to the database and returns the corresponding . + /// + /// The object to be added to the database. + /// A task representing the asynchronous operation, with a result containing the added quote's data. + /// + /// This method converts the provided object into a using . + /// It then inserts the quote into the PostgreSQL database using a parameterized SQL query with the help of Npgsql. + /// After successfully inserting the quote, the corresponding is returned to the caller. + /// Error handling is in place to catch any issues during the database insertion process, with the exception message logged in case of failure. + /// public async Task AddQuoteAsync(Quote quote) { QuoteExtension extension = new QuoteExtension(); @@ -64,35 +74,72 @@ namespace WF_WebAdmin.Service } - public Task RemoveQuote(Quote quote) + /// + /// Asynchronously handles the removal of a quote and returns the corresponding . + /// + /// The object to be removed. + /// A task representing the asynchronous operation, with a result corresponding to the removed quote. + /// + /// This method takes a object, converts it into a using the + /// , and then returns the DTO. Note that while this function is named `RemoveQuote`, + /// it currently only converts the quote to a DTO and does not actually perform any database removal operation. + /// You may need to implement additional logic to remove the quote from the database. + /// + public Task RemoveQuote(Quote quote) { QuoteExtension extension = new QuoteExtension(); QuoteDTO quoteDTO = extension.QuoteToDTO(quote); + // Return the DTO as the result of this asynchronous operation (though no removal logic is currently implemented) return Task.FromResult(quoteDTO); } + + /// + /// Asynchronously validates a quote and returns the corresponding . + /// + /// The object to be validated. + /// A task representing the asynchronous operation, with a result corresponding to the validated quote. + /// + /// This method takes a object, converts it into a using the + /// , and returns the DTO. The method is named `validQuote`, but currently, it only + /// converts the quote into a DTO and does not perform any actual validation logic. + /// If you intend to validate the quote (e.g., updating its status in a database), you will need to implement + /// the actual validation logic separately. + /// public Task validQuote(Quote quote) { QuoteExtension extension = new QuoteExtension(); QuoteDTO quoteDTO = extension.QuoteToDTO(quote); + // Return the DTO as the result of this asynchronous operation (though no validation logic is currently implemented) return Task.FromResult(quoteDTO); } + + /// + /// Asynchronously updates a quote and returns the corresponding . + /// + /// The object to be updated. + /// A task representing the asynchronous operation, with a result corresponding to the updated quote. + /// + /// This method takes a object, converts it into a using the + /// , and returns the DTO. The method is named `updateQuote`, but currently, it only + /// converts the quote into a DTO and does not perform any actual update logic. + /// If you intend to update the quote (e.g., modifying the quote in a database or data source), + /// you will need to implement the actual update logic separately. + /// public Task updateQuote(Quote quote) { QuoteExtension extension = new QuoteExtension(); QuoteDTO quoteDTO = extension.QuoteToDTO(quote); + // Return the DTO as the result of this asynchronous operation (though no update logic is currently implemented) return Task.FromResult(quoteDTO); } - - - public Task addQuote(Quote quote) { throw new NotImplementedException(); diff --git a/WF-WebAdmin/WF-WebAdmin/Service/QuoteServiceStub.cs b/WF-WebAdmin/WF-WebAdmin/Service/QuoteServiceStub.cs index fcf6102..f4978b5 100644 --- a/WF-WebAdmin/WF-WebAdmin/Service/QuoteServiceStub.cs +++ b/WF-WebAdmin/WF-WebAdmin/Service/QuoteServiceStub.cs @@ -9,143 +9,285 @@ namespace WF_WebAdmin.Service; private readonly string _char = Path.Combine(Environment.CurrentDirectory, "wwwroot", "fake-dataCaracter.json"); private readonly string _src = Path.Combine(Environment.CurrentDirectory, "wwwroot", "fake-dataSource.json"); + + /// + /// Asynchronously saves a list of quotes to a JSON file. + /// + /// The list of objects to be serialized and saved to the file. + /// A task representing the asynchronous operation of saving the quotes to the file. + /// + /// This method serializes the provided list of objects into JSON format using . + /// The serialized JSON is then saved to the file path specified in the field. The JSON is written + /// with indentation for better readability. + /// If the file does not already exist, it will be created. + /// public async Task saveQuoteJson(List quotes) - { - var json = JsonSerializer.Serialize(quotes, new JsonSerializerOptions { WriteIndented = true }); - await File.WriteAllTextAsync(_jsonFilePath, json); - } + { + var json = JsonSerializer.Serialize(quotes, new JsonSerializerOptions { WriteIndented = true }); + await File.WriteAllTextAsync(_jsonFilePath, json); + } + + + /// + /// Asynchronously adds a new quote to the list and saves it to a JSON file. + /// + /// The object to be added to the list. + /// A task representing the asynchronous operation of adding the quote and saving the updated list to the file. + /// + /// This method retrieves the current list of quotes using the method, assigns a new ID to the + /// provided quote (incremented from the maximum existing ID), and adds the quote to the list. After updating the list, + /// the method saves the updated list back to the JSON file using . + /// If the list is empty, the new quote is assigned an ID of 1. + /// + public async Task addQuote(Quote quote) + { + var data = await getAllQuote(); + quote.Id = data.Count > 0 ? data.Max(p => p.Id) + 1 : 1; + data.Add(quote); + await saveQuoteJson(data); + } - public async Task addQuote(Quote quote) + + /// + /// Asynchronously removes a quote from the list and saves the updated list to a JSON file. + /// + /// The object to be removed from the list. + /// A task representing the asynchronous operation of removing the quote and saving the updated list to the file. + /// + /// This method retrieves the current list of quotes using the method. + /// It searches for the provided quote by its `Id` and, if found, removes it from the list. + /// After removing the quote, the method saves the updated list back to the JSON file using . + /// If the quote is not found in the list, no action is taken. + /// + public async Task removeQuote(Quote quote) + { + var data = await getAllQuote(); + var q = data.FirstOrDefault(p => p.Id == quote.Id); + if (q != null) { - var data = await getAllQuote(); - quote.Id = data.Count > 0 ? data.Max(p => p.Id) + 1 : 1; - data.Add(quote); + data.Remove(q); await saveQuoteJson(data); } + } - public async Task removeQuote(Quote quote) - { - var data = await getAllQuote(); - var q = data.FirstOrDefault(p => p.Id == quote.Id); - if (q != null) - { - data.Remove(q); - await saveQuoteJson(data); - } - } - public async Task validQuote(Quote quote) + public async Task validQuote(Quote quote) + { + throw new NotImplementedException(); + } + + + /// + /// Asynchronously updates the details of an existing quote and saves the updated list to a JSON file. + /// + /// The object containing the updated details of the quote. + /// A task representing the asynchronous operation of updating the quote and saving the updated list to the file. + /// + /// This method retrieves the current list of quotes using the method. + /// It searches for the quote with the provided `Id` and, if found, updates its properties (e.g., content, character, image path, etc.) + /// with the values from the provided `quote` object. After updating the quote, the method saves the updated list back to the JSON file + /// using . If the quote with the specified `Id` is not found, no action is taken. + /// + public async Task updateQuote(Quote quote) + { + var data = await getAllQuote(); + var q = data.FirstOrDefault(p => p.Id == quote.Id); + if (q != null) { - throw new NotImplementedException(); + q.Content = quote.Content; + q.Charac = quote.Charac; + q.ImgPath = quote.ImgPath; + q.TitleSrc = quote.TitleSrc; + q.DateSrc = quote.DateSrc; + q.Langue = quote.Langue; + await saveQuoteJson(data); } + } + - public async Task updateQuote(Quote quote) + /// + /// Asynchronously retrieves all quotes from a JSON file. + /// + /// A task representing the asynchronous operation, with a result of a list of objects. + /// + /// This method checks if the JSON file exists at the specified file path (). + /// If the file does not exist, it logs a message and returns an empty list of quotes. + /// If the file exists, it reads the JSON content, deserializes it into a list of objects, + /// and returns that list. If the deserialization results in a null value, an empty list is returned. + /// + public async Task> getAllQuote() + { + if (!File.Exists(_jsonFilePath)) { - var data = await getAllQuote(); - var q = data.FirstOrDefault(p => p.Id == quote.Id); - if (q != null) - { - q.Content = quote.Content; - q.Charac = quote.Charac; - q.ImgPath = quote.ImgPath; - q.TitleSrc = quote.TitleSrc; - q.DateSrc = quote.DateSrc; - q.Langue = quote.Langue; - await saveQuoteJson(data); - } + Console.Out.WriteLine($"{_jsonFilePath} not found"); + return new List(); } - public async Task> getAllQuote() - { - if (!File.Exists(_jsonFilePath)) - { - Console.Out.WriteLine($"{_jsonFilePath} not found"); - return new List(); - } + var json = await File.ReadAllTextAsync(_jsonFilePath); + return JsonSerializer.Deserialize>(json) ?? new List(); + } - var json = await File.ReadAllTextAsync(_jsonFilePath); - return JsonSerializer.Deserialize>(json) ?? new List(); - } - public async Task> getSomeQuote(int nb, int page) + /// + /// Asynchronously retrieves a subset of quotes based on the specified page number and the number of quotes per page. + /// + /// The number of quotes to retrieve per page. + /// The page number for pagination. + /// A task representing the asynchronous operation, with a result of a list of objects for the specified page. + /// + /// This method retrieves all quotes using the method and then calculates the range of quotes + /// to be returned based on the provided `nb` (number of quotes per page) and `page` (the page number). It ensures that + /// the returned subset does not exceed the total number of quotes available. + /// + /// If the calculated range is larger than the available quotes, it returns a subset of quotes from the end of the list. + /// If the requested page number exceeds the total number of pages, the method will return the last available page of quotes. + /// + public async Task> getSomeQuote(int nb, int page) + { + var quotes = await getAllQuote(); + if ((page - 1) * nb + nb > quotes.Count()) { - var quotes = await getAllQuote(); - if((page - 1) * nb + nb > quotes.Count()) + if (nb > quotes.Count()) { - if (nb > quotes.Count()) - { - return quotes.GetRange(0, quotes.Count()); - } - return quotes.GetRange(quotes.Count()-nb, nb); + return quotes.GetRange(0, quotes.Count()); } - return quotes.GetRange((page - 1) * nb, nb); + return quotes.GetRange(quotes.Count() - nb, nb); } + return quotes.GetRange((page - 1) * nb, nb); + } + - public async Task getOnequote(int id) + /// + /// Asynchronously retrieves a single quote based on its ID. + /// + /// The unique identifier of the to be retrieved. + /// A task representing the asynchronous operation, with a result of the object if found, otherwise null. + /// + /// This method retrieves all quotes using the method and searches for the quote that matches the provided `id`. + /// If a matching quote is found, it returns that quote; otherwise, it returns `null`. + /// + public async Task getOnequote(int id) + { + var data = await getAllQuote(); + var q = data.FirstOrDefault(p => p.Id == id); + if (q != null) { - var data = await getAllQuote(); - var q = data.FirstOrDefault(p => p.Id == id); - if (q != null) - { - return q; - } - return null; + return q; } + return null; + } + - public async Task> reserchQuote(string reserch, List argument) + public async Task> reserchQuote(string reserch, List argument) { throw new NotImplementedException(); } - public async Task> getAllQuoteInvalid() - { - var quotes = await getAllQuote(); - quotes = quotes.Where(q => q.IsValid == false).ToList(); - return quotes; - } - public async Task> getSomeQuoteInvalid(int nb, int page) + /// + /// Asynchronously retrieves all invalid quotes from the list. + /// + /// A task representing the asynchronous operation, with a result of a list of invalid objects. + /// + /// This method retrieves all quotes using the method and filters them by the `IsValid` property. + /// It returns only those quotes where `IsValid` is set to `false`. + /// If no invalid quotes are found, an empty list is returned. + /// + public async Task> getAllQuoteInvalid() + { + var quotes = await getAllQuote(); + quotes = quotes.Where(q => q.IsValid == false).ToList(); + return quotes; + } + + + /// + /// Asynchronously retrieves a subset of invalid quotes based on the specified page number and the number of quotes per page. + /// + /// The number of invalid quotes to retrieve per page. + /// The page number for pagination. + /// A task representing the asynchronous operation, with a result of a list of invalid objects for the specified page. + /// + /// This method retrieves all invalid quotes using the method and then calculates the range of invalid quotes + /// to be returned based on the provided `nb` (number of quotes per page) and `page` (the page number). It ensures that + /// the returned subset does not exceed the total number of invalid quotes available. + /// + /// If the calculated range is larger than the available invalid quotes, it returns a subset of quotes from the end of the list. + /// If the requested page number exceeds the total number of pages, the method will return the last available page of invalid quotes. + /// + public async Task> getSomeQuoteInvalid(int nb, int page) + { + var quotes = await getAllQuoteInvalid(); + if ((page - 1) * nb + nb > quotes.Count()) { - var quotes = await getAllQuoteInvalid(); - if ((page - 1) * nb + nb > quotes.Count()) + if (nb > quotes.Count()) { - if (nb > quotes.Count()) - { - return quotes.GetRange(0, quotes.Count()); - } - return quotes.GetRange(quotes.Count() - nb, nb); + return quotes.GetRange(0, quotes.Count()); } - return quotes.GetRange((page - 1) * nb, nb); + return quotes.GetRange(quotes.Count() - nb, nb); } + return quotes.GetRange((page - 1) * nb, nb); + } + - public async Task getNbQuote() + /// + /// Asynchronously retrieves the total number of quotes. + /// + /// A task representing the asynchronous operation, with a result of the total number of objects. + /// + /// This method retrieves all quotes using the method and returns the count of quotes. + /// It provides the total number of quotes currently available in the data source. + /// + public async Task getNbQuote() + { + var data = await getAllQuote(); + return data.Count; + } + + + /// + /// Asynchronously retrieves a list of characters from a JSON file. + /// + /// A task representing the asynchronous operation, with a result of a list of objects. + /// + /// This method checks if the JSON file containing character data exists at the specified file path (`_char`). + /// If the file does not exist, it logs a message to the console and returns an empty list of characters. + /// If the file exists, it reads the JSON content, deserializes it into a list of objects, + /// and returns that list. If the deserialization results in a null value, an empty list is returned. + /// + public async Task> getChar() + { + if (!File.Exists(_char)) { - var data = await getAllQuote(); - return data.Count; + Console.Out.WriteLine($"{_char} not found"); + return new List(); } - public async Task> getChar() - { - if (!File.Exists(_char)) - { - Console.Out.WriteLine($"{_char} not found"); - return new List(); - } + var json = await File.ReadAllTextAsync(_char); + return JsonSerializer.Deserialize>(json) ?? new List(); + } - var json = await File.ReadAllTextAsync(_char); - return JsonSerializer.Deserialize>(json) ?? new List(); - } - public async Task> getSrc() + /// + /// Asynchronously retrieves a list of sources from a JSON file. + /// + /// A task representing the asynchronous operation, with a result of a list of objects. + /// + /// This method checks if the JSON file containing source data exists at the specified file path (`_src`). + /// If the file does not exist, it logs a message to the console and returns an empty list of sources. + /// If the file exists, it reads the JSON content, deserializes it into a list of objects, + /// and returns that list. If the deserialization results in a null value, an empty list is returned. + /// + public async Task> getSrc() + { + if (!File.Exists(_src)) { - if (!File.Exists(_src)) - { - Console.Out.WriteLine($"{_src} not found"); - return new List(); - } - - var json = await File.ReadAllTextAsync(_src); - return JsonSerializer.Deserialize>(json) ?? new List(); + Console.Out.WriteLine($"{_src} not found"); + return new List(); } -} - \ No newline at end of file + + var json = await File.ReadAllTextAsync(_src); + return JsonSerializer.Deserialize>(json) ?? new List(); + } + +} diff --git a/WF-WebAdmin/WF-WebAdmin/Service/UserServiceStub.cs b/WF-WebAdmin/WF-WebAdmin/Service/UserServiceStub.cs index 67cb03f..53770a3 100644 --- a/WF-WebAdmin/WF-WebAdmin/Service/UserServiceStub.cs +++ b/WF-WebAdmin/WF-WebAdmin/Service/UserServiceStub.cs @@ -8,12 +8,32 @@ public class UserServiceStub : IUserService private readonly string _jsonFilePath = Path.Combine(Environment.CurrentDirectory, "wwwroot", "fake_data_users.json"); + /// + /// Asynchronously saves a list of users to a JSON file. + /// + /// The list of objects to be saved to the file. + /// A task representing the asynchronous operation. + /// + /// This method serializes the provided list of objects into a JSON format using the `JsonSerializer`. + /// It then writes the serialized JSON string to the file specified by the `_jsonFilePath`. The JSON is written with indentation for readability. + /// public async Task saveUsersJson(List users) { var json = JsonSerializer.Serialize(users, new JsonSerializerOptions { WriteIndented = true }); await File.WriteAllTextAsync(_jsonFilePath, json); } + + /// + /// Asynchronously removes a user from the list of users and saves the updated list to a JSON file. + /// + /// The object to be removed from the list. + /// A task representing the asynchronous operation. + /// + /// This method retrieves the list of all users using the method, + /// then searches for the specified user by their `Id`. If a matching user is found, + /// they are removed from the list, and the updated list is saved back to the JSON file using the method. + /// public async Task removeUser(User user) { var data = await getAllUser(); @@ -25,18 +45,49 @@ public class UserServiceStub : IUserService } } + + /// + /// Asynchronously updates the role of a user, setting the user as an administrator. + /// + /// The object whose role is to be updated. + /// A task representing the asynchronous operation of updating the user's role. + /// + /// This method updates the `IsAdmin` property of the specified user to `true`, indicating that the user is an administrator. + /// It then calls the method to persist the updated user information. + /// public Task updateRole(User user) { user.IsAdmin = true; return updateUser(user); } + + /// + /// Asynchronously downgrades the role of a user, removing their administrator privileges. + /// + /// The object whose role is to be downgraded. + /// A task representing the asynchronous operation of downgrading the user's role. + /// + /// This method updates the `IsAdmin` property of the specified user to `false`, removing their administrator status. + /// It then calls the method to persist the updated user information. + /// public Task downgradeRole(User user) { user.IsAdmin = false; return updateUser(user); } + + /// + /// Asynchronously retrieves a list of all users from a JSON file. + /// + /// A task representing the asynchronous operation, with a result of a list of objects. + /// + /// This method checks if the JSON file containing user data exists at the specified file path (`_jsonFilePath`). + /// If the file does not exist, it logs a message to the console and returns an empty list of users. + /// If the file exists, it reads the JSON content, deserializes it into a list of objects, + /// and returns that list. If the deserialization results in a null value, an empty list is returned. + /// public async Task> getAllUser() { if (!File.Exists(_jsonFilePath)) @@ -49,6 +100,19 @@ public class UserServiceStub : IUserService return JsonSerializer.Deserialize>(json) ?? new List(); } + + /// + /// Asynchronously retrieves a paginated list of users from a JSON file. + /// + /// The number of users to retrieve per page. + /// The page number for the data to retrieve. + /// A task representing the asynchronous operation, with a result of a list of objects. + /// + /// This method retrieves all users using the method, then calculates the range of users to return + /// based on the specified page number and the number of users per page (`nb`). + /// It returns the corresponding subset of users for the given page. If the page exceeds the available number of users, + /// it returns the last `nb` users available. + /// public async Task> getSomeUser(int nb, int page) { var users = await getAllUser(); @@ -59,6 +123,17 @@ public class UserServiceStub : IUserService return users.GetRange((page - 1) * nb, nb); } + + /// + /// Asynchronously retrieves a single user by their ID from the JSON file. + /// + /// The ID of the user to retrieve. + /// A task representing the asynchronous operation, with a result of the object if found, otherwise null. + /// + /// This method retrieves all users using the method, + /// then searches for the user with the specified `id`. If a user with the given ID is found, + /// the user is returned. Otherwise, it returns null. + /// public async Task getOneUser(int id) { var data = await getAllUser(); @@ -70,28 +145,47 @@ public class UserServiceStub : IUserService return null; } + public Task> reserchUsers(string reserch, List args) { throw new NotImplementedException(); } + /// + /// Asynchronously retrieves the total number of users from the JSON file. + /// + /// A task representing the asynchronous operation, with a result of the total number of users. + /// + /// This method retrieves all users using the method and returns the count of users in the list. + /// public async Task getNbUser() { var data = await getAllUser(); return data.Count; } + + /// + /// Asynchronously updates the details of a user in the JSON file. + /// + /// The object containing the updated user details. + /// A task representing the asynchronous operation of updating the user. + /// + /// This method retrieves all users using the method, then searches for the user with the specified ID. + /// If a user with the given ID is found, it updates their details (Name, Email, Image, IsAdmin) based on the provided `user` object. + /// After updating the user, the modified list of users is saved back to the JSON file using the method. + /// public async Task updateUser(User user) { - var data = await getAllUser(); - var person = data.FirstOrDefault(p => p.Id == user.Id); - if (person != null) - { - person.Name = user.Name; - person.Email = user.Email; - person.Image = user.Image; - person.IsAdmin = user.IsAdmin; - await saveUsersJson(data); - } + var data = await getAllUser(); + var person = data.FirstOrDefault(p => p.Id == user.Id); + if (person != null) + { + person.Name = user.Name; + person.Email = user.Email; + person.Image = user.Image; + person.IsAdmin = user.IsAdmin; + await saveUsersJson(data); + } } } \ No newline at end of file diff --git a/WF-WebAdmin/WF-WebAdmin/Shared/CultureSelector.razor b/WF-WebAdmin/WF-WebAdmin/Shared/CultureSelector.razor index 44d3c10..7a48e2b 100644 --- a/WF-WebAdmin/WF-WebAdmin/Shared/CultureSelector.razor +++ b/WF-WebAdmin/WF-WebAdmin/Shared/CultureSelector.razor @@ -21,6 +21,16 @@ new CultureInfo("fr-FR") }; + /// + /// Gets or sets the current culture for the application, triggering a navigation to set the culture cookie when changed. + /// + /// + /// The getter retrieves the current culture of the application using . + /// The setter checks if the current UI culture matches the provided value. If they are the same, no action is taken. + /// If the cultures differ, it constructs a query string that includes the new culture and a redirect URI, + /// and then navigates to a "/Culture/SetCulture" endpoint to set the culture cookie. + /// The user is redirected to the same page with the new culture applied after the redirect. + /// private CultureInfo Culture { get => CultureInfo.CurrentCulture; @@ -40,4 +50,5 @@ this.NavigationManager.NavigateTo("/Culture/SetCulture" + query, forceLoad: true); } } + } \ No newline at end of file diff --git a/WF-WebAdmin/WF-WebAdmin/wwwroot/fake_data_quiz.json b/WF-WebAdmin/WF-WebAdmin/wwwroot/fake_data_quiz.json index b29ec18..f7d7fd6 100644 --- a/WF-WebAdmin/WF-WebAdmin/wwwroot/fake_data_quiz.json +++ b/WF-WebAdmin/WF-WebAdmin/wwwroot/fake_data_quiz.json @@ -7,7 +7,7 @@ "AnswerC": "do officia", "AnswerD": "ut nostrud", "CAnswer": "C", - "IsValid": false, + "IsValid": true, "UserProposition": "Brooks Martinez" }, {