Backend: Implement async error handling wrapper and refactor entry and media controllers to use it
This commit is contained in:
1 parent
ad58164df4
commit
4a1d2bc614
8 files changed
+92
-33
No files matched your search
@@ -2,6 +2,7 @@ const EntryRating = require('../models/EntryRating');
|
||||
const collectionManager = require('../services/collectionManager');
|
||||
const entryManager = require('../services/entryManager');
|
||||
const entryService = require("../services/entryService");
|
||||
const asyncWrapper = require('../utils/asyncWrapper');
|
||||
|
||||
const _collectionManager = collectionManager;
|
||||
const _entryManager = entryManager;
|
||||
@@ -15,78 +16,77 @@ class EntryController {
|
||||
}
|
||||
return EntryController.instance;
|
||||
}
|
||||
|
||||
// * GET ENTRY BY ID
|
||||
async getAllEntries(req, res) {
|
||||
getAllEntries = asyncWrapper(async (req, res) => {
|
||||
const entries = await _entryService.getAllEntries();
|
||||
|
||||
res.status(200).json(entries);
|
||||
}
|
||||
});
|
||||
|
||||
// * GET ENTRY BY ID
|
||||
async getEntryById(req, res) {
|
||||
getEntryById = asyncWrapper(async (req, res) => {
|
||||
const { id } = req.params;
|
||||
|
||||
let entry = await _entryManager.getUpdatedEntry(id);
|
||||
|
||||
res.status(200).json(entry);
|
||||
}
|
||||
});
|
||||
|
||||
// * POST - FETCH AND CREATE ENTRY USING TMDB ID
|
||||
async fetchAndSaveNewEntry(req, res) {
|
||||
fetchAndSaveNewEntry = asyncWrapper(async (req, res) => {
|
||||
const { tmdb_id, is_tvserie } = req.body;
|
||||
|
||||
const createdEntry = await _entryManager.fetchAndSaveNewEntry(tmdb_id, is_tvserie);
|
||||
|
||||
res.status(201).json(createdEntry);
|
||||
}
|
||||
});
|
||||
// * PUT - ENTRY UPDATE
|
||||
async updateEntry(req, res) {
|
||||
updateEntry = asyncWrapper(async (req, res) => {
|
||||
const { id } = req.params;
|
||||
const { entryData } = req.body;
|
||||
|
||||
const updatedEntry = await _entryService.updateEntryById(id, entryData, true);
|
||||
|
||||
res.status(201).json(updatedEntry);
|
||||
}
|
||||
});
|
||||
|
||||
// * PUT - ENTRY Ratings UPDATE
|
||||
async updateEntryRatings(req, res) {
|
||||
updateEntryRatings = asyncWrapper(async (req, res) => {
|
||||
const { id } = req.params;
|
||||
const { entryRatings } = req.body;
|
||||
|
||||
const updatedEntryRatings = await _entryService.updateEntryRatings(id, entryRatings);
|
||||
|
||||
res.status(200).json(updatedEntryRatings);
|
||||
}
|
||||
});
|
||||
|
||||
// * PUT - ENTRY Ratings UPDATE
|
||||
async updateEntryIds(req, res) {
|
||||
updateEntryIds = asyncWrapper(async (req, res) => {
|
||||
const { id } = req.params;
|
||||
const { entryIds } = req.body;
|
||||
|
||||
const updatedEntryIds = await _entryService.updateEntryRatings(id, entryIds);
|
||||
|
||||
res.status(200).json(updatedEntryIds);
|
||||
}
|
||||
});
|
||||
|
||||
// * DELETE - ENTRY BY ID
|
||||
async deleteEntry(req, res) {
|
||||
deleteEntry = asyncWrapper(async (req, res) => {
|
||||
const { id } = req.params;
|
||||
|
||||
await _entryService.deleteEntryById(id);
|
||||
res.status(200).json({ message: `Entry with ID ${id} deleted.` });
|
||||
}
|
||||
});
|
||||
|
||||
// * GET - SYNC ALL ENTIRES DATA
|
||||
async syncSeries(req, res, next) {
|
||||
syncSeries = asyncWrapper(async (req, res, next) => {
|
||||
const entries = await _entryManager.SyncSeriesData();
|
||||
|
||||
res.status(201).json(entries);
|
||||
}
|
||||
});
|
||||
|
||||
// * GET - Get seasons of an entry
|
||||
async getEntrySeasons(req, res) {
|
||||
getEntrySeasons = asyncWrapper(async (req, res) => {
|
||||
const { id } = req.params;
|
||||
|
||||
const seasons = await _entryService.getEntrySeasons(id)
|
||||
@@ -96,18 +96,18 @@ class EntryController {
|
||||
}
|
||||
|
||||
res.status(200).json(seasons);
|
||||
}
|
||||
});
|
||||
|
||||
// * PUT - REQUEST ENTRY REFRECH
|
||||
async refrechEntry(req, res, next) {
|
||||
refrechEntry = asyncWrapper(async (req, res, next) => {
|
||||
const { id } = req.params;
|
||||
|
||||
const entry = await _entryManager.refrechEntryById(id)
|
||||
res.status(201).json(entry);
|
||||
}
|
||||
});
|
||||
|
||||
// * PUT - REQUEST COLLECTION REFRECH
|
||||
async fetchEntryCollection(req, res, next) {
|
||||
fetchEntryCollection = asyncWrapper(async (req, res, next) => {
|
||||
const { id } = req.params;
|
||||
|
||||
const collection = await _collectionManager.fetchCollectionByEntryId(id);
|
||||
@@ -117,7 +117,7 @@ class EntryController {
|
||||
} else {
|
||||
res.status(204);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = new EntryController();
|
||||
@@ -8,6 +8,7 @@ const Collection = require('../models/Collection');
|
||||
const configService = require('../services/configService.js');
|
||||
const { logToFile } = require('../services/logService');
|
||||
const mediaOptimizerService = require('../services/mediaOptimizerService.js');
|
||||
const asyncWrapper = require('../utils/asyncWrapper');
|
||||
|
||||
const _mediaOptimizerService = mediaOptimizerService;
|
||||
|
||||
@@ -19,8 +20,7 @@ class MediaController {
|
||||
}
|
||||
return MediaController.instance;
|
||||
}
|
||||
|
||||
async getMedias(req, res) {
|
||||
getMedias = asyncWrapper(async (req, res) => {
|
||||
|
||||
const allCustomMedia = await Media.findAll({
|
||||
where: {
|
||||
@@ -41,7 +41,7 @@ class MediaController {
|
||||
});
|
||||
|
||||
res.status(200).json(allCustomMedia);
|
||||
}
|
||||
});
|
||||
|
||||
getMedia(req, res, next) {
|
||||
const { category, type, size, fileName } = req.params;
|
||||
|
||||
Reference in new issue
Block a user