O.Hassan
O.Hassan

Reputation: 1

adding an object to a list in play framework

I am new to play, whenever I use list.add(Object) to the list and print the size of the list, it remains 0 !!!

My Method is to like a tutorial, it checks if the logged-in user has liked this tutorial before, if yes, it increments the likeCount of the tutorial by one, and add the tutorial to the like list of the user. If no, it renders that he already likes it. since the tutorial is not saved in the list, I am not able to check if it is already liked or not !!!

Models:

    @Entity
    public class RegisteredUser extends Model {
public String name;
    @ManyToMany 
public List<Tutorial> contributedTutorials;

     public RegisteredUser(String name) {
     this.name = name;
     this.likeList = newArrayList<Tutorial>();
     this.save();
     }
     }

    @Entity
    public class Tutorial extends Model {
public  String Title;
    public int likeCount;
    public Tutorial(String title) {
    this.title = title;
    this.likeCount = 0;
    }

Controller: public Tutorials extends Controller { public static void likeTutorial() {

   if (session.get("RegisteredUserId") != null && session.get("tutID") != null ) {
    {   
        long uId = Long.parseLong(session.get("RegisteredUserId"));
        RegisteredUser user = RegisteredUser.findById(uId);
        long tId = Long.parseLong(session.get("tutID"));
        Tutorial tut = Tutorial.findById(tId);
        int x = tut.likeCount;
        x++;
        if (!(user.likeList.contains(tut))) 
            // to check that this user didn't like this tut before
        { 
            Tutorial.em().createQuery("update Tutorial set likeCount ="+ x +" where id=" +tId).executeUpdate();
            tut.refresh();
            user.updateLikeList(tut); // THIS IS NOT WORKING!!!
            renderText("You have successfully liked this Tutorial " + user.likeList.size());
        } 
        }
        renderText("Opps Something went Wrong!!");
    }
}
}

The view :

     <a href="@{Tutorials.likeTutorial()}">Like </a>  

Upvotes: 0

Views: 533

Answers (2)

O.Hassan
O.Hassan

Reputation: 1

I made all adjustments You mentioned above

And The mapping in RegisteredUser Model

@ManyToMany
public List<Tutorial> likeList;

and I have added the mapping to Tutorial Model

    @ManyToMany  
    public List<RegisteredUser> likersList;

and adjusted the method in the controller as follows

if (! user.likeList.contains(tut)) {

tut.likeCount++; 
//tut.likersList.add(user); // however this raised an error too! at the next line
tut.save();
//as for the likeCount update, it has worked perfectly, Thank You

// Since it's a ManyToMany relationship, you only need to add it to one list and the other will reflect properly if mappedBy properly 
//I have modified Model Tutorial as shown above and it didn't work too !
user.likeList.add(tut); 
user.save(); // this raised an error!! 

renderText("You have successfully liked this Tutorial " + user.likeList.size());

}

Upvotes: 0

kamasheto
kamasheto

Reputation: 1030

+You don't need to call the this.save() and this.likeList = newArrayList<Tutorial>(); in the constructor. Actually the latter is syntactically wrong.

+Passing the tutorial ID as a session variable is very wrong. You need to pass it as a GET parameter to the action.

+Replace your check with:

// to check that this user didn't like this tut before
if (! user.likeList.contains(tut)) {
    // Tutorial.em().createQuery("update Tutorial set likeCount ="+ x +" where id=" +tId).executeUpdate();
    // tut.refresh();
    // user.updateLikeList(tut); // THIS IS NOT WORKING!!!
    tut.likeCount++;
    tut.save();

    // Since it's a ManyToMany relationship, you only need to add it to one list and the other will reflect properly if mappedBy properly
    user.likeList.add(tut); // note that the example you showed uses contributedTutorials not likeList
    user.save();

    renderText("You have successfully liked this Tutorial " + user.likeList.size());
} 

Upvotes: 0

Related Questions