From 764a44ecc6d4536d968685f9c7e41d81808200f7 Mon Sep 17 00:00:00 2001 From: borealiscat Date: Thu, 14 Nov 2024 21:26:40 +0000 Subject: [PATCH] Ooptimo backend test --- controllers/SiteController.php | 22 +++++++++ models/OoptimoUser.php | 79 +++++++++++++++++++++++++++++++++ services/OoptimoUserService.php | 35 +++++++++++++++ views/site/index.php | 3 +- views/site/test1.php | 37 ++++++++++++++- views/site/test2.php | 41 +++++++++++++++++ web/css/site.css | 5 +++ 7 files changed, 220 insertions(+), 2 deletions(-) create mode 100644 models/OoptimoUser.php create mode 100644 services/OoptimoUserService.php create mode 100644 views/site/test2.php diff --git a/controllers/SiteController.php b/controllers/SiteController.php index 54a70f1..553a8e3 100644 --- a/controllers/SiteController.php +++ b/controllers/SiteController.php @@ -2,6 +2,8 @@ namespace app\controllers; +use app\services\OoptimoUserService; +use yii\data\ArrayDataProvider; use yii\web\Controller; class SiteController extends Controller{ @@ -33,4 +35,24 @@ public function actionIndex(){ public function actionTest1(){ return $this->render('test1'); } + + /** + * Displays test 2. + * + * @return string + */ + public function actionTest2(){ + $ooptimoUserService = new OoptimoUserService(); + $data = $ooptimoUserService->fetchData(); + //Procesado de los datos en la capa de servicios + $usuarios = $ooptimoUserService->process($data); + + $dataProvider = new ArrayDataProvider([ + 'allModels' => $usuarios, + 'pagination' => ['pageSize'=>15] + ]); + + // Mostrar los usuarios + return $this->render('test2', ['usuarios' => $usuarios, 'dataProvider' => $dataProvider]); + } } diff --git a/models/OoptimoUser.php b/models/OoptimoUser.php new file mode 100644 index 0000000..756da2e --- /dev/null +++ b/models/OoptimoUser.php @@ -0,0 +1,79 @@ +id; + } + + /** + * @param mixed $id + */ + public function setId($id) + { + $this->id = $id; + } + + /** + * @return mixed + */ + public function getUserId() + { + return $this->userId; + } + + /** + * @param mixed $userId + */ + public function setUserId($userId) + { + $this->userId = $userId; + } + + /** + * @return mixed + */ + public function getTitle() + { + return $this->title; + } + + /** + * @param mixed $title + */ + public function setTitle($title) + { + $this->title = $title; + } + + /** + * @return mixed + */ + public function getBody() + { + return $this->body; + } + + /** + * @param mixed $body + */ + public function setBody($body) + { + $this->body = $body; + } + +} \ No newline at end of file diff --git a/services/OoptimoUserService.php b/services/OoptimoUserService.php new file mode 100644 index 0000000..d27243f --- /dev/null +++ b/services/OoptimoUserService.php @@ -0,0 +1,35 @@ +apiUrl); + + if ($response === false) { + return []; + } + + // Decodificar JSON + return json_decode($response, true); + } + + function process($usuarios){ + $usuarios = array_map( function($usuario){ + $tmpUser = new OoptimoUser(); + $tmpUser->setTitle( htmlspecialchars($usuario['title']) ); + $tmpUser->setBody(htmlspecialchars($usuario['body'])); + $tmpUser->setId( htmlspecialchars($usuario['id']) ); + $tmpUser->setUserId( htmlspecialchars($usuario['userId']) ); + return $tmpUser; + }, $usuarios); + return $usuarios; + } + +} \ No newline at end of file diff --git a/views/site/index.php b/views/site/index.php index 50f2905..7167a22 100644 --- a/views/site/index.php +++ b/views/site/index.php @@ -14,7 +14,8 @@

Solve the following exercises:

If you have any questions or issues, feel free to send an email with your queries to asunyer@ooptimo.com, and we will try to assist you as soon as possible.

diff --git a/views/site/test1.php b/views/site/test1.php index d08b3e3..f601add 100644 --- a/views/site/test1.php +++ b/views/site/test1.php @@ -8,7 +8,7 @@
-

Test 1 — Consume API

+

Test 1 — Consume API (filtrando los datos por backend, mediante framework Yii2)

The goal is to load data from an external API and display it on screen.

We will use the "Fake API" JSONPlaceholder to load data.
Use this endpoint: https://jsonplaceholder.typicode.com/posts, and display a list on this same page with the data retrieved.

@@ -28,6 +28,41 @@

Posts

+
+ + \ No newline at end of file diff --git a/views/site/test2.php b/views/site/test2.php new file mode 100644 index 0000000..3064aeb --- /dev/null +++ b/views/site/test2.php @@ -0,0 +1,41 @@ +title = 'Test 2 — ooptimo'; +?> +
+
+
+
+

Test 2 — Consumir API (filtrando los datos por backend, mediante framework Yii2)

+

El objetivo es cargar datos de una API externa y mostrarlos en pantalla.

+

Usaremos la "Fake API" JSONPlaceholder para cargar datos.
Usa este endpoint: https://jsonplaceholder.typicode.com/posts, y muestra una lista en esta misma página con los datos obtenidos.

+ +
Ayuda
+
    +
  • Estamos usando el framework MVC Yii v2: Guia.
  • +
  • La API externa es la de JSONPlaceholder: Guia
  • +
  • Los archivos relevantes estan en /controllers, /models y /views.
  • +
  • Para los estilos puedes editar directamente el archivo /web/css/site.css.
  • +
+
+
+
+ +
+ +
+
+

Posts

+ $dataProvider, + ]); + + ?> +
+
+
diff --git a/web/css/site.css b/web/css/site.css index 9b92ad0..7b7fab4 100644 --- a/web/css/site.css +++ b/web/css/site.css @@ -28,3 +28,8 @@ main > .container { margin-top: 5px; color: #999; } + +.pagination a{ + margin:2px; + text-decoration: none; +}