diff --git a/WF_EF_Api/Contextlib/DbQuoteManager.cs b/WF_EF_Api/Contextlib/DbQuoteManager.cs index 9d8b2f7..24915ce 100644 --- a/WF_EF_Api/Contextlib/DbQuoteManager.cs +++ b/WF_EF_Api/Contextlib/DbQuoteManager.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Entity; +using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Internal; using Shared; @@ -21,9 +22,6 @@ namespace Contextlib } - - - public async Task AddQuote(Quote quote) { if (quote == null) @@ -36,26 +34,25 @@ namespace Contextlib public async Task> GetAllQuote() { - List quotes = _repo.GetItems(item=> item.IsValid, - 0, _repo.Count(), [] - ).ToList(); + List quotes = _context.quotes.Where(item => item.IsValid) + .Include(q => q.Source).Include(q => q.Character).ThenInclude(c => c.Images) + .ToList(); return new PaginationResult(quotes.Count, 0, quotes.Count, quotes); } public async Task> GetAllQuoteLang(int index, int pageSize, int lang) { - List quotes = _repo.GetItems(item => item.IsValid && item.Langage==(LangEnum)lang , - index, pageSize, [] - ).ToList(); - + List quotes = _context.quotes.Where(item => item.IsValid && item.Langage == (LangEnum)lang) + .Include(q => q.Source).Include(q => q.Character).ThenInclude(c => c.Images) + .Skip(index * pageSize).Take(pageSize).ToList(); return new PaginationResult(quotes.Count, index, pageSize, quotes); } public async Task GetDailyQuote(DateOnly date, int lang) { - List quotes = _repo.GetItems(item => item.IsValid && item.Langage == (LangEnum)lang, - 0, _repo.Count(), [] - ).ToList(); + List quotes = _context.quotes.Where(item => item.IsValid && item.Langage == (LangEnum)lang) + .Include(q => q.Source).Include(q => q.Character).ThenInclude(c => c.Images).Include(q => q.Favorite) + .ToList(); Quote quote = _repo.GetById(date.DayNumber % quotes.Count()) ?? quotes.First(); return quote; @@ -63,36 +60,36 @@ namespace Contextlib public async Task> GetFavorites(int index, int pageSize, int UserId) { - List quotes = _repo.GetItems(item => item.IsValid && item.Favorite.Where(p=>p.Id==UserId)!=null , - index, pageSize, [nameof(Quote.Favorite)] - ).ToList(); + List quotes = _context.quotes.Where(item => item.IsValid && item.Favorite.Where(p => p.Id == UserId) != null) + .Include(q => q.Source).Include(q => q.Character).ThenInclude(c => c.Images).Include(q => q.Favorite) + .Skip(index * pageSize).Take(pageSize).ToList(); return new PaginationResult(quotes.Count, index, pageSize, quotes); } public async Task> GetInvalidQuote(int index, int pageSize, int lang) { - List quotes = _repo.GetItems(item => !item.IsValid && item.Langage == (LangEnum)lang, - index, pageSize, [] - ).ToList(); + List quotes = _context.quotes.Where(item => !item.IsValid && item.Langage == (LangEnum)lang) + .Include(q => q.Source).Include(q => q.Character).ThenInclude(c => c.Images) + .Skip(index * pageSize).Take(pageSize).ToList(); return new PaginationResult(quotes.Count, index, pageSize, quotes); } public async Task> GetInvalidQuote(int index, int pageSize) { - List quotes = _repo.GetItems(item => !item.IsValid, - index, pageSize, [] - ).ToList(); + List quotes = _context.quotes.Where(item => !item.IsValid) + .Include(q => q.Source).Include(q => q.Character).ThenInclude(c => c.Images) + .Skip(index * pageSize).Take(pageSize).ToList(); return new PaginationResult(quotes.Count, index, pageSize, quotes); } public async Task GetLastQuoteId() { - PaginationResult users = await GetAllQuote(); + PaginationResult quotes = await GetAllQuote(); int lastQuoteId = 0; - foreach (Quote quote in users.items) + foreach (Quote quote in quotes.items) { if (quote.Id >= lastQuoteId) { @@ -104,7 +101,9 @@ namespace Contextlib public async Task GetQuoteById(int id) { - Quote? quote = _repo.GetById(id, item => item.Id == id, []); + Quote? quote = _context.quotes.Where(item => item.Id == id) + .Include(q => q.Source).Include(q => q.Character).ThenInclude(c => c.Images) + .First(); if (quote == null) { throw new KeyNotFoundException($"Error : No quotes found with the ID: {id}."); diff --git a/WF_EF_Api/StubbedContextLib/StubWTFContext.cs b/WF_EF_Api/StubbedContextLib/StubWTFContext.cs index 7381211..677e88e 100644 --- a/WF_EF_Api/StubbedContextLib/StubWTFContext.cs +++ b/WF_EF_Api/StubbedContextLib/StubWTFContext.cs @@ -69,7 +69,7 @@ namespace StubbedContextLib new Quote() { Id = 7, Content = "Je suis la dernière Targaryen. Je suis la reine des dragons", IdCharacter = 7, IdSource = 3, IdUsersPropose = 1, IsValid = true, Langage = LangEnum.vf, Likes = 11025 }, new Quote() { Id = 8, Content = "Je ne suis pas prêt à affronter ça. C'est trop pour moi.", IdCharacter = 8, IdSource = 5, IdUsersPropose = 1, IsValid = true, Langage = LangEnum.vf, Likes = 11025 }, new Quote() { Id = 9, Content = "Aidez-moi, Obi-Wan Kenobi, vous êtes mon seul espoir.", IdCharacter = 9, IdSource = 5, IdUsersPropose = 1, IsValid = true, Langage = LangEnum.vf, Likes = 11025 }, - new Quote() { Id = 10, Content = "La quoi ?", IdCharacter = 10, IdSource = 4, IdUsersPropose = 1, IsValid = true, Langage = LangEnum.vf, Likes = 11025 } + new Quote() { Id = 10, Content = "La quoi ?", IdCharacter = 10, IdSource = 4, IdUsersPropose = 1, IsValid = false, Langage = LangEnum.vf, Likes = 11025 } ); modelBuilder.Entity().HasData( diff --git a/WF_EF_Api/WfApi/Controllers/CommentariesController.cs b/WF_EF_Api/WfApi/Controllers/CommentariesController.cs index 737acea..6d47769 100644 --- a/WF_EF_Api/WfApi/Controllers/CommentariesController.cs +++ b/WF_EF_Api/WfApi/Controllers/CommentariesController.cs @@ -26,9 +26,9 @@ namespace WfApi.Controllers { try { - var result = _commentary.GetCommentaryByQuote(id, index, count); + var result =await _commentary.GetCommentaryByQuote(id, index, count); - if (result.IsCompletedSuccessfully) + if (result != null) { return await Task.FromResult(Ok(result)); } diff --git a/WF_EF_Api/WfApi/Controllers/QuotesController.cs b/WF_EF_Api/WfApi/Controllers/QuotesController.cs index e2dd44d..521a2a2 100644 --- a/WF_EF_Api/WfApi/Controllers/QuotesController.cs +++ b/WF_EF_Api/WfApi/Controllers/QuotesController.cs @@ -25,13 +25,16 @@ namespace WfApi.Controllers //===================================== ROUTE GET ===================================== [HttpGet("{id}")] // Indiquer que l'id est dans l'URL + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status204NoContent)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task GetQuote(int id) { try { - var result = _quote.GetQuoteById(id); + var result = await _quote.GetQuoteById(id); - if (result.IsCompletedSuccessfully) + if (result!=null) { return await Task.FromResult(Ok(result)); } @@ -48,13 +51,16 @@ namespace WfApi.Controllers [HttpGet("all")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status204NoContent)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task GetAllQuote(int index = 0, int count = 10) { try { - var result = _quote.GetSomeQuote(index, count); + var result = await _quote.GetSomeQuote(index, count); - if (result.IsCompletedSuccessfully) + if (result != null) { return await Task.FromResult(Ok(result)); } @@ -69,13 +75,16 @@ namespace WfApi.Controllers } } [HttpGet("allbylang")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status204NoContent)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task GetAllQuoteByLang(TypeLangageDTO lang,int index = 0, int count = 10) { try { - var result = _quote.GetAllQuoteLang(index, count,(int)lang); + var result = await _quote.GetAllQuoteLang(index, count,(int)lang); - if (result.IsCompletedSuccessfully) + if (result != null) { return await Task.FromResult(Ok(result)); } @@ -90,15 +99,18 @@ namespace WfApi.Controllers } } [HttpGet("dailyquote")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status204NoContent)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task GetDailyQuote([FromQuery] int year, [FromQuery] int month, [FromQuery] int day, TypeLangageDTO lang) { try { DateOnly date = new DateOnly(year, month, day); - var result = _quote.GetDailyQuote(date, (int)lang); + var result = await _quote.GetDailyQuote(date, (int)lang); - if (result.IsCompletedSuccessfully) + if (result != null) { return await Task.FromResult(Ok(result)); } @@ -113,13 +125,16 @@ namespace WfApi.Controllers } } [HttpGet("invalid")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status204NoContent)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task GetInvalidQuote(TypeLangageDTO lang, int index = 0, int count = 10) { try { - var result = _quote.GetInvalidQuote(index, count, (int)lang); + var result =await _quote.GetInvalidQuote(index, count, (int)lang); - if (result.IsCompletedSuccessfully) + if (result != null) { return await Task.FromResult(Ok(result)); } @@ -134,13 +149,16 @@ namespace WfApi.Controllers } } [HttpGet("suggest")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status204NoContent)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task GetSuggestQuote(TypeLangageDTO lang, int index = 0, int count = 10) { try { - var result = _quote.GetSuggestions(index, count, (int)lang); + var result = await _quote.GetSuggestions(index, count, (int)lang); - if (result.IsCompletedSuccessfully) + if (result != null) { return await Task.FromResult(Ok(result)); } @@ -155,13 +173,16 @@ namespace WfApi.Controllers } } [HttpGet("searchByCharacter")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status204NoContent)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task GetQuoteSearchByCharacter(TypeLangageDTO lang, string character, int index = 0, int count = 10) { try { - var result = _quote.SearchByCharacter(character, index, count, (int)lang); + var result = await _quote.SearchByCharacter(character, index, count, (int)lang); - if (result.IsCompletedSuccessfully) + if (result != null) { return await Task.FromResult(Ok(result)); } @@ -176,13 +197,16 @@ namespace WfApi.Controllers } } [HttpGet("searchByCharac")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status204NoContent)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task GetQuoteSearchBySource(TypeLangageDTO lang, string source, int index = 0, int count = 10) { try { - var result = _quote.SearchBySource(source, index, count, (int)lang); + var result = await _quote.SearchBySource(source, index, count, (int)lang); - if (result.IsCompletedSuccessfully) + if (result != null) { return await Task.FromResult(Ok(result)); } @@ -197,13 +221,16 @@ namespace WfApi.Controllers } } [HttpGet("searchByContent")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status204NoContent)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task GetQuoteSearchByContent(TypeLangageDTO lang, string content, int index = 0, int count = 10) { try { - var result = _quote.SearchByContent(content, index, count, (int)lang); + var result =await _quote.SearchByContent(content, index, count, (int)lang); - if (result.IsCompletedSuccessfully) + if (result != null) { return await Task.FromResult(Ok(result)); } @@ -221,6 +248,9 @@ namespace WfApi.Controllers //===================================== ROUTE POST ===================================== [HttpPost] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status204NoContent)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task CreateQuote([FromBody] QuoteDTO newQuote) { try @@ -247,6 +277,9 @@ namespace WfApi.Controllers } //===================================== ROUTE PUT ===================================== [HttpPut()] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status204NoContent)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task UpdateQuote([FromQuery] int id, [FromBody] QuoteDTO updatedquote) { try @@ -266,6 +299,9 @@ namespace WfApi.Controllers } } [HttpPut("valide")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status204NoContent)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task ValideQuote([FromQuery] int id) { try @@ -289,6 +325,9 @@ namespace WfApi.Controllers //===================================== ROUTE DELETE ===================================== [HttpDelete("delete")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status204NoContent)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task DeleteQuote([FromQuery] int idQuote) { try diff --git a/WF_EF_Api/WfApi/Controllers/UsersController.cs b/WF_EF_Api/WfApi/Controllers/UsersController.cs index 1950bbd..701f486 100644 --- a/WF_EF_Api/WfApi/Controllers/UsersController.cs +++ b/WF_EF_Api/WfApi/Controllers/UsersController.cs @@ -53,9 +53,9 @@ namespace WfApi.Controllers { try { - var result = _user.GetUserById(id); + var result =await _user.GetUserById(id); - if (result.IsCompletedSuccessfully) + if (result != null) { return await Task.FromResult(Ok(result)); } @@ -104,9 +104,9 @@ namespace WfApi.Controllers { try { - var result = _user.GetSomeUser(index, count); + var result =await _user.GetSomeUser(index, count); - if (result.IsCompletedSuccessfully) + if (result != null) { return await Task.FromResult(Ok(result)); } @@ -163,9 +163,9 @@ namespace WfApi.Controllers try { - var result = _user.GetHashPassword(username); + var result =await _user.GetHashPassword(username); - if (result.IsCompletedSuccessfully) + if (result != null) { return await Task.FromResult(Ok(result)); } @@ -222,9 +222,9 @@ namespace WfApi.Controllers try { - var result = _user.GetUserByUsername(username); - - if (result.IsCompletedSuccessfully) + var result =await _user.GetUserByUsername(username); + + if (result != null) { return await Task.FromResult(Ok(result)); } @@ -280,9 +280,9 @@ namespace WfApi.Controllers } try { - var result = _user.GetUserByEmail(email); + var result =await _user.GetUserByEmail(email); - if (result.IsCompletedSuccessfully) + if (result != null) { return await Task.FromResult(Ok(result)); } @@ -329,9 +329,9 @@ namespace WfApi.Controllers { try { - var result = _user.CountUser(); + var result =await _user.CountUser(); - if (result.IsCompletedSuccessfully) + if (result!=null) { return await Task.FromResult(Ok(result)); } @@ -389,9 +389,9 @@ namespace WfApi.Controllers } try { - var result = _user.ExistUsername(username); + var result =await _user.ExistUsername(username); - if (result.IsCompletedSuccessfully) + if (result!=null) { return await Task.FromResult(Ok(result)); } @@ -448,9 +448,9 @@ namespace WfApi.Controllers } try { - var result = _user.ExistEmail(email); + var result =await _user.ExistEmail(email); - if (result.IsCompletedSuccessfully) + if (result!=null) { return await Task.FromResult(Ok(result)); } diff --git a/WF_EF_Api/WfApi/Program.cs b/WF_EF_Api/WfApi/Program.cs index 184781b..5c683a3 100644 --- a/WF_EF_Api/WfApi/Program.cs +++ b/WF_EF_Api/WfApi/Program.cs @@ -15,6 +15,7 @@ builder.Services.AddScoped, CommentaryService> //EF builder.Services.AddScoped(); + builder.Services.AddScoped, DbUsersManager>(); builder.Services.AddScoped, DbQuoteManager>(); //builder.Services.AddScoped, DbCharacterManager>();