-
Notifications
You must be signed in to change notification settings - Fork 1
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
Step 2 - Implementing Functions #10
base: ms-training
Are you sure you want to change the base?
Conversation
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.
Super solid. The only thing I would change are the status code if you want to, and not instantiating a new class in your create and delete methods.
src/index.test.ts
Outdated
} catch (error: any) { | ||
expect(error).toBeInstanceOf(CustomError) | ||
expect(error.message).toBe("No user found.") | ||
expect(error.status).toEqual(500) |
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.
I'm no expert in this, but I'm not sure status 500 is the most semantic status you can use here. On the one hand, it is the server running the code to find the user. On the other hand, it's a specific resource that can't be found, likely queried by the client. Aron should probably weigh in on what the best status codes to use here are. Personally, I implemented error codes in the 400 range.
src/index.test.ts
Outdated
@@ -55,13 +86,20 @@ describe('Tests will go here!', () => { | |||
]) | |||
}) | |||
|
|||
it.skip('returns null if no users are found', () => { | |||
const result = users.getUserById(10) | |||
it('returns CustomError if no users are found', () => { |
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.
The behavior here would probably be defined by a product owner or someone other than you or I, but there is a semantic argument to be made that when getting all users, returning an empty is totally fine instead of returning an error. Technically, all users were found, there just aren't any. And as long as you return an empty array and not null, any front end implementation shouldn't choke on an empty array.
So the question becomes, is finding no users an error?
src/index.test.ts
Outdated
} catch (error: any) { | ||
expect(error).toBeInstanceOf(CustomError) | ||
expect(error.message).toBe("No users found.") | ||
expect(error.status).toEqual(500) |
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.
A more specific status code can likely be given here as well.
src/index.test.ts
Outdated
} catch (error: any) { | ||
expect(error).toBeInstanceOf(CustomError) | ||
expect(error.message).toBe("User with id already exists.") | ||
expect(error.status).toEqual(500) |
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.
Here too.
src/index.test.ts
Outdated
} catch (error: any) { | ||
expect(error).toBeInstanceOf(CustomError) | ||
expect(error.message).toBe("No user with that id found.") | ||
expect(error.status).toEqual(500) |
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.
And here.
src/index.test.ts
Outdated
} catch (error: any) { | ||
expect(error).toBeInstanceOf(CustomError) | ||
expect(error.message).toBe("Different user with same id already exists.") | ||
expect(error.status).toEqual(500) |
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.
Yup, and here.
src/index.test.ts
Outdated
} catch (error: any) { | ||
expect(error).toBeInstanceOf(CustomError) | ||
expect(error.message).toBe("No user found by that id.") | ||
expect(error.status).toEqual(500) |
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.
The last one.
constructor(status: number, message: string) { | ||
super(message); | ||
this.status = status | ||
Object.setPrototypeOf(this, CustomError.prototype) |
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.
src/index.ts
Outdated
const user = this.users.find(existingUser => existingUser.id === id) | ||
|
||
if (user) { | ||
new UserAPI(this.users.filter(user => user.id !== id)) |
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.
I still firmly believe there is a way to do this without instantiating a new version of the class.
src/index.ts
Outdated
existingUser = { ...updatedUser } | ||
return { id, ...existingUser } | ||
} else | ||
throw new CustomError(500, "Different user with same id already exists.") |
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.
Clever.
src/index.ts
Outdated
const foundUser = this.users.find(user => user.id === id) | ||
if (foundUser === undefined) { | ||
throw new CustomError(500, "No user found.") | ||
} else return foundUser |
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.
Braces
src/index.ts
Outdated
|
||
public getUserById = (id: number): User => { | ||
const foundUser = this.users.find(user => user.id === id) | ||
if (foundUser === undefined) { |
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.
!foundUser
src/index.ts
Outdated
if (userIdExists) { | ||
throw new CustomError(500, "User with id already exists.") | ||
} else { | ||
new UserAPI([user, ...this.users]) |
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.
Why is this returning a new instance of the api
const user = this.users.find(existingUser => existingUser.id === id) | ||
|
||
if (user) { | ||
this.users = this.users.filter(user => user.id !== id) |
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.
Nice
if (user) { | ||
this.users = this.users.filter(user => user.id !== id) | ||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
braces
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 comment
The reason will be displayed to describe this comment to others. Learn more.
cool
1. What this does:
2. Ticket:
Discussed with Aron.
3. How to test:
-All tests are currently passing.