diff --git a/README.md b/README.md index e027304..2353dfd 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,4 @@ -OOPTIMO Job Application tests — PHP ------------- +# OOPTIMO Job Application tests — PHP Please follow the instructions below carefully. If you have any questions, feel free to reach out. @@ -8,7 +7,7 @@ Please follow the instructions below carefully. If you have any questions, feel This exercise consists of adding new functionalities to this app. To do so, run the app and you will find the instructions on how to implement each exercise. -*Note: This is a project based on the basic template of [Yii 2](https://www.yiiframework.com/) but you don't need to know much about Yii or do it "the yii way". PHP, MVC, Composer and git is enought.* +_Note: This is a project based on the basic template of [Yii 2](https://www.yiiframework.com/) but you don't need to know much about Yii or do it "the yii way". PHP, MVC, Composer and git is enought._ ## Instructions @@ -16,9 +15,9 @@ To do so, run the app and you will find the instructions on how to implement eac 2. Clone the repository to your local machine to work on the test. 3. Create a new branch to work on. 4. Solve the problem(s) described in the **Task** section. - 1. Install dependencies: `composer install` - 2. Run `php yii serve` to start the development server at `http://localhost:8080/`. - 3. Write your code. + 1. Install dependencies: `composer install` + 2. Run `php yii serve` to start the development server at `http://localhost:8080/`. + 3. Write your code. 5. Push your changes to your fork. 6. Submit a pull request (PR) @@ -48,4 +47,4 @@ To do so, run the app and you will find the instructions on how to implement eac We will review your pull request and provide feedback. There is no need to merge the PR, as we will handle that. Once reviewed, your PR will be closed. -#### Thank you for your time, and good luck! +### Thank you for your time, and good luck! diff --git a/composer.json b/composer.json index f60a8f9..d56093b 100644 --- a/composer.json +++ b/composer.json @@ -5,6 +5,7 @@ "homepage": "https://www.yiiframework.com/", "type": "project", "license": "BSD-3-Clause", + "version": "0.0.2", "support": { "issues": "https://github.com/yiisoft/yii2/issues?state=open", "forum": "https://www.yiiframework.com/forum/", @@ -17,7 +18,8 @@ "php": ">=7.4.0", "yiisoft/yii2": "~2.0.45", "yiisoft/yii2-bootstrap5": "~2.0.2", - "yiisoft/yii2-symfonymailer": "~2.0.3" + "yiisoft/yii2-symfonymailer": "~2.0.3", + "yiisoft/yii2-httpclient": "^2.0" }, "require-dev": { "yiisoft/yii2-debug": "~2.1.0", diff --git a/controllers/SiteController.php b/controllers/SiteController.php index 54a70f1..7713115 100644 --- a/controllers/SiteController.php +++ b/controllers/SiteController.php @@ -3,12 +3,17 @@ namespace app\controllers; use yii\web\Controller; +use app\models\JasonPlaceholderAPI; +use yii\data\ArrayDataProvider; +use yii\data\Pagination; -class SiteController extends Controller{ +class SiteController extends Controller +{ /** * {@inheritdoc} */ - public function actions(){ + public function actions() + { return [ 'error' => [ 'class' => 'yii\web\ErrorAction', @@ -21,16 +26,36 @@ public function actions(){ * * @return string */ - public function actionIndex(){ + public function actionIndex() + { return $this->render('index'); } - - /** - * Displays test 1. - * - * @return string - */ - public function actionTest1(){ - return $this->render('test1'); - } + + /** + * Displays test 1. + * + * @return string + */ + public function actionTest1() + { + + // obtenemos los datos de la API + $model = new JasonPlaceholderAPI(); + $data = $model->getData(); + + // Configurar la paginación + $pagination = new Pagination([ + 'totalCount' => count($data), + 'pageSize' => 10, + ]); + + // Crear el proveedor de datos + $dataProvider = new ArrayDataProvider([ + 'allModels' => $data, // Datos obtenidos de la API (Array) + 'pagination' => $pagination, // Configuración de la paginación + ]); + + // Renderizar la vista con los datos + return $this->render('test1', ['dataProvider' => $dataProvider]); + } } diff --git a/models/JasonPlaceholderAPI.php b/models/JasonPlaceholderAPI.php new file mode 100644 index 0000000..d93febd --- /dev/null +++ b/models/JasonPlaceholderAPI.php @@ -0,0 +1,60 @@ +createRequest() + ->setMethod('GET') + ->setUrl($this->base_url . $endpoint) + ->send(); + + + // Si la petición fue exitosa, obtenemos los datos, los mapeamos y los devolvemos + if ($response->isOk) { + $data = $response->data; + $posts = []; + // Mapeamos los datos obtenidos para devolver solo los campos que nos interesan y en el formato deseado + foreach ($data as $item) { + $post = [ + 'userId' => $item['userId'], + 'id' => $item['id'], + 'title' => $item['title'], + 'body' => $item['body'] + ]; + $posts[] = $post; + } + return $posts; + } else { + // Registrar el error si la respuesta no es exitosa + Yii::error('Error en la respuesta de la API: ' . $response->content, __METHOD__); + return []; + } + } catch (\Exception $e) { + // Capturar y registrar cualquier excepción que ocurra durante la petición + Yii::error('Error al consumir la API: ' . $e->getMessage(), __METHOD__); + return []; + } + } +} diff --git a/views/site/_post.php b/views/site/_post.php new file mode 100644 index 0000000..50927d5 --- /dev/null +++ b/views/site/_post.php @@ -0,0 +1,15 @@ + +
= ucfirst($post['body']) ?>
+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.
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.