diff --git a/api/index.js b/api/index.js index 2071ada46411affdcb8c404a1e9f08987028915b..bb67126fd14181974f5e3f476fdc13ac6f209c5a 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 6fb1b1ba2b4c06ff3866545497b1f53065a2622b..241463ef093d4a5f933c3721cf8e87fdad948d57 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 aa964ec61b3903cadcd723e160fad473a883f4ac..ac354ecf10601eb32de654020584c2e68425dbd2 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 4e2deac3eec7b08a6e40ac23f61cc43063789610..cdd09cd0a2a48cd24c9628d8a28e2f17658380ee 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 7d142c46bf254b05c822373a8e8503802a9e8eb1..f41e8c6edfb8c67a90060e84caa45412567c58e2 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 0238341aa52cedd3957320a49b95ef65705e7890..b450bcec37973e729a33e97bea908ffcf8408b0b 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 9e7c4479cba9f50caf57dae5e5b57697ccb8044d..9917d0dc46d5e989eb665778c070b6cb7e6f706d 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({