Upload files to 'controllers'
This commit is contained in:
parent
a2422d3da8
commit
260b71c90e
|
@ -0,0 +1,107 @@
|
|||
const Episode = require("../models/episodes");
|
||||
const Serie = require("../models/series");
|
||||
|
||||
module.exports = {
|
||||
index: async (req, res, next) => {
|
||||
try {
|
||||
// Get all the episodes
|
||||
const episode = await Episode.find({})
|
||||
.lean()
|
||||
.sort({ createdAt: -1 })
|
||||
.limit(9); // the correct order is -1
|
||||
//res.render("index", { episode: episode });
|
||||
//res.render('index', { name: 'John' });
|
||||
res.json(episode);
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
},
|
||||
|
||||
newEpisode: async (req, res, next) => {
|
||||
try {
|
||||
// 1. Find the actual anime
|
||||
const anime = await Serie.findById(req.body.anime);
|
||||
// 2. Create a new episode
|
||||
const newEpisode = req.body;
|
||||
delete newEpisode.anime;
|
||||
const episode = new Episode(newEpisode);
|
||||
episode.anime = anime;
|
||||
//anime.markModified('episode.anime');
|
||||
await episode.save();
|
||||
// 3. Add newly created episode to the actual anime
|
||||
anime.episodes.push(episode);
|
||||
//episode.markModified('anime.episodes');
|
||||
//anime.markModified('anime.serie');
|
||||
await anime.save();
|
||||
// We're done!
|
||||
res.json(episode);
|
||||
// res.redirect("/");
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
},
|
||||
|
||||
getEpisode: async (req, res, next) => {
|
||||
try {
|
||||
const episode = await Episode.findOne({ slug: req.params.slug });
|
||||
if (!episode) return next();
|
||||
res.json(episode)
|
||||
//res.render("show", { episode: episode });
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
},
|
||||
|
||||
/* getEpisode: async (req, res, next) => {
|
||||
const episode = await Episode.findById(req.params.episodeId);
|
||||
res.render('show', { episode: episode });
|
||||
//res.json(episode);
|
||||
}, */
|
||||
|
||||
replaceEpisode: async (req, res, next) => {
|
||||
try {
|
||||
const { episodeId } = req.params;
|
||||
const newEpisode = req.body;
|
||||
const result = await Episode.findByIdAndUpdate(episodeId, newEpisode);
|
||||
res.json({ success: true });
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
},
|
||||
|
||||
updateEpisode: async (req, res, next) => {
|
||||
try {
|
||||
const { episodeId } = req.params;
|
||||
const newEpisode = req.body;
|
||||
const result = await Episode.findByIdAndUpdate(episodeId, newEpisode);
|
||||
//res.json({ success: true });
|
||||
res.redirect("/");
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
},
|
||||
|
||||
deleteEpisode: async (req, res, next) => {
|
||||
try {
|
||||
const { episodeId } = req.params;
|
||||
// Get a episode
|
||||
const episode = await Episode.findById(episodeId);
|
||||
if (!episode) {
|
||||
return res.status(404).json({ error: "Episode no existe" });
|
||||
}
|
||||
const animeId = episode.anime;
|
||||
// Get a anime
|
||||
const anime = await Serie.findById(animeId);
|
||||
// Remove the episode
|
||||
await episode.remove();
|
||||
// Remove episode from the anime's selling list
|
||||
console.log("anime", anime);
|
||||
anime.episodes.pull(episode);
|
||||
console.log("anime", anime);
|
||||
await anime.save();
|
||||
res.json({ success: true });
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
}
|
||||
};
|
|
@ -0,0 +1,159 @@
|
|||
const Serie = require("../models/series");
|
||||
const Episode = require("../models/episodes");
|
||||
|
||||
module.exports = {
|
||||
/* original code without page (you have to modify the routes leave it alone in /)
|
||||
index: async (req, res, next) => {
|
||||
const serie = await Serie.find({})
|
||||
.sort({ createdAt: -1 })
|
||||
.limit(9);
|
||||
res.render("series", {serie: serie});
|
||||
}, */
|
||||
|
||||
|
||||
index: async (req, res, next) => {
|
||||
const serie = await Serie.find({})
|
||||
.sort({ createdAt: -1 })
|
||||
res.json(serie);
|
||||
},
|
||||
|
||||
/*
|
||||
Uncomment this code to use the page with ejs
|
||||
index: async (req, res, next) => {
|
||||
var perPage = 4;
|
||||
var page = req.params.page || 1;
|
||||
|
||||
try {
|
||||
const serie = await Serie.find({})
|
||||
.sort({ createdAt: -1 })
|
||||
.skip(perPage * page - perPage)
|
||||
.limit(perPage)
|
||||
.lean()
|
||||
.exec(function(err, serie) {
|
||||
Serie.count().exec(function(err, count) {
|
||||
if (err) return next(err);
|
||||
res.render("series", {
|
||||
serie: serie,
|
||||
current: page,
|
||||
pages: Math.ceil(count / perPage)
|
||||
});
|
||||
});
|
||||
});
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
}, */
|
||||
|
||||
newSerie: async (req, res, next) => {
|
||||
try {
|
||||
const newSerie = new Serie(req.body);
|
||||
const serie = await newSerie.save();
|
||||
//res.render("addserie",{serie: serie})
|
||||
res.json(serie);
|
||||
//res.redirect("/users/dashboard");
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
},
|
||||
//.lean(true) high performance .populate('episodes', 'slug -_id', null, {sort: { createdAt: -1 } });
|
||||
getSerie: async (req, res, next) => {
|
||||
try {
|
||||
const serie = await Serie.findOne({ slug: req.params.slug }).populate({
|
||||
path: "episodes",
|
||||
select: "slug -_id",
|
||||
//, match: { x: 1 },
|
||||
options: { sort: { createdAt: -1 }, limit: 9 }
|
||||
});
|
||||
if (!serie) return next();
|
||||
//res.render("anime", { serie: serie });
|
||||
res.json(serie);
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
},
|
||||
/* getSerie: async (req, res, next) => {
|
||||
const { serieId } = req.params;
|
||||
const serie = await Serie.findById(serieId);
|
||||
res.render('anime', { serie: serie });
|
||||
//res.json(serie);
|
||||
}, */
|
||||
|
||||
replaceSerie: async (req, res, next) => {
|
||||
// enforce that req.body must contain all the fields
|
||||
try {
|
||||
const { serieId } = req.params;
|
||||
const newSerie = req.body;
|
||||
const result = await Serie.findByIdAndUpdate(serieId, newSerie);
|
||||
res.json({ success: true });
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
},
|
||||
updateSerie: async (req, res, next) => {
|
||||
try {
|
||||
// req.body may contain any number of fields
|
||||
const { serieId } = req.params;
|
||||
const newSerie = req.body;
|
||||
const result = await Serie.findByIdAndUpdate(serieId, newSerie);
|
||||
//res.json({ success: true });
|
||||
res.redirect("/");
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
},
|
||||
|
||||
//the same as getseries
|
||||
getSerieEpisodes: async (req, res, next) => {
|
||||
try {
|
||||
const { serieId } = req.params;
|
||||
const serie = await Serie.findById(serieId).populate("episodes");
|
||||
//res.render('episodes', { serie : serie });
|
||||
res.json(serie.episodes);
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
},
|
||||
|
||||
newSerieEpisode: async (req, res, next) => {
|
||||
try {
|
||||
const { serieId } = req.params;
|
||||
// Create a new episode
|
||||
const newEpisode = new Episode(req.body);
|
||||
// Get series
|
||||
const serie = await Serie.findById(serieId);
|
||||
// Assing serie as episode's anime
|
||||
newEpisode.anime = serie;
|
||||
// Save the episode
|
||||
await newEpisode.save();
|
||||
// Add episode to the serie's selling array 'episodes'
|
||||
serie.episodes.push(newEpisode);
|
||||
// Save the series
|
||||
await serie.save();
|
||||
res.json(newEpisode);
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
We can interact with mongoose in 3 different ways:
|
||||
1) Callbacks
|
||||
2) Promises
|
||||
3) [X] Async/Await (Promises) - this use for the code
|
||||
*/
|
||||
|
||||
//let is used to isolate a code even if they have the same name are two different variables
|
||||
//const their values after declaring they can be changed as long as the parent object is called
|
||||
|
||||
/* Example promises:
|
||||
index: (req, res, next) => {
|
||||
Serie.find({})
|
||||
.then(series => {
|
||||
res.status(200).json // res.render(series)
|
||||
})
|
||||
.catch(err => {
|
||||
next(err);
|
||||
});
|
||||
},
|
||||
*/
|
Loading…
Reference in New Issue