Major Backend SI Update part 2: added controllers

This commit is contained in:
X4Lo committed 2025-02-15 22:42:57 +01:00
1 parent a51e207707
commit dbb00fdb61
38 files changed
+2129 -1231

No files matched your search

+76 -3
View File
@@ -15,12 +15,15 @@ const NotFoundError = require("../errors/NotFoundError");
const mediaOptimizerService = require("./mediaOptimizerService.js");
const mediaManager = require("./mediaManager.js");
const { fetchSeasonData } = require("./tmdbService.js");
const Network = require("../models/Network.js");
const networkService = require("./networkService.js");
const _mediaManager = mediaManager;
const _mediaOptimizerService = mediaOptimizerService;
const _networkService = networkService;
class EntryService {
constructor() {
if (!EntryService.instance) {
EntryService.instance = this;
@@ -35,6 +38,7 @@ class EntryService {
{ model: EntryRating, as: 'entryRating' },
{ model: Media, as: 'medias' },
{ model: Season, as: 'seasons' },
{ model: Network, as: 'networks' },
{
model: Genre,
as: 'genres',
@@ -67,6 +71,7 @@ class EntryService {
{ model: EntrySeen, as: 'entrySeen' },
{ model: Season, as: 'seasons' },
{ model: Media, as: 'medias' },
{ model: Network, as: 'networks' },
{
model: Collection, as: 'collections',
include: [
@@ -133,7 +138,7 @@ class EntryService {
let savedEntry = await movie.save();
// ? ids
const ids = await EntryId.create({
await EntryId.create({
entry_id: savedEntry.id,
tmdb_id: data.id,
imdb_id: data.imdb_id,
@@ -147,8 +152,23 @@ class EntryService {
})
});
// ? networks
if (data.networks) {
for (const networkData of data.networks) {
// Create or update the network
const [oldNetwork, newNetwork] = await _networkService.createOrUpdateNetwork(networkData);
let network = oldNetwork || newNetwork;
if (!network) {
throw new NotFoundError("Network not found");
}
// Associate the new network with the entry
await savedEntry.addNetwork(network);
}
}
// ? ratings
const ratings = await EntryRating.create({
await EntryRating.create({
entry_id: savedEntry.id,
user_rating: 0,
tmdb_rating: data.vote_average,
@@ -260,6 +280,59 @@ class EntryService {
return updatedEntry;
}
async updateEntryRatings(id, entryRatingsData) {
if (!id) {
throw new BadRequestError("Entry id not provided");
}
let entry = await Entry.findByPk(id, {
include: [
{ model: EntryRating, as: 'entryRating' },
]
});
if (!entry) {
throw new NotFoundError("Entry with the provided id was not found")
}
if (entry.entryRating.id != entryRatingsData.id) {
throw new BadRequestError("EntryRatings id does not match");
}
entry.entryRating.user_rating = entryRatingsData.user_rating ?? entry.entryRating.user_rating;
entry.entryRating.tmdb_rating = entryRatingsData.tmdb_rating ?? entry.entryRating.tmdb_rating;
entry.entryRating.imdb_rating = entryRatingsData.imdb_rating ?? entry.entryRating.imdb_rating;
entry.entryRating.rotten_tomatoes_rating = entryRatingsData.rotten_tomatoes_rating ?? entry.entryRating.rotten_tomatoes_rating;
return await entry.entryRating.save();
}
async updateEntryIds(id, entryIdsData) {
if (!id) {
throw new BadRequestError("Entry id not provided");
}
let entry = await Entry.findByPk(id, {
include: [
{ model: EntryId, as: 'entryId' },
]
});
if (!entry) {
throw new NotFoundError("Entry with the provided id was not found")
}
if (entry.entryRating.id != entryRatingsData.id) {
throw new BadRequestError("EntryIds id does not match");
}
entry.entryId.tmdb_id = entryIdsData.tmdb_id ?? entry.entryId.tmdb_id;
entry.entryId.imdb_id = entryIdsData.imdb_id ?? entry.entryId.imdb_id;
entry.entryId.tvdb_id = entryIdsData.tvdb_id ?? entry.entryId.tvdb_id;
await entry.entryId.save();
}
async deleteEntryById(id) {
if (!id) {
throw new BadRequestError("Entry id not provided");