<file_sep># spotted-api
An Api for spots locator application
<file_sep>const Mongoose = require('mongoose')

const SpotSchema = Mongoose.Schema({
  name: String,
  type: String,
  description: String,
  difficulty: String,
  location: {
    lat: Number,
    lng: Number,
    placeId: String,
    address: String
  },
  indoor: Boolean,
  media: {
    pictures: [String],
    videos: [String]
  }
}, { collection: 'spots' })

module.exports = Mongoose.model('Spot', SpotSchema)
<file_sep>const spotModel = require('../mongoose/spot.js')
const fakeSpot = require('../fixtures.js')

module.exports = {
  getSpots: (req, res) => {
    spotModel.find().exec((err, data) => {
      if (err) {
        res.status(500).send('Something broke : ', err)
      }
      res.json(data)
    })
  },
  getVisibleSpots: (req, res, corners) => {
    const coords = JSON.parse(corners)

    spotModel.find({
      "localisation.lat": { $gte: coords.swc.lat, $lte: coords.nec.lat },
      "localisation.lng": { $gte: coords.swc.lng, $lte: coords.nec.lng },
    }).exec((err, data) => {
      if (err) {
        res.status(500).send(`Something is broken : ${err}`)
      }
      res.json(data)
    })
  },
  addSpot: (req, res, spot) => {
    const newSpot = JSON.parse(spot)

    spotModel.create(newSpot, (err, data) => {
      if (err) {
        res.status(500).send(`Seomething broke : ${err}`)
      }
      res.status(200).send(`The spot : ${data.name} has been succesfully added !!`)
    })
  },
  updateSpot: (req, res, spot) => {
    const newSpot = JSON.parse(spot)

    spotModel.updateOne({_id: newSpot._id}, newSpot, (err, data) => {
    if (err) {
      res.status(500).send(`Something broke : ${err}`)
    }
    res.status(200).send(`The spot : ${newSpot.name} has been succesfully updated !!`)
    })
  },
  deleteSpot: (req, res, spot) => {
    const spotToRemove = JSON.parse(spot)

    spotModel.deleteOne(spotToRemove, (err, data) => {
      if (err) {
        res.status(500).send(`Something broke : ${err}`)
      }
      res.status(200).send(`The spot : ${spotToRemove._id} has been succesfully deleted !!`)
    })
  }
}<file_sep>module.exports = {
  options: {
    host: 'localhost',
    SERVER_PORT: 7000,
    BDD_URL: 'mongodb://localhost:27017/spotted',
  }
}<file_sep>const express = require('express')
const server = express()
const Mongoose = require('mongoose')
const config = require('config')
const spotModel = require('./mongoose/spot.js')
const bdd = config.get('options.BDD_URL')
const routes = require('./routes')
const port = config.get('options.SERVER_PORT')

server.listen(config.get('options.SERVER_PORT'))

console.info('listening on port :', port)

Mongoose.connect(bdd)

routes(server)
<file_sep>const spotsController = require('../controllers')

module.exports = (app) => {
  app.get('/api/spots', (req, res) => {
    spotsController.getSpots(req, res)
  })

  app.get('/api/visiblespots/:corners', (req, res) => {
    spotsController.getVisibleSpots(req, res, req.params.corners)
  })

  app.put('/api/addspot/:spot', (req, res) => {
    spotsController.addSpot(req, res, req.params.spot)
  })

  app.put('/api/updatespot/:spot', (req, res) => {
    console.log('req.params ===> ', require('util').inspect(req.params, { colors: true, depth: 2 }))
    spotsController.updateSpot(req, res, req.params.spot)
  })

  app.put('/api/deletespot/:spot', (req, res) => {
    spotsController.deleteSpot(req, res, req.params.spot)
  })
}
