<commit_before>Writing Your First Application
==============================

.. note:: If you're not yet familiar with the fundamental architecture of a
          Fabric network, you may want to visit the :doc:`blockchain` and
          :doc:`build_network` documentation prior to continuing.

In this section we'll be looking at two sample applications to see how Fabric
apps work. These apps (and the smart contract they use) -- collectively known as
"Fabcar" -- involve querying a ledger that has been pre-populated with cars and
then updating that ledger to add new cars and to change car owners.

We’ll go through three principle steps:

  **1. Setting up a development environment.** Our application needs a network to
  interact with, so we'll download one stripped down to just the components we need
  to run queries and updates:

  .. image:: images/AppConceptsOverview.png

  **2. Learning the parameters of the sample smart contract our app will use.** Our
  smart contract contains various functions that allow us to interact with the ledger
  in different ways. We’ll go in and inspect that smart contract to learn about the
  functions our applications will be using.

  **3. Developing the applications to be able to query and update Fabric records.**
  We'll get into the app code itself (our apps have been written in Javascript) and
  manually manipulate the variables to run different kinds of queries and updates.

After completing this tutorial you should have a basic understanding of how
an application is programmed in conjunction with a smart contract to interact
with the ledger on a Fabric network. All in about 30 minutes.

First, let's get our network and our apps...

Setting up a Development Environment
------------------------------------

Visit the :doc:`prereqs` page and ensure you have the necessary dependencies
installed on your machine.

Also, let's remove any containers and chaincode (smart contracts run on
chaincode) images you might have running to ensure you don't have a port
conflict or Docker issue.

First, remove the containers:

.. code:: bash

  docker rm -f $(docker ps -aq)

Next, delete the chaincode image:

.. code:: bash

  docker rmi dev-peer0.org1.example.com-fabcar-1.0

Now that your machine is set up, navigate to a directory where you want the
samples downloaded to and issue the clone command:

.. code:: bash

  git clone https://github.com/hyperledger/fabric-samples.git

This command will create a directory, ``fabric-samples`` (in which multiple
samples and tutorial apps have now been downloaded), and a specific
subdirectory, ``fabcar``.

Enter that directory:

.. code:: bash

  cd fabric-samples/fabcar

Now take a look at what's inside your ``fabcar`` directory.

.. code:: bash

  ls

You should see the following:

.. code:: bash

   chaincode	invoke.js	network		package.json	query.js	startFabric.sh

Use the ``startFabric.sh`` script to launch the network. This script downloads
and extracts the Fabric docker images, so it will take a few minutes to
complete:

.. code:: bash

  ./startFabric.sh

One last thing. We need to install the SDK (software development kit) node
modules:

.. code:: bash

  npm install
<commit_msg>[FAB-6307] Fix WYFA to reflect new folder<commit_after>Writing Your First Application
==============================

.. note:: If you're not yet familiar with the fundamental architecture of a
          Fabric network, you may want to visit the :doc:`blockchain` and
          :doc:`build_network` documentation prior to continuing.

In this section we'll be looking at two sample applications to see how Fabric
apps work. These apps (and the smart contract they use) -- collectively known as
"Fabcar" -- involve querying a ledger that has been pre-populated with cars and
then updating that ledger to add new cars and to change car owners.

We’ll go through three principle steps:

  **1. Setting up a development environment.** Our application needs a network to
  interact with, so we'll download one stripped down to just the components we need
  to run queries and updates:

  .. image:: images/AppConceptsOverview.png

  **2. Learning the parameters of the sample smart contract our app will use.** Our
  smart contract contains various functions that allow us to interact with the ledger
  in different ways. We’ll go in and inspect that smart contract to learn about the
  functions our applications will be using.

  **3. Developing the applications to be able to query and update Fabric records.**
  We'll get into the app code itself (our apps have been written in Javascript) and
  manually manipulate the variables to run different kinds of queries and updates.

After completing this tutorial you should have a basic understanding of how
an application is programmed in conjunction with a smart contract to interact
with the ledger on a Fabric network. All in about 30 minutes.

First, let's get our network and our apps...

Setting up a Development Environment
------------------------------------

Visit the :doc:`prereqs` page and ensure you have the necessary dependencies
installed on your machine.

Also, let's remove any containers and chaincode (smart contracts run on
chaincode) images you might have running to ensure you don't have a port
conflict or Docker issue.

First, remove the containers:

.. code:: bash

  docker rm -f $(docker ps -aq)

Next, delete the chaincode image:

.. code:: bash

  docker rmi dev-peer0.org1.example.com-fabcar-1.0

Now that your machine is set up, navigate to a directory where you want the
samples downloaded to and issue the clone command:

.. code:: bash

  git clone https://github.com/hyperledger/fabric-samples.git

This command will create a directory, ``fabric-samples`` (in which multiple
samples and tutorial apps have now been downloaded), and a specific
subdirectory, ``fabcar``.

Enter that directory:

.. code:: bash

  cd fabric-samples/fabcar

Now take a look at what's inside your ``fabcar`` directory.

.. code:: bash

  ls

You should see the following:

.. code:: bash

   creds invoke.js package.json query.js startFabric.sh

Use the ``startFabric.sh`` script to launch the network. This script downloads
and extracts the Fabric docker images, so it will take a few minutes to
complete:

.. code:: bash

  ./startFabric.sh

One last thing. We need to install the SDK (software development kit) node
modules:

.. code:: bash

  npm install

{'fraction_non_alphanumeric': 0.07891050583657587, 'fraction_numerical': 0.003424124513618677, 'mean_word_length': 3.7111436950146626, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2990437', 'n_tokens_mistral': 1710, 'n_tokens_neox': 1584, 'n_words': 915}