From 586643496be7524aa3c46ff03372248772cc5101 Mon Sep 17 00:00:00 2001 From: Rob Colbert Date: Mon, 3 Feb 2025 00:14:33 -0500 Subject: [PATCH] created --- src/app/services/broadcast-show.ts | 124 +++++++++++++++++++++++++++++ src/app/services/episode.ts | 123 ++++++++++++++++++++++++++++ 2 files changed, 247 insertions(+) create mode 100644 src/app/services/broadcast-show.ts create mode 100644 src/app/services/episode.ts diff --git a/src/app/services/broadcast-show.ts b/src/app/services/broadcast-show.ts new file mode 100644 index 0000000..a1626de --- /dev/null +++ b/src/app/services/broadcast-show.ts @@ -0,0 +1,124 @@ +// app/services/broadcast-show.ts +// Copyright (C) 2025 DTP Technologies, LLC +// All Rights Reserved + +import mongoose, { Types } from "mongoose"; + +import BroadcastShow, { + BroadcastShowStatus, + IBroadcastShow, +} from "../models/broadcast-show.js"; + +import { + DtpPlatform, + DtpService, + DtpServiceUpdate, + WebError, + WebPaginationParameters, +} from "../../lib/dtplib.js"; +import TextService from "./text.js"; + +export interface BroadcastShowDefinition { + title: string; + description: string; +} + +export interface BroadcastShowLibrary { + shows: Array; + totalShowCount: number; +} + +export class BroadcastShowService extends DtpService { + populateBroadcastShow: Array; + + static get name() { + return "BroadcastShowService"; + } + static get slug() { + return "broadcastShow"; + } + + constructor(platform: DtpPlatform) { + super(platform, BroadcastShowService); + this.populateBroadcastShow = [ + { + path: "recentEpisodes", + }, + ]; + } + + async create(definition: BroadcastShowDefinition): Promise { + const textService = this.getService("text"); + + const show = new BroadcastShow(); + show.status = BroadcastShowStatus.Offline; + show.title = textService.filter(definition.title); + show.description = textService.filter(definition.description); + await show.save(); + + return show.toObject(); + } + + async update( + show: IBroadcastShow | Types.ObjectId, + definition: BroadcastShowDefinition + ): Promise { + const textService = this.getService("text"); + + const update: DtpServiceUpdate = {}; + update.$set = {}; + + update.$set.title = textService.filter(definition.title); + update.$set.description = textService.filter(definition.description); + + const newShow = await BroadcastShow.findByIdAndUpdate(show._id, update, { + new: true, + populate: this.populateBroadcastShow, + }).lean(); + + if (!newShow) { + throw new WebError(404, "show does not exist"); + } + + return newShow; + } + + async getById(showId: Types.ObjectId): Promise { + const show = await BroadcastShow.findById(showId) + .populate(this.populateBroadcastShow) + .lean(); + return show; + } + + async getLive( + pagination: WebPaginationParameters + ): Promise { + const search = { status: BroadcastShowStatus.Live }; + + const shows = await BroadcastShow.find(search) + .sort({ created: -1 }) + .skip(pagination.skip) + .limit(pagination.cpp) + .populate(this.populateBroadcastShow) + .lean(); + + const totalShowCount = await BroadcastShow.countDocuments(search); + return { shows, totalShowCount }; + } + + async getAll( + pagination: WebPaginationParameters + ): Promise { + const shows = await BroadcastShow.find() + .sort({ created: -1 }) + .skip(pagination.skip) + .limit(pagination.cpp) + .populate(this.populateBroadcastShow) + .lean(); + + const totalShowCount = await BroadcastShow.estimatedDocumentCount(); + return { shows, totalShowCount }; + } +} + +export default BroadcastShowService; diff --git a/src/app/services/episode.ts b/src/app/services/episode.ts new file mode 100644 index 0000000..d54f25e --- /dev/null +++ b/src/app/services/episode.ts @@ -0,0 +1,123 @@ +// app/services/episode.ts +// Copyright (C) 2025 DTP Technologies, LLC +// All Rights Reserved + +import mongoose, { Types } from "mongoose"; + +import Episode, { EpisodeStatus, IEpisode } from "../models/episode.js"; + +import { + DtpPlatform, + DtpService, + DtpServiceUpdate, + WebError, + WebPaginationParameters, +} from "../../lib/dtplib.js"; + +import TextService from "./text.js"; + +export interface EpisodeDefinition { + title: string; + description: string; +} + +export interface EpisodeLibrary { + episodes: Array; + totalEpisodeCount: number; +} + +export class EpisodeService extends DtpService { + populateEpisode: Array; + + static get name() { + return "EpisodeService"; + } + static get slug() { + return "episode"; + } + + constructor(platform: DtpPlatform) { + super(platform, EpisodeService); + this.populateEpisode = [ + { + path: "show", + }, + { + path: "video", + }, + { + path: "feedItems", + }, + ]; + } + + async create(definition: EpisodeDefinition): Promise { + const textService = this.getService("text"); + const NOW = new Date(); + + const episode = new Episode(); + episode.created = NOW; + episode.title = textService.filter(definition.title); + episode.description = textService.filter(definition.description); + await episode.save(); + + return episode.toObject(); + } + + async update( + episode: IEpisode | Types.ObjectId, + definition: EpisodeDefinition + ): Promise { + const textService = this.getService("text"); + + const update: DtpServiceUpdate = {}; + update.$set = {}; + + update.$set.title = textService.filter(definition.title); + update.$set.description = textService.filter(definition.description); + + const newEpisode = await Episode.findByIdAndUpdate(episode._id, update, { + new: true, + populate: this.populateEpisode, + }).lean(); + if (!newEpisode) { + throw new WebError(404, "show does not exist"); + } + return newEpisode; + } + + async getById(episodeId: Types.ObjectId): Promise { + const show = await Episode.findById(episodeId) + .populate(this.populateEpisode) + .lean(); + return show; + } + + async getLive(pagination: WebPaginationParameters): Promise { + const search = { status: EpisodeStatus.Live }; + + const episodes = await Episode.find(search) + .sort({ created: -1 }) + .skip(pagination.skip) + .limit(pagination.cpp) + .populate(this.populateEpisode) + .lean(); + + const totalEpisodeCount = await Episode.countDocuments(search); + return { episodes, totalEpisodeCount }; + } + + async getAll(pagination: WebPaginationParameters): Promise { + const episodes = await Episode.find() + .sort({ created: -1 }) + .skip(pagination.skip) + .limit(pagination.cpp) + .populate(this.populateEpisode) + .lean(); + + const totalEpisodeCount = await Episode.estimatedDocumentCount(); + return { episodes, totalEpisodeCount }; + } +} + +export default EpisodeService;