Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/polls.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordartcafe <github@dartcafe.de>2020-08-22 08:09:01 +0300
committerdartcafe <github@dartcafe.de>2020-08-22 08:09:01 +0300
commitb0b220ef34e65a3f4e4fb12603142c376e5573f5 (patch)
tree16a0096f81717ae2e6647dc5736c5b7f4e64e033
parent6b73deac12a2dabc4d94b92ce91eebd04620e3bb (diff)
move option sequence to backend #1058
-rw-r--r--appinfo/routes.php5
-rw-r--r--lib/Controller/OptionController.php13
-rw-r--r--lib/Service/OptionService.php43
-rw-r--r--src/js/components/Base/OptionCloneDate.vue25
-rw-r--r--src/js/components/VoteTable/VoteTable.vue2
-rw-r--r--src/js/store/modules/subModules/options.js22
6 files changed, 85 insertions, 25 deletions
diff --git a/appinfo/routes.php b/appinfo/routes.php
index 1a3ff5c3..e88c0752 100644
--- a/appinfo/routes.php
+++ b/appinfo/routes.php
@@ -41,12 +41,13 @@ return [
['name' => 'poll#clone', 'url' => '/polls/clone/{pollId}', 'verb' => 'GET'],
['name' => 'poll#getParticipantsEmailAddresses', 'url' => '/polls/addresses/{pollId}', 'verb' => 'GET'],
+ ['name' => 'option#list', 'url' => '/polls/{pollId}/options', 'verb' => 'GET'],
+ ['name' => 'option#reorder', 'url' => '/polls/{pollId}/options/reorder', 'verb' => 'POST'],
['name' => 'option#add', 'url' => '/option', 'verb' => 'POST'],
['name' => 'option#update', 'url' => '/option/{optionId}', 'verb' => 'PUT'],
['name' => 'option#delete', 'url' => '/option/{optionId}', 'verb' => 'DELETE'],
['name' => 'option#confirm', 'url' => '/option/{optionId}/confirm', 'verb' => 'PUT'],
- ['name' => 'option#reorder', 'url' => '/options/reorder', 'verb' => 'POST'],
- ['name' => 'option#list', 'url' => '/polls/{pollId}/options', 'verb' => 'GET'],
+ ['name' => 'option#sequence', 'url' => '/option/{optionId}/sequence', 'verb' => 'POST'],
// ['name' => 'option#listByToken', 'url' => '/options/get/s/{token}', 'verb' => 'GET'],
['name' => 'vote#set', 'url' => '/vote/set', 'verb' => 'POST'],
diff --git a/lib/Controller/OptionController.php b/lib/Controller/OptionController.php
index 466902ee..f7a36ce6 100644
--- a/lib/Controller/OptionController.php
+++ b/lib/Controller/OptionController.php
@@ -118,4 +118,17 @@ class OptionController extends Controller {
public function reorder($pollId, $options) {
return new DataResponse(['options' => $this->optionService->reorder($pollId, $options)], Http::STATUS_OK);
}
+
+ /**
+ * Reorder options
+ * @NoAdminRequired
+ * @param int $optionId
+ * @param int $step
+ * @param string $unit
+ * @param int $amount
+ * @return DataResponse
+ */
+ public function sequence($optionId, $step, $unit, $amount) {
+ return new DataResponse(['options' => $this->optionService->sequence($optionId, $step, $unit, $amount)], Http::STATUS_OK);
+ }
}
diff --git a/lib/Service/OptionService.php b/lib/Service/OptionService.php
index 14d6e8a1..25242c66 100644
--- a/lib/Service/OptionService.php
+++ b/lib/Service/OptionService.php
@@ -23,6 +23,7 @@
namespace OCA\Polls\Service;
+use DateTime;
use Exception;
use OCP\AppFramework\Db\DoesNotExistException;
use OCA\Polls\Exceptions\NotAuthorizedException;
@@ -201,6 +202,48 @@ class OptionService {
}
/**
+ * Make a sequence of date poll options
+ * @NoAdminRequired
+ * @param int $optionId
+ * @param int $step
+ * @param string $unit
+ * @param int $amount
+ * @return array Array of Option objects
+ * @throws NotAuthorizedException
+ */
+ public function sequence($optionId, $step, $unit, $amount) {
+
+ $baseDate = new DateTime;
+ $origin = $this->optionMapper->find($optionId);
+
+ if (!$this->acl->set($origin->getPollId())->getAllowEdit()) {
+ throw new NotAuthorizedException;
+ }
+
+ if ($step === 0) {
+ return $this->optionMapper->findByPoll($origin->getPollId());
+ }
+
+ $baseDate->setTimestamp($origin->getTimestamp());
+
+ for ($i=0; $i < $amount; $i++) {
+
+ $this->option = new Option();
+ $this->option->setPollId($origin->getPollId());
+ $this->option->setConfirmed(0);
+ $this->option->setTimestamp($baseDate->modify($step . ' ' . $unit)->getTimestamp());
+ $this->option->setPollOptionText($baseDate->format('c'));
+ $this->option->setOrder($baseDate->getTimestamp());
+ try {
+ $this->optionMapper->insert($this->option);
+ } catch (UniqueConstraintViolationException $e) {
+ \OC::$server->getLogger()->warning('skip adding ' . $baseDate->format('c') . 'for pollId' . $origin->getPollId() . '. Option alredy exists.');
+ }
+ }
+ return $this->optionMapper->findByPoll($origin->getPollId());
+ }
+
+ /**
* Copy options from $fromPoll to $toPoll
* @NoAdminRequired
* @param int $fromPollId
diff --git a/src/js/components/Base/OptionCloneDate.vue b/src/js/components/Base/OptionCloneDate.vue
index 5eaa532a..08399406 100644
--- a/src/js/components/Base/OptionCloneDate.vue
+++ b/src/js/components/Base/OptionCloneDate.vue
@@ -47,7 +47,7 @@
<script>
import moment from '@nextcloud/moment'
-import { showInfo, showError } from '@nextcloud/dialogs'
+// import { showInfo, showError } from '@nextcloud/dialogs'
import { Multiselect } from '@nextcloud/vue'
import { dateUnits } from '../../mixins/dateMixins'
@@ -85,25 +85,12 @@ export default {
methods: {
createSequence() {
- for (var i = 0; i < this.sequence.amount; i++) {
- this.$store.dispatch('poll/options/add', {
- timestamp: moment.unix(this.option.timestamp).add(
- this.sequence.step * (i + 1),
- this.sequence.unit.value
- ).unix(),
+ this.$store
+ .dispatch('poll/options/sequence', {
+ option: this.option,
+ sequence: this.sequence,
})
- .catch((error) => {
- const dateString = moment.unix(this.option.timestamp).add(
- this.sequence.step * (i + 1),
- this.sequence.unit.value).format('LLLL')
- if (error.response.status === 409) {
- showInfo(t('polls', 'Option "{dateString}" already exists.', { dateString: dateString }))
- } else {
- showError(t('polls', 'Option "{dateString}" could not be added.', { dateString: dateString }))
- }
- })
- this.$emit('close')
- }
+ this.$emit('close')
},
},
}
diff --git a/src/js/components/VoteTable/VoteTable.vue b/src/js/components/VoteTable/VoteTable.vue
index 3837e27c..4cd91fb6 100644
--- a/src/js/components/VoteTable/VoteTable.vue
+++ b/src/js/components/VoteTable/VoteTable.vue
@@ -148,7 +148,7 @@ export default {
userId: this.userToRemove,
})
.then(() => {
- showSuccess(t('polls', 'User {userId} removed', { userId: this.userToRemove} ))
+ showSuccess(t('polls', 'User {userId} removed', { userId: this.userToRemove }))
})
this.modal = false
this.userToRemove = ''
diff --git a/src/js/store/modules/subModules/options.js b/src/js/store/modules/subModules/options.js
index e2536710..f6eb72e7 100644
--- a/src/js/store/modules/subModules/options.js
+++ b/src/js/store/modules/subModules/options.js
@@ -184,9 +184,8 @@ const actions = {
},
reorder(context, payload) {
- const endPoint = 'apps/polls/options/reorder'
- return axios.post(generateUrl(endPoint), {
- pollId: context.rootState.poll.id,
+ const endPoint = 'apps/polls/polls'
+ return axios.post(generateUrl(endPoint.concat(context.rootState.poll.id, '/options/reorder')), {
options: payload,
})
.then((response) => {
@@ -198,6 +197,23 @@ const actions = {
throw error
})
},
+
+ sequence(context, payload) {
+ const endPoint = 'apps/polls/option'
+ return axios.post(generateUrl(endPoint.concat('/', payload.option.id, '/sequence')), {
+ step: payload.sequence.step,
+ unit: payload.sequence.unit.value,
+ amount: payload.sequence.amount,
+ })
+ .then((response) => {
+ context.commit('set', { options: response.data.options })
+ })
+ .catch((error) => {
+ console.error('Error creating sequence', { error: error.response }, { payload: payload })
+ context.dispatch('reload')
+ throw error
+ })
+ },
}
export default { state, mutations, getters, actions, namespaced }