var Youtube = require("youtube-api");
var GLOBAL_CONFIG = require("../../global-config.js");

module.exports = function(Search) {
  Search.afterCreate = function(next) {
    Youtube.authenticate({
      type: "key",
      key: GLOBAL_CONFIG.youtubeApiKey
    });

    var pthis = this;

    Youtube.search.list({q:this.keyword, part:"snippet", maxResults:30, type:"video"}, function(err, data) {
      var youtubeUrl, orderNumber=0;

      data.items.forEach(function(item) {
        youtubeUrl = "http://www.youtube.com/watch?v=" + item.id.videoId;
        pthis.musics.create({title:item.snippet.title, type:"youtube", url:youtubeUrl, order:orderNumber}, function(err, obj){
        });
        orderNumber++;
      });

      next();
    });
  };
};

@@ -136,18 +136,14 @@
 rch.
-afterCreat
+youtub
 e = 
@@ -151,16 +151,23 @@
 unction(
+pthis, 
 next) %7B%0A
@@ -264,31 +264,8 @@
 );%0A%0A
-    var pthis = this;%0A%0A
     
@@ -722,12 +722,113 @@
 %7D);%0A  %7D;
+%0A%0A  Search.afterCreate = function(next) %7B%0A    var pthis = this;%0A    Search.youtube(pthis, next);%0A  %7D;
 %0A%7D;%0A

Refactor search API.