Skip to content

Commit

Permalink
week 3 tickets
Browse files Browse the repository at this point in the history
  • Loading branch information
Shooshte committed May 25, 2019
1 parent fe70a96 commit 8381aee
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
33 changes: 28 additions & 5 deletions src/dao/commentsDAO.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ export default class CommentsDAO {
try {
// TODO Ticket: Create/Update Comments
// Construct the comment document to be inserted into MongoDB.
const commentDoc = { someField: "someValue" }
const commentDoc = {
movie_id: new ObjectId(movieId),
email: user.email,
text: comment,
date: date,
}

return await comments.insertOne(commentDoc)
} catch (e) {
Expand All @@ -70,8 +75,8 @@ export default class CommentsDAO {
// Use the commentId and userEmail to select the proper comment, then
// update the "text" and "date" fields of the selected comment.
const updateResponse = await comments.updateOne(
{ someField: "someValue" },
{ $set: { someOtherField: "someOtherValue" } },
{ _id: new ObjectId(commentId), email: userEmail },
{ $set: { text, date } },
)

return updateResponse
Expand All @@ -96,6 +101,7 @@ export default class CommentsDAO {
// Use the userEmail and commentId to delete the proper comment.
const deleteResponse = await comments.deleteOne({
_id: ObjectId(commentId),
email: userEmail,
})

return deleteResponse
Expand All @@ -116,11 +122,28 @@ export default class CommentsDAO {
try {
// TODO Ticket: User Report
// Return the 20 users who have commented the most on MFlix.
const pipeline = []
const pipeline = [
{
$group: {
_id: "$email",
count: {
$sum: 1,
},
},
},
{
$sort: {
count: -1,
},
},
{
$limit: 20,
},
]

// TODO Ticket: User Report
// Use a more durable Read Concern here to make sure this data is not stale.
const readConcern = comments.readConcern
const readConcern = { level: "majority" }

const aggregateResult = await comments.aggregate(pipeline, {
readConcern,
Expand Down
24 changes: 23 additions & 1 deletion src/dao/moviesDAO.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,31 @@ export default class MoviesDAO {
// TODO Ticket: Get Comments
// Implement the required pipeline.
const pipeline = [
// matches all movies to queryed id from movies collection
{
$match: {
_id: ObjectId(id),
_id: new ObjectId(id),
},
},
// adds a new comments field sorted by date descending
{
$lookup: {
from: "comments",
pipeline: [
{
$match: {
$expr: {
$eq: ["$movie_id", new ObjectId(id)],
},
},
},
{
$sort: {
date: -1,
},
},
],
as: "comments",
},
},
]
Expand Down
6 changes: 3 additions & 3 deletions src/dao/usersDAO.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default class UsersDAO {
// Insert a user with the "name", "email", and "password" fields.
// TODO Ticket: Durable Writes
// Use a more durable Write Concern for this operation.
await users.insertOne(userInfo)
await users.insertOne(userInfo, { w: 3 })
return { success: true }
} catch (e) {
if (String(e).startsWith("MongoError: E11000 duplicate key error")) {
Expand Down Expand Up @@ -166,8 +166,8 @@ export default class UsersDAO {
// TODO Ticket: User Preferences
// Use the data in "preferences" to update the user's preferences.
const updateResponse = await users.updateOne(
{ someField: someValue },
{ $set: { someOtherField: someOtherValue } },
{ email: email },
{ $set: { preferences: preferences } },
)

if (updateResponse.matchedCount === 0) {
Expand Down

0 comments on commit 8381aee

Please sign in to comment.