62 rudimentary photo submission into database#296
Conversation
…or photos, added tests for the functionality
…fix) and fetched objects from db in photoController instead of strings
ethanszeto
left a comment
There was a problem hiding this comment.
You did good for quite a complicated ticket with semi-unclear directions (thats on me). Just a couple things to clean up here and there, and the failing tests, but it looks like most of the code is written.
app/controllers/photoController.js
Outdated
| req.body.creationTime = req.body.creationTime ? new Date(req.body.creationTime) : req.body.creationTime; | ||
| req.body.modificationTime = req.body.modificationTime | ||
| ? new Date(req.body.modificationTime) | ||
| : req.body.modificationTime; |
There was a problem hiding this comment.
CreationTime and ModificationTime should be automatically added to the object during Validation. Look into the JSON Schema (validationSchema) to build a validator for you to accomplish this.
app/controllers/photoController.js
Outdated
| ? new Date(req.body.modificationTime) | ||
| : req.body.modificationTime; | ||
| // create a PhotoCreate object from the request | ||
| const photoCreate = new PhotoCreate(req.body); |
There was a problem hiding this comment.
API Models is deprecated for validation (Sorry, was not clear initially on this)
app/controllers/photoController.js
Outdated
| // fetch users | ||
| const fetchUsers = async (emails, role) => { | ||
| const users = await UsersAccessor.getUsersByEmail(emails); | ||
| if (users.length !== emails.length) { | ||
| throw new ErrorInvalidRequestBody(`Invalid ${role} emails`); | ||
| } | ||
| return users; | ||
| }; | ||
| // fetch tags | ||
| const fetchTags = async (tagNames) => { | ||
| const tags = await PhotoTagAccessor.getTagsByName(tagNames); | ||
| if (tags.length !== tagNames.length) { | ||
| throw new ErrorInvalidRequestBody(`Invalid tag names`); | ||
| } | ||
| return tags; | ||
| }; | ||
| const { url, tags, photographers, photoTime, rights, creationTime, modificationTime } = photoCreate; | ||
| // fetch the photographers | ||
| const photographerUsers = await fetchUsers(photographers, "photographers"); | ||
| // fetch the photo tags | ||
| const photoTags = await fetchTags(tags); | ||
| // create a new photo using the photographers and photo tags |
There was a problem hiding this comment.
In order to get the MongoIds of the users/tags/etc, I believe there are helper methods available. For PhotoTag, you might have to create that helper method yourself.
const tagNamesToIds = (
[/*list of photo tag names*/]
) => {
[/*list of photo tag ids*/]
}
app/controllers/photoController.js
Outdated
| tags: photoTags, | ||
| photographers: photographerUsers, |
There was a problem hiding this comment.
Remember that, although the populated record will return every detail, when you are inserting into the database, these references are actually just the _id's of the objects, not the whole objects themselves.
You can think of tags and photographers as foreign keys to another record that exists in a different collection.
app/controllers/photoController.js
Outdated
| // validation needed? | ||
| // return |
There was a problem hiding this comment.
You can add outgoing validation here yes
| static async createPhoto(photo) { | ||
| await Connection.open(); | ||
| const createPhoto = await Photo.create(photo); | ||
| return createPhoto; | ||
| } |
| const photo = await Photo.findById(new mongoose.Types.ObjectId(photoID)) | ||
| .populate("photographers") | ||
| .populate({ | ||
| path: "tags", | ||
| populate: { | ||
| path: "creatingUser", | ||
| }, | ||
| }); |
| try { | ||
| await Connection.open(); | ||
| const tags = await PhotoTag.find({ tagName: { $in: tagNames } }); | ||
| return tags; | ||
| } catch (error) { | ||
| console.error(error); | ||
| throw new Error(error); | ||
| } | ||
| } |
There was a problem hiding this comment.
Validation/Error handling should exist in the controller level, not the accessor
| tags: { type: [string], required: true }, | ||
| photographers: { type: [string], required: true }, |
app/models/apiModels/photo.js
Outdated
| // import Photography_status from "../enums/photography_status.js"; | ||
| //import { ErrorInternalAPIModelValidation } from "../../error/internalErrors.js"; |
There was a problem hiding this comment.
If unused, you can remove these
Thats fine.
You can, not part of the scope of the ticket, however, it may prove to be useful for someone in the future.
Phototags are not required when submitting/uploading a photo |
…a to validate photo creation. did not modify tests
Pull Request
Brief Summary
Questions / Considerations for the Future
API Changes
Database Changes
New Tests
Closes #62