Skip to content

Kalina 199 3 - #297

Open
KalinaM11 wants to merge 4 commits into
mainfrom
kalina-199-3
Open

Kalina 199 3#297
KalinaM11 wants to merge 4 commits into
mainfrom
kalina-199-3

Conversation

@KalinaM11

Copy link
Copy Markdown

Pull Request

Brief Summary

  • Added a filter route and a filter method to the PhotoTagController.
  • Created a new file under tests > photoTag called getPhotoTagsFilter for testing.
  • Changed the getTagsByUser(userID) in PhotoTagAccessor to getTagsByUserID because there were two methods named getTagsByUser except one used ID and the other used emails. Sending an id through this method would go to the one that used emails instead of the one that used IDs. Should hopefully not change anything else I checked that this method is not used anywhere else.

Questions / Considerations for the Future

  • Just the two methods with the same name, one is now named something different.
  • There are merge conflicts, and I fixed them twice, but once I do all of my tests fail. Before the merge conflicts, they all pass.

API Changes

  • New /photo-tag/filter endpoint in photoTagRoutes.

Database Changes

  • n/a

New Tests

  • New testing file under photoTags for the filter functionality.

Closes #199

Finished all testing. Changed one of the getTagsByUser to getTagsByUserID because there were two methods named the same thing and sending an id through this method would go to the one that used emails instead of the one that used IDs.
@KalinaM11
KalinaM11 requested review from a team as code owners February 5, 2025 19:11

@ethanszeto ethanszeto left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not bad! Some refactoring could be done, but the important part is that you wrote code well-within the patterns we have, which is amazing.

Comment thread app/controllers/photoTagController.js Outdated
* @param {Request} req
* @param {Response} res
*/
static async filter(req, res) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

(n-b) filter is pretty generic for a method name, maybe introduce some more specificity so its easy to understand for other developers what this function does

Comment on lines +56 to +80
// get tags by color and email
if (color && userEmail) {
// get the tags by color
const photoTagByColor = await PhotoTagAccessor.getTagsByColor(color);
// get the user's id based on their email
const decodedEmail = decodeURIComponent(userEmail);
const userId = await Utils.getUserIdByEmail(decodedEmail)
// get the phototags by the id
const photoTagsById = await PhotoTagAccessor.getTagsByUserID(userId);
// filter the photo tags to find the tags by color and email
const photoTagsByColorAndEmail = photoTagByColor.filter(photoTag =>
photoTagsById.some(tag => tag._id.toString() === photoTag._id.toString())
);
photoTags = photoTagsByColorAndEmail;
}
// else get tags by color
else if (color) {
photoTags = await PhotoTagAccessor.getTagsByColor(color);
}
// else get tags by userEmail
else if (userEmail) {
const decodedEmail = decodeURIComponent(userEmail);
const userId = await Utils.getUserIdByEmail(decodedEmail);
photoTags = await PhotoTagAccessor.getTagsByUserID(userId);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is a good standard implementation of the cases you can get to try and filter through photo tags. However, there is a terser way to accomplish this sort of functionality. I would encourage you to look at Arushi's search article by options for an idea on how to make this more slick:

try {
const query = {};
var limit;
for (const searchOption of Object.keys(mapping)) {
if (req.body.hasOwnProperty(searchOption)) {
query[searchOption] = await mapping[searchOption](req.body[searchOption]);
}
}
if (req.body.hasOwnProperty("before") && req.body.hasOwnProperty("after")) {
query.$and = [{ approvalTime: { $gte: req.body.after } }, { approvalTime: { $lte: req.body.before } }];
} else if (req.body.hasOwnProperty("before")) {
query.approvalTime = { $lte: req.body.before };
} else if (req.body.hasOwnProperty("after")) {
query.approvalTime = { $gte: req.body.after };
}
// limits are not a part of query, thus handled separately
if (req.body.hasOwnProperty("limit")) {
limit = Number(req.body.limit);
}
// access the database and retrieve the matching articles
const matchingArticles = await ArticlesAccessor.searchArticles(query, limit);
res.status(200).json(matchingArticles);

static async getTagsByUserID(userID) {
await Connection.open();
const tags = await PhotoTag.find({ user: new mongoose.Types.ObjectId(userID) });
const tags = await PhotoTag.find({ creatingUser: new mongoose.Types.ObjectId(userID) });

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nice

* @param {String} user email - The email of the user
* @returns tags
*/
static async getTagsByUser(userEmail) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Perhaps naming this method something else would make it easier to identify how to use it vs the method above

@ethanszeto

Copy link
Copy Markdown
Member

Please also merge main into this branch to resolve merge conflicts

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.

Endpoint - GET PhotoTag search by options

2 participants