-
Notifications
You must be signed in to change notification settings - Fork 1
create CRUD functions for UserAPI class #4
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
lauriewaller
wants to merge
1
commit into
master
Choose a base branch
from
test-myApi
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 all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,104 @@ | ||
console.log("TEST") | ||
console.log("HI AARON!") | ||
|
||
// user object has these fields | ||
// id (optional) | ||
// name | ||
// age | ||
// color | ||
|
||
// -create a class called UserAPI | ||
// it has a class property called users, which is just a map {} | ||
// add the following functions to the class, using es6 syntax (arrow functions) | ||
// 1. getUser by id (eg. getUser = (id)= > { return ...}) | ||
// 2. getUsers (returns all users in array) | ||
// 3. createUser has argument of user object {}. Make sure to assign an id property to the user object | ||
// 4. deleteUser by id | ||
// 5. updateUser has argument of user object {} | ||
|
||
// each of the functions will manipulate / access the users class property | ||
// use immutability via the spread operator: { ...user, id: "myID" } | ||
// show that these operations work by creating an instance of your class, and calling the methods | ||
// and console logging the output | ||
|
||
// const myApi = new UserApI() | ||
// myApi.xxx() | ||
|
||
class UserAPI { | ||
constructor() { | ||
this.users = {}; | ||
} | ||
|
||
getUsers = () => Object.values(this.users); | ||
|
||
getUserById = (id) => { | ||
return this.users[id]; | ||
}; | ||
|
||
createUser = (user) => { | ||
const users = this.users; | ||
const newId = () => { | ||
if (Object.keys(users).length === 0) { | ||
return 1; | ||
} else { | ||
let max = Math.max(...Object.keys(users)); | ||
return max + 1; | ||
} | ||
}; | ||
const userWithId = { ...user, id: newId() }; | ||
this.users = { ...this.users, [newId()]: userWithId }; | ||
return userWithId; | ||
}; | ||
|
||
updateUser = (user) => { | ||
this.users = { ...this.users, [user.id]: user }; | ||
return user; | ||
//use spread operator to update by key | ||
}; | ||
|
||
deleteUser = (id) => { | ||
const { [id]: user, ...updatedUsers } = this.users; | ||
this.users = updatedUsers; | ||
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. @rachoac what would be correct to return for this function? I should have made a clear note on this. |
||
}; | ||
} | ||
|
||
const myApi = new UserAPI(); | ||
|
||
const userOne = { | ||
id: null, | ||
name: "hank", | ||
age: 7, | ||
color: "orange", | ||
}; | ||
|
||
const userTwo = { | ||
id: null, | ||
name: "rob", | ||
age: 5, | ||
color: "green", | ||
}; | ||
|
||
const userThree = { | ||
id: null, | ||
name: "elbert", | ||
age: 8, | ||
color: "blue", | ||
}; | ||
|
||
const userThreeUpdate = { | ||
id: 3, | ||
name: "elbert", | ||
age: 9, | ||
color: "blue", | ||
}; | ||
|
||
console.log(myApi.createUser(userOne)); | ||
console.log(myApi.createUser(userTwo)); | ||
console.log(myApi.createUser(userThree)); | ||
console.log(myApi.getUsers()); | ||
console.log(myApi.getUserById(1)); | ||
console.log(myApi.updateUser(userThreeUpdate)); | ||
console.log(myApi.getUsers()); | ||
console.log(myApi.deleteUser(1)); | ||
console.log(myApi.getUsers()); | ||
|
||
|
||
|
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.
@rachoac is this correct to use the computed property here to update by id?