package jdit.testing.dao;

import jdit.testing.domain.Division;
import jdit.testing.domain.Player;
import org.skife.jdbi.v2.*;
import org.skife.jdbi.v2.util.LongMapper;

import java.util.List;

/**
 * Date: 2/21/15
 * Time: 12:38 AM
 *
 * @author Artem Prigoda
 */
public class TeamDao {

    private final DBI dbi;

    public TeamDao(DBI dbi) {
        this.dbi = dbi;
    }

    public long addTeam(final String name, final Division division, final List<Player> players) {
        return dbi.inTransaction(new TransactionCallback<Long>() {
            @Override
            public Long inTransaction(Handle h, TransactionStatus status) throws Exception {
                Long teamId = h.createStatement("insert into teams(name, division) values (:name, :division)")
                        .bind("name", name)
                        .bind("division", division.name())
                        .executeAndReturnGeneratedKeys(LongMapper.FIRST)
                        .first();
                for (Player player : players) {
                    Long playerId = h.createStatement(
                            "insert into players(first_name, last_name, number, position) values (?,?,?,?)")
                            .bind(0, player.firstName)
                            .bind(1, player.lastName)
                            .bind(2, player.number)
                            .bind(3, player.position.code)
                            .executeAndReturnGeneratedKeys(LongMapper.FIRST)
                            .first();
                    h.insert("insert into roster(team_id, player_id) values (?,?)", teamId, playerId);
                }
                return teamId;
            }
        });
    }
}

@@ -651,32 +651,210 @@
 ption %7B%0A        
+        return addTeam0(h, name, division, players);%0A            %7D%0A        %7D);%0A    %7D%0A%0A    private Long addTeam0(Handle h, String name, Division division, List%3CPlayer%3E players) %7B%0A
         Long tea
@@ -952,32 +952,24 @@
             
-        
 .bind(%22name%22
@@ -988,32 +988,24 @@
             
- 
-       
 .bind(%22divis
@@ -1027,24 +1027,16 @@
 name())%0A
-        
         
@@ -1104,32 +1104,24 @@
             
-      
-  
 .first();%0A  
@@ -1114,32 +1114,24 @@
   .first();%0A
-        
         for 
@@ -1170,24 +1170,16 @@
         
- 
-       
 Long pla
@@ -1225,24 +1225,16 @@
         
-   
-     
 %22insert 
@@ -1322,32 +1322,24 @@
             
-        
 .bind(0, pla
@@ -1369,32 +1369,24 @@
             
-       
- 
 .bind(1, pla
@@ -1415,32 +1415,24 @@
             
-        
 .bind(2, pla
@@ -1459,32 +1459,24 @@
             
-  
-      
 .bind(3, pla
@@ -1510,32 +1510,24 @@
             
-    
-    
 .executeAndR
@@ -1579,32 +1579,24 @@
             
-       
- 
 .first();%0A  
@@ -1593,24 +1593,16 @@
 irst();%0A
-        
         
@@ -1700,30 +1700,18 @@
     
- 
-       %7D%0A    
+%7D%0A
         
     
@@ -1702,28 +1702,24 @@
   %7D%0A        
-    
 return teamI
@@ -1725,34 +1725,8 @@
 Id;%0A
-            %7D%0A        %7D);%0A
     

Move transactional adding to a separate method