Skip to content

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
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
106 changes: 104 additions & 2 deletions src/index.ts
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 };
Copy link
Author

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?

return user;
//use spread operator to update by key
};

deleteUser = (id) => {
const { [id]: user, ...updatedUsers } = this.users;
this.users = updatedUsers;
Copy link
Author

Choose a reason for hiding this comment

The 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());