Ivan - Backend - Create /ticket/get path [skip ci]

This commit is contained in:
ivan 2016-09-03 21:46:08 -03:00
parent e0ae9d113e
commit e22b3d999c
8 changed files with 179 additions and 17 deletions

View File

@ -1,11 +1,13 @@
<?php
include 'ticket/create.php';
include 'ticket/comment.php';
include 'ticket/get.php';
$ticketControllers = new ControllerGroup();
$ticketControllers->setGroupPath('/ticket');
$ticketControllers->addController(new CreateController);
$ticketControllers->addController(new CommentController);
$ticketControllers->addController(new TicketGetController);
$ticketControllers->finalize();

View File

@ -9,6 +9,7 @@ class CreateController extends Controller {
private $content;
private $departmentId;
private $language;
private $ticketNumber;
public function validations() {
return [
@ -31,17 +32,16 @@ class CreateController extends Controller {
}
public function handler() {
$this->storeRequestData();
$this->storeTicket();
Response::respondSuccess();
}
private function storeRequestData() {
$this->title = Controller::request('title');
$this->content = Controller::request('content');
$this->departmentId = Controller::request('departmentId');
$this->language = Controller::request('language');
$this->storeTicket();
Response::respondSuccess([
'ticketNumber' => $this->ticketNumber
]);
}
private function storeTicket() {
@ -65,5 +65,7 @@ class CreateController extends Controller {
$author->store();
$ticket->store();
$this->ticketNumber = $ticket->ticketNumber;
}
}

View File

@ -0,0 +1,27 @@
<?php
use Respect\Validation\Validator as DataValidator;
DataValidator::with('CustomValidations', true);
class TicketGetController extends Controller {
const PATH = '/get';
public function validations() {
return [
'permission' => 'user',
'requestData' => [
'ticketNumber' => [
'validation' => DataValidator::length(6),
'error' => ERRORS::INVALID_TICKET
]
]
];
}
public function handler() {
$ticketNumber = Controller::request('ticketNumber');
$ticket = Ticket::getByTicketNumber($ticketNumber);
Response::respondSuccess($ticket->toArray());
}
}

View File

@ -29,7 +29,16 @@ abstract class DataStore {
}
}
return ($validProp) ? $propToValidate : 'id';
return ($validProp) ? self::from_camel_case($propToValidate) : 'id';
}
private static function from_camel_case($input) {
preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
$ret = $matches[0];
foreach ($ret as &$match) {
$match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
}
return implode('_', $ret);
}
public function __construct($beanInstance = null) {
@ -62,6 +71,7 @@ abstract class DataStore {
}
public function &__get($name) {
'hello';
if (!array_key_exists($name, $this->properties) || !$this->properties[$name]) {
$this->properties[$name] = $this->parseBeanProp($name);
}

View File

@ -4,8 +4,6 @@ use RedBeanPHP\Facade as RedBean;
class Ticket extends DataStore {
const TABLE = 'ticket';
private $author;
public static function getProps() {
return array(
'ticketNumber',
@ -26,6 +24,10 @@ class Ticket extends DataStore {
public static function getTicket($value, $property = 'id') {
return parent::getDataStore($value, $property);
}
public static function getByTicketNumber($value) {
return Ticket::getTicket($value, 'ticketNumber');
}
public function getDefaultProps() {
return array(
@ -36,10 +38,6 @@ class Ticket extends DataStore {
public function store() {
parent::store();
if ($this->author instanceof User) {
$this->author->store();
}
}
public function generateUniqueTicketNumber() {
$ticketQuantity = Ticket::count('ticket');
@ -57,4 +55,74 @@ class Ticket extends DataStore {
return $ticketNumber;
}
public function toArray() {
$author = $this->author;
return [
'ticketNumber' => $this->ticketNumber,
'title' => $this->title,
'content' => $this->content,
'department' => [
'id' => $this->department->id,
'name' => $this->department->name
],
'date' => $this->date,
'file' => $this->file,
'language' => $this->language,
'unread' => !!$this->unread,
'closed' => !!$this->closed,
'author' => $this->authorToArray(),
'owner' => $this->ownerToArray(),
'comments' => $this->commentsToArray()
];
}
public function authorToArray() {
$author = $this->author;
if ($author && !$author->isNull()) {
return [
'id' => $author->id,
'name' => $author->name,
'email' => $author->email
];
} else {
return [];
}
}
public function ownerToArray() {
$owner = $this->owner;
if ($owner && !$owner->isNull()) {
return [
'id' => $owner->id,
'name' => $owner->name,
'email' => $owner->email
];
} else {
return [];
}
}
public function commentsToArray() {
$comments = [];
foreach ($this->ownCommentList as $comment) {
$comments[] = [
'content'=> $comment->content,
'author' => [
'id'=> 15,
'name' => $comment->author->name,
'email' => $comment->author->email,
'staff' => $comment->author->staff
],
'date'=> $comment->date,
'file'=> $comment->file
];
}
return $comments;
}
}

View File

@ -19,3 +19,4 @@ require './user/edit-password.rb'
require './user/edit-email.rb'
require './ticket/create.rb'
require './ticket/comment.rb'
require './ticket/get.rb'

View File

@ -1,9 +1,9 @@
describe '/ticket/create' do
request('/user/logout')
Scripts.createUser('jonhsnow@os4.com','jonhpass','Jonh Snow')
Scripts.createUser('jonsnow@os4.com','jonpass','Jon Snow')
result = request('/user/login', {
email: 'jonhsnow@os4.com',
password: 'jonhpass'
email: 'jonsnow@os4.com',
password: 'jonpass'
})
$csrf_userid = result['data']['userId']

52
tests/ticket/get.rb Normal file
View File

@ -0,0 +1,52 @@
describe '/ticket/get/' do
request('/user/logout')
Scripts.createUser('cersei@os4.com', 'cersei','Cersei Lannister')
result = request('/user/login', {
email: 'cersei@os4.com',
password: 'cersei'
})
$csrf_userid = result['data']['userId']
$csrf_token = result['data']['token']
result = request('/ticket/create', {
title: 'Should we pay?',
content: 'A Lannister always pays his debts.',
departmentId: 1,
csrf_userid: $csrf_userid,
csrf_token: $csrf_token
})
@ticketNumber = result['data']['ticketNumber']
#it 'should fail if ticketNumber is invalid' do
#end
#it 'should fail if ticket does not belong to user' do
#end
it 'should successfully return the ticket information' do
result = request('/ticket/get', {
ticketNumber: @ticketNumber,
csrf_userid: $csrf_userid,
csrf_token: $csrf_token
})
ticket = $database.getRow('ticket', @ticketNumber, 'ticket_number')
(result['status']).should.equal('success')
(result['data']['ticketNumber']).should.equal(ticket['ticket_number'])
(result['data']['title']).should.equal(ticket['title'])
(result['data']['content']).should.equal(ticket['content'])
(result['data']['department']['id']).should.equal('1')
(result['data']['department']['name']).should.equal($database.getRow('department', 1)['name'])
(result['data']['date']).should.equal(ticket['date'])
(result['data']['file']).should.equal(ticket['file'])
(result['data']['language']).should.equal(ticket['language'])
(result['data']['unread']).should.equal(false)
(result['data']['author']['name']).should.equal('Cersei Lannister')
(result['data']['author']['email']).should.equal('cersei@os4.com')
(result['data']['owner']).should.equal([])
(result['data']['comments']).should.equal([])
end
end