diff --git a/src/api/controllers/Webinar.controller.ts b/src/api/controllers/Webinar.controller.ts index b5b2b18..326cfa7 100644 --- a/src/api/controllers/Webinar.controller.ts +++ b/src/api/controllers/Webinar.controller.ts @@ -39,15 +39,20 @@ export const getWebinarById = async ( response: Response, next: NextFunction ) => { - await WebinarService.fetchWebinarById(request.params.webinarId) - .then((data) => { - request.handleResponse.successRespond(response)(data); - next(); - }) - .catch((error: any) => { - request.handleResponse.errorRespond(response)(error.message); - next(); - }); + let webinarId = request.params.webinarId; + if (webinarId) { + await WebinarService.fetchWebinarById(request.params.webinarId) + .then((data) => { + request.handleResponse.successRespond(response)(data); + next(); + }) + .catch((error: any) => { + request.handleResponse.errorRespond(response)(error.message); + next(); + }); + } else { + request.handleResponse.errorRespond(response)("WebinarId not found"); + } }; /** * @todo implement a @function getWebinars that calls @@ -135,15 +140,20 @@ export const updateWebinar = async ( response: Response, next: NextFunction ) => { - await WebinarService.updateWebinar(request.params.webinarId, request.body) - .then((data) => { - request.handleResponse.successRespond(response)(data); - next(); - }) - .catch((error: any) => { - request.handleResponse.errorRespond(response)(error.message); - next(); - }); + let webinarId = request.params.webinarId; + if (webinarId) { + await WebinarService.updateWebinar(request.params.webinarId, request.body) + .then((data) => { + request.handleResponse.successRespond(response)(data); + next(); + }) + .catch((error: any) => { + request.handleResponse.errorRespond(response)(error.message); + next(); + }); + } else { + request.handleResponse.errorRespond(response)("WebinarId not found"); + } }; /** * @todo implement a @function deleteWebinar that calls @@ -159,13 +169,18 @@ export const deleteWebinar = async ( response: Response, next: NextFunction ) => { - await WebinarService.removeWebinar(request.params.webinarId) - .then((data) => { - request.handleResponse.successRespond(response)(data); - next(); - }) - .catch((error: any) => { - request.handleResponse.errorRespond(response)(error.message); - next(); - }); + let webinarId = request.params.webinarId; + if (webinarId) { + await WebinarService.removeWebinar(request.params.webinarId) + .then((data) => { + request.handleResponse.successRespond(response)(data); + next(); + }) + .catch((error: any) => { + request.handleResponse.errorRespond(response)(error.message); + next(); + }); + } else { + request.handleResponse.errorRespond(response)("WebinarId not found"); + } }; diff --git a/src/api/interfaces/IWebinar.ts b/src/api/interfaces/IWebinar.ts index 1cff9e4..90784d3 100644 --- a/src/api/interfaces/IWebinar.ts +++ b/src/api/interfaces/IWebinar.ts @@ -8,6 +8,7 @@ interface IWebinar extends Document { time: Date; tags?: string[]; link?: string; + webinarType: string; deletedAt?: Date; }; diff --git a/src/api/models/Webinar.model.ts b/src/api/models/Webinar.model.ts index d605918..4103000 100644 --- a/src/api/models/Webinar.model.ts +++ b/src/api/models/Webinar.model.ts @@ -10,6 +10,7 @@ const WebinarSchema = new Schema( time: { type: Date, required: true }, tags: [{ type: String, required: false }], link: { type: String, required: true }, + webinarType: { type: String, enum: ["PAST", "UPCOMING"], required: true }, deletedAt: { type: Date, required: false, default: null }, }, { timestamps: true } diff --git a/src/api/services/Webinar.service.ts b/src/api/services/Webinar.service.ts index f46ff40..d2683f7 100644 --- a/src/api/services/Webinar.service.ts +++ b/src/api/services/Webinar.service.ts @@ -2,13 +2,15 @@ import { DocumentDefinition, FilterQuery } from "mongoose"; import { IWebinar } from "../interfaces"; import WebinarModel from "../models/Webinar.model"; /** - * @todo create @function insertWebinar to save a webinar in the database + * Save a webinar in the database + * @param {IWebinar} webinarData + * @returns {IWebinar} New webinar document */ export const insertWebinar = async ( webinarData: DocumentDefinition ) => { return await WebinarModel.create(webinarData) - .then(async (webinar) => { + .then((webinar) => { return webinar; }) .catch((error) => { @@ -16,12 +18,13 @@ export const insertWebinar = async ( }); }; /** - * @todo create @function fetchWebinarById to fetch a webinar in the system + * Fetch a webinar in the database * @param webinarId @type string + * @returns {IWebinar} Webinar document for relevent ID */ export const fetchWebinarById = async (webinarId: string) => { return await WebinarModel.findById(webinarId) - .then(async (webinar) => { + .then((webinar) => { return webinar; }) .catch((error) => { @@ -29,11 +32,12 @@ export const fetchWebinarById = async (webinarId: string) => { }); }; /** - * @todo create @function fetchWebinars to fetch all the webinars in the system + * Fetch all the webinars in the database + * @returns {IWebinar} All webinar documents in the database */ export const fetchWebinars = async () => { - return await WebinarModel.find() - .then(async (webinars) => { + return await WebinarModel.aggregate([{ $match: { deletedAt: { $eq: null } } }]) + .then((webinars) => { return webinars; }) .catch((error) => { @@ -41,11 +45,12 @@ export const fetchWebinars = async () => { }); }; /** - * @todo create @function fetchPastWebinars to fetch all the past webinars in the system + * Fetch all the past webinars in the database + * @returns {IWebinar} All the past webinar documents in the database */ export const fetchPastWebinars = async () => { return await WebinarModel.find({ webinarType: "PAST" }) - .then(async (webinars) => { + .then((webinars) => { return webinars; }) .catch((error) => { @@ -53,13 +58,14 @@ export const fetchPastWebinars = async () => { }); }; /** - * @todo create @function fetchUpcomingWebinar to fetch an upcoming webinars in the system + * Fetch an upcoming webinars in the database + * @returns {IWebinar} All the upcoming webinar documents in the database */ export const fetchUpcomingWebinar = async () => { return await WebinarModel.findOne({ webinarType: "UPCOMING" }) .limit(1) .sort({ $natural: -1 }) - .then(async (webinar) => { + .then((webinar) => { return webinar; }) .catch((error) => { @@ -67,7 +73,7 @@ export const fetchUpcomingWebinar = async () => { }); }; /** - * @todo create @function updateWebinar to update a webinar in the system + * Update a webinar in the database * @param webinarId @type string * @param updateData @type DocumentDefinition */ @@ -95,7 +101,7 @@ export const updateWebinar = async ( }); }; /** - * @todo create @function removeWebinar to delete a webinar + * Delete a webinar in the database * @param webinarId @type string */ export const removeWebinar = async (webinarId: string) => {