Skip to content

Commit

Permalink
Use FOS REST controllers
Browse files Browse the repository at this point in the history
Less boilerplate route definitions
  • Loading branch information
Chris Young committed Sep 13, 2016
1 parent c2f9ca6 commit 012176f
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 167 deletions.
4 changes: 4 additions & 0 deletions app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ fos_user:
form:
type: goldfish_user_profile

fos_rest:
routing_loader:
default_format: json

# Swiftmailer Configuration
swiftmailer:
transport: "%mailer_transport%"
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"angular-smart-table": "^2.1.8"
},
"resolutions": {
"angular": "1.4.8",
"angular": "~1.4.6",
"angular-route": "~1.4.6"
}
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,23 @@
<?php

namespace Darkbluesun\GoldfishBundle\Controller;
namespace Darkbluesun\GoldfishBundle\Controller\API;

use FOS\RestBundle\Routing\ClassResourceInterface;
use JMS\Serializer\SerializationContext;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Darkbluesun\GoldfishBundle\Entity\Workspace;
use Darkbluesun\GoldfishBundle\Entity\Client;
use Darkbluesun\GoldfishBundle\Entity\ClientComment;
use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
use Symfony\Component\Security\Acl\Permission\MaskBuilder;

/**
* Client controller.
*
* @Route("/api/clients")
*/
class ClientController extends Controller
class ClientController extends Controller implements ClassResourceInterface
{
/**
* Lists all Client entities.
*
* @Route("/", name="clients")
* @Method("GET")
*/
public function getcAction()
public function cgetAction()
{
$user = $this->get('security.context')->getToken()->getUser();
$workspace = $user->getWorkspace();
Expand All @@ -52,22 +40,14 @@ public function getcAction()
}

/**
* Gets an existing Client entity.
* @Security("is_granted('VIEW', client)")
* @Route("/{id}", name="clients_get")
* @Method("GET")
*/
public function getAction(Client $client)
{
return new Response($this->get('serializer')->serialize($client,'json',SerializationContext::create()->setGroups(['client_details'])));
}
/**
* Creates a new Client entity.
*
* @Route("", name="clients_create")
* @Method("POST")
*/
public function createAction(Request $request)

public function postAction(Request $request)
{
$serializer = $this->get('serializer');
$em = $this->getDoctrine()->getManager();
Expand All @@ -86,12 +66,9 @@ public function createAction(Request $request)
}

/**
* Updates an existing Client entity.
* @Security("is_granted('EDIT', client)")
* @Route("/{id}", name="clients_update")
* @Method("POST")
*/
public function updateAction(Request $request, Client $client)
public function putAction(Request $request, Client $client)
{
$em = $this->getDoctrine()->getManager();
$created = $client->getCreatedAt();
Expand All @@ -102,13 +79,7 @@ public function updateAction(Request $request, Client $client)
return $this->getAction($client);
}

/**
* Adds a comment to a client
*
* @Route("/{id}/comment", name="clients_comment")
* @Method("POST")
*/
public function commentAction(Request $request, Client $client) {
public function postCommentAction(Request $request, Client $client) {
$comment = new ClientComment;
$em = $this->getDoctrine()->getManager();

Expand All @@ -120,14 +91,10 @@ public function commentAction(Request $request, Client $client) {
return new Response($this->get('serializer')->serialize($client,'json',SerializationContext::create()->setGroups(['client_details'])));
}


/**
* Deletes a Client entity.
* @Security("is_granted('DELETE', client)")
* @Route("/{id}", name="clients_delete")
* @Method("DELETE")
*/
public function destroyAction(Request $request, Client $client)
public function deleteAction(Request $request, Client $client)
{
$em = $this->getDoctrine()->getManager();
$em->remove($client);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,22 @@
<?php

namespace Darkbluesun\GoldfishBundle\Controller;
namespace Darkbluesun\GoldfishBundle\Controller\API;

use FOS\RestBundle\Routing\ClassResourceInterface;
use JMS\Serializer\SerializationContext;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Darkbluesun\GoldfishBundle\Entity\Project;
use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
use Symfony\Component\Security\Acl\Permission\MaskBuilder;

/**
* Project controller.
*
* @Route("/api/projects")
*/
class ProjectController extends Controller
class ProjectController extends Controller implements ClassResourceInterface
{

/**
* Lists all Project entities.
*
* @Route("/", name="project")
* @Method("GET")
*/
public function getcAction()
public function cgetAction()
{
return new Response(
$this->get('serializer')->serialize(
Expand All @@ -39,22 +26,13 @@ public function getcAction()
}

/**
* Get a Project
* @Security("is_granted('VIEW', project)")
* @Route("/{id}", name="project_get")
* @Method("GET")
*/
public function getAction(Project $project)
{
return new Response($this->get('serializer')->serialize($project,'json',SerializationContext::create()->setGroups(['project_details'])));
}

/**
* Creates a new Project entity.
*
* @Route("", name="project_create")
* @Method("POST")
*/
public function postAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
Expand All @@ -73,12 +51,9 @@ public function postAction(Request $request)
}

/**
* Updates an existing Project entity.
* @Security("is_granted('EDIT', project)")
* @Route("/{id}", name="project_update")
* @Method("POST")
*/
public function updateAction(Request $request, Project $project)
public function putAction(Request $request, Project $project)
{
$em = $this->getDoctrine()->getManager();
$created = $project->getCreatedAt();
Expand All @@ -90,10 +65,7 @@ public function updateAction(Request $request, Project $project)
}

/**
* Deletes a Project.
* @Security("is_granted('DELETE', project)")
* @Route("/{id}", name="project_delete")
* @Method("DELETE")
*/
public function deleteAction(Project $project)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,23 @@
<?php

namespace Darkbluesun\GoldfishBundle\Controller;
namespace Darkbluesun\GoldfishBundle\Controller\API;

use FOS\RestBundle\Routing\ClassResourceInterface;
use JMS\Serializer\SerializationContext;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Darkbluesun\GoldfishBundle\Entity\Task;
use Darkbluesun\GoldfishBundle\Entity\TimeEntry;
use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
use Symfony\Component\Security\Acl\Permission\MaskBuilder;

/**
* Task controller.
*
* @Route("/api/tasks")
*/
class TaskController extends Controller
class TaskController extends Controller implements ClassResourceInterface
{
/**
* Lists all Task entities.
*
* @Route("/", name="tasks_list")
* @Method("GET")
*/
public function getcAction()
public function cgetAction()
{
return new Response(
$this->get('serializer')->serialize(
Expand All @@ -39,10 +27,7 @@ public function getcAction()
}

/**
* Gets a Task.
* @Security("is_granted('VIEW', task)")
* @Route("/{id}", name="tasks_get")
* @Method("GET")
*/
public function getAction(Task $task)
{
Expand All @@ -53,12 +38,6 @@ public function getAction(Task $task)
));
}

/**
* Creates a new Task.
*
* @Route("", name="tasks_create")
* @Method("POST")
*/
public function postAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
Expand All @@ -77,12 +56,9 @@ public function postAction(Request $request)
}

/**
* Updates an existing Task.
* @Security("is_granted('EDIT', task)")
* @Route("/{id}", name="tasks_update")
* @Method("POST")
*/
public function updateAction(Request $request, Task $task)
public function putAction(Request $request, Task $task)
{
$em = $this->getDoctrine()->getManager();
$created = $task->getCreatedAt();
Expand All @@ -95,10 +71,7 @@ public function updateAction(Request $request, Task $task)
}

/**
* Deletes a Task.
* @Security("is_granted('DELETE', task)")
* @Route("/{id}", name="tasks_delete")
* @Method("DELETE")
*/
public function deleteAction(Request $request, Task $task)
{
Expand All @@ -109,10 +82,7 @@ public function deleteAction(Request $request, Task $task)
}

/**
* Lists all Comments belonging to this thing.
* @Security("is_granted('VIEW', task)")
* @Route("/{id}/comments", name="task_comment_list")
* @Method("GET")
*/
public function getCommentsAction(Task $task)
{
Expand All @@ -124,10 +94,7 @@ public function getCommentsAction(Task $task)
}

/**
* List all time entries
* @Security("is_granted('VIEW', task)")
* @Route("/{id}/timesheet/", name="task_timesheet")
* @Method("GET")
*/
public function getTimesheetAction(Task $task) {
return new Response(
Expand All @@ -138,10 +105,7 @@ public function getTimesheetAction(Task $task) {
}

/**
* Time add
* @Security("is_granted('EDIT', task)")
* @Route("/{id}/addtime", name="task_add_time")
* @Method("POST")
*/
public function postTimeAction(Request $request, Task $task) {
$em = $this->getDoctrine()->getManager();
Expand Down
24 changes: 24 additions & 0 deletions src/Darkbluesun/GoldfishBundle/Controller/API/UserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Darkbluesun\GoldfishBundle\Controller\API;

use FOS\RestBundle\Routing\ClassResourceInterface;
use JMS\Serializer\SerializationContext;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class UserController extends Controller implements ClassResourceInterface
{
public function cgetAction()
{
$user = $this->getUser();
$workspace = $user->getWorkspace();

return new Response(
$this->get('serializer')->serialize(
$workspace->getUsers(), 'json',
SerializationContext::create()->setGroups(['user_list'])
));
}

}
Loading

0 comments on commit 012176f

Please sign in to comment.