Skip to content
Snippets Groups Projects
index.js 2.81 KiB
Newer Older
AA2875's avatar
AA2875 committed
const express = require('express') 
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})

const db = mongoose.connection
db.on('error', console.error.bind(console, 'connection error:'))
db.once('open', function() {
    console.log("Database test connected")
})

// scheema
const jumpSchema = new mongoose.Schema({
    number: { type: String, required: true },
AA2875's avatar
AA2875 committed
    date: { type: String, required: true },
AA2875's avatar
AA2875 committed
    place: { type: String, required: true },
    plane: { type: String, required: true },
    canopy: { type: String, required: true },
AA2875's avatar
AA2875 committed
    height: { type: String, required: true },
AA2875's avatar
AA2875 committed
    falltime: { type: String, required: true },
    comments: { type: String, required: false },
    link: { type: String, required: false }
AA2875's avatar
AA2875 committed
})
  
// model
const Jump = mongoose.model('Jump', jumpSchema, 'jumps')
  
app.post('/jumps', async (request, response) => {
    console.log(request.body)
AA2875's avatar
AA2875 committed
    const { number, date, place, plane, canopy, height, falltime, comments, link  } = request.body
    console.log(number, date, place, plane, canopy, height, falltime, comments, link )
AA2875's avatar
AA2875 committed
    const jump = new Jump({
        number: number,
AA2875's avatar
AA2875 committed
        date: date,
AA2875's avatar
AA2875 committed
        place: place,
        plane: plane,
        canopy: canopy,
AA2875's avatar
AA2875 committed
        height: height,
AA2875's avatar
AA2875 committed
        falltime: falltime,
        comments: comments,
        link: link
AA2875's avatar
AA2875 committed
    })
    try {
        const savedJump = await jump.save()
        response.json(savedJump)  
    } catch (error) {
        if (error.name === "ValidationError") {
            return response.status(400)
        }
        response.status(500)
    }
AA2875's avatar
AA2875 committed
})

app.get('/jumps/:id', async (request, response) => {
    const jump = await Jump.findById(request.params.id)
    if (jump) {
        response.json(jump)
    }
    else {
        response.status(404).end()
    }
})

app.get('/jumps', async (request, response) => {
    const jumps = await Jump.find({})
    response.json(jumps)
})

app.delete('/jumps/:id', async (request, response) => {
    const deletedJump = await Jump.findByIdAndRemove(request.params.id)
    if (deletedJump) response.json(deletedJump)
    else response.status(404).end()
})

// update user data
app.put('/jumps/:id', async (request, response) => {
    const { text } = request.body
    const { id } = request.params
    const jump = await Jump.findByIdAndUpdate(
AA2875's avatar
AA2875 committed
        id,
        {'text': text},
        {'new': true}
AA2875's avatar
AA2875 committed
    )
    response.json(jump)
AA2875's avatar
AA2875 committed
})
AA2875's avatar
AA2875 committed
  

// app listen port 3000
app.listen(port, () => {
    console.log('Example app listening on port 3000')
})