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 @@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.
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.