Chapter 2: Ticket - Create/Update Comments

I’m trying to implement the method below but, I do not get the code in the mflix status page. The tests pass and on the status page I get the following message: “Create / Update Comments: Was able to update a comment that was not owned”.
Here is my code:

public boolean updateComment(String commentId, String text, String email) {
// TODO> Ticket - Update User reviews: implement the functionality that enables updating an
// user own comments
// TODO> Ticket - Handling Errors: Implement a try catch block to
// handle a potential write exception when given a wrong commentId.

if( commentId == null
        || commentId.isEmpty()
        || email == null
        || email.isEmpty() ){
  throw new IncorrectDaoOperation("Error updating comment");
}

Bson query = Filters.and(Filters.eq("_id", new ObjectId(commentId)), Filters.eq("email",email));

Document commentDoc = new Document("text", text);
Document comment = new Document("$set", commentDoc);

try {
  return commentCollection.findOneAndUpdate(query, comment) != null;

}catch (Exception e){
  throw new IncorrectDaoOperation(e.getMessage());
}

}
What could be wrong?
Thank you in advance for your help.

I solved it in the following way

  Comment comment = null;
    Bson queryFilter = Filters.and(
           Filters.eq("_id", new ObjectId(commentId)),
           Filters.eq("email", email)
    );
    comment = commentCollection.find(queryFilter).first();
    if(comment != null) {
      UpdateResult updateResult = commentCollection.updateOne(queryFilter, set("text", text));
      // TODO> Ticket - Handling Errors: Implement a try catch block to
      // handle a potential write exception when given a wrong commentId.
      return updateResult.wasAcknowledged();
    }
return false;

I’ve tried your code, but the problem persists. Maybe it’s something wrong in some other part of the code. It’s weird all tests pass and I to receive all previous validation code.
Thank you for your help.

You made the modification of the index.html file. You have to have the following change: window.mflix = {useFacets: true}

Yes, I did the modification.

Is the error same after applying this code? Please share the error and code.

Kanika

Below is the application log:

2019-05-29 09:24:02.398 INFO 12988 — [nio-5000-exec-7] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet ‘dispatcherServlet’
2019-05-29 09:24:02.398 INFO 12988 — [nio-5000-exec-7] o.s.web.servlet.DispatcherServlet : FrameworkServlet ‘dispatcherServlet’: initialization started
2019-05-29 09:24:02.678 INFO 12988 — [nio-5000-exec-7] o.s.web.servlet.DispatcherServlet : FrameworkServlet ‘dispatcherServlet’: initialization completed in 280 ms
2019-05-29 09:24:04.786 INFO 12988 — [io-5000-exec-10] org.mongodb.driver.connection : Opened connection [connectionId{localValue:6, serverValue:395178}] to mflix-shard-00-00-u1ek9.mongodb.net:27017
2019-05-29 09:24:04.828 INFO 12988 — [nio-5000-exec-9] org.mongodb.driver.connection : Opened connection [connectionId{localValue:5, serverValue:395178}] to mflix-shard-00-00-u1ek9.mongodb.net:27017
2019-05-29 09:24:04.828 INFO 12988 — [nio-5000-exec-6] org.mongodb.driver.connection : Opened connection [connectionId{localValue:4, serverValue:395178}] to mflix-shard-00-00-u1ek9.mongodb.net:27017
2019-05-29 09:24:06.049 INFO 12988 — [nio-5000-exec-8] org.mongodb.driver.connection : Opened connection [connectionId{localValue:8, serverValue:362766}] to mflix-shard-00-00-u1ek9.mongodb.net:27017
2019-05-29 09:24:06.049 INFO 12988 — [nio-5000-exec-7] org.mongodb.driver.connection : Opened connection [connectionId{localValue:9, serverValue:362766}] to mflix-shard-00-00-u1ek9.mongodb.net:27017
2019-05-29 09:24:06.051 INFO 12988 — [nio-5000-exec-5] org.mongodb.driver.connection : Opened connection [connectionId{localValue:7, serverValue:362766}] to mflix-shard-00-00-u1ek9.mongodb.net:27017
2019-05-29 09:24:08.377 WARN 12988 — [nio-5000-exec-7] mflix.api.daos.MovieDocumentMapper : Unable to map document null to Movie object: null
2019-05-29 09:24:08.377 WARN 12988 — [nio-5000-exec-7] mflix.api.daos.MovieDocumentMapper : Skipping document
2019-05-29 09:24:09.960 ERROR 12988 — [nio-5000-exec-1] Service$$EnhancerBySpringCGLIB$$cf2c3fd0 : Cannot validate user token Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1MjIxNzI3NzMsIm5iZiI6MTUyMjE3Mjc3MywianRpIjoiYjFlYmI0ZDQtNjZlZS00MTY4LTg0MWQtZGNhODJkMThmN2NhIiwiZXhwIjoxNTIyMTczNjczLCJpZGVudGl0eSI6eyJlbWFpbCI6ImZvb2JhekBiYXIuY29tIiwibmFtZSI6ImZvbyBiYXIiLCJwYXNzd29yZCI6bnVsbCwicHJlZmVyZW5jZXMiOnsiZmF2b3JpdGVfY2FzdCI6Ik1lZyBSeWFuIiwicHJlZmVycmVkX2xhbmd1YWdlIjoiRW5nbGlzaCJ9fSwiZnJlc2giOmZhbHNlLCJ0eXBlIjoiYWNjZXNzIiwidXNlcl9jbGFpbXMiOnsidXNlciI6eyJlbWFpbCI6ImZvb2JhekBiYXIuY29tIiwibmFtZSI6ImZvbyBiYXIiLCJwYXNzd29yZCI6bnVsbCwicHJlZmVyZW5jZXMiOnsiZmF2b3JpdGVfY2FzdCI6Ik1lZyBSeWFuIiwicHJlZmVycmVkX2xhbmd1YWdlIjoiRW5nbGlzaCJ9fX19.q9z_tG7gEqaRMfrbTpj9Jz52vocqOBWgEpCd3KC6giI: error thrown - JWT signature does not match locally computed signature. JWT validity cannot be asserted and should not be trusted.
2019-05-29 09:24:09.964 ERROR 12988 — [nio-5000-exec-1] mflix.api.security.JWTAuthEntryPoint : Unauthorized error: Full authentication is required to access this resource

Finally, my new code with the help from Jesus Guillermo:

public boolean updateComment(String commentId, String text, String email) {
Comment comment = null;
Bson queryFilter = Filters.and(
Filters.eq("_id", new ObjectId(commentId)),
Filters.eq(“email”, email)
);
comment = commentCollection.find(queryFilter).first();
if(comment != null) {
UpdateResult updateResult = commentCollection.updateOne(queryFilter, Updates.set(“text”, text));
return updateResult.wasAcknowledged();
}
return false;
}

Hi @Tarcisio_de_Paulo_Rosa_51215,

Please try the solution given below:

Kanika

Unfortunately, my time for course completion has expired.
Thank you, everyone, for your help.

I have the same issue - all the tests in UpdateCreateCommentTest are passing, but I get “Create/Update Comments: Unable to update comment” when I’m trying to get the code at status page.

P.S. The same issue with DeleteCommentTest - tests are passing but code can not be obtained from status page.

How his can be fixed?

I guess that issue was in
}catch (Exception e){
throw new IncorrectDaoOperation(e.getMessage());
}

mb try to return false

Have you done Ticket: Get Comments right? You should sort comments before $lookup stage.
None test check this requirement, but when you go to Ticket: Create/Update Comments and try to check its status you will fail. It is because one of edge cases taking last added coment.

So, briefly. You have to sort comment at Get Comments Ticket.

same here. Tried different things, nothing seems working.

Verify the types of variables and fields in queries. Some of the places you need an ObjectId.

I have different issue. API respond with 400 when trying to update comment:

{"error":"Could not update comment. Not authorized to update comments of other users"}

But all previous checks (including user management) passes.

Looks like “Create/Update Comments” is composed of following steps:

  1. Create comment using user A
  2. Try to update that comment as different user (B) - verify whether user can update others comments
  3. Try to update same comment as its creator (A)

The last step fails for me because it actually tries to update (within second and third step) comment which wasn’t created during first step.

Okay I know what I did wrong. On the first step, API returns complete list of all comments for given movie. I didn’t sort them properly. Thats why on step 2 and 3 used unexpected comment.

All tests within GetCommentsTest passes when comments are sorted improperly:

asList(
    match(expr(new Document("$eq", asList("$movie_id", "$$movieId")))),
    sort(descending("year")) // Should be "date" - nevertheless all tests passes
),
1 Like