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

Danger types #38

Open
wants to merge 5 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
4 changes: 4 additions & 0 deletions app/config/security.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ security:
pattern: ^/
form_login:
provider: fos_userbundle
json_login:
check_path: /api/v1/login
logout: true
anonymous: true
logout_on_user_change: true
Expand All @@ -27,6 +29,8 @@ security:
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/, role: ROLE_ADMIN }
- { path: ^/api/v1/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api/v1/doc, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api/, role: ROLE_USER }


3 changes: 3 additions & 0 deletions src/AppBundle/AppBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@

class AppBundle extends Bundle
{
public function __construct()
{
}
}
50 changes: 47 additions & 3 deletions src/AppBundle/Controller/DangerPointController.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,20 @@ public function idAction($id)
* description="The typeId of the point"
* )
* @SWG\Parameter(
* name="area",
* in="formData",
* type="boolean",
* required=true,
* description="Is the point an area?"
* )
* @SWG\Parameter(
* name="danger",
* in="formData",
* type="boolean",
* required=true,
* description="Is it dangerous?"
* )
* @SWG\Parameter(
* name="id",
* in="path",
* type="integer",
Expand Down Expand Up @@ -215,7 +229,18 @@ public function updateAction($id, Request $request)
$typeId = $request->get('typeId');
if (!(empty($typeId))) {
$this->logger->info('typeId: ', array($typeId));
$entry->setTypeId($typeId);
$type = $this->getDoctrine()->getRepository('AppBundle:DangerType')->find($typeId);
$entry->setType($type);
$changed = true;
}
$area = $request->get('area');
if (!(empty($area))) {
$entry->setArea($area);
$changed = true;
}
$danger = $request->get('danger');
if (!(empty($area))) {
$entry->setArea($danger);
$changed = true;
}
if ($changed) {
Expand Down Expand Up @@ -267,6 +292,20 @@ public function updateAction($id, Request $request)
* required=true,
* description="The typeId of the point"
* )
* @SWG\Parameter(
* name="area",
* in="formData",
* type="boolean",
* required=true,
* description="Is the point an area?"
* )
* @SWG\Parameter(
* name="danger",
* in="formData",
* type="boolean",
* required=true,
* description="Is it dangerous?"
* )
* @SWG\Response(
* response=200,
* description="Returns one DangerPoint with id",
Expand All @@ -283,15 +322,20 @@ public function postAction(Request $request)
$title = $request->get('title');
$description = $request->get('description');
$typeId = $request->get('typeId');
$area = $request->get('area');
$danger = $request->get('danger');
if (empty($lat) || empty($lon) || empty($typeId)) {
return new View('NULL VALUES ARE NOT ALLOWED'.$lat, Response::HTTP_NOT_ACCEPTABLE);
}
$data->setTitle($title);
$data->setDescription($description);
$data->setTypeId($typeId);
$type = $this->getDoctrine()->getRepository('AppBundle:DangerType')->find($typeId);
$data->setType($type);
$data->setPos(sprintf('SRID=4326;POINT(%f %f)', $lat, $lon));
$data->setCreatedNow($user);

$data->setArea($area);
$data->setDanger($danger);
print_r($data);
$em = $this->getDoctrine()->getManager();
$newObj = $em->merge($data);
$em->flush();
Expand Down
79 changes: 79 additions & 0 deletions src/AppBundle/Controller/DangerTypeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

/*
* This file is part of the ADFC Radschulwegplan Backend package.
*
* <https://github.com/ADFC-Hamburg/adfc-radschulwegplan-backend>
*
* (c) 2018 by James Twellmeyer <jet02@twellmeyer.eu>
* (c) 2018 by Sven Anders <github2018@sven.anders.hamburg>
*
* Released under the GPL 3.0
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Please also visit our (german) webpage about the project:
*
* <https://hamburg.adfc.de/verkehr/themen-a-z/kinder/schulwegplanung/>
*
*/

namespace AppBundle\Controller;

use AppBundle\Entity\DangerType;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\View\View;
use Psr\Log\LoggerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Swagger\Annotations as SWG;
use Symfony\Component\HttpFoundation\Response;

/*

TODO:

* Check paramters
* Test cases in php
* Call API with json
* Roles/Permissions

*/

/**
* @Route("api/v1/danger_type")
*/
class DangerTypeController extends FOSRestController
{
private $logger;

public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}

/**
* Get all DangerTypes.
*
* @SWG\Response(
* response=200,
* description="Returns the DangerTypes",
* )
* )
* @Rest\Get("")
* FIXME Can not set SWG Schema type=array
*/
public function getAllAction()
{
$repo = $this->getDoctrine()->getRepository('AppBundle:DangerType');
$restresult = $repo->findAll();

if (0 == count($restresult)) {
$repo->insertDangerTypes();
$restresult = $repo->findAll();
}

return $restresult;
}
}
77 changes: 77 additions & 0 deletions src/AppBundle/Controller/LoginController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/*
* This file is part of the ADFC Radschulwegplan Backend package.
*
* <https://github.com/ADFC-Hamburg/adfc-radschulwegplan-backend>
*
* (c) 2018 by James Twellmeyer <jet02@twellmeyer.eu>
* (c) 2018 by Sven Anders <github2018@sven.anders.hamburg>
*
* Released under the GPL 3.0
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Please also visit our (german) webpage about the project:
*
* <https://hamburg.adfc.de/verkehr/themen-a-z/kinder/schulwegplanung/>
*
*/

namespace AppBundle\Controller;

use AppBundle\Entity\User;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\View\View;
use Nelmio\ApiDocBundle\Annotation\Model;
use Swagger\Annotations as SWG;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

// see: https://symfony.com/blog/new-in-symfony-3-3-json-authentication
class LoginController extends FosRestController
{
/**
* Login with JSON. Does NOT work with urlencoded parameters.
*
* @SWG\Parameter(
* name="username",
* in="formData",
* type="string",
* required=true,
* description="Username of the user which logs-in."
* )
* @SWG\Parameter(
* name="password",
* in="formData",
* type="string",
* required=true,
* description="Password of the user which logs-in."
* )
* @SWG\Response(
* response=200,
* description="Returns the User with username={username}",
* @Model(type=User::class)
* )
* @SWG\Response(
* response=401,
* description="Invalid Credentials",
* )
* @Rest\Post("/api/v1/login")
*/
public function loginAction(Request $request)
{
$userName = $request->get('username');
$user = $this->getDoctrine()
->getRepository('AppBundle:User')
->findOneBy(array('username' => $userName));

if (!$user) {
return new View('user not found', Response::HTTP_NOT_FOUND);
}

return $user;
}
}
18 changes: 11 additions & 7 deletions src/AppBundle/DataFixtures/DangerPointFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,36 @@ class DangerPointFixtures extends Fixture implements ORMFixtureInterface, Depend
public function load(ObjectManager $manager)
{
$user = $this->getReference('student1-user');
// create 10 DangerPoints
for ($i = 0; $i < 10; ++$i) {
// create 6 DangerPoints
for ($i = 0; $i < 6; ++$i) {
$pt = new DangerPoint();
$pt->setTitle('point '.$i);
$pt->setDescription('point desc '.$i);
$pt->setTypeId($i);
$pt->setType($this->getReference('DangerType-'.$i));
$lat = 53.5 + (mt_rand(0, 1000) / 100);
$lon = 10 + (mt_rand(0, 100) / 100);
$pt->setPos(sprintf('SRID=4326;POINT(%f %f)', $lat, $lon));
$pt->setCreatedNow($user);
$pt->setArea(true);
$pt->setDanger(false);
$manager->persist($pt);
$manager->flush();
$this->addReference('danger-point' + $i, $pt);
}

$user = $this->getReference('student2-user');
// create 10 DangerPoints
for ($i = 10; $i < 20; ++$i) {
// create 6 DangerPoints
for ($i = 6; $i < 12; ++$i) {
$pt = new DangerPoint();
$pt->setTitle('point '.$i);
$pt->setDescription('point desc '.$i);
$pt->setTypeId($i);
$pt->setType($this->getReference('DangerType-'.$i));
$lat = 53.5 + (mt_rand(0, 1000) / 100);
$lon = 9.9 + (mt_rand(0, 100) / 100);
$pt->setPos(sprintf('SRID=4326;POINT(%f %f)', $lat, $lon));
$pt->setCreatedNow($user);
$pt->setArea(false);
$pt->setDanger(true);
$manager->persist($pt);
$manager->flush();
$this->addReference('danger-point' + $i, $pt);
Expand All @@ -67,7 +71,7 @@ public function load(ObjectManager $manager)
public function getDependencies()
{
return array(
SchoolUserFixtures::class,
SchoolUserFixtures::class, DangerTypeFixtures::class,
);
}
}
41 changes: 41 additions & 0 deletions src/AppBundle/DataFixtures/DangerTypeFixtures.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of the ADFC Radschulwegplan Backend package.
*
* <https://github.com/ADFC-Hamburg/adfc-radschulwegplan-backend>
*
* (c) 2018 by James Twellmeyer <jet02@twellmeyer.eu>
* (c) 2018 by Sven Anders <github2018@sven.anders.hamburg>
*
* Released under the GPL 3.0
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Please also visit our (german) webpage about the project:
*
* <https://hamburg.adfc.de/verkehr/themen-a-z/kinder/schulwegplanung/>
*
*/

namespace AppBundle\DataFixtures;

use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Bundle\FixturesBundle\ORMFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;

class DangerTypeFixtures extends Fixture implements ORMFixtureInterface
{
public function load(ObjectManager $manager)
{
$repo = $manager->getRepository('AppBundle:DangerType');
$arr = $repo->insertDangerTypes();
$i = 0;
foreach ($arr as $t) {
$typeName = 'DangerType-'.$i;
$this->addReference($typeName, $t);
++$i;
}
}
}
Loading