<file_sep><?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity(repositoryClass="App\Repository\CurrencyRepository")
 */

 //create Currency database
class Currency
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    public function getId(): ?int
    {
        return $this->id;
    }
    // validation assert
    /**
     * @ORM\Column(type="text")
     * @Assert\Currency
    **/
    private $name;

    //getters and setters
    public function getName()
    {
        return $this->name;
    }

    public function setName($name)
    {
        $this->name = $name;
    }
}
<file_sep># currency-calculator

		Currency Calcylator -  Readme file
For the needs of our project, we used PHP framework, Symfony 4 and we were programming mostly in PHP and  JavaScript.
Also we used CSS and Bootstrap for stylesheet and twig template engine for our pages.

First of all we built ourController.php for rendering the pages and get used it as our server. After we set the requirements we needed,
we created our routes: Home (for home page), login (for login page), admin (for administrator page) and delete 
(for deleting the currencies from administrator page).

On home page (templates/pages/index.html.twig) we have a form where there are the options of base currency,
amount (if it is empty, a prompt message shows up and reloads the page) and exchange.
After user gives his options and click the calculation button, a JavaScript function make the calculation and send the proper message
on the screen.

On administrator page (templates/pages/admin.html.twig) we have access after log in authentication (templates/pages/login.html.twig),
the administrator has the ability to add and delete a currency for the home page. The currency must be validated.

Furthermore we created a JavaScript file (public/index.js) with a function which makes the calculation of currencies after getting APIs
for them and sends the proper message to home page and also a callback function which deletes from the database and by extension from the
screen, the currencies from administrator page.

Finally, for our database we created an Entity with the use of Composer (src/Enity/Currency.php).
Composer also helped us for the annotations, form creation (for administrator page), validator and authentication
(creation of config/security.yaml where you can find username (administrator) and password for login page).
<file_sep>function currencyCalculation(){
  var base = document.querySelector(".base").value;
  var amount = document.querySelector(".amount").value;
  var exchange = document.querySelector(".exchange").value;
  var request = new XMLHttpRequest();
  // get API for the currencies
  request.open("GET", "https://api.exchangeratesapi.io/latest?base=" + base);
  request.onload = function(){
    var data = JSON.parse(request.responseText);
    var keyNames = Object.keys(data.rates);
    var values = Object.values(data.rates);
    console.log(keyNames);
    if (amount === ""){
      alert("Please fill the 'Amount' box!");
      location.reload();
    }
    if (base === exchange){
      var result = amount;
    } else {
        for (var i = 0; i < keyNames.length; i++){
          if (keyNames[i] === exchange){
          var result = (amount) * (values[i]);
            break;
          }
        }
      }

    document.querySelector(".show-result").innerHTML = "Today, " + data.date + ", " + amount + " " + base + " are equal to " + result + " " + exchange + "!"
  };
  request.send();
}
// callback function for deleting currency
var currencies = document.querySelectorAll(".admin-currencies");
for (var i = 0; i < currencies.length; i++){
  currencies[i].addEventListener("click", function(e){
    if (e.target.className === "btn btn-danger btn-sm"){
      var id = e.target.getAttribute("id");
      var route = `/currency-calculator/public/delete/${id}`;

      var newReq = new XMLHttpRequest();
      newReq.open("DELETE", route);
      newReq.onload = function(){
        location.reload();
      };
      newReq.send();
    }
  });
}
<file_sep><?php
namespace  App\Controller;

use App\Entity\Currency;

//requirements
use Symfony\Component\HttpFoundation\Response;
//use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;


//create ourController
class ourController extends Controller{

  /**
    * @Route("/", name="home")
    * @Method({"GET"})
  **/

  public function index(){
    $currencies = $this->getDoctrine()->getRepository(Currency::class)->findAll();

    return $this->render('pages/index.html.twig', array("currencies" => $currencies));
  }
  /**
    * @Route("/login", name="login")
    * @Method({"GET", "POST"})
    */

  public function login(AuthenticationUtils $authenticationUtils){

      // get the login error if there is one
      $error = $authenticationUtils->getLastAuthenticationError();

      // last username entered by the user
      $lastUsername = $authenticationUtils->getLastUsername();

      return $this->render('pages/login.html.twig', [
          'last_username' => $lastUsername,
          'error'         => $error,
      ]);
  }

  /**
    * @Route("/admin", name="admin")
    * @Method({"GET", "POST"})
  **/

  public function adminPage(Request $request, ValidatorInterface $validator){
    $currency = new Currency();

    // form creation
    $form = $this->createFormBuilder($currency)
            ->add("name", TextType::class, array("attr" => array("class" => "form-control admin-form")))
            ->add("save", SubmitType::class, array("label" => "Add", "attr" => array("class" => "btn btn-primary btn-add")))
            ->getForm();

      $form->handleRequest($request);

      //get data from the form to database
      if($form->isSubmitted() && $form->isValid()){
        $currency = $form->getData();
        $entityManager = $this->getDoctrine()->getManager();
        $entityManager->persist($currency);
        $entityManager->flush();

        return $this->redirectToRoute("admin");
      }

      $currencies = $this->getDoctrine()->getRepository(Currency::class)->findAll();

       return $this->render('pages/admin.html.twig', array("form" => $form->createView(), "currencies" => $currencies));
  }
  /**
    * @Route("/delete/{id}", name="delete")
    * @Method({"DELETE"})
  **/
  public function delete(Request $request, $id){

    //delete from database
    $currency = $this->getDoctrine()->getRepository(Currency::class)->find($id);
    $entityManager = $this->getDoctrine()->getManager();
    $entityManager->remove($currency);
    $entityManager->flush();

    $response = new Response();
    $response->send();

  }

  /**
    * @Route("/logout", name="logout")
    * @Method({"GET"})
  **/
    
}

?>
