diff --git a/backend/routes/entry.routes.js b/backend/routes/entry.routes.js index d097ff9..6be0be5 100644 --- a/backend/routes/entry.routes.js +++ b/backend/routes/entry.routes.js @@ -1,15 +1,18 @@ const express = require('express'); +const entryRouter = express.Router(); + const { fetchEntryData } = require('../services/tmdbService'); +const { saveEntry, refrechEntry } = require('../services/entryService'); +const { bulkImportMovies, bulkImportTmdb, bulkImportEpisodes } = require('../services/importerService'); + const Entry = require('../models/Entry'); const EntryId = require('../models/EntryId'); const EntryRating = require('../models/EntryRating'); const Genre = require('../models/Genre'); const EntrySeen = require('../models/EntrySeen'); -const { saveEntry, refrechEntry } = require('../services/entryService'); const Season = require('../models/Season'); -const { bulkImportMovies, bulkImportTmdb, bulkImportEpisodes } = require('../services/importerService'); const Media = require('../models/Media'); -const entryRouter = express.Router(); +const Episode = require('../models/Episode'); // ! /entries // * GET - ALL ENTRIES @@ -85,6 +88,47 @@ entryRouter.get('/:id', async (req, res) => { console.log(error); } }); + +// * GET - Get seasons of an entry +entryRouter.get('/:id/seasons', async (req, res) => { + try { + const { id } = req.params; + + if (id == null) { // ! entry id not provided + res.status(404).json({ message: 'Entry id not provided' }); + } + + const entry = await Entry.findByPk(id); + + if (!entry) { + res.status(404).json({ message: 'Entry not found' }); + } + + const seasons = await Season.findAll({ + where: { + entry_id: id + }, + attributes: ['id', 'season_number'], + include: [ + { model: Episode, as: 'episodes', attributes: ['id', 'episode_number'], } + ], + order: [ + ['season_number', 'ASC'], + [{ model: Episode, as: 'episodes' }, 'episode_number', 'ASC'], + ] + }) + + if (seasons.length == 0) { // ! no content + res.status(404).json({ message: 'No seasons' }); + } + + res.status(200).json(seasons); + } catch (error) { + res.status(500).json({ error: error.message }); + console.log(error); + } +}); + // ! POST // * POST - CREATE ENTRY entryRouter.post('/', async (req, res) => { diff --git a/backend/routes/season.routes.js b/backend/routes/season.routes.js index f52fd77..2a1e8fd 100644 --- a/backend/routes/season.routes.js +++ b/backend/routes/season.routes.js @@ -4,7 +4,7 @@ const Episode = require('../models/Episode'); const seasonRouter = express.Router(); // ! /seasons -// POST /seasons - Create a new season +// * POST - Create a new season seasonRouter.post('/', async (req, res) => { try { const season = await Season.create(req.body); @@ -14,7 +14,7 @@ seasonRouter.post('/', async (req, res) => { } }); -// GET /seasons - Retrieve all seasons +// * GET - Retrieve all seasons seasonRouter.get('/', async (req, res) => { try { const seasons = await Season.findAll(); @@ -24,7 +24,7 @@ seasonRouter.get('/', async (req, res) => { } }); -// GET /seasons/:id - Retrieve a specific season by id +// * GET - Retrieve a specific season by id seasonRouter.get('/:id', async (req, res) => { try { const season = await Season.findByPk(req.params.id, { @@ -44,6 +44,7 @@ seasonRouter.get('/:id', async (req, res) => { } }); +// * GET - Retrieve episodes of a season seasonRouter.get('/:id/episodes', async (req, res) => { try { const episodes = await Episode.findAll({ @@ -62,7 +63,7 @@ seasonRouter.get('/:id/episodes', async (req, res) => { } }); -// PUT /seasons/:id - Update a season +// * PUT - Update a season seasonRouter.put('/:id', async (req, res) => { try { const [updated] = await Season.update(req.body, { @@ -79,7 +80,7 @@ seasonRouter.put('/:id', async (req, res) => { } }); -// DELETE /seasons/:id - Delete a season +// * DELETE - Delete a season seasonRouter.delete('/:id', async (req, res) => { try { const deleted = await Season.destroy({ diff --git a/frontend/src/app/core/services/season.service.ts b/frontend/src/app/core/services/season.service.ts index 1a234f5..a6ed3e0 100644 --- a/frontend/src/app/core/services/season.service.ts +++ b/frontend/src/app/core/services/season.service.ts @@ -31,4 +31,10 @@ export class SeasonService { `${this.backend_apiUrl}/${this.apiAction}/${id}/episodes` ); } + + getSeasonsWithEpisodesByEntryId(entryId: number) { + return this.http.get( + `${this.backend_apiUrl}/entries/${entryId}/seasons` + ); + } } 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 c1631f3..9afe859 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 @@ -204,7 +204,19 @@

Collections

-->
-

Watch History

+
+

Watch History

+ +
+
diff --git a/frontend/src/app/features/entry/entry-page/entry-page.component.ts b/frontend/src/app/features/entry/entry-page/entry-page.component.ts index d86bbf9..27261e2 100644 --- a/frontend/src/app/features/entry/entry-page/entry-page.component.ts +++ b/frontend/src/app/features/entry/entry-page/entry-page.component.ts @@ -19,6 +19,7 @@ import { SeasonService } from '../../../core/services/season.service'; import { LibraryService } from '../../../core/services/library.service'; import { ToastrService } from 'ngx-toastr'; import { EntrySeen } from '../../../shared/models/entrySeen.model'; +import { EpisodeSeenModalComponent } from '../../../shared/components/modals/episode-seen-modal/episode-seen-modal.component'; @Component({ selector: 'app-entry-page', @@ -397,6 +398,45 @@ export class EntryPageComponent implements OnInit { this.seenHistoryToShow = temp.slice(startIndex, endIndex); } + addEpisodeSeen() { + if (!this.entry) return; + + const dialogRef = this.dialog.open(EpisodeSeenModalComponent, { + width: '360px', + data: { + entryId: this.entry.id, + }, + }); + + dialogRef.afterClosed().subscribe((result) => { + // ? if an episode-seen was created then refresh the watch history table + if (result && result.created) { + // ? call this function to fetch the new episode-seen history + this.getEpisodesSeenHistory(); + + // ? Updating the episodes seen progress bar in the season card + // ? by fetching a new updated episodes list + this.entry.seasons.forEach(async (season) => { + if (season.season_number == result.season_number) { + this.seasonService.getSeasonEpisodes(season.id).subscribe({ + next: (episodes) => { + // console.log( + // `Loaded Season ${season.season_number} Episodes (${episodes.length})` + // ); + season.episodes = episodes; + }, + error: (error) => { + console.log(`Couldn't fetch season episodes data:`, error); + // TODO show notification + season.episodes = []; + }, + }); + } + }); + } + }); + } + editEpisodeSeen(episodeSeen: EpisodeSeenDto) { if (episodeSeen == null) return; @@ -456,11 +496,11 @@ export class EntryPageComponent implements OnInit { this.episodeSeenService.removeEpisodeSeen(episodeSeen.id).subscribe({ next: () => { // console.log(`Episode seen deleted.`); - // call this function to fetch the new episode-seen history + // ? call this function to fetch the new episode-seen history this.getEpisodesSeenHistory(); - // trying to update the episodes seen progress bar in the season card - // by fetching a new updated episodes list + // ? Updating the episodes seen progress bar in the season card + // ? by fetching a new updated episodes list this.entry.seasons.forEach(async (season) => { if (season.season_number == episodeSeen.season_number) { this.seasonService.getSeasonEpisodes(season.id).subscribe({ diff --git a/frontend/src/app/shared/components/modals/episode-seen-modal/episode-seen-modal.component.css b/frontend/src/app/shared/components/modals/episode-seen-modal/episode-seen-modal.component.css new file mode 100644 index 0000000..e69de29 diff --git a/frontend/src/app/shared/components/modals/episode-seen-modal/episode-seen-modal.component.html b/frontend/src/app/shared/components/modals/episode-seen-modal/episode-seen-modal.component.html new file mode 100644 index 0000000..dc57a97 --- /dev/null +++ b/frontend/src/app/shared/components/modals/episode-seen-modal/episode-seen-modal.component.html @@ -0,0 +1,43 @@ +
+ +
+
+
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+
+ + +
+
+ +
+
\ No newline at end of file diff --git a/frontend/src/app/shared/components/modals/episode-seen-modal/episode-seen-modal.component.spec.ts b/frontend/src/app/shared/components/modals/episode-seen-modal/episode-seen-modal.component.spec.ts new file mode 100644 index 0000000..d990ed3 --- /dev/null +++ b/frontend/src/app/shared/components/modals/episode-seen-modal/episode-seen-modal.component.spec.ts @@ -0,0 +1,23 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { EpisodeSeenModalComponent } from './episode-seen-modal.component'; + +describe('EpisodeSeenModalComponent', () => { + let component: EpisodeSeenModalComponent; + let fixture: ComponentFixture; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + imports: [EpisodeSeenModalComponent] + }) + .compileComponents(); + + fixture = TestBed.createComponent(EpisodeSeenModalComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/frontend/src/app/shared/components/modals/episode-seen-modal/episode-seen-modal.component.ts b/frontend/src/app/shared/components/modals/episode-seen-modal/episode-seen-modal.component.ts new file mode 100644 index 0000000..c4385d2 --- /dev/null +++ b/frontend/src/app/shared/components/modals/episode-seen-modal/episode-seen-modal.component.ts @@ -0,0 +1,133 @@ +import { Component, Inject } from '@angular/core'; +import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog'; + +import { + trigger, + state, + style, + transition, + animate, +} from '@angular/animations'; +import { formatDate } from '@angular/common'; +import flatpickr from 'flatpickr'; +import { Season } from '../../../models/season.model'; +import { SeasonService } from '../../../../core/services/season.service'; +import { Episode } from '../../../models/episode.models'; +import { EpisodeSeenService } from '../../../../core/services/episode-seen.service'; + +@Component({ + selector: 'app-episode-seen-modal', + templateUrl: './episode-seen-modal.component.html', + styleUrl: './episode-seen-modal.component.css', + animations: [ + trigger('fadeInOut', [ + state('in', style({ opacity: 1 })), + transition(':enter', [style({ opacity: 0 }), animate(600)]), + transition(':leave', [animate(600, style({ opacity: 0 }))]), + ]), + trigger('scaleInOut', [ + transition(':enter', [ + state('in', style({ opacity: 1 })), + style({ transform: 'scale(0)' }), + animate('200ms ease-in-out', style({ transform: 'scale(1)' })), + ]), + transition(':leave', [ + animate('200ms ease-in-out', style({ transform: 'scale(0)' })), + ]), + ]), + ], +}) +export class EpisodeSeenModalComponent { + public entryId: number = 0; + public watchDate: string = ''; + public watchedWith: string = ''; + public seasons: Season[] = []; + + public selectedSeason: Season | null = {} as Season; + public selectedEpisode: Episode | null = {} as Episode; + + constructor( + public dialogRef: MatDialogRef, + @Inject(MAT_DIALOG_DATA) public data: any, + private seasonsService: SeasonService, + private episodeSeenService: EpisodeSeenService + ) {} + + ngOnInit(): void { + if (!this.data.entryId) return; + this.entryId = this.data.entryId; + + this.seasonsService + .getSeasonsWithEpisodesByEntryId(this.data.entryId) + .subscribe({ + next: (seasons) => { + this.seasons = seasons; + }, + error: (error) => { + console.log(`Error gettings seasons: `, error); + }, + }); + + flatpickr('#myDatepicker', { + inline: true, + defaultDate: this.watchDate, + altInput: true, + allowInput: false, + altFormat: 'F j, Y', + dateFormat: 'Y-m-d', + }); + } + + onSeasonChange(event: any): void { + this.selectedEpisode = null; + } + + onConfirm(): void { + if (!this.selectedSeason) { + return; + } + + if (!this.selectedEpisode) { + return; + } + + if (!this.watchDate) { + return; + } + + const date_seen = new Date(this.watchDate); + const seen_with = this.watchedWith.split('\n').join(', '); + + this.episodeSeenService + .createEpisodeSeen( + this.entryId, + this.selectedEpisode?.id, + date_seen, + seen_with + ) + .subscribe({ + next: (episodeSeen) => { + if (episodeSeen) { + return this.dialogRef.close({ + created: true, + // ? returning the season number so it can be used to refrech the- + // ? season card progress bar in the entry page + season_number: this.selectedSeason?.season_number, + }); + } + }, + error: (error) => { + console.log(`Episode seen create error: `, error); + // TODO show notification + }, + }); + + this.dialogRef.close({ + created: false, + }); + } + + onClose(): void { + this.dialogRef.close(); + } +} diff --git a/frontend/src/app/shared/components/modals/seen-modal/seen-modal.component.html b/frontend/src/app/shared/components/modals/seen-modal/seen-modal.component.html index b13cf3c..a62b8d3 100644 --- a/frontend/src/app/shared/components/modals/seen-modal/seen-modal.component.html +++ b/frontend/src/app/shared/components/modals/seen-modal/seen-modal.component.html @@ -2,7 +2,6 @@
diff --git a/frontend/src/app/shared/shared.module.ts b/frontend/src/app/shared/shared.module.ts index 5ffdfc6..6ea2d31 100644 --- a/frontend/src/app/shared/shared.module.ts +++ b/frontend/src/app/shared/shared.module.ts @@ -1,7 +1,6 @@ import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; - // * modals import { SeenModalComponent } from './components/modals/seen-modal/seen-modal.component'; import { ConfirmModalComponent } from './components/modals/confirm-modal/confirm-modal.component'; @@ -14,6 +13,7 @@ import { RatingModule } from 'ngx-bootstrap/rating'; import { FormsModule, ReactiveFormsModule } from '@angular/forms'; import { CollectionEditModalComponent } from '../features/collections/user-collection-info/collection-edit-modal/collection-edit-modal.component'; import { FloatFormatPipe } from './pipes/float-format/float-format.pipe'; +import { EpisodeSeenModalComponent } from './components/modals/episode-seen-modal/episode-seen-modal.component'; @NgModule({ imports: [ @@ -33,7 +33,7 @@ import { FloatFormatPipe } from './pipes/float-format/float-format.pipe'; SeenModalComponent, CollectionEditModalComponent, ConfirmModalComponent, - + EpisodeSeenModalComponent, ], exports: [ // * pipes diff --git a/frontend/src/assets/sass/main.css b/frontend/src/assets/sass/main.css index 5bad7a5..9fbb737 100644 --- a/frontend/src/assets/sass/main.css +++ b/frontend/src/assets/sass/main.css @@ -62,7 +62,7 @@ input::placeholder { /*header*/ .top-header .navbar { background-color: #ffffff; - height: 70px; + height: 50px; position: fixed; left: 70px; right: 0; @@ -165,7 +165,7 @@ input::placeholder { } .top-header .navbar .nav-item { - height: 70px; + height: 50px; display: flex; align-items: center; } @@ -531,7 +531,7 @@ footer { height: calc(100vh - 70px); /* overflow: hidden; */ - margin-top: 70px; + margin-top: 50px; margin-left: 70px; } diff --git a/frontend/src/assets/sass/main.scss b/frontend/src/assets/sass/main.scss index af379be..84fb8b6 100644 --- a/frontend/src/assets/sass/main.scss +++ b/frontend/src/assets/sass/main.scss @@ -114,7 +114,7 @@ $dropdownAnimation: 0.6s cubic-bezier(0.25, 0.8, 0.25, 1) 0s normal forwards 1 .top-header { .navbar { background-color: $headerbg; - height: 70px; + height: 50px; position: fixed; // border-bottom: 1px solid var(--bs-border-color); left: 260px; @@ -220,7 +220,7 @@ $dropdownAnimation: 0.6s cubic-bezier(0.25, 0.8, 0.25, 1) 0s normal forwards 1 } .nav-item { - height: 70px; + height: 50px; display: flex; align-items: center; @@ -437,7 +437,7 @@ $dropdownAnimation: 0.6s cubic-bezier(0.25, 0.8, 0.25, 1) 0s normal forwards 1 align-items: center; justify-content: start; width: 260px; - height: 70px; + height: 50px; padding: 0 1rem; gap: 0.5rem; // border-bottom: 1px solid var(--bs-border-color); @@ -751,7 +751,7 @@ $dropdownAnimation: 0.6s cubic-bezier(0.25, 0.8, 0.25, 1) 0s normal forwards 1 /* main content */ .main-wrapper { - margin-top: 70px; + margin-top: 50px; padding-bottom: 20px; margin-left: 260px; transition: ease-out 0.3s;