diff --git a/backend/routes/episodeSeenRoutes.js b/backend/routes/episodeSeenRoutes.js index 9ea0ac9..11a7ab5 100644 --- a/backend/routes/episodeSeenRoutes.js +++ b/backend/routes/episodeSeenRoutes.js @@ -115,11 +115,7 @@ episodeSeenRouter.get('/by-entry/:entryId', async (req, res) => { order: [['date_seen', 'DESC']] }); - if (episodeSeens.length > 0) { - res.status(200).json(episodeSeens); - } else { - res.status(404).json({ message: 'No records found for the given entry ID.' }); - } + res.status(200).json(episodeSeens); } catch (error) { res.status(500).json({ error: error.message }); } @@ -128,9 +124,46 @@ episodeSeenRouter.get('/by-entry/:entryId', async (req, res) => { // * PUT - UPDATE EPISODE SEEN episodeSeenRouter.put('/:id', async (req, res) => { try { - const { date_seen, seen_with } = req.body; + const { date_seen, seen_with, episode_id } = req.body; const updated = await EpisodeSeen.update({ date_seen, seen_with }, { where: { id: req.params.id } }); + // * episode update + // Fetch the Entry based on entry_id + const episode = await Episode.findByPk(episode_id); + if (!episode) { + return res.status(404).json({ error: "Episode not found" }); + } + + const episodeSeens = await EpisodeSeen.findAll({ + where: { + episode_id: episode_id + }, + order: [ + ['date_seen', 'DESC'] + ] + }); + + let update = {}; + const dateSeen = new Date(date_seen); + + if (episodeSeens.length == 0) { // ? case of first episodeSeen + update.date_first_seen = date_seen; + update.date_last_seen = date_seen; + } else { // ? case of 2nd+ episodeSeen + if (!episode.date_first_seen || dateSeen < new Date(episode.date_first_seen)) { + update.date_first_seen = date_seen; + } + if (!episode.date_last_seen || dateSeen > new Date(episode.date_last_seen)) { + update.date_last_seen = date_seen; + } + } + + // Update the Episode with new first or last seen dates + if (Object.keys(update).length > 0) { + await episode.update(update); + } + // + end episode update + if (updated[0] > 0) { const updatedEpisodeSeen = await EpisodeSeen.findByPk(req.params.id); res.status(200).json(updatedEpisodeSeen); @@ -145,7 +178,57 @@ episodeSeenRouter.put('/:id', async (req, res) => { // * DELETE - EPISODE SEEN episodeSeenRouter.delete('/:id', async (req, res) => { try { + // getting the episode seen object + const oldEpisodeSeen = await EpisodeSeen.findByPk(req.params.id); + if (!oldEpisodeSeen) + res.status(404).json({ message: 'Episode seen record not found.' }); + + + // * episode update + const episode = await Episode.findByPk(oldEpisodeSeen.episode_id); + if (!episode) { + return res.status(404).json({ message: "Episode not found" }); + } + + // deleting the episode seen const deleted = await EpisodeSeen.destroy({ where: { id: req.params.id } }); + + const episodeSeens = await EpisodeSeen.findAll({ + where: { + episode_id: episode.id + }, + order: [ + ['date_seen', 'DESC'] + ] + }); + + let update = {}; + + if (episodeSeens.length == 0) { // ? case of 0 episodeSeen for the episode + update.date_first_seen = null; + update.date_last_seen = null; + } else { // ? case of 1+ episodeSeen for the episode + for (let i = 0; i < episodeSeens.length; i++) { + if (i == 0) { + update.date_first_seen = episodeSeens[i].date_seen; + update.date_last_seen = episodeSeens[i].date_seen; + } else { + if (episodeSeens[i].date_seen < update.date_first_seen) { + update.date_first_seen = episodeSeens[i].date_seen; + } + if (episodeSeens[i].date_seen > update.date_last_seen) { + update.date_last_seen = episodeSeens[i].date_seen; + } + } + } + } + + // Update the Episode with new first or last seen dates + if (Object.keys(update).length > 0) { + await episode.update(update); + } + // + end episode update + if (deleted) { res.status(204).send(); } else { @@ -153,6 +236,7 @@ episodeSeenRouter.delete('/:id', async (req, res) => { } } catch (error) { res.status(500).json({ error: error.message }); + console.log(error) } }); diff --git a/backend/services/entryService.js b/backend/services/entryService.js index 8f9acdd..37b6883 100644 --- a/backend/services/entryService.js +++ b/backend/services/entryService.js @@ -8,6 +8,8 @@ const { fetchSeasonData } = require('./tmdbService.js'); const Episode = require('../models/Episode.js'); async function saveEntry(data) { + if (data == null) return; + // creating the object var is_tvseries = data.release_date == null ? true : false; diff --git a/frontend/src/app/core/services/episode-seen.service.ts b/frontend/src/app/core/services/episode-seen.service.ts index fffb0e0..3d7bc33 100644 --- a/frontend/src/app/core/services/episode-seen.service.ts +++ b/frontend/src/app/core/services/episode-seen.service.ts @@ -1,6 +1,7 @@ import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { EpisodeSeen } from '../../shared/models/episodeSeen.model'; +import { EpisodeSeenDto } from '../../shared/models/dtos/episode-seen.dto'; @Injectable({ providedIn: 'root', @@ -41,6 +42,17 @@ export class EpisodeSeenService { ); } + updateEpisodeSeen(episodeSeen: EpisodeSeenDto) { + return this.http.put( + `${this.backend_apiUrl}/${this.apiAction}/${episodeSeen.id}`, + { + date_seen: episodeSeen.date_seen, + seen_with: episodeSeen.seen_with, + episode_id: episodeSeen.episodeId, + } + ); + } + removeEpisodeSeen(id: number) { return this.http.delete( `${this.backend_apiUrl}/${this.apiAction}/${id}` diff --git a/frontend/src/app/features/collections/collections-routing.module.ts b/frontend/src/app/features/collections/collections-routing.module.ts index a4656b4..ace2b0e 100644 --- a/frontend/src/app/features/collections/collections-routing.module.ts +++ b/frontend/src/app/features/collections/collections-routing.module.ts @@ -3,6 +3,7 @@ import { Routes, RouterModule } from '@angular/router'; import { UserCollectionsPageComponent } from './user-collections/userCollections-page/userCollections-page.component'; import { UserCollectionInfoPageComponent } from './user-collection-info/userCollection-info-page/userCollection-info-page.component'; +// ! /user-collections const routes: Routes = [ { path: '', diff --git a/frontend/src/app/features/entry/entry-page/entry-page.component.html b/frontend/src/app/features/entry/entry-page/entry-page.component.html index c3c3cd0..c160fb2 100644 --- a/frontend/src/app/features/entry/entry-page/entry-page.component.html +++ b/frontend/src/app/features/entry/entry-page/entry-page.component.html @@ -16,7 +16,8 @@