From 6eaa80172be81e6be995a0d1121ad0a08a514189 Mon Sep 17 00:00:00 2001 From: lebeaulato Date: Mon, 17 Mar 2025 17:31:38 +0100 Subject: [PATCH] Controller de quote --- WF_EF_Api/DTO/QuoteDTO.cs | 4 +- WF_EF_Api/DTO/TypeLangageDTO.cs | 15 ++ WF_EF_Api/Shared/IQuoteService.cs | 2 +- WF_EF_Api/StubApi/QuoteService.cs | 136 ++++++++++++++++++ .../WfApi/Controllers/QuotesController.cs | 71 +++++++++ WF_EF_Api/WfApi/Program.cs | 3 +- 6 files changed, 227 insertions(+), 4 deletions(-) create mode 100644 WF_EF_Api/DTO/TypeLangageDTO.cs create mode 100644 WF_EF_Api/StubApi/QuoteService.cs create mode 100644 WF_EF_Api/WfApi/Controllers/QuotesController.cs diff --git a/WF_EF_Api/DTO/QuoteDTO.cs b/WF_EF_Api/DTO/QuoteDTO.cs index 6d35842..510fa30 100644 --- a/WF_EF_Api/DTO/QuoteDTO.cs +++ b/WF_EF_Api/DTO/QuoteDTO.cs @@ -13,9 +13,9 @@ namespace DTO public string Character { get; set; } public string ImagePath { get; set; } public string TitleSource { get; set; } - public DateTime DateSource { get; set; } + public int DateSource { get; set; } public int Like { get; set; } - public string Langage { get; set; } + public TypeLangageDTO Langage { get; set; } public TypeSrcEnumDTO Type { get; set; } } } diff --git a/WF_EF_Api/DTO/TypeLangageDTO.cs b/WF_EF_Api/DTO/TypeLangageDTO.cs new file mode 100644 index 0000000..1320c27 --- /dev/null +++ b/WF_EF_Api/DTO/TypeLangageDTO.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DTO +{ + public enum TypeLangageDTO + { + vo, + vf, + ve + } +} diff --git a/WF_EF_Api/Shared/IQuoteService.cs b/WF_EF_Api/Shared/IQuoteService.cs index 5a4e5e9..d9ece83 100644 --- a/WF_EF_Api/Shared/IQuoteService.cs +++ b/WF_EF_Api/Shared/IQuoteService.cs @@ -25,7 +25,7 @@ namespace Shared // Retrieves all available quotes but filtered by language. // 'lang' is a language code used to filter the quotes in the chosen language. - Task> GetAllQuoteLang(int lang); + Task> GetAllQuoteLang(int index, int pageSize, int lang); // Retrieves a page of quote suggestions based on the index (pagination) and language. // 'index' is the starting point for pagination diff --git a/WF_EF_Api/StubApi/QuoteService.cs b/WF_EF_Api/StubApi/QuoteService.cs new file mode 100644 index 0000000..d45e6b6 --- /dev/null +++ b/WF_EF_Api/StubApi/QuoteService.cs @@ -0,0 +1,136 @@ +using DTO; +using Shared; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace StubApi +{ + public class QuoteService : IQuoteService + { + private readonly List _quotes; + + public QuoteService() + { + _quotes = new List + { + new QuoteDTO{Id=1, Content="Que la force soit avec toi", Character="Obi-Wan", ImagePath="http://image1", TitleSource="Star Wars", DateSource=1977, Like=32, Langage=TypeLangageDTO.vo, Type=TypeSrcEnumDTO.movie}, + new QuoteDTO{Id=2, Content="Je suis ton père", Character="Darth Vader", ImagePath="http://image2", TitleSource="Star Wars", DateSource=1980, Like=120, Langage=TypeLangageDTO.vo, Type=TypeSrcEnumDTO.movie}, + new QuoteDTO{Id=3, Content="Que la vengeance commence", Character="Anakin Skywalker", ImagePath="http://image3", TitleSource="Star Wars: Episode III", DateSource=2005, Like=95, Langage=TypeLangageDTO.vo, Type=TypeSrcEnumDTO.movie}, + new QuoteDTO{Id=4, Content="J'adore l'odeur du napalm au matin", Character="Kilgore", ImagePath="http://image4", TitleSource="Apocalypse Now", DateSource=1979, Like=56, Langage=TypeLangageDTO.vo, Type=TypeSrcEnumDTO.movie}, + new QuoteDTO{Id=5, Content="Je suis Groot", Character="Groot", ImagePath="http://image5", TitleSource="Guardians of the Galaxy", DateSource=2014, Like=200, Langage=TypeLangageDTO.vo, Type=TypeSrcEnumDTO.movie}, + new QuoteDTO{Id=6, Content="La vie est un rêve", Character="Don Quichotte", ImagePath="http://image6", TitleSource="Don Quichotte", DateSource=1605, Like=34, Langage=TypeLangageDTO.vf, Type=TypeSrcEnumDTO.book}, + new QuoteDTO{Id=7, Content="To infinity and beyond!", Character="Buzz Lightyear", ImagePath="http://image7", TitleSource="Toy Story", DateSource=1995, Like=180, Langage=TypeLangageDTO.vo, Type=TypeSrcEnumDTO.movie}, + new QuoteDTO{Id=8, Content="Je ne suis pas un héros", Character="Bruce Wayne", ImagePath="http://image8", TitleSource="The Dark Knight", DateSource=2008, Like=250, Langage=TypeLangageDTO.vo, Type=TypeSrcEnumDTO.movie}, + new QuoteDTO{Id=9, Content="Je suis le maître du jeu", Character="Jigsaw", ImagePath="http://image9", TitleSource="Saw", DateSource=2004, Like=65, Langage=TypeLangageDTO.vo, Type=TypeSrcEnumDTO.movie}, + new QuoteDTO{Id=10, Content="Liberté, égalité, fraternité", Character="La devise de la France", ImagePath="http://image10", TitleSource="Révolution française", DateSource=1789, Like=300, Langage=TypeLangageDTO.vf, Type=TypeSrcEnumDTO.book}, + new QuoteDTO{Id=11, Content="Un petit pas pour l'homme, un grand pas pour l'humanité", Character="Neil Armstrong", ImagePath="http://image11", TitleSource="Moon landing", DateSource=1969, Like=500, Langage=TypeLangageDTO.vo, Type=TypeSrcEnumDTO.book}, + new QuoteDTO{Id=12, Content="C'est la vie", Character="Charles", ImagePath="http://image12", TitleSource="French Movie", DateSource=2013, Like=45, Langage=TypeLangageDTO.vf, Type=TypeSrcEnumDTO.movie}, + new QuoteDTO{Id=13, Content="Voici Johnny!", Character="Jack Torrance", ImagePath="http://image13", TitleSource="The Shining", DateSource=1980, Like=300, Langage=TypeLangageDTO.vo, Type=TypeSrcEnumDTO.movie}, + new QuoteDTO{Id=14, Content="Je suis un simple serveur", Character="Hannibal", ImagePath="http://image14", TitleSource="The Silence of the Lambs", DateSource=1991, Like=200, Langage=TypeLangageDTO.vo, Type=TypeSrcEnumDTO.movie}, + new QuoteDTO{Id=15, Content="N'attends pas le moment parfait, prends le moment et rends-le parfait", Character="Zig Ziglar", ImagePath="http://image15", TitleSource="Motivation", DateSource=1995, Like=150, Langage=TypeLangageDTO.vf, Type=TypeSrcEnumDTO.book}, + new QuoteDTO{Id=16, Content="Le seul vrai voyage est de changer de regard", Character="Marcel Proust", ImagePath="http://image16", TitleSource="À la recherche du temps perdu", DateSource=1913, Like=125, Langage=TypeLangageDTO.vf, Type=TypeSrcEnumDTO.book}, + new QuoteDTO{Id=17, Content="La force sera avec toi, toujours", Character="Obi-Wan Kenobi", ImagePath="http://image17", TitleSource="Star Wars: Episode IV", DateSource=1977, Like=320, Langage=TypeLangageDTO.vo, Type=TypeSrcEnumDTO.movie}, + new QuoteDTO{Id=18, Content="J'ai toujours rêvé d'être un gangster", Character="Henry Hill", ImagePath="http://image18", TitleSource="Goodfellas", DateSource=1990, Like=500, Langage=TypeLangageDTO.vo, Type=TypeSrcEnumDTO.movie}, + new QuoteDTO{Id=19, Content="Je vous ai compris", Character="De Gaulle", ImagePath="http://image19", TitleSource="Discours de Gaulle", DateSource=1958, Like=250, Langage=TypeLangageDTO.vf, Type=TypeSrcEnumDTO.book}, + new QuoteDTO{Id=20, Content="Courage, fuyons!", Character="Frédéric", ImagePath="http://image20", TitleSource="Le Pourceau", DateSource=1834, Like=75, Langage=TypeLangageDTO.vf, Type=TypeSrcEnumDTO.book} + }; + } + + public async Task AddQuote(QuoteDTO quote) + { + throw new NotImplementedException(); + } + + public async Task CountQuotes() + { + throw new NotImplementedException(); + } + + public async Task> GetAllQuote() + { + throw new NotImplementedException(); + } + + public async Task> GetAllQuoteLang(int index, int pageSize, int lang) + { + //TypeLangageDTO langageType = lang; + //var listQuote = _quotes.Find(q => q.Langage == langageType); + //return new PaginationResult(_quotes.Count, index, pageSize, _quotes.Skip(index * pageSize).Take(pageSize).ToList()); + throw new NotImplementedException(); + + } + + public async Task GetDayliQuote(int lang) + { + throw new NotImplementedException(); + } + + public async Task> GetFavorites(int index, int pageSize, int UserId) + { + throw new NotImplementedException(); + } + + public async Task> GetInvalidQuote(int index, int pageSize) + { + throw new NotImplementedException(); + } + + public async Task GetLastQuoteId() // JE VOIS PAS L'INTERET + { + throw new NotImplementedException(); + } + + public async Task GetQuoteById(int id) + { + return _quotes.FirstOrDefault(q => q.Id == id); + } + + public async Task> GetSomeQuote(int index, int pageSize) + { + return new PaginationResult(_quotes.Count, index, pageSize, _quotes.Skip(index * pageSize).Take(pageSize).ToList()); + } + + public async Task> GetSuggestions(int index, int pageSize, int lang) + { + throw new NotImplementedException(); + } + + public async Task> GetValidQuote(int index, int pageSize) + { + throw new NotImplementedException(); + } + + public async Task RemoveQuote(int quoteId) + { + throw new NotImplementedException(); + } + + public async Task> SearchByCharacter(string character, int index, int pageSize, int lang) + { + throw new NotImplementedException(); + } + + public async Task> SearchByContent(string content, int index, int pageSize, int lang) + { + throw new NotImplementedException(); + } + + public async Task> SearchBySource(string source, int index, int pageSize, int lang) + { + throw new NotImplementedException(); + } + + public async Task UpdateQuote(int quoteId, QuoteDTO quote) + { + throw new NotImplementedException(); + } + + public async Task ValidateQuote(int quoteId, bool isValidate) + { + throw new NotImplementedException(); + } + } +} diff --git a/WF_EF_Api/WfApi/Controllers/QuotesController.cs b/WF_EF_Api/WfApi/Controllers/QuotesController.cs new file mode 100644 index 0000000..0a50e01 --- /dev/null +++ b/WF_EF_Api/WfApi/Controllers/QuotesController.cs @@ -0,0 +1,71 @@ +using DTO; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Shared; +using System.Net; + +namespace WfApi.Controllers +{ + [ApiController] + [Route("api/v1/quote")] //Version API + public class QuotesController : ControllerBase + { + private readonly IQuoteService _quote; + + private readonly ILogger _logger; + public QuotesController(IQuoteService quoteService, ILogger logger) + { + _quote = quoteService; + _logger = logger; + + } + + + + //===================================== ROUTE GET ===================================== + [HttpGet("id/{id}")] // Indiquer que l'id est dans l'URL + public async Task GetQuote(int id) + { + try + { + var result = _quote.GetQuoteById(id); + + if (result.IsCompletedSuccessfully) + { + return await Task.FromResult(Ok(result)); + } + else + { + return NoContent(); + } + } + catch (Exception) + { + return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" }); + } + } + + + [HttpGet("all")] // Indiquer que l'id est dans l'URL + public async Task GetAllQuote(int index = 0, int count = 10) + { + try + { + var result = _quote.GetSomeQuote(index, count); + + if (result.IsCompletedSuccessfully) + { + return await Task.FromResult(Ok(result)); + } + else + { + return NoContent(); + } + } + catch (Exception) + { + return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" }); + } + } + } +} diff --git a/WF_EF_Api/WfApi/Program.cs b/WF_EF_Api/WfApi/Program.cs index 0e7d0a2..5813f96 100644 --- a/WF_EF_Api/WfApi/Program.cs +++ b/WF_EF_Api/WfApi/Program.cs @@ -5,6 +5,7 @@ using StubApi; var builder = WebApplication.CreateBuilder(args); builder.Services.AddScoped, UserService>(); +builder.Services.AddScoped, QuoteService>(); // Add services to the container. builder.Services.AddControllers(); @@ -14,7 +15,7 @@ builder.Services.AddOpenApiDocument(options => { document.Info = new NSwag.OpenApiInfo { Version = "v1", - Title = "WtfAPI", + Title = "What The Fantasy's API", Description = "What The Fantasy Site API", TermsOfService = "", Contact = new NSwag.OpenApiContact