Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/webinar service and controller #62

Merged
merged 5 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 42 additions & 27 deletions src/api/controllers/Webinar.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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");
}
};
32 changes: 19 additions & 13 deletions src/api/services/Webinar.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,72 +2,78 @@ 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<IWebinar>
) => {
return await WebinarModel.create(webinarData)
.then(async (webinar) => {
.then((webinar) => {
return webinar;
})
.catch((error) => {
throw new Error(error.message);
});
};
/**
* @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) => {
throw new Error(error.message);
});
};
/**
* @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) => {
throw new Error(error.message);
});
};
/**
* @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" })
senuravihanjayadeva marked this conversation as resolved.
Show resolved Hide resolved
.then(async (webinars) => {
.then((webinars) => {
return webinars;
})
.catch((error) => {
throw new Error(error.message);
});
};
/**
* @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) => {
throw new Error(error.message);
});
};
/**
* @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<IWebinar>
*/
Expand Down Expand Up @@ -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) => {
Expand Down