From 1734512b0be39ad5814453b56e909a20ee51b191 Mon Sep 17 00:00:00 2001 From: kekentin Date: Wed, 2 Apr 2025 11:44:44 +0200 Subject: [PATCH] correction route Quote --- WF_EF_Api/Contextlib/DbQuoteManager.cs | 43 ++++++++------- WF_EF_Api/Dto2Entities/Extention.cs | 4 ++ WF_EF_Api/ServicesApi/QuoteService.cs | 6 ++- .../WfApi/Controllers/QuotesController.cs | 52 ++++++++++++------- 4 files changed, 64 insertions(+), 41 deletions(-) diff --git a/WF_EF_Api/Contextlib/DbQuoteManager.cs b/WF_EF_Api/Contextlib/DbQuoteManager.cs index d72841a..ef69d71 100644 --- a/WF_EF_Api/Contextlib/DbQuoteManager.cs +++ b/WF_EF_Api/Contextlib/DbQuoteManager.cs @@ -54,20 +54,11 @@ namespace Contextlib { List quotes = await _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) -<<<<<<< HEAD .ToListAsync(); + if (quotes.Count() == 0) return null; Quote quote = quotes[date.DayNumber % quotes.Count()]; - /*Quote quote = await _context.quotes.Where(item => item.Id == date.DayNumber % quotes.Count()) - .Include(q => q.Source).Include(q => q.Character).ThenInclude(c => c.Images).Include(q => q.Favorite) - .FirstOrDefaultAsync() ?? quotes.First();*/ - //Quote quote = _repo.GetById(date.DayNumber % quotes.Count()) ?? quotes.First(); -======= - .ToList(); - if (quotes.Count() == 0) return null; - Quote quote = quotes[ date.DayNumber % quotes.Count() ] ; ->>>>>>> 8471e6cfd50227037555f6650ed659ac091cc613 return quote; } @@ -113,15 +104,12 @@ namespace Contextlib return lastQuoteId; } - public async Task GetQuoteById(int id) + public async Task GetQuoteById(int 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}."); - } + .FirstOrDefault(); + return quote; } @@ -186,8 +174,9 @@ namespace Contextlib public async Task RemoveQuote(int quoteId) { - - _repo.Delete( _repo.GetById(quoteId) ); + var quote = _repo.GetById(quoteId); + if (quote == null) throw new KeyNotFoundException(); + _repo.Delete( quote ); await _context.SaveChangesAsync(); } @@ -217,11 +206,27 @@ namespace Contextlib q.IdUsersPropose = quote.IdUsersPropose; change = true; } - if (quote.Content != null || quote.Content =="") + if (quote.Content != null || quote.Content == "") { q.Content = quote.Content; change = true; } + if (quote.IsValid != q.IsValid) + { + q.IsValid = quote.IsValid; + change = true; + } + if (quote.Likes != q.Likes) + { + q.Likes = quote.Likes; + change = true; + } + if (quote.Langage != q.Langage) + { + q.Langage = quote.Langage; + change = true; + } + _repo.Update(q); if (change) _context.SaveChanges(); } diff --git a/WF_EF_Api/Dto2Entities/Extention.cs b/WF_EF_Api/Dto2Entities/Extention.cs index 0fda06d..0077a6f 100644 --- a/WF_EF_Api/Dto2Entities/Extention.cs +++ b/WF_EF_Api/Dto2Entities/Extention.cs @@ -388,13 +388,17 @@ namespace Dto2Entities Quote quote = new Quote(); quote.Id = item.Id; quote.Content = item.Content; + quote.Source = new Source(); quote.Source.Year = item.DateSource; + quote.Character = new Character(); quote.Character.Name = item.Character; quote.Source.Title = item.TitleSource; quote.Langage = item.Langage.ToEntity(); + quote.Character.Images = new Images(); quote.Character.Images.ImgPath = item.ImagePath; quote.Likes = item.Like; quote.Source.TypeSrc = item.Type.ToEntity(); + quote.IsValid = item.IsValide; return quote; } diff --git a/WF_EF_Api/ServicesApi/QuoteService.cs b/WF_EF_Api/ServicesApi/QuoteService.cs index fc1be81..cdf66a2 100644 --- a/WF_EF_Api/ServicesApi/QuoteService.cs +++ b/WF_EF_Api/ServicesApi/QuoteService.cs @@ -65,9 +65,11 @@ namespace ServicesApi return await quoteService.GetLastQuoteId(); } - public async Task GetQuoteById(int id) + public async Task GetQuoteById(int id) { - return quoteService.GetQuoteById(id).Result.ToDto(); + var quote= quoteService.GetQuoteById(id).Result; + if (quote != null) return quote.ToDto(); + else return null; } public async Task> GetSomeQuote(int index, int pageSize) diff --git a/WF_EF_Api/WfApi/Controllers/QuotesController.cs b/WF_EF_Api/WfApi/Controllers/QuotesController.cs index f3a1eb2..1eda2f1 100644 --- a/WF_EF_Api/WfApi/Controllers/QuotesController.cs +++ b/WF_EF_Api/WfApi/Controllers/QuotesController.cs @@ -33,7 +33,10 @@ namespace WfApi.Controllers try { var result = await _quote.GetQuoteById(id); - + if (result == null) + { + throw new KeyNotFoundException($"Error : No quotes found with the ID: {id}."); + } if (result!=null) { return await Task.FromResult(Ok(result)); @@ -256,20 +259,27 @@ namespace WfApi.Controllers { try { - if (newQuote == null) + try { - return BadRequest(new { message = "Les données de la quote sont requises." }); - } + if (newQuote == null) + { + return BadRequest(new { message = "Les données de la quote sont requises." }); + } - var existingPlayer = _quote.GetQuoteById(newQuote.Id).Result; - if (existingPlayer != null) - { - return Conflict(new { message = "Une quote avec cet ID existe déjà." }); - } - _quote.AddQuote(newQuote); + if (await _quote.GetQuoteById(newQuote.Id) != null) + { + return Conflict(new { message = "Une quote avec cet ID existe déjà." }); + } + newQuote.IsValide=false; + var quote=_quote.AddQuote(newQuote); - return CreatedAtAction(nameof(GetAllQuote), new { id = newQuote.Id }, newQuote); + return CreatedAtAction(nameof(CreateQuote), new { id = newQuote.Id }, quote); + } + catch (KeyNotFoundException e) + { + return StatusCode((int)HttpStatusCode.NotFound, e); + } } catch (Exception) { @@ -331,22 +341,24 @@ namespace WfApi.Controllers [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task DeleteQuote([FromQuery] int idQuote) { - try - { - var result = _quote.RemoveQuote(idQuote); - - if (result.IsCompletedSuccessfully) + try { + try { - return await Task.FromResult(Ok(result)); + _quote.RemoveQuote(idQuote).Wait(); + + + return await Task.FromResult(Ok()); + + } - else + catch (KeyNotFoundException e) { - return NotFound(); + return StatusCode((int)HttpStatusCode.NotFound, e); } } catch (Exception) { - return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" }); + return StatusCode((int) HttpStatusCode.InternalServerError, new { message = "Erreur interne du serveur." }); } } }