Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Juliana Apolo #78

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor/*
17 changes: 17 additions & 0 deletions src/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM php:7.2.15-apache
RUN docker-php-ext-install pdo_mysql mysqli

RUN apt-get update && \
apt-get install -y --no-install-recommends git zip unzip

RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" && \
php -r "if (hash_file('sha384', 'composer-setup.php') === '48e3236262b34d30969dca3c37281b3b4bbe3221bda826ac6a9a62d6444cdb0dcd0615698a5cbe587c3f0fe57a54d8f5') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" && \
php composer-setup.php && \
php -r "unlink('composer-setup.php');" && \
mv composer.phar /usr/local/bin/composer

COPY ./www/composer.json /var/www/html/

RUN composer install

RUN a2enmod rewrite
31 changes: 31 additions & 0 deletions src/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Teste para o Picpay
Este projeto tem o propósito montar uma API REST e um front-end que busque usuários por palavra-chave.

## Dependências
- Docker
- Composer
- PHP 7.2.15
- Apache
- MySQL 5.7
- Slim Framework

## Front-end
http://localhost

## API
http://localhost/api/users?q=apolonio&page=1
http://localhost/api/user/01207a7b-e948-4efb-8412-82a1507decba
### Basic Auth
User: picpay | Pass: picpaypass

## Instalação
```
#Entrar no diretório para iniciar o docker
$ cd src/

#Iniciar os containers no docker
$ docker-compose up --build

#Acessar a aplicação no browser
http://localhost
```
114 changes: 114 additions & 0 deletions src/data/dump.sql

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions src/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
php:
build: .
ports:
- "80:80"
- "443:443"
volumes:
- ./www:/var/www/html
- /var/www/html/vendor
links:
- db
db:
image: mysql:5.7
volumes:
- ./data/:/docker-entrypoint-initdb.d
environment:
- MYSQL_ROOT_PASSWORD=phprs
- MYSQL_DATABASE=picpay_db
- MYSQL_USER=picpay
- MYSQL_PASSWORD=picpaypass
ports:
- 33306:3306
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
links:
- db
ports:
- 8181:80
environment:
- PMA_ARBITRARY=1
4 changes: 4 additions & 0 deletions src/www/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
16 changes: 16 additions & 0 deletions src/www/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"require": {
"slim/slim": "^3.0",
"slim/twig-view": "^2.4",
"tuupola/slim-basic-auth": "^3.2",
"xandros15/slim-pagination": "^0.1.0"
},
"autoload": {
"psr-4": {
"MyApp\\":"./"
}
},
"require-dev": {
"squizlabs/php_codesniffer": "*"
}
}
20 changes: 20 additions & 0 deletions src/www/config/config.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
$config = array(
'displayErrorDetails' => true,
'addContentLengthHeader' => false,
'db' => array(
'host' => 'db',
'user' => 'picpay',
'pass' => 'picpaypass',
'dbname' => 'picpay_db'
),
'api' => array(
'pagination' => array(
'per_page' => 15
),
'authentication' => array(
'user' => 'picpay',
'password' => 'picpaypass'
)
)
);
67 changes: 67 additions & 0 deletions src/www/controllers/ApiController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace MyApp\controllers;

use Psr\Container\ContainerInterface;
use Xandros15\SlimPagination\Pagination;
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

class ApiController
{
protected $container;

// constructor receives container instance
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}

public function getUsers(Request $request, Response $response, array $args)
{
$per_page = $this->container['settings']['api']['pagination']['per_page'];
$q = $request->getQueryParam('q');
$page = $request->getQueryParam('page') ? $request->getQueryParam('page') : '1';
$users = array("per_page" => $per_page, "total_results" => 0, "total_pages" => 0,"page" => $page, "results" => array());
if (isset($q)) {
$q = implode(" +", explode(" ", $request->getQueryParam('q')));
$query = sprintf(
"SELECT
u.*
FROM users u
LEFT JOIN users_relevance ur
ON ur.id = u.id
WHERE
MATCH (name,username) AGAINST ('+%s' IN BOOLEAN MODE)
ORDER BY ur.relevance desc,u.id",
$q
);

$query_count = sprintf("SELECT count(0) total FROM (%s) tmp", $query);
$queryObj = $this->container['db']->prepare($query_count);
$queryObj->execute();
$total = $queryObj->fetchAll();
$users["per_page"] = $per_page;
$users["total_results"] = $total[0]["total"] ;
$users["total_pages"] = ceil($total[0]["total"] / $per_page);

$start = $page - 1;
$start = $start * $per_page;
$query_results = sprintf("%s LIMIT %s,%s", $query, $start, $per_page);
$queryObj = $this->container['db']->prepare($query_results);
$queryObj->execute();
$users["results"] = $queryObj->fetchAll();
}

return $response->withJson($users, 200)->withHeader('Content-type', 'application/json');
}

public function getUser(Request $request, Response $response, array $args)
{
$queryObj = $this->container['db']->prepare("SELECT * FROM users WHERE id=:id");
$queryObj->bindParam("id", $args['id']);
$queryObj->execute();
$users = $queryObj->fetchObject();
return $response->withJson($users, 200)->withHeader('Content-type', 'application/json');
}
}
49 changes: 49 additions & 0 deletions src/www/controllers/HomeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace MyApp\controllers;

use Psr\Container\ContainerInterface;
use Xandros15\SlimPagination\Pagination;
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

class HomeController
{
protected $container;

// constructor receives container instance
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}

public function home(Request $request, Response $response, array $args)
{
$q = $request->getQueryParam('q');
$page = $request->getQueryParam('page');
$results = "";
$pagination = false;
if (isset($q)) {
$service_url = sprintf('http://localhost/api/users?q=%s&page=%s', urlencode($q), urlencode($page));
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$username = $this->container['settings']['api']['authentication']['user'];
$password = $this->container['settings']['api']['authentication']['password'];
curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
$curl_response = curl_exec($curl);
curl_close($curl);
$results = json_decode($curl_response);

$pagination = new Pagination($request, $this->container->get('router'), [
Pagination::OPT_TOTAL => $results->total_results ? $results->total_results : 1,
Pagination::OPT_PER_PAGE => $results->per_page //number of items
]);
}

return $this->container['view']->render($response, 'index.twig', [
'q' => $q,
'users' => $results,
'pagination' => $pagination
]);
}
}
26 changes: 26 additions & 0 deletions src/www/css/app.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
.main {
width: 50%;
margin: 50px auto;
}

.title{
text-align: center;
}

/* Bootstrap 4 text input with search icon */

.has-search .form-control {
padding-left: 2.375rem;
}

.has-search .form-control-feedback {
position: absolute;
z-index: 2;
display: block;
width: 2.375rem;
height: 2.375rem;
line-height: 2.375rem;
text-align: center;
pointer-events: none;
color: #aaa;
}
Loading