From 62a319396319de7c5b2b386991b3c979442e9780 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20FAMIBELLE-PRONZOLA?= Date: Sat, 4 Jun 2022 01:15:42 +0400 Subject: [PATCH] Create services with validateParoles and translateLyrics --- src/api/parole/services/parole.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/api/parole/services/parole.js b/src/api/parole/services/parole.js index 3d6f3c5..d235a7d 100644 --- a/src/api/parole/services/parole.js +++ b/src/api/parole/services/parole.js @@ -3,6 +3,7 @@ const qs = require('qs') const axios = require('axios') const { createCoreService } = require('@strapi/strapi').factories; +const { ValidationError } = require("@strapi/utils").errors class Translator { constructor() { @@ -37,5 +38,32 @@ module.exports = createCoreService('api::parole.parole', ({strapi}) => ({ const translator = new Translator() const data = await translator.get(origin, target, text) return data.translations[0].text + }, + async translateLyrics(parolesFR) { + const anglais = await this.translate('FR', 'EN', parolesFR) + const espagnol = await this.translate('FR', 'ES', parolesFR) + const allemand = await this.translate('FR', 'DE', parolesFR) + const italien = await this.translate('FR', 'IT', parolesFR) + + return { + francais: parolesFR, + anglais: anglais + '\n\n (Translated by DeepL)', + espagnol: espagnol + '\n\n (Traducido por DeepL)', + allemand: allemand + '\n\n (Übersetzt von DeepL)', + italien: italien + '\n\n (Tradotto da DeepL)' + } + }, + validateParoles(titre, transcription) { + if (!titre || titre.trim().length === 0) { + throw new ValidationError('Champ obligatoire. Veuillez choisir un titre.'); + } + + if (!transcription || transcription.trim().length === 0) { + throw new ValidationError('Champ obligatoire. Veuillez renseigner la transcription.') + } + + if (transcription.trim().length < 10) { + throw new ValidationError('La transcription doit contenir au moins 10 caractères.') + } } }));