Illuminatiiiiii
3/14/2019 - 1:18 AM

Updating and Editing Games

const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const fileUpload = require("express-fileupload");

mongoose.connect("mongodb://juicewrld:juice123@ds023078.mlab.com:23078/youtube", {
    useNewUrlParser: true
}, function(error){
    if(error){
        console.log(error);
    }else{
        console.log("Connected to the database");
    }
});

var gameSchema = new mongoose.Schema({
    title: String,
    creator: String,
    width: Number,
    height: Number,
    fileName: String,
    thumbnailFile: String
});

var Game = mongoose.model("Game", gameSchema);
 
//Sets the public folder as the external file folder
app.use(express.static("public"));

app.use(bodyParser.urlencoded({extended: true}));

app.use(fileUpload());
 
//Officially sets the view engine as ejs, therefore setting the default file type for readering to .ejs
app.set("view engine", "ejs");
 
app.get("/", function (req, res) {
    res.render("homepage");
});

app.get("/game/:id", function (req, res) {
    var id = req.params.id;

    Game.findById(id, function(error, foundGame){
        if(error){
            console.log("Couldn't find game with that id");
        }else{
            res.render("game", {
                title: foundGame.title,
                creator: foundGame.creator,
                width: foundGame.width,
                height: foundGame.height,
                fileName: foundGame.fileName
            });
        }
    });
});

app.get("/game/:id/edit", function(req, res){
    var id = req. params.id; //id from route params

    //Use the id to get information on the game and send it to the edit page
    Game.findById(id, function(error, foundGame){
        if(error){
            console.log("Couldn't find game with that id");
        }else{
            res.render("edit", { //The edit a game page
                title: foundGame.title,
                creator: foundGame.creator,
                width: foundGame.width,
                height: foundGame.height,
                fileName: foundGame.fileName,
                id: id
            }); 
        }
    });    
});

app.post("/update/:id", function(req, res){
    var id = req.params.id;

    //Update game in database
    Game.findOneAndUpdate(id, {
        title: req.body.title,
        creator: req.body.creator,
        width: req.body.width,
        height: req.body.height
    }, function(err, updatedGame){
        if(err){
            console.log("Couldn't update game");
            console.log(err);
        }else{
            res.redirect("/list");
        }
    })

});
 
app.get("/list", function (req, res) {
 
    Game.find({}, function(error, games){
        if(error){
            console.log("There was a problem retrieving all of the games from the database.");
            console.log(error);
        }else{
            res.render("list", {
                gamesList: games
            });
        }
    });

});

app.get("/addgame", function(req, res){
    res.render("addgame");
});

app.post("/addgame", function(req, res){
    var data = req.body;

    var gameFile = req.files.gameFile;
    var imageFile = req.files.imageFile;

    gameFile.

    gameFile.mv("public/games/" + gameFile.name, function(error){
        if(error){
            console.log("Couldn't upload the game file");
            console.log(error);
        }else{
            console.log("Game file successfully uploaded.");
        }
    });

    imageFile.mv("public/games/thumbnails/" + imageFile.name, function(error){
        if(error){
            console.log("Couldn't upload the image file");
            console.log(error);
        }else{
            console.log("Image file successfully uploaded.");
        }
    });

    Game.create({
        title: data.title,
        creator: data.creator,
        width: data.width,
        height: data.height,
        fileName: gameFile.name,
        thumbnailFile: imageFile.name
    }, function(error, data){
        if(error){
            console.log("There was a problem adding this game to the database");
        }else{
            console.log("Game added to database");
            console.log(data);
        }

    });
    res.redirect("/list");
});
 
app.listen("3000", function () {
    console.log("Gaming Website has started up! Made by Illuminati Productions.");
});
<% include partials/header %>

<h1>Edit this game: </h1>

<form action="/update/<%= id %>" method="POST" enctype="multipart/form-data">
  <div class="form-group">
    <label>Name</label>
    <input type="text" class="form-control" value="<%= title %>" name="title">
  </div>
  <div class="form-group">
    <label>Creator</label>
    <input type="text" class="form-control" value="<%= creator %>" name="creator">
  </div>
  <div class="form-group">
    <label>Size</label>
    <input type="text" class="form-control" value="<%= width %>" name="width">
    <input type="text" class="form-control" value="<%= height %>" name="height">
  </div>
  <!--
  <div class="form-group">
    <label>File Name</label>
    <input type="text" class="form-control" placeholder="booger.swf" name="fileName">
  </div>
  <div class="form-group">
    <label>Thumbnail Name</label>
    <input type="text" class="form-control" placeholder="thumbnail.png" name="thumbnailFile">
  </div>
  -->
  
  <button type="submit" class="btn btn-primary">Submit</button>
</form>



<% include partials/footer %>