1
0
mirror of https://framagit.org/tykayn/date-poll-api synced 2023-08-25 08:23:11 +02:00

update of a vote stack returns a vote stack

This commit is contained in:
Tykayn 2021-06-10 10:17:42 +02:00 committed by tykayn
parent 18d251270b
commit fd28433dd8
6 changed files with 213 additions and 38 deletions

View File

@ -46,6 +46,7 @@
"doctrine/doctrine-migrations-bundle": "^3.1",
"doctrine/orm": "^2.8",
"friendsofsymfony/rest-bundle": "^3.0",
"gedmo/doctrine-extensions": "^3.0",
"jms/serializer-bundle": "^3.9",
"nelmio/cors-bundle": "^2.1",
"sensio/framework-extra-bundle": "^6.1",

149
composer.lock generated
View File

@ -4,8 +4,57 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "104b8c116015bbc6f7f3efa3d0618a90",
"content-hash": "9f35ffaf799b2f21e80284cb4800a791",
"packages": [
{
"name": "behat/transliterator",
"version": "v1.3.0",
"source": {
"type": "git",
"url": "https://github.com/Behat/Transliterator.git",
"reference": "3c4ec1d77c3d05caa1f0bf8fb3aae4845005c7fc"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Behat/Transliterator/zipball/3c4ec1d77c3d05caa1f0bf8fb3aae4845005c7fc",
"reference": "3c4ec1d77c3d05caa1f0bf8fb3aae4845005c7fc",
"shasum": ""
},
"require": {
"php": ">=5.3.3"
},
"require-dev": {
"chuyskywalker/rolling-curl": "^3.1",
"php-yaoi/php-yaoi": "^1.0",
"phpunit/phpunit": "^4.8.36|^6.3"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.2-dev"
}
},
"autoload": {
"psr-4": {
"Behat\\Transliterator\\": "src/Behat/Transliterator"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"Artistic-1.0"
],
"description": "String transliterator",
"keywords": [
"i18n",
"slug",
"transliterator"
],
"support": {
"issues": "https://github.com/Behat/Transliterator/issues",
"source": "https://github.com/Behat/Transliterator/tree/v1.3.0"
},
"time": "2020-01-14T16:39:13+00:00"
},
{
"name": "composer/package-versions-deprecated",
"version": "1.11.99.1",
@ -1682,6 +1731,104 @@
},
"time": "2021-01-02T11:26:24+00:00"
},
{
"name": "gedmo/doctrine-extensions",
"version": "v3.0.5",
"source": {
"type": "git",
"url": "https://github.com/doctrine-extensions/DoctrineExtensions.git",
"reference": "f956c3c4d0c0ffdc5dd879288073772e439b6c1a"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/doctrine-extensions/DoctrineExtensions/zipball/f956c3c4d0c0ffdc5dd879288073772e439b6c1a",
"reference": "f956c3c4d0c0ffdc5dd879288073772e439b6c1a",
"shasum": ""
},
"require": {
"behat/transliterator": "~1.2",
"doctrine/annotations": "^1.2",
"doctrine/cache": "^1.0",
"doctrine/collections": "^1.0",
"doctrine/common": "^2.13 || ^3.0",
"doctrine/event-manager": "^1.0",
"php": "^7.2 || ^8.0"
},
"conflict": {
"doctrine/mongodb": "<1.3",
"doctrine/mongodb-odm": "<2.0",
"sebastian/comparator": "<2.0"
},
"provide": {
"ext-mongo": "1.6.12"
},
"require-dev": {
"alcaeus/mongo-php-adapter": "^1.1",
"doctrine/doctrine-bundle": "^2.3",
"doctrine/mongodb-odm": "^2.0",
"doctrine/orm": "^2.6.3",
"friendsofphp/php-cs-fixer": "^2.16",
"phpunit/phpunit": "^8.5",
"symfony/yaml": "^4.1"
},
"suggest": {
"doctrine/mongodb-odm": "to use the extensions with the MongoDB ODM",
"doctrine/orm": "to use the extensions with the ORM"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.0-dev"
}
},
"autoload": {
"psr-4": {
"Gedmo\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Gediminas Morkevicius",
"email": "gediminas.morkevicius@gmail.com"
},
{
"name": "Gustavo Falco",
"email": "comfortablynumb84@gmail.com"
},
{
"name": "David Buchmann",
"email": "david@liip.ch"
}
],
"description": "Doctrine2 behavioral extensions",
"homepage": "http://gediminasm.org/",
"keywords": [
"Blameable",
"behaviors",
"doctrine2",
"extensions",
"gedmo",
"loggable",
"nestedset",
"sluggable",
"sortable",
"timestampable",
"translatable",
"tree",
"uploadable"
],
"support": {
"email": "gediminas.morkevicius@gmail.com",
"issues": "https://github.com/doctrine-extensions/DoctrineExtensions/issues",
"source": "https://github.com/doctrine-extensions/DoctrineExtensions/tree/v3.0.5",
"wiki": "https://github.com/Atlantic18/DoctrineExtensions/tree/main/doc"
},
"time": "2021-04-23T09:16:01+00:00"
},
{
"name": "jms/metadata",
"version": "2.5.0",

View File

@ -3,6 +3,7 @@
namespace App\Controller\api\v1;
use App\Controller\EmailsController;
use App\Entity\Choice;
use App\Entity\Comment;
use App\Entity\Owner;
use App\Entity\Poll;
@ -162,16 +163,17 @@ class VoteController extends EmailsController {
$data = $request->getContent();
$data = json_decode( $data, true );
if ( $whocanchangeanswers == 'everybody' ) {
$table_votes_by_choice_id = [];
$voteStack->patchVotes( $data[ 'votes' ] );
if ( $whocanchangeanswers == 'everybody' ) {
$table_votes_by_choice_id = $this->patchVotes( $voteStack, $data[ 'votes' ] );
} else if ( $whocanchangeanswers == 'self' ) {
// someone with the right token of this vote stack only can change this
if ( ! $modifierToken || $voteStack->getOwner()->getModifierToken() !== $modifierToken ) {
return $this->json( [ 'message' => 'your token does not allow you to modify this vote ' ],
403 );
}
$voteStack->patchVotes( $data[ 'votes' ] );
$this->patchVotes( $voteStack, $data[ 'votes' ] );
// everything is ok, we can update all the votes of the vote stack
} else if ( $whocanchangeanswers == 'nobody' ) {
// only the poll admin with the poll modifier token can change this
@ -179,7 +181,7 @@ class VoteController extends EmailsController {
return $this->json( [ 'message' => 'your token does not allow you to modify this vote ' ],
403 );
}
$voteStack->patchVotes( $data[ 'votes' ] );
$this->patchVotes( $voteStack, $data[ 'votes' ] );
}
@ -191,13 +193,7 @@ class VoteController extends EmailsController {
// save everything
$jsonResponse = [
'message' => 'ok',
'whocanchangeanswers' => $whocanchangeanswers,
'modifier_token' => $voteStack->getOwner()->getModifierToken(),
'vote_stack' => $voteStack,
'data' => $id,
];
$jsonResponse = $voteStack->displayForAdmin();
return $this->json( $jsonResponse, 200 );
@ -205,6 +201,55 @@ class VoteController extends EmailsController {
}
public function patchVotes( $stackOfVotes, $data ) {
$table_votes_by_choice_id = [];
$votes = $stackOfVotes->getVotes();
$choices = $stackOfVotes->getPoll()->getChoices();
$table_choices_by_id = [];
$em = $this->getDoctrine()->getManager();
foreach ( $choices as $c ) {
$table_choices_by_id[ $c->getId() ] = $c;
}
foreach ( $votes as $stack_vote ) {
$table_votes_by_choice_id[ $stack_vote->getChoice()->getId() ] = $stack_vote;
}
foreach ( $data as $vote ) {
$newValue = $vote[ 'value' ];
$choice_id = $vote[ 'choice_id' ];
// if the new value is null, remove the vote
if ( isset( $table_choices_by_id[ $choice_id ] ) ) {
if ( isset( $table_votes_by_choice_id[ $choice_id ] ) ) {
$voteFound = $table_votes_by_choice_id[ $choice_id ];
if ( $newValue ) {
$voteFound->setValue( $newValue );
$em->persist( $voteFound );
} else {
$stackOfVotes->removeVote( $voteFound );
$em->remove( $voteFound );
}
} else if ( $newValue ) {
// create vote for this stack
$newVote = new Vote();
$newVote->setChoice( $em->find( Choice::class, $choice_id ) )
->setValue( $newValue );
$em->persist( $newVote );
$stackOfVotes->addVote( $newVote );
}
}
}
$em->persist( $stackOfVotes );
$em->flush();
return $table_votes_by_choice_id;
}
/**
* @Route(
* path = "/{id}/token/{modifierToken}",

View File

@ -98,6 +98,7 @@ class Owner {
public function displayForAdmin() {
return [
'pseudo' => $this->getPseudo(),
'email' => $this->getEmail(),
'modifier_token' => $this->getModifierToken(),
'created_at' => $this->getCreatedAtAsString(),
];

View File

@ -26,7 +26,7 @@ class StackOfVotes {
*/
public $pseudo;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Vote", mappedBy="stacksOfVotes", cascade={"persist","remove"})
* @ORM\OneToMany(targetEntity="App\Entity\Vote", mappedBy="stacksOfVotes", orphanRemoval=true, cascade={"persist","remove"})
* @Serializer\Expose()
*/
public $votes;
@ -161,29 +161,4 @@ class StackOfVotes {
return $this;
}
/**
* only update the values of votes
*
* @param $votes
*/
public function patchVotes( $votes ) {
$table_votes_by_id = [];
$stackVotes = $this->getVotes();
foreach ( $stackVotes as $stack_vote ) {
$table_votes_by_id[ $stack_vote->getId() ] = $stack_vote;
}
foreach ( $votes as $vote ) {
$newValue = $vote[ 'value' ];
$id = $vote[ 'id' ];
// if the new value is null, remove the vote
if ( $newValue && isset( $table_votes_by_id[ $id ] ) ) {
$table_votes_by_id[ $id ]->setValue( $newValue );
// $this->addVote($table_votes_by_id[$id]);
}
}
}
}

View File

@ -1,4 +1,7 @@
{
"behat/transliterator": {
"version": "v1.3.0"
},
"composer/package-versions-deprecated": {
"version": "1.11.99.1"
},
@ -115,6 +118,9 @@
"config/packages/fos_rest.yaml"
]
},
"gedmo/doctrine-extensions": {
"version": "v3.0.5"
},
"jms/metadata": {
"version": "2.5.0"
},