From 7c5d5eda3f555431313d8638f9c55b67b029aa01 Mon Sep 17 00:00:00 2001 From: Nick Kleine-Tebbe <nick@kleine-tebbe.de> Date: Wed, 26 Jan 2022 20:39:39 +0100 Subject: [PATCH] kolloquium titles are loaded from folders 2 --- api/index.js | 11 +++-- api/routes/kolloquiums.js | 87 ++++++++++++++++++++++++++++++++--- components/KolloquiumItem.vue | 6 +-- nuxt.config.js | 1 - package-lock.json | 1 + package.json | 1 + pages/index.vue | 33 +++++++++++-- 7 files changed, 121 insertions(+), 19 deletions(-) diff --git a/api/index.js b/api/index.js index 2071ada..bb67126 100644 --- a/api/index.js +++ b/api/index.js @@ -1,16 +1,21 @@ const express = require('express') - +var bodyParser = require('body-parser') // Create express instance const app = express() // Require API routes const users = require('./routes/users') -const test = require('./routes/kolloquiums') +const kolloquiums = require('./routes/kolloquiums') // Import API Routes + +// parse application/x-www-form-urlencoded +app.use(bodyParser.urlencoded({ extended: false })) +// parse application/json +app.use(bodyParser.json()) + app.use(users) app.use(kolloquiums) - // Export express app module.exports = app diff --git a/api/routes/kolloquiums.js b/api/routes/kolloquiums.js index 6fb1b1b..241463e 100644 --- a/api/routes/kolloquiums.js +++ b/api/routes/kolloquiums.js @@ -10,10 +10,10 @@ function getDirectories(path) { } fs.mkdir('Kolloquiums',function(err) { - if (err.code === "EEXIST") { + if (err && err.code === "EEXIST") { console.log('Kolloquiums Directory already existed') } - else if (err.code != "EEXIST") { + else if (err && err.code != "EEXIST") { return console.error(err); } else { @@ -24,17 +24,90 @@ fs.mkdir('Kolloquiums',function(err) { // Get Kolloquiums router.use('/getKolloquiums', (req, res) => { var directories = getDirectories('Kolloquiums') - res.json({ + return res.json({ kolloquiums: directories }); }) // Delete Kolloquium -router.use('/deleteKolloquiums', (req, res) => { - console.log(req) - res.json({ - status: 'success' +router.use('/deleteKolloquium', (req, res) => { + console.log('deleting Kolloquium') + let { title } = req.body + if(title == '') { + console.error('Folder has no name') + return res.json({ + status: 'error', + message: 'Folder has no name' + }) + } + fs.rmdir("Kolloquiums/" + title, function(err) { + if (err) { + console.error(err); + return res.json({ + status: 'error', + message: err + }) + } + }); + return res.json({ + status: 'success', + message: 'removed Kolloquium ' + title }) }) +// Create Kolloquium +router.use('/createKolloquium', (req, res) => { + console.log('creating Kolloquium') + console.log(req.body) + let { title } = req.body + fs.mkdir('Kolloquiums/' + title,function(err) { + if (err && err.code === "EEXIST") { + console.warn('Directory "' + title + '" already existed') + return res.json({ + status: 'warning', + message: 'Directory "' + title + '" already existed' + }) + } + else if (err && err.code != "EEXIST") { + console.error(err); + return res.json({ + status: 'error', + message: err + }) + } + else { + console.log('Directory "' + title + '" created successfully!'); + return res.json({ + status: 'success', + message: 'Directory "' + title + '" created successfully!' + }) + } + }); +}) + +// Rename Kolloquium +router.use('/renameKolloquium', (req, res) => { + console.log('renaming Kolloquium') + console.log(req.body); + let { oldTitle, newTitle } = req.body + if( oldTitle == '' || newTitle == '') { + console.error('at least one of the names was empty') + return res.json({ + status: 'error', + message: 'at least one of the names was empty' + }) + } + fs.rename('Kolloquiums/' + oldTitle, 'Kolloquiums/' + newTitle, (err) => { + if(err) { + console.error(err); + return res.json({ + status: 'error', + message: err + }) + } + + console.log("Directory renamed successfully."); + }); +}) + module.exports = router diff --git a/components/KolloquiumItem.vue b/components/KolloquiumItem.vue index aa964ec..ac354ec 100644 --- a/components/KolloquiumItem.vue +++ b/components/KolloquiumItem.vue @@ -58,11 +58,11 @@ export default { }, methods: { save() { - this.$emit("update:inEdit", false); - this.$emit("update:title", this.title); + this.$emit("update:inEdit", {inEdit: false, title: this.title}); + // this.$emit("update:title", this.title); // done in update:inEdit }, edit() { - this.$emit("update:inEdit", true); + this.$emit("update:inEdit", {inEdit: true, title: this.title}); }, deleteMe() { this.$emit("deleteKolloquium") diff --git a/nuxt.config.js b/nuxt.config.js index 4e2deac..cdd09cd 100644 --- a/nuxt.config.js +++ b/nuxt.config.js @@ -1,4 +1,3 @@ - export default { // Nuxt target see https://nuxtjs.org/api/configuration-target target: 'server', diff --git a/package-lock.json b/package-lock.json index 7d142c4..f41e8c6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "dependencies": { "@nuxt/http": "latest", "@nuxtjs/axios": "^5.13.6", + "body-parser": "^1.19.1", "express": "latest", "nuxt": "latest" }, diff --git a/package.json b/package.json index 0238341..b450bce 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "dependencies": { "@nuxt/http": "latest", "@nuxtjs/axios": "^5.13.6", + "body-parser": "^1.19.1", "express": "latest", "nuxt": "latest" }, diff --git a/pages/index.vue b/pages/index.vue index 9e7c447..9917d0d 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -98,21 +98,44 @@ export default { }); this.selectedKolloquium = ""; this.kolloquiums = this.kolloquiums.filter(kolloquium => kolloquium.title.length > 0); - this.kolloquiums = [...this.kolloquiums, {title: '', inEdit: true}]; - + this.kolloquiums = [...this.kolloquiums, {title: '', inEdit: true, isNew: true}]; }, - toggleEdit(kolloquium, status) { + toggleEdit(kolloquium, {inEdit, title}) { + let createNew = kolloquium.inEdit && kolloquium.isNew; + let changeName = kolloquium.inEdit && !kolloquium.isNew; + this.kolloquiums.forEach(kolloquium => { kolloquium.inEdit = false; }); - kolloquium.inEdit = status; + kolloquium.inEdit = inEdit; + + if(createNew){ + kolloquium.title = title + kolloquium.isNew = false + if (!title || title == '') { + this.deleteKolloquium(title) + return + } + this.$axios.post('api/createKolloquium', { title: title }) + } + else if (changeName) { + if (title == '') { + return + } + kolloquium.title = title + this.$axios.post('api/renameKolloquium', { oldTitle: kolloquium.title, newTitle: title}) + } }, deleteKolloquium(kolloquiumToDelete) { this.kolloquiums = this.kolloquiums.filter(kolloquium => kolloquium.title != kolloquiumToDelete.title); + this.selectedKolloquium = "" + if(kolloquiumToDelete != ''){ + this.$axios.post('api/deleteKolloquium', { title: kolloquiumToDelete.title }) + } }, }, async asyncData ({ $http }) { - const data = await $http.$get('/api/kolloquiums/getKolloquiums') + const data = await $http.$get('/api/getKolloquiums/') let kolloquiumList = [] data.kolloquiums.forEach(title => { kolloquiumList.push({ -- GitLab