Skip to content
Snippets Groups Projects
Commit 7e8eb317 authored by AA2875's avatar AA2875
Browse files

Muokkausta sulavemmaks

parent b7de531b
No related branches found
No related tags found
No related merge requests found
......@@ -3,13 +3,10 @@ const cors = require('cors')
const app = express()
const port = 3000
// cors - allow connection from different domains and ports
app.use(cors())
// convert json string to json object (from request)
app.use(express.json())
// mongo
const mongoose = require('mongoose')
const mongoDB = 'mongodb+srv://eemuli:N33k3r123@test.147x5qr.mongodb.net/hyppynet'
mongoose.connect(mongoDB, {useNewUrlParser: true, useUnifiedTopology: true})
......@@ -20,7 +17,6 @@ db.once('open', function() {
console.log("Database connected")
})
// scheema
const jumpSchema = new mongoose.Schema({
number: { type: String, required: true },
date: { type: String, required: true },
......@@ -32,8 +28,7 @@ const jumpSchema = new mongoose.Schema({
comments: { type: String, required: false },
link: { type: String, required: false }
})
// model
const Jump = mongoose.model('Jump', jumpSchema, 'jumps')
app.post('/jumps', async (request, response) => {
......@@ -83,12 +78,11 @@ app.delete('/jumps/:id', async (request, response) => {
else response.status(404).end()
})
// update user data
app.put('/jumps/:id', async (request, response) => {
const { number,date,place,plane,canopy,height,falltime,comments,link } = request.body
const { number, date, place, plane, canopy, height, falltime, comments, link } = request.body
const { id } = request.params
const jump = await Jump.findByIdAndUpdate(
id,
id,
{'number': number,
'date': date,
'place': place,
......@@ -99,7 +93,6 @@ app.put('/jumps/:id', async (request, response) => {
'comments': comments,
'link': link},
{'new': true}
// Tähä kaikki slotit?
)
response.json(jump)
})
......
......@@ -139,12 +139,44 @@ function makeEditable(span) {
input.value = span.innerHTML
let li = span.parentElement
let id = li.parentElement.parentElement.id
let specName = li.className
let originalValue = span.innerHTML
span.remove()
li.appendChild(input)
input.focus()
input.addEventListener('focusout', () => saveJump(id))
// input takas spaniks
let validateForUpdate = function () {
if (input.value != '') {
saveJump(id)
savedEdit(li)
} else {
input.value = originalValue
input.style.color = 'red'
input.style.transitionDuration = '0s'
setTimeout(() => {
input.style.transitionDuration = '1s'
input.style.color = 'black'
}, 1000);
}
}
input.addEventListener('focusout', validateForUpdate)
input.addEventListener('keypress', (event) => {
if (event.key === 'Enter') {
validateForUpdate
input.blur()
}
} )
}
function savedEdit(li) {
let input = li.firstChild
let savedValue = input.value
input.remove()
let span = document.createElement('span')
let spanClass = document.createAttribute('class')
spanClass.value = 'fullJumpInfoValue'
span.setAttributeNode(spanClass)
span.appendChild(document.createTextNode(savedValue))
li.appendChild(span)
return li
}
function createHyperlink(link) {
......@@ -290,13 +322,14 @@ function editJump(id){
function getJumpInfo(id) {
let jump = document.getElementById(id)
let number = jump.getElementsByClassName('jumpNumber')[0].innerHTML
number = number.slice(1,number.length)
let date = jump.getElementsByClassName('headerJumpDate')[0].innerHTML
let data = {
'number': number,
'date': date
}
let infoKeys = ['place','plane','canopy','height','falltime','comments']
infoKeys.forEach(infoName => {
try {
let li = jump.getElementsByClassName(infoName)[0]
......@@ -328,7 +361,9 @@ async function saveJump(id){ // Erheitä esim text newJump.value
},
body: JSON.stringify(data)
})
console.log(response)
let jumpIndex = jumpsJSON.findIndex(jump => jump._id === id)
data['_id'] = id
jumpsJSON[jumpIndex] = data
}
async function removeJump(id) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment