File

src/episode/providers/episode.service.ts

Description

Service for managing episodes in the application. Provides methods to create, read, update, and delete episodes.

Index

Methods

Constructor

constructor(episodeRepository: Repository<Episode>, seasonService: SeasonService)

Initializes the EpisodeService with the Episode repository and SeasonService.

Parameters :
Name Type Optional Description
episodeRepository Repository<Episode> No
  • The repository for the Episode entity.
seasonService SeasonService No
  • The service responsible for season operations.

Methods

Public Async createEpisode
createEpisode(dto: CreateEpisodeRequestDto)

Creates a new episode.

Parameters :
Name Type Optional Description
dto CreateEpisodeRequestDto No
  • The data transfer object containing episode details.
Returns : unknown

The created episode.

Public Async deleteEpisode
deleteEpisode(id: number)

Deletes an episode by its unique ID.

Parameters :
Name Type Optional Description
id number No
  • The unique identifier of the episode to be deleted.
Returns : any
Public Async findById
findById(id: number)

Retrieves an episode by its unique ID.

Parameters :
Name Type Optional Description
id number No
  • The unique identifier of the episode.
Returns : unknown

The episode with the specified ID.

Public Async getAllEpisodesWithSeasonId
getAllEpisodesWithSeasonId(id: number)

Retrieves all episodes associated with a specific season ID.

Parameters :
Name Type Optional Description
id number No
  • The unique identifier of the season.
Returns : unknown

A list of episodes for the specified season.

Public Async updateEpisode
updateEpisode(id: number, dto: UpdateEpisodeRequestDto)

Updates an existing episode by its unique ID.

Parameters :
Name Type Optional Description
id number No
  • The unique identifier of the episode to be updated.
dto UpdateEpisodeRequestDto No
  • The data transfer object containing updated episode details.
Returns : unknown

The updated episode.

import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Episode } from '../episode.entity';
import { Repository } from 'typeorm';
import { EPISODE_NOT_FOUND_ERROR } from '../constants/episode.errors.constants';
import { CreateEpisodeRequestDto } from '../dtos/request/create-episode.dto';
import { UpdateEpisodeRequestDto } from '../dtos/request/update-episode.dto';
import { SeasonService } from 'src/season/providers/season.service';
import { Season } from 'src/season/season.entity';

/**
 * Service for managing episodes in the application.
 * Provides methods to create, read, update, and delete episodes.
 */
@Injectable()
export class EpisodeService {
  /**
   * Initializes the EpisodeService with the Episode repository and SeasonService.
   * @param episodeRepository - The repository for the Episode entity.
   * @param seasonService - The service responsible for season operations.
   */
  constructor(
    @InjectRepository(Episode)
    private readonly episodeRepository: Repository<Episode>,
    private readonly seasonService: SeasonService,
  ) {}

  /**
   * Retrieves an episode by its unique ID.
   * @param id - The unique identifier of the episode.
   * @returns The episode with the specified ID.
   * @throws NotFoundException if the episode is not found.
   */
  public async findById(id: number) {
    const episode = await this.episodeRepository.findOneBy({
      id,
    });

    if (!episode) {
      throw new NotFoundException(EPISODE_NOT_FOUND_ERROR);
    }

    return episode;
  }

  /**
   * Retrieves all episodes associated with a specific season ID.
   * @param id - The unique identifier of the season.
   * @returns A list of episodes for the specified season.
   */
  public async getAllEpisodesWithSeasonId(id: number) {
    return this.episodeRepository.find({
      where: { season: { id } },
      order: { episodeNumber: 'ASC' },
      relations: ['videoLinks'],
    });
  }

  /**
   * Deletes an episode by its unique ID.
   * @param id - The unique identifier of the episode to be deleted.
   * @throws NotFoundException if the episode is not found.
   */
  public async deleteEpisode(id: number) {
    const { affected } = await this.episodeRepository.delete(id);
    if (!affected) {
      throw new NotFoundException(EPISODE_NOT_FOUND_ERROR);
    }
  }

  /**
   * Creates a new episode.
   * @param dto - The data transfer object containing episode details.
   * @returns The created episode.
   */
  public async createEpisode(dto: CreateEpisodeRequestDto) {
    const season = await this.seasonService.getSeasonById(dto.seasonId);
    const episode = this.episodeRepository.create({
      ...dto,
      season: { id: season.id },
    });

    return this.episodeRepository.save(episode);
  }

  /**
   * Updates an existing episode by its unique ID.
   * @param id - The unique identifier of the episode to be updated.
   * @param dto - The data transfer object containing updated episode details.
   * @returns The updated episode.
   * @throws NotFoundException if the episode is not found.
   */
  public async updateEpisode(id: number, dto: UpdateEpisodeRequestDto) {
    const episode = await this.episodeRepository.findOne({
      where: { id },
      relations: ['season'],
    });

    if (!episode) {
      throw new NotFoundException(EPISODE_NOT_FOUND_ERROR);
    }

    let season: Season = episode?.season;

    if (dto.seasonId) {
      season = await this.seasonService.getSeasonById(dto.seasonId);
    }

    Object.assign(episode, {
      ...dto,
      season: { id: season?.id },
    });

    return this.episodeRepository.save(episode);
  }
}

results matching ""

    No results matching ""