/**
  * DELETE /api/favouriteTransactions/{favouriteTransactions}
  * @param FavouriteTransaction $favourite=
  * @return Response
  */
 public function destroy(FavouriteTransaction $favourite)
 {
     try {
         $favourite->delete();
         return response([], Response::HTTP_NO_CONTENT);
     } catch (\Exception $e) {
         //Integrity constraint violation
         if ($e->getCode() === '23000') {
             $message = 'FavouriteTransaction could not be deleted. It is in use.';
         } else {
             $message = 'There was an error';
         }
         return response(['error' => $message, 'status' => Response::HTTP_BAD_REQUEST], Response::HTTP_BAD_REQUEST);
     }
 }
 /**
  * @test
  * @return void
  */
 public function it_can_delete_a_favourite_transaction()
 {
     DB::beginTransaction();
     $this->logInUser();
     $favourite = FavouriteTransaction::first();
     $response = $this->call('DELETE', '/api/favouriteTransactions/' . $favourite->id);
     $this->assertEquals(204, $response->getStatusCode());
     $response = $this->call('DELETE', '/api/favouriteTransactions/' . $favourite->id);
     $this->assertEquals(404, $response->getStatusCode());
     DB::rollBack();
 }
Exemplo n.º 3
0
 private function truncate()
 {
     User::truncate();
     Savings::truncate();
     Budget::truncate();
     Account::truncate();
     Transaction::truncate();
     FavouriteTransaction::truncate();
     SavedFilter::truncate();
     DB::table('budgets_favourite_transactions')->truncate();
     DB::table('budgets_transactions')->truncate();
 }
Exemplo n.º 4
0
 /**
  * Define your route model bindings, pattern filters, etc.
  *
  * @param  \Illuminate\Routing\Router  $router
  * @return void
  */
 public function boot(Router $router)
 {
     parent::boot($router);
     //		$router->model('accounts', Account::class);
     Route::bind('accounts', function ($id) {
         return Account::forCurrentUser()->findOrFail($id);
     });
     Route::bind('budgets', function ($id) {
         return Budget::forCurrentUser()->findOrFail($id);
     });
     Route::bind('transactions', function ($id) {
         return Transaction::forCurrentUser()->findOrFail($id);
     });
     Route::bind('favouriteTransactions', function ($id) {
         return FavouriteTransaction::forCurrentUser()->findOrFail($id);
     });
     Route::bind('savedFilters', function ($id) {
         return SavedFilter::forCurrentUser()->findOrFail($id);
     });
 }
 /**
  * Todo: I could also test switching from type transfer to another type, that the fromAccount and toAccount keys are removed.
  * @test
  * @return void
  */
 public function it_can_update_a_favourite_transfer_transaction()
 {
     DB::beginTransaction();
     $this->logInUser();
     $favourite = FavouriteTransaction::forCurrentUser()->first();
     $response = $this->call('PUT', '/api/favouriteTransactions/' . $favourite->id, ['name' => 'koala', 'type' => 'transfer', 'description' => 'kangaroo', 'merchant' => 'wombat', 'total' => 5, 'from_account_id' => 2, 'to_account_id' => 1, 'budget_ids' => [2, 3]]);
     $content = json_decode($response->getContent(), true);
     //dd($content);
     $this->checkFavouriteTransactionKeysExist($content);
     $this->assertEquals('koala', $content['name']);
     $this->assertEquals('transfer', $content['type']);
     $this->assertEquals('kangaroo', $content['description']);
     $this->assertEquals('wombat', $content['merchant']);
     $this->assertEquals('5', $content['total']);
     $this->assertArrayNotHasKey('account', $content);
     $this->assertEquals(2, $content['fromAccount']['id']);
     $this->assertEquals(1, $content['toAccount']['id']);
     $this->assertEquals(2, $content['budgets'][0]['id']);
     $this->assertEquals(3, $content['budgets'][1]['id']);
     $this->assertEquals(200, $response->getStatusCode());
     DB::rollBack();
 }
 /**
  *
  */
 public function run()
 {
     $users = User::all();
     foreach ($users as $user) {
         foreach ($this->favourites as $favourite) {
             $newFavourite = new FavouriteTransaction(['name' => $favourite['name'], 'type' => $favourite['type'], 'description' => $favourite['description'], 'merchant' => $favourite['merchant'], 'total' => $favourite['total']]);
             $newFavourite->user()->associate($user);
             if ($favourite['type'] === 'transfer') {
                 $newFavourite->fromAccount()->associate(Account::where('user_id', $user->id)->where('name', $favourite['fromAccount'])->first());
                 $newFavourite->toAccount()->associate(Account::where('user_id', $user->id)->where('name', $favourite['toAccount'])->first());
             } else {
                 $newFavourite->account()->associate(Account::where('user_id', $user->id)->where('name', $favourite['account'])->first());
             }
             $newFavourite->save();
             $budgetIds = [];
             foreach ($favourite['budgets'] as $budgetName) {
                 $budgetIds[] = Budget::where('user_id', $user->id)->where('name', $budgetName)->pluck('id');
             }
             $newFavourite->budgets()->attach($budgetIds);
         }
     }
 }