3 Commits bec28c562c ... 6881844161

Author SHA1 Message Date
  Ben Ward 6881844161 plug in backend DB 3 years ago
  Ben Ward b0bdc4ea78 plug in backend DB 3 years ago
  Ben Ward c5966386f9 allow external access 3 years ago
8 changed files with 674 additions and 116 deletions
  1. 466 97
      package-lock.json
  2. 5 0
      package.json
  3. 123 0
      server/controllers/messages.js
  4. 11 0
      server/db/db.js
  5. 11 0
      server/db/models/message.js
  6. 13 0
      server/routes/messages.js
  7. 22 0
      server/server.js
  8. 23 19
      webpack.config.js

File diff suppressed because it is too large
+ 466 - 97
package-lock.json


+ 5 - 0
package.json

@@ -24,6 +24,11 @@
     "webpack-dev-server": "^4.6.0"
   },
   "dependencies": {
+    "body-parser": "^1.19.1",
+    "cors": "^2.8.5",
+    "express": "^4.17.1",
+    "mongoose": "^6.1.1",
+    "nodemon": "^2.0.15",
     "react": "^17.0.2",
     "react-dom": "^17.0.2"
   }

+ 123 - 0
server/controllers/messages.js

@@ -0,0 +1,123 @@
+const Message = require('../db/models/message')
+
+createMessage = (req, res) => {
+    const body = req.body
+
+    if (!body) {
+        return res.status(400).json({
+            success: false,
+            error: 'You must provide a message',
+        })
+    }
+
+    const message = new Message(body)
+
+    if (!message) {
+        return res.status(400).json({ success: false, error: err })
+    }
+
+    message
+        .save()
+        .then(() => {
+            return res.status(201).json({
+                success: true,
+                id: message._id,
+                message: 'Message created!',
+            })
+        })
+        .catch(error => {
+            return res.status(400).json({
+                error,
+                message: 'Message not created!',
+            })
+        })
+}
+
+updateMessage = async (req, res) => {
+    const body = req.body
+
+    if (!body) {
+        return res.status(400).json({
+            success: false,
+            error: 'You must provide a body to update',
+        })
+    }
+
+    Message.findOne({ _id: req.params.id }, (err, message) => {
+        if (err) {
+            return res.status(404).json({
+                err,
+                message: 'Message not found!',
+            })
+        }
+        message.message = body.message
+        message
+            .save()
+            .then(() => {
+                return res.status(200).json({
+                    success: true,
+                    id: message._id,
+                    message: 'Message updated!',
+                })
+            })
+            .catch(error => {
+                return res.status(404).json({
+                    error,
+                    message: 'Message not updated!',
+                })
+            })
+    })
+}
+
+deleteMessage = async (req, res) => {
+    await Message.findOneAndDelete({ _id: req.params.id }, (err, message) => {
+        if (err) {
+            return res.status(400).json({ success: false, error: err })
+        }
+
+        if (!message) {
+            return res
+                .status(404)
+                .json({ success: false, error: `Message not found` })
+        }
+
+        return res.status(200).json({ success: true, data: message })
+    }).catch(err => console.log(err))
+}
+
+getMessageById = async (req, res) => {
+    await Message.findOne({ _id: req.params.id }, (err, message) => {
+        if (err) {
+            return res.status(400).json({ success: false, error: err })
+        }
+
+        if (!message) {
+            return res
+                .status(404)
+                .json({ success: false, error: `Message not found` })
+        }
+        return res.status(200).json({ success: true, data: message })
+    }).catch(err => console.log(err))
+}
+
+getMessages = async (req, res) => {
+    await Message.find({}, (err, messages) => {
+        if (err) {
+            return res.status(400).json({ success: false, error: err })
+        }
+        if (!messages.length) {
+            return res
+                .status(404)
+                .json({ success: false, error: `Message not found` })
+        }
+        return res.status(200).json({ success: true, data: messages })
+    }).catch(err => console.log(err))
+}
+
+module.exports = {
+    createMessage,
+    updateMessage,
+    deleteMessage,
+    getMessages,
+    getMessageById,
+}

+ 11 - 0
server/db/db.js

@@ -0,0 +1,11 @@
+const mongoose = require('mongoose')
+
+mongoose
+    .connect('mongodb://127.0.0.1:27017/cinema', { useNewUrlParser: true })
+    .catch(e => {
+        console.error('Connection error', e.message)
+    })
+
+const db = mongoose.connection
+
+module.exports = db

+ 11 - 0
server/db/models/message.js

@@ -0,0 +1,11 @@
+const mongoose = require('mongoose')
+const Schema = mongoose.Schema
+
+const Message = new Schema(
+    {
+        message: { type: String, required: true },
+    },
+    { timestamps: true },
+)
+
+module.exports = mongoose.model('messages', Message)

+ 13 - 0
server/routes/messages.js

@@ -0,0 +1,13 @@
+const express = require('express')
+
+const MessageCtrl = require('../controllers/messages')
+
+const router = express.Router()
+
+router.post('/message', MessageCtrl.createMessage)
+router.put('/message/:id', MessageCtrl.updateMessage)
+router.delete('/message/:id', MessageCtrl.deleteMessage)
+router.get('/message/:id', MessageCtrl.getMessageById)
+router.get('/messages', MessageCtrl.getMessages)
+
+module.exports = router

+ 22 - 0
server/server.js

@@ -0,0 +1,22 @@
+const express = require('express')
+const bodyParser = require('body-parser')
+const cors = require('cors')
+const app = express()
+const apiPort = 3000
+const messageRouter = require('./routes/messages')
+
+app.use(bodyParser.urlencoded({ extended: true }))
+app.use(cors())
+app.use(bodyParser.json())
+
+const db = require('./db/db.js')
+db.on('error', console.error.bind(console, 'MongoDB connection error:'))
+
+app.get('/', (req, res) => {
+    res.send('Hello World!')
+})
+
+
+app.use('/api', messageRouter)
+
+app.listen(apiPort, () => console.log(`Server running on port ${apiPort}`))

+ 23 - 19
webpack.config.js

@@ -1,22 +1,26 @@
 const HtmlWebPackPlugin = require("html-webpack-plugin");
-const htmlPlugin = new HtmlWebPackPlugin({
- template: "./src/index.html",
- filename: "./index.html"
-});
+const htmlPlugin = new HtmlWebPackPlugin({template: "./src/index.html", filename: "./index.html"});
 module.exports = {
-mode: 'development',
-  module: {
-    rules: [{
-   test: /\.js$/,
-   exclude: /node_modules/,
-   use: {
-     loader: "babel-loader"
-   }
- },
-  {
-   test: /\.css$/,
-   use: ["style-loader", "css-loader"]
-  }
-]},
- plugins: [htmlPlugin]
+    devServer: {
+        allowedHosts: [
+          '.bwlw.uk',
+          'localhost'
+        ]
+    },
+    mode: 'development',
+    module: {
+        rules: [
+            {
+                test: /\.js$/,
+                exclude: /node_modules/,
+                use: {
+                    loader: "babel-loader"
+                }
+            }, {
+                test: /\.css$/,
+                use: ["style-loader", "css-loader"]
+            }
+        ]
+    },
+    plugins: [htmlPlugin]
 };

Some files were not shown because too many files changed in this diff