package twistrating.eventhandlers;

import com.firebase.client.*;
import org.axonframework.eventhandling.annotation.EventHandler;
import twistrating.events.TwistRatedEvent;

public class RatingEventHandler {

    private static final String likeCount = "likeCount";
    private static final String neutralCount = "neutralCount";
    private static final String dislikeCount = "dislikeCount";

    private final Firebase firebase;

    public RatingEventHandler(Firebase firebase) {
        this.firebase = firebase;
    }

    @EventHandler
    public void handle(TwistRatedEvent event) {
        String property;
        switch (event.getRating()) {
            case 1:
                property = likeCount;
                break;
            case 0:
                property = neutralCount;
                break;
            case -1:
                property = dislikeCount;
                break;
            default:
                property = neutralCount;
                break;
        }

        String targetPath = event.getTwistId() + "/" + property;

        Firebase firebaseTwist = firebase.child(targetPath);
        if (firebaseTwist != null) {
            firebaseTwist.runTransaction(new Transaction.Handler() {
                @Override
                public Transaction.Result doTransaction(MutableData mutableData) {
                    int count = mutableData.getValue(int.class);
                    mutableData.setValue(count + 1);

                    return Transaction.success(mutableData);
                }

                @Override
                public void onComplete(FirebaseError firebaseError, boolean b, DataSnapshot dataSnapshot) {
                }
            });
        }

//        Twist twist = Twist.find.byId(event.getTwistId());
//        if (twist != null) {
//            switch (event.getRating()) {
//                case 1:
//                    twist.likeCount++;
//                    break;
//                case 0:
//                    twist.neutralCount++;
//                    break;
//                case -1:
//                    twist.dislikeCount++;
//                    break;
//            }
//            System.out.println("Found twist:" + twist.id + " with likeCount:" + twist.likeCount);
//            twist.save();
//        }
    }
}

@@ -165,16 +165,48 @@
 edEvent;
+%0Aimport twistrating.views.Twist;
 %0A%0Apublic
@@ -1742,18 +1742,40 @@
      %7D%0A%0A
-//
+        // TODO: Remove%0A
         
@@ -1825,18 +1825,16 @@
 tId());%0A
-//
         
@@ -1850,26 +1850,24 @@
  != null) %7B%0A
-//
             
@@ -1895,18 +1895,16 @@
 ng()) %7B%0A
-//
         
@@ -1919,18 +1919,16 @@
 case 1:%0A
-//
         
@@ -1950,34 +1950,32 @@
 st.likeCount++;%0A
-//
                 
@@ -1977,34 +1977,32 @@
          break;%0A
-//
                 
@@ -2009,18 +2009,16 @@
 case 0:%0A
-//
         
@@ -2043,34 +2043,32 @@
 neutralCount++;%0A
-//
                 
@@ -2070,34 +2070,32 @@
          break;%0A
-//
                 
@@ -2103,18 +2103,16 @@
 ase -1:%0A
-//
         
@@ -2145,18 +2145,16 @@
 ount++;%0A
-//
         
@@ -2172,18 +2172,16 @@
  break;%0A
-//
         
@@ -2186,18 +2186,16 @@
       %7D%0A
-//
         
@@ -2284,18 +2284,16 @@
 Count);%0A
-//
         
@@ -2310,18 +2310,16 @@
 save();%0A
-//
         

Enable updating of sqlite.