-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[notifications] Implements GET and POST messages
- Loading branch information
1 parent
6d27a3e
commit 62366c0
Showing
4 changed files
with
299 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,234 @@ | ||
<?php | ||
defined('BASEPATH') OR exit('No direct script access allowed'); | ||
|
||
require APPPATH.'/libraries/Jsv4/Validator.php'; | ||
require APPPATH.'/libraries/Jsv4/ValidationException.php'; | ||
include( APPPATH.'controllers/Authentication.php' ); | ||
/** | ||
* @apiDefine tokenParam | ||
* @apiHeader {String} Token Token do usuario que realizara a ação. | ||
*/ | ||
class Messages extends Authentication { | ||
/** | ||
* @api {post} /routes/:idRoute/buses/:idBus/notifications Adicionar uma mensagem a um onibus | ||
* @apiName PostBusMessage | ||
* @apiGroup Messages | ||
* @apiPermission tracker | ||
* | ||
* @apiUse tokenParam | ||
* | ||
* @apiParam (URLParam) {Integer} idRoute ID unico da rota que possui o onibus da mensagem. | ||
* @apiParam (URLParam) {Integer} idBus ID unico do onibus que a mensagem pertence. | ||
* @apiParam (BodyParam) {String} titke Título da mensagem a ser adicionada. | ||
* @apiParam (BodyParam) {String} [message] Texto da mensagem. | ||
* | ||
* @apiSuccess (201 - MessageCreated) {Integer} id ID unico da mensagem. | ||
* @apiSuccess (201 - MessageCreated) {Integer} idRoute ID unico da rota que possui o onibus da mensagem. | ||
* @apiSuccess (201 - MessageCreated) {Integer} idBus ID unico do onibus que a mensagem pertence. | ||
* @apiSuccess (201 - MessageCreated) {String} titke Título da mensagem a ser adicionada. | ||
* @apiSuccess (201 - MessageCreated) {String} message Texto da mensagem. | ||
* | ||
* @apiError (400 - InvalidJSON) root O json enviado é invalido. Isso pode ocorrer por falta de parametros, erros de tipos e erros de sintaxe. | ||
* | ||
* @apiParamExample {json} Exemplo de requisição: | ||
* { | ||
* "title": "Onibus quebrado", | ||
* "message": "O galaticus quebrou e não funcionará essa semana :(" | ||
* } | ||
* @apiSuccessExample Exemplo de respota com sucesso: | ||
* HTTP/1.1 201 CREATED | ||
* { | ||
* "title": "Onibus quebrado", | ||
* "message": "O galaticus quebrou e não funcionará essa semana :(", | ||
* "id_routes": 86, | ||
* "id_bus": 1, | ||
* "id": 10 | ||
* } | ||
*/ | ||
/** | ||
* @api {post} /routes/:idRoute/notifications Adicionar uma mensagem a uma rota | ||
* @apiName PostRouteMessage | ||
* @apiGroup Messages | ||
* @apiPermission tracker | ||
* | ||
* @apiUse tokenParam | ||
* | ||
* @apiParam (URLParam) {Integer} idRoute ID unico da rota que a mensagem pertence. | ||
* @apiParam (BodyParam) {String} titke Título da mensagem a ser adicionada. | ||
* @apiParam (BodyParam) {String} [message] Texto da mensagem. | ||
* | ||
* @apiSuccess (201 - MessageCreated) {Integer} id ID unico da mensagem. | ||
* @apiSuccess (201 - MessageCreated) {Integer} idRoute ID unico da rota que a mensagem pertence. | ||
* @apiSuccess (201 - MessageCreated) {String} title Título da mensagem a ser adicionada. | ||
* @apiSuccess (201 - MessageCreated) {String} message Texto da mensagem. | ||
* | ||
* @apiError (400 - InvalidJSON) root O json enviado é invalido. Isso pode ocorrer por falta de parametros, erros de tipos e erros de sintaxe. | ||
* | ||
* @apiParamExample {json} Exemplo de requisição: | ||
* { | ||
* "title": "Horario de funcionamento", | ||
* "message": "Amanhã a rota intracampus não funcionará durante a tarde." | ||
* } | ||
* @apiSuccessExample Exemplo de respota com sucesso: | ||
* HTTP/1.1 201 CREATED | ||
* { | ||
* "title": "Onibus quebrado", | ||
* "message": "Amanhã a rota intracampus não funcionará durante a tarde.", | ||
* "id_routes": 86, | ||
* "id": 10 | ||
* } | ||
*/ | ||
function addMessage($idRoute, $idBus){ | ||
//check read permissions | ||
$token = $this->authenticate(); | ||
if(!$token->valid(Authentication::TRACKER_PERMISSION)) | ||
return $this->makeUnauthorizedResponse(); | ||
|
||
$validator = $this->validateJson($this->input->raw_input_stream, APPPATH.'/controllers/Schemas/MessageAdd.json'); | ||
if($validator->valid){ | ||
$this->loadModel(); | ||
$notification = json_decode($this->input->raw_input_stream); | ||
$notification->id_routes = $idRoute; | ||
$notification->id_bus = empty($idBus) ? NULL : $idBus; | ||
|
||
$notification->id = $this->messages_model->insert($notification); | ||
if($notification->id == null) | ||
return $this->makeJsonRespose(["error" => "INTERNAL_ERROR"], 500); | ||
|
||
if(empty($idBus)) | ||
unset($notification->id_bus); | ||
|
||
return $this->makeJsonRespose($notification, 201); | ||
}else | ||
return $this->makeJsonRespose($validator->errors, 400); | ||
} | ||
/** | ||
* @api {get} /routes/:idRoute/notifications Requisitar mensagens de uma rota | ||
* @apiName GetRoutesMessages | ||
* @apiGroup Messages | ||
* @apiPermission client | ||
* | ||
* @apiExample Exemplo de uso: | ||
* curl -i http://host/BusTrackerAPI/index.php/routes/86/notifications?buses=true | ||
* | ||
* @apiUse tokenParam | ||
* | ||
* @apiParam (URLParam) {Integer} idRoute ID unico da rota que a mensagem pertence. | ||
* @apiParam (URLParam) {boolean} buses Se true, retorna tambem as mensagens dos onibus da rota. | ||
* Caso contrário, retorna apenas as mensagens destinadas a rota. | ||
* | ||
* @apiSuccess {Integer} id_messages ID unico da mensagem. | ||
* @apiSuccess {Integer} id_routes ID unico da rota que a mensagem pertence. | ||
* @apiSuccess {String} title Título da mensagem | ||
* @apiSuccess {String} message Texto da mensagem | ||
* | ||
* | ||
* @apiSuccessExample Resposta sucesso: | ||
* HTTP/1.1 200 OK | ||
* [ | ||
* { | ||
* "id_messages": 16, | ||
* "id_routes": 86, | ||
* "title": "Tem titulo, mas, sem mensagem.", | ||
* "message": null, | ||
* "date": "2016-07-21 22:48:41" | ||
* }, | ||
* ... | ||
* { | ||
* "id_messages": 92, | ||
* "id_routes": 86, | ||
* "title": "Horario de funcionamento", | ||
* "message": "Amanhã a rota intracampus não funcionará durante a tarde.", | ||
* "date": "2016-07-21 22:28:24" | ||
* } | ||
* ] | ||
* @apiSuccessExample Resposta sucesso com mensagens dos onibus tambem: | ||
* HTTP/1.1 200 OK | ||
* [ | ||
* {//essa é um mensagem de rota, porque id_bus = null | ||
* "id_messages": 11, | ||
* "id_routes": 86, | ||
* "title": "Horario de funcionamento", | ||
* "message": "Amanhã a rota intracampus não funcionará durante a tarde.", | ||
* "date": "2016-07-21 22:23:11", | ||
* "id_bus": null | ||
* }, | ||
* ... | ||
* {//essa é um mensagem de um onibus que pertence a rota, porque id_bus != null | ||
* "id_messages": 10, | ||
* "id_routes": 86, | ||
* "title": "Onibus quebrado", | ||
* "message": "O galaticus quebrou :(", | ||
* "date": "2016-07-21 22:06:17", | ||
* "id_bus": 1 | ||
* } | ||
* ] | ||
*/ | ||
/** | ||
* @api {get} /routes/:idRoute/buses/:idBus/notifications Requisitar mensagens de um onibus | ||
* @apiName GetBusMessages | ||
* @apiGroup Messages | ||
* @apiPermission client | ||
* | ||
* @apiExample Exemplo de uso: | ||
* curl -i http://host/BusTrackerAPI/index.php/routes/86/buses/1/notifications | ||
* | ||
* @apiUse tokenParam | ||
* | ||
* @apiParam (URLParam) {Integer} idRoute ID unico da rota que a mensagem pertence. | ||
* @apiParam (URLParam) {Integer} idBus ID unico do onibus que a mensagem pertence. | ||
* | ||
* | ||
* @apiSuccess {Integer} id_messages ID unico da mensagem. | ||
* @apiSuccess {Integer} id_routes ID unico da rota que a mensagem pertence. | ||
* @apiSuccess {String} title Título da mensagem | ||
* @apiSuccess {String} message Texto da mensagem | ||
* | ||
* | ||
* @apiSuccessExample Resposta successo: | ||
* HTTP/1.1 200 OK | ||
* [ | ||
* { | ||
* "id_messages": 10, | ||
* "id_routes": 86, | ||
* "title": "Onibus quebrado", | ||
* "message": "O galaticus quebrou :(", | ||
* "date": "2016-07-21 22:06:17", | ||
* "id_bus": 1 | ||
* }, | ||
* ... | ||
* ] | ||
*/ | ||
function getMessages($idRoutes, $idBus){ | ||
$token = $this->authenticate(); | ||
if(!$token->valid(Authentication::CLIENT_PERMISSION)) | ||
return $this->makeUnauthorizedResponse(); | ||
|
||
if($this->input->get("buses") == "true"){ | ||
$idBus = 0; | ||
} | ||
|
||
$this->loadModel(); | ||
$notifications = $this->messages_model->index($idRoutes, $idBus); | ||
|
||
return $this->makeJsonRespose($notifications, 200); | ||
} | ||
function loadModel(){ | ||
$this->load->model('messages_model', '', TRUE); | ||
} | ||
function validateJson($json, $schemaPath){ | ||
$route = json_decode($json); | ||
|
||
$schema = json_decode(file_get_contents($schemaPath)); | ||
$validator = Jsv4\Validator::validate( $route, $schema); | ||
|
||
return $validator; | ||
|
||
} | ||
function makeJsonRespose($output, $statusCode){ | ||
return $this->output | ||
->set_content_type('application/json') | ||
->set_status_header($statusCode) | ||
->set_output(json_encode($output, JSON_NUMERIC_CHECK)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"type": "object", | ||
"properties": { | ||
"title": { | ||
"type": "string" | ||
}, | ||
"message": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"title" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
defined('BASEPATH') OR exit('No direct script access allowed'); | ||
|
||
class Messages_model extends CI_Model{ | ||
public $title; | ||
public $message; | ||
public $id_bus; | ||
public $id_routes; | ||
public $date; | ||
const table = 'messages'; | ||
const pointsTable = 'points_route'; | ||
const busTable = 'bus'; | ||
const notificationPositionTable = 'notifications_update_position'; | ||
public static $google_routes_path; | ||
|
||
public function __construct(){ | ||
parent::__construct(); | ||
} | ||
public function index($idRoute, $idBus){ | ||
$busConditionSearch = !empty($idBus) ? "id_bus = ".$idBus : "id_bus IS NULL"; | ||
$busConditionSearch = $idBus === 0 ? "TRUE" : $busConditionSearch; | ||
$selectedCollums = "id_messages, id_routes, title, message, date"; | ||
$selectedCollums .= !empty($idBus) || $idBus === 0 ? ", id_bus" : ""; | ||
|
||
return $this->db->select($selectedCollums)->from(self::table) | ||
->where("id_routes = ".$idRoute." AND ".$busConditionSearch) | ||
->order_by("date", "desc")->get()->result(); | ||
} | ||
public function insert($notification){ | ||
$this->title = $notification->title; | ||
$this->message = $notification->message; | ||
$this->id_bus = $notification->id_bus; | ||
$this->id_routes = $notification->id_routes; | ||
$this->date = date('Y-m-d H:i:s'); | ||
|
||
$this->db->insert(self::table, $this); | ||
$id = $this->db->insert_id(); | ||
|
||
return $id; | ||
} | ||
|
||
} |