-
Notifications
You must be signed in to change notification settings - Fork 0
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
anmol678
wants to merge
3
commits into
master
Choose a base branch
from
anmol/page-controller
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
* @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; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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!