diff --git a/server/controllers/ticket.php b/server/controllers/ticket.php index eaab3316..726ed42c 100644 --- a/server/controllers/ticket.php +++ b/server/controllers/ticket.php @@ -1,11 +1,13 @@ setGroupPath('/ticket'); $ticketControllers->addController(new CreateController); $ticketControllers->addController(new CommentController); +$ticketControllers->addController(new TicketGetController); $ticketControllers->finalize(); \ No newline at end of file diff --git a/server/controllers/ticket/create.php b/server/controllers/ticket/create.php index bf840b8d..64dab1e7 100644 --- a/server/controllers/ticket/create.php +++ b/server/controllers/ticket/create.php @@ -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; } } diff --git a/server/controllers/ticket/get.php b/server/controllers/ticket/get.php new file mode 100644 index 00000000..fcc9a16f --- /dev/null +++ b/server/controllers/ticket/get.php @@ -0,0 +1,27 @@ + '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()); + } +} \ No newline at end of file diff --git a/server/models/DataStore.php b/server/models/DataStore.php index 0248aaa9..c2006219 100644 --- a/server/models/DataStore.php +++ b/server/models/DataStore.php @@ -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); } diff --git a/server/models/Ticket.php b/server/models/Ticket.php index 311df4b3..8de49c28 100644 --- a/server/models/Ticket.php +++ b/server/models/Ticket.php @@ -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; + } } diff --git a/tests/init.rb b/tests/init.rb index 3948f566..c552e0a2 100644 --- a/tests/init.rb +++ b/tests/init.rb @@ -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' diff --git a/tests/ticket/create.rb b/tests/ticket/create.rb index 979895d9..f9756732 100644 --- a/tests/ticket/create.rb +++ b/tests/ticket/create.rb @@ -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'] diff --git a/tests/ticket/get.rb b/tests/ticket/get.rb new file mode 100644 index 00000000..c40b2886 --- /dev/null +++ b/tests/ticket/get.rb @@ -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 \ No newline at end of file