Skip to content
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

Open
wants to merge 9 commits into
base: ms-training
Choose a base branch
from
Open

Conversation

michmitz
Copy link

@michmitz michmitz commented Dec 17, 2021

1. What this does:

  • Implements CRUD functions for user class:
    • Get user by id
    • Get all users
    • Create user
    • Delete user
    • Update user
  • Creates CustomError class with customized status code and message
  • Tests edge cases for each function, such as when no user can be found by id.

2. Ticket:
Discussed with Aron.

3. How to test:
-All tests are currently passing.

Copy link
Contributor

@ianandrewson ianandrewson left a 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.

} catch (error: any) {
expect(error).toBeInstanceOf(CustomError)
expect(error.message).toBe("No user found.")
expect(error.status).toEqual(500)
Copy link
Contributor

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.

@@ -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', () => {
Copy link
Contributor

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?

} catch (error: any) {
expect(error).toBeInstanceOf(CustomError)
expect(error.message).toBe("No users found.")
expect(error.status).toEqual(500)
Copy link
Contributor

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.

} catch (error: any) {
expect(error).toBeInstanceOf(CustomError)
expect(error.message).toBe("User with id already exists.")
expect(error.status).toEqual(500)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here too.

} catch (error: any) {
expect(error).toBeInstanceOf(CustomError)
expect(error.message).toBe("No user with that id found.")
expect(error.status).toEqual(500)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here.

} catch (error: any) {
expect(error).toBeInstanceOf(CustomError)
expect(error.message).toBe("Different user with same id already exists.")
expect(error.status).toEqual(500)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, and here.

} catch (error: any) {
expect(error).toBeInstanceOf(CustomError)
expect(error.message).toBe("No user found by that id.")
expect(error.status).toEqual(500)
Copy link
Contributor

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)
Copy link
Contributor

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))
Copy link
Contributor

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.")
Copy link
Contributor

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
Copy link
Contributor

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) {
Copy link
Contributor

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])
Copy link
Contributor

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)
Copy link
Contributor

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.")
Copy link
Contributor

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants