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

Create pageController #25

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
78 changes: 78 additions & 0 deletions src/controllers/pageController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { PageLink } from '../models';
import { PageLinkAttributes, PageLinkInstance } from '../models/PageLink';

/**
* Creates a page with the given attribues
*
* @param attributes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add types to your JSDoc comments!

* @returns The newly created page.
*/
export function createPage(attributes: PageLinkAttributes) : Promise<PageLinkInstance> {
const newPageLink: PageLinkInstance = await PageLink.create(attribues);
return newPageLink;
}

/**
* Gets the page with the given id.
*
* @param id
*/
export function getPage(id: number): Promise<PageLinkInstance | null> {
const pageLink: PageLinkInstance | null = await PageLink.findById(id);
return pageLink;
}

/**
* Gets all pages.
*
* @param id
*/
export function getAllPages(): Promise<PageLinkInstance[]> {
const pageLink: PageLinkInstance[] = await PageLink.all();
return pageLink;
}

/**
* Updates a page with the given attributes. Returns the updated page, or undefined if the page is not updated.
*
* @export
* @param {number} id
* @param {*} attributes
* @returns {(any | undefined)}
*/
export function updatePage(id: number, attributes: PageLinkAttributes): Promise<PageLinkInstance | null> {
const [numberOfUpdatedPageLinks, updatedPageLinks] = await PageLink.update(
attributes,
{
where: { id },
returning: true,
}
);

if (numberOfUpdatedPageLinks === 1) {
return updatedPageLinks[0];
} else if (numberOfUpdatedPageLinks !== 0) {
throw new Error(
'More than 1 rows updated from single id in `updatePage`! This is bad!'
);
}
return null;
}

/**
* Deletes a page. Returns true if the page was successfully deleted, false otherwise.
*
* @param id
*/
export function deletePage(id: number): Promise<boolean> {
const numberOfDeletedPageLinks = await PageLink.destroy({
where: { id },
});

if (numberOfDeletedPageLinks === 1) {
return true;
} else if (numberOfDeletedPageLinks !== 0) {
throw new Error('More than 1 rows deleted from single id! This is bad!');
}
return false;
}