using DTO; using DTOToEntity; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace API.Controllers { [Authorize] [ApiController] [Route("api/v{version:apiVersion}/[controller]")] [ApiVersion("1.0")] public class VocabularyController : ControllerBase { private readonly IVocabularyService _service; private readonly ILogger _logger; public VocabularyController(IVocabularyService vocService, ILogger logger) { _service = vocService; _logger = logger; } [HttpGet] public async Task>> GetVocabularies(int index, int count) { try { _logger.LogInformation("Getting vocabularies "); var vocabularies = await _service.Gets(index, count); return vocabularies; } catch (Exception ex) { // Journaliser l'exception _logger.LogError(ex, "Une erreur s'est produite lors de la récupération des vocabulaires."); // Retourner une réponse d'erreur return StatusCode(400, ex.Message); } } [HttpGet("{word}")] public async Task> GetVocabulary(string word) { try { _logger.LogInformation("Getting a vocabulary with id {id}", word); var vocabulary = await _service.GetById(word); return vocabulary; } catch (Exception ex) { // Journaliser l'exception _logger.LogError(ex, "Une erreur s'est produite lors de la récupération du vocabulaire avec l'ID {word}.", word); // Retourner une réponse d'erreur return StatusCode(400, ex.Message); } } [HttpPut] public async Task> UpdateVocabulary([FromQuery] VocabularyDTO vocabulary) { try { _logger.LogInformation("Updating a vocabulary with word : {word}", vocabulary.word); var updatedVocabulary = await _service.Update(vocabulary); return updatedVocabulary; } catch (Exception ex) { // Journaliser l'exception _logger.LogError(ex, "Une erreur s'est produite lors de la mise à jour du vocabulaire avec l'ID {word}.", vocabulary.word); // Retourner une réponse d'erreur return StatusCode(400, ex.Message); } } [HttpDelete("{word}")] public async Task> DeleteVocabulary(string word) { try { _logger.LogInformation("Deleting a vocabulary with word : {word}", word); var vocabulary = await _service.Delete(word); return vocabulary; } catch (Exception ex) { // Journaliser l'exception _logger.LogError(ex, "Une erreur s'est produite lors de la suppression du vocabulaire avec l'ID {word}.", word); // Retourner une réponse d'erreur return StatusCode(400, ex.Message); } } [HttpPost] public async Task> AddVocabulary([FromQuery] VocabularyDTO vocabulary) { try { _logger.LogInformation("Adding a vocabulary with word : {word}", vocabulary.word); var newVocabulary = await _service.Add(vocabulary); return newVocabulary; } catch (Exception ex) { // Journaliser l'exception _logger.LogError(ex, "Une erreur s'est produite lors de l'ajout du vocabulaire avec l'ID {word}.", vocabulary.word); // Retourner une réponse d'erreur return StatusCode(400, ex.Message); } } [HttpGet("langue/{langue}")] public async Task>> GetByLangue(string langue, int index, int count) { try { _logger.LogInformation("Getting vocabularies by langue {langue}",langue); var vocabularies = await _service.GetByLangue(index, count, langue); return vocabularies; } catch (Exception ex) { // Journaliser l'exception _logger.LogError(ex, "Une erreur s'est produite lors de la récupération des vocabulaires par langue {langue}.",langue); // Retourner une réponse d'erreur return StatusCode(400, ex.Message); } } [HttpPost("AddTranslation")] public async Task> AddTranslation([FromQuery] string vocId, long translationId) { try { _logger.LogInformation("Adding a translation with id : {id}", vocId); var newVocabulary = await _service.AddTranslationToVocabulary(vocId, translationId); return newVocabulary; } catch (Exception ex) { // Journaliser l'exception _logger.LogError(ex, "Une erreur s'est produite lors de l'ajout de la traduction avec l'ID {id}.", vocId); // Retourner une réponse d'erreur return StatusCode(400, ex.Message); } } } }