@georgiancollege/express-mvc
Version:
Express MVC @ Georgian College
221 lines (204 loc) • 5.28 kB
JavaScript
const Movie = require('../Models/movie');
/**
* This function sanitizes the array of strings
*
* @param {string[]} unsanitizedArray
* @returns {string[]}
*/
function SanitizeArray(unsanitizedArray)
{
let sanitizedArray = [];
for (const unsanitizedString of unsanitizedArray)
{
sanitizedArray.push(unsanitizedString.trim());
}
return sanitizedArray;
}
/* API Functions */
/**
* This function displays the Movie List
*
* @export
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
*/
function DisplayMovieList(req, res, next)
{
// Find all Movies in the Movie collection
Movie.find({})
.then(function(data)
{
res.status(200).json({success: true, msg:"Movie List Retrieved and Displayed", data: data});
})
.catch(function(err)
{
console.error(err);
});
}
/**
* This function displays a single movie by the provided ID
*
* @export
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
*/
function DisplayMovieByID(req, res, next)
{
try
{
// Get the id = require(the url
let id = req.params.id;
// Find the Movie by id
Movie.findById({_id: id})
.then(function(data)
{
res.status(200).json({success: true, msg: "Move Retrieved by ID", data: data})
})
.catch(function(err)
{
console.error(err);
});
}
catch
{
res.json({success: false, msg:"No Data to Display"});
}
}
/**
* This function adds a new movie to the database
*
* @export
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
*/
function AddMovie(req, res, next)
{
try
{
// Sanitize the array
let genres = SanitizeArray((req.body.genres).split(","));
let directors = SanitizeArray((req.body.directors).split(","));
let writers = SanitizeArray((req.body.writers).split(","));
let actors = SanitizeArray((req.body.actors).split(","));
// Instantiate a new Movie
let movie = new Movie({
movieID: req.body.movieID,
title: req.body.title,
studio: req.body.studio,
genres: genres,
directors: directors,
writers: writers,
actors: actors,
length: req.body.length,
year: req.body.year,
shortDescription: req.body.shortDescription,
mpaRating: req.body.mpaRating,
criticsRating: req.body.criticsRating
});
// Create a new movie and pass it to the db
Movie.create(movie)
.then(function()
{
res.status(200).json({success: true, msg: "Movie Added Successfully", data: movie});
})
.catch(function(err)
{
console.error(err);
});
}
catch
{
res.json({success: false, msg:"No Data to Add"});
}
}
/**
* This function removes a movie = require(the database by the provided ID
*
* @export
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
*/
function UpdateMovie(req, res, next)
{
try
{
// Get the id = require(the url
let id = req.params.id;
// Sanitize the array
let genres = SanitizeArray((req.body.genres).split(","));
let directors = SanitizeArray((req.body.directors).split(","));
let writers = SanitizeArray((req.body.writers).split(","));
let actors = SanitizeArray((req.body.actors).split(","));
// Instantiate a new Movie Object
let movieToUpdate = new Movie({
_id: id,
movieID: req.body.movieID,
title: req.body.title,
studio: req.body.studio,
genres: genres,
directors: directors,
writers: writers,
actors: actors,
length: req.body.length,
year: req.body.year,
shortDescription: req.body.shortDescription,
mpaRating: req.body.mpaRating,
criticsRating: req.body.criticsRating
});
// Find the Movie by id and then update
Movie.updateOne({_id: id}, movieToUpdate)
.then(function()
{
res.status(200).json({success: true, msg: "Movie Updated Successfully", data: movieToUpdate});
})
.catch(function(err)
{
console.error(err);
});
}
catch
{
res.json({success: false, msg:"No Data to Update"});
}
}
/**
* This function removes a movie = require(the database by the provided ID
*
* @export
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
*/
function DeleteMovie(req, res, next)
{
try
{
// Get the id = require(the url
let id = req.params.id;
// Find the Movie by id and then delete
Movie.deleteOne({_id: id})
.then(function()
{
res.json(id);
})
.catch(function(err)
{
console.error(err);
});
}
catch
{
res.json({success: false, msg:"No Data to Delete"});
}
}
module.exports = {
DisplayMovieList,
DisplayMovieByID,
AddMovie,
UpdateMovie,
DeleteMovie
}