-
Notifications
You must be signed in to change notification settings - Fork 1
Step 2 - Implementing Functions #10
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
Open
michmitz
wants to merge
9
commits into
ms-training
Choose a base branch
from
ms-training-2
base: ms-training
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 all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
88eabdf
getUserById and getUsers tests passing
michmitz 7d1567e
Tests passing for createUser
michmitz 1621738
Update functions to throw error, test errors thrown
michmitz 82aef28
updateUser function, testing edge cases and errors
michmitz cd63759
Update status codes
michmitz 08dd7a6
Remove instantiation of new class in functions
michmitz ef0d608
Remove error when users array is empty
michmitz e20000b
Fix updateUser function and test
michmitz 142a02e
Fix updateUsers to only return updated user
michmitz 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 hidden or 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
This file contains hidden or 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 |
---|---|---|
|
@@ -7,54 +7,78 @@ export interface User { | |
readonly color: string | ||
} | ||
|
||
export interface ErrorMessage { | ||
readonly message: string | ||
} | ||
|
||
export class CustomError extends Error { | ||
status: number | ||
|
||
constructor(status: number, message: string) { | ||
super(message); | ||
this.status = status | ||
Object.setPrototypeOf(this, CustomError.prototype) | ||
} | ||
} | ||
|
||
export class UserAPI { | ||
private users: ReadonlyArray<User> | ||
|
||
constructor() { | ||
this.users = [ | ||
{ | ||
id: 1, | ||
name: "Michelle", | ||
age: 30, | ||
color: "rainbow", | ||
}, | ||
{ | ||
id: 2, | ||
name: "Vintage Aaron", | ||
age: 35, | ||
color: "blue", | ||
}, | ||
{ | ||
id: 3, | ||
name: "Derek", | ||
age: 28, | ||
color: "green", | ||
}, | ||
{ | ||
name: "George", | ||
age: 32, | ||
color: "red", | ||
}, | ||
] | ||
constructor(users: ReadonlyArray<User>) { | ||
this.users = users | ||
} | ||
|
||
public getUserById = (id: number): User => { | ||
const foundUser = this.users.find(user => user.id === id) | ||
if (!foundUser) { | ||
throw new CustomError(404, "No user found.") | ||
} else { | ||
return foundUser | ||
} | ||
} | ||
|
||
public getUserById = (id: number): User | null => { | ||
console.log("Get user by id") | ||
public getUsers = (): ReadonlyArray<User> => { | ||
return this.users | ||
} | ||
|
||
public getUsers = (): ReadonlyArray<User> | null => { | ||
console.log("Get users") | ||
} | ||
public createUser = (user: User): User => { | ||
const userIdExists = this.users.find(existingUser => existingUser.id === user.id) | ||
|
||
public createUser = (user: User): User | null => { | ||
console.log("Create user", user) | ||
} | ||
if (userIdExists) { | ||
throw new CustomError(405, "User with id already exists.") | ||
} else { | ||
this.users = [user, ...this.users] | ||
return user | ||
} | ||
} | ||
|
||
public deleteUserById = (id: number): User | null => { | ||
console.log("Create user by id") | ||
} | ||
public deleteUserById = (id: number): User => { | ||
const user = this.users.find(existingUser => existingUser.id === id) | ||
|
||
public updateUser = (id: number, user: User): User | null => { | ||
console.log("Update user") | ||
if (user) { | ||
this.users = this.users.filter(user => user.id !== id) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice |
||
return user | ||
} else throw new CustomError(404, "No user with that id found.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. braces |
||
} | ||
|
||
public updateUser = (id: number, updatedUser: User): User => { | ||
let targetUser = this.users.find(existingUser => existingUser.id === id) | ||
|
||
if (targetUser) { | ||
this.users = this.users.map(user => { | ||
if (user.id === id) { | ||
if (user.name === updatedUser.name) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cool |
||
return { id, ...updatedUser } | ||
} else { | ||
throw new CustomError(405, "Different user with same id already exists.") | ||
} | ||
} else { | ||
return user | ||
} | ||
}) | ||
return { id, ...updatedUser } | ||
} else { | ||
throw new CustomError(404, "No user found by that id.") | ||
} | ||
} | ||
} | ||
} |
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.
Very curious why you included this. I have no idea if it's right or wrong or anything; this isn't something I did in my implementation and I'm curious as to why you did this.