Major Backend SI Update part 2: added controllers
This commit is contained in:
1 parent
a51e207707
commit
dbb00fdb61
38 files changed
+2129
-1231
No files matched your search
@@ -0,0 +1,102 @@
|
||||
const Episode = require("../models/Episode");
|
||||
const Season = require("../models/Season");
|
||||
|
||||
|
||||
class SeasonController {
|
||||
|
||||
constructor() {
|
||||
if (!SeasonController.instance) {
|
||||
SeasonController.instance = this;
|
||||
}
|
||||
return SeasonController.instance;
|
||||
}
|
||||
|
||||
async createSeason(req, res) {
|
||||
try {
|
||||
const season = await Season.create(req.body);
|
||||
res.status(201).send(season);
|
||||
} catch (error) {
|
||||
res.status(400).send(error.message);
|
||||
}
|
||||
}
|
||||
|
||||
async getSeasons(req, res) {
|
||||
try {
|
||||
const seasons = await Season.findAll();
|
||||
res.send(seasons);
|
||||
} catch (error) {
|
||||
res.status(500).send(error.message);
|
||||
}
|
||||
}
|
||||
|
||||
async getSeaonById(req, res) {
|
||||
try {
|
||||
const season = await Season.findByPk(req.params.id, {
|
||||
include: [{
|
||||
model: Episode,
|
||||
as: 'episodes'
|
||||
}]
|
||||
});
|
||||
|
||||
if (season) {
|
||||
res.send(season);
|
||||
} else {
|
||||
res.status(404).send('Season not found');
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).send(error.message);
|
||||
}
|
||||
}
|
||||
|
||||
async getSeasonEpisodes(req, res) {
|
||||
try {
|
||||
const episodes = await Episode.findAll({
|
||||
where: {
|
||||
season_id: req.params.id
|
||||
}
|
||||
});
|
||||
|
||||
if (episodes) {
|
||||
res.send(episodes);
|
||||
} else {
|
||||
res.status(404).send('Episodes not found');
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).send(error.message);
|
||||
}
|
||||
}
|
||||
|
||||
async updateSeason(req, res) {
|
||||
try {
|
||||
const [updated] = await Season.update(req.body, {
|
||||
where: { id: req.params.id }
|
||||
});
|
||||
if (updated) {
|
||||
const updatedSeason = await Season.findByPk(req.params.id);
|
||||
res.send(updatedSeason);
|
||||
} else {
|
||||
res.status(404).send('Season not found');
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).send(error.message);
|
||||
}
|
||||
}
|
||||
|
||||
async deleteSeason(req, res) {
|
||||
try {
|
||||
const deleted = await Season.destroy({
|
||||
where: { id: req.params.id }
|
||||
});
|
||||
if (deleted) {
|
||||
res.status(204).send('Season deleted');
|
||||
} else {
|
||||
res.status(404).send('Season not found');
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).send(error.message);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = new SeasonController();
|
||||
Reference in new issue
Block a user