package com.nestedworld.nestedworld.database.updater;

import android.support.annotation.NonNull;

import com.nestedworld.nestedworld.database.models.Friend;
import com.nestedworld.nestedworld.database.updater.base.EntityUpdater;
import com.nestedworld.nestedworld.events.http.OnFriendsUpdatedEvent;
import com.nestedworld.nestedworld.network.http.models.response.users.friend.FriendsResponse;

import org.greenrobot.eventbus.EventBus;

import retrofit2.Call;
import retrofit2.Response;

public class FriendsUpdater extends EntityUpdater<FriendsResponse> {

    /*
    ** Life cycle
     */
    @NonNull
    @Override
    public Call<FriendsResponse> getRequest() {
        return getApi().getFriends();
    }

    @Override
    public void updateEntity(@NonNull final Response<FriendsResponse> response) {
        //Delete old entity
        Friend.deleteAll(Friend.class);

        //Update foreign key
        for (Friend friend : response.body().friends) {
            friend.fkfuser = friend.info.save();
        }

        //Save entity
        Friend.saveInTx(response.body().friends);

        //Send event
        EventBus.getDefault().post(new OnFriendsUpdatedEvent());
    }
}

@@ -151,16 +151,73 @@
 Friend;%0A
+import com.nestedworld.nestedworld.database.models.User;%0A
 import c
@@ -276,24 +276,24 @@
 ityUpdater;%0A
-
 import com.n
@@ -443,16 +443,45 @@
 esponse;
+%0Aimport com.orm.query.Select;
 %0A%0Aimport
@@ -516,16 +516,40 @@
 ntBus;%0A%0A
+import java.util.List;%0A%0A
 import r
@@ -950,39 +950,281 @@
     
+List%3C
 Friend
-.deleteAll(Friend.class);
+%3E oldFriend = Select.from(Friend.class).list();%0A        for (Friend friend : oldFriend) %7B%0A            User friendInfo = friend.info();%0A            if (friendInfo != null) %7B%0A                friendInfo.delete();%0A            %7D%0A            friend.delete();%0A        %7D
 %0A%0A  

fix zombi entity