-
Notifications
You must be signed in to change notification settings - Fork 0
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
Walter/card patch delete #59
Conversation
… into walter/cardPatchDelete
…ellyPro into walter/cardPatchDelete
… into walter/cardPatchDelete
…ECT-PorkBellyPro into walter/cardPatchDelete
Appears to be a problem with the linter resolving the path to the shared folder. Linting locally passes. |
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.
overall pretty good, some minor changes needed.
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.
lg
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.
last bit of stuff
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.
There are better ways to do some of these.
try { | ||
await dbs.withTransaction(async () => { | ||
// removes card in collection belonging to user | ||
const cardToDelete = await this.parent.Cards.findById(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.
Use the ObjectId object constructed earlier instead of giving the string id
here.
const cardToDelete = await this.parent.Cards.findById(id); | ||
if (!cardToDelete) { | ||
throw new HttpStatusError(410); | ||
} else if (user.id !== cardToDelete.user.toString()) { |
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.
Directly compare the user ObjectIds with ObjectId.prototype.equals(other)
.
} else if (user.id !== cardToDelete.user.toString()) { | |
} else if (!user._id.equals(cardToDelete.user)) { |
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.
Throws an error with linter. Error is dangling underscore. Note that user.id gives a string type of id.
} else if (user.id !== cardToDelete.user.toString()) { | ||
throw new HttpStatusError(401); | ||
} else { | ||
await this.parent.Cards.findByIdAndDelete(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.
Use Model.prototype.remove()
to delete a document that you had already found instead of finding it again.
await this.parent.Cards.findByIdAndDelete(id); | |
await cardToDelete.remove(); |
Reference: https://mongoosejs.com/docs/api/model.html#model_Model-remove
// if card is not found | ||
if (!updatedCard) { | ||
throw new HttpStatusError(404); | ||
} else if (updatedCard.user.toString() !== user.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.
Directly compare ObjectIds with equals()
} else if (updatedCard.user.toString() !== user.id) { | |
} else if (!updatedCard.user.equals(user._id)) { |
const tag = await this.parent.Tags.findById(tagId); | ||
if (tag == null) { | ||
throw new HttpStatusError(400); | ||
} else if (tag.user.toString() !== user.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.
Directly compare ObjectIds with equals()
} else if (tag.user.toString() !== user.id) { | |
} else if (!tag.user.equals(user._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.
Not quite right yet.
if (!cardToDelete) { | ||
throw new HttpStatusError(410); | ||
} else if (user.id !== cardToDelete.user.toString()) { | ||
} else if (!user.id.equals(cardToDelete.user)) { |
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.
Wrong property. id
is the string representation of the document id. _id
is the actual mongoose.Types.ObjectId
.
} else if (!user.id.equals(cardToDelete.user)) { | |
} else if (!user._id.equals(cardToDelete.user)) { |
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.
const tag = await this.parent.Tags.findById(tagId); | ||
if (tag == null) { | ||
throw new HttpStatusError(400); | ||
} else if (tag.user.toString() !== user.id) { | ||
} else if (!user.id.equals(tag.user)) { |
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.
Same as above, wrong property.
} else if (!user.id.equals(tag.user)) { | |
} else if (!user._id.equals(tag.user)) { |
// if card is not found | ||
if (!updatedCard) { | ||
throw new HttpStatusError(404); | ||
} else if (updatedCard.user.toString() !== user.id) { | ||
} else if (!user.id.equals(updatedCard.user)) { |
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.
Same as above, wrong property.
} else if (!user.id.equals(updatedCard.user)) { | |
} else if (!user._id.equals(updatedCard.user)) { |
throw new HttpStatusError(401); | ||
} else { | ||
updatedCard.set(updateDetails); | ||
await updatedCard.save(); | ||
} | ||
|
||
const response: CardPatchResponse = { | ||
id: updatedCard.id, | ||
id: updatedCard.id.toString(), |
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.
Change unnecessary, id
is already a string.
d363c69
to
654af8a
Compare
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.
Looks good to me.
Hi all. I have finished writing unit tests for PATCH and DELETE endpoints.
Please confirm that the tests are valid.
For PATCH, please confirm that the image (optional field for cards) can be removed by specifying them as null.
Closes #25.
Closes #26.