コード例 #1
0
 /**
  * Creates a new Apis model from a swagger url.
  * If creation is successful, the browser will be redirected to the 'view' page.
  *
  * @param integer $swaggerId
  *
  * @return mixed
  */
 public function actionCreateapi($swaggerId)
 {
     $swaggerAPI = $this->findModel($swaggerId);
     $myId = \Yii::$app->user->id;
     $query = ObjectFromSwagger::find();
     $query->where(['api_from_swagger' => $swaggerId]);
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     // Check if there is already another API with the same name, that has not be created by me
     $alreadyTakenName = Apis::find();
     $alreadyTakenName->joinWith(['createdBy']);
     $alreadyTakenName->where(['name' => $swaggerAPI->name]);
     $alreadyTakenName->where(['not', 'created_by' => $myId]);
     if ($swaggerAPI->load(Yii::$app->request->post()) && $swaggerAPI->save()) {
         if ($alreadyTakenName->one() === null) {
             $buildFromSwagger = new BuildFromSwagger();
             $buildFromSwagger->setSwaggerAPI($swaggerId);
             if ($buildFromSwagger->BuildAPI()) {
                 // Build all the Objects
                 $buildFromSwagger->BuildObjects();
                 // Notify users who follow me!
                 $myId = \Yii::$app->user->id;
                 $change = new NotifUserHelper();
                 $followersNotified = $change->userChangedCreatedApi($myId);
                 return $this->redirect(['apis/view', 'id' => $buildFromSwagger->getAPIId(), 'followersNotified' => $followersNotified]);
             }
         }
     }
     // Check if there is already an API with same version by me. If there is, display a message.
     $message = Apis::find()->where(['name' => $swaggerAPI->name, 'created_by' => $myId])->one() === null ? '' : 'Overwrite already existing API?';
     $message = $alreadyTakenName->one() === null ? $message : 'This API name is already taken. Another one should be provided!';
     return $this->render('createapi', ['model' => $swaggerAPI, 'dataProvider' => $dataProvider, 'message' => $message]);
 }
コード例 #2
0
 /**
  * Creates a new Apis model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Apis();
     $model->cbs = 0;
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         $myId = \Yii::$app->user->id;
         $change = new NotifUserHelper();
         $followersNotified = null;
         $followersNotified = $change->userChangedCreatedApi($myId);
         // Automatically follow the APIs I create
         $followModel = new FollowUserApi();
         $followModel->follower = $myId;
         $followModel->api = $model->id;
         $followModel->save();
         // Elastic Search Insertion
         $esi = new ElasticSearchPut();
         $api = $this->findModel($model->id);
         $esi->setApi($api);
         $esi->MakeJSON();
         $esi->InsertUpdate();
         return $this->redirect(['view', 'id' => $model->id, 'followersNotified' => $followersNotified]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }