# Migration of TOAR database from Django to FastAPI
 
Gedanken zum Projekt, beziehungsweise, welche Tests gemacht wurden (und welche Tests noch ausstehen):  
https://gitlab.version.fz-juelich.de/toar/toar-db/-/issues/33  
(oder aber auch: https://gitlab.version.fz-juelich.de/toar/toar-db/-/tree/sabine_%2333_testfastAPI/fastAPIproject)  
Besonders in dem Issue habe ich mich bemüht, alles ein bisschen klarer aufzuschreiben.  

## installation guide (soon available in English)
Ich habe eine Test-Datenbank angelegt (noch nicht komplett belegt) und als toardb_dump.sql im Gitlab abgelegt – hier kommt gleich die Anleitung, wie man sie installiert.  

Nach dem Download **und der Installation (siehe unten)** von toardb_fastapi kann man mittels
```
pytest
```
sehen, ob alles funktioniert  
(aus einem unerklärlichen Grund kann die Datenbank beim letzten Test nicht richtig aufgeräumt werden -- Fehler wird noch behoben).  

Wenn man eine App gestartet hat, kann man auch mittels curl die „produktive“ Schnittstelle testen.  
Hierzu findet man Testbeispiele in „production_tests.sh“.
Es fehlt noch das Definieren einer übergeordneten App, so dass man alle Module gleichzeitig abfragen kann (und nicht jeweils mittels uvicorn neu starten muss).

**Nun zur Installation und Einrichtung der Datenbank:**  
Bitte genau nach der Anleitung  
https://gitlab.version.fz-juelich.de/toar/toar-db/-/blob/master/INSTALL_POSTGIS.md  
vorgehen (ich hatte es erfolglos anders versucht).  
Im Anschluss von der Shell:  
```
psql -U toaradmin -d toardb -h localhost -f toardb_dump.sql
```
ausführen.  

**Die Installation von toardb_fastapi geht folgendermaßen:**  
Nach dem Clonen (`git clone https://gitlab.version.fz-juelich.de/schroeder5/toardb_fastapi.git`)  
richtet man sich am besten eine virtuelle Umgebung ein und installiert hier die Requirements, also:  
```
python3 –m venv venv
source venv/bin/activate
pip install –r requirements.txt
```

## Simple database interface: TOAR table data

run as:
```
uvicorn data.data:app --reload
```
after having followed the installation instructions of the TOAR database in the toar-db project.  

For example try:  
get all data of table data (you should **NOT** do this!):  
http://127.0.0.1:8000/data/  

get 4 arbitrary rows of table data:  
http://127.0.0.1:8000/data/?limit=4  

get all data of timeseries with id=2:  
http://127.0.0.1:8000/data/2

upload a file to the table data of the TOAR database: 
(at the moment every data will be uploaded to timeseries with timeseries_id=2 -- hardwired)  
1) file in current directory:  
```
curl -X POST -H 'Content-Type: multipart/form-data; charset=utf-8; boundary=__X_PAW_BOUNDARY__' -F "file=@o3_CO002_2012_2017_v1-0.dat" "http://127.0.0.1:8000/data/"
```
2) file with absolute path:  
```
curl -X POST -H 'Content-Type: multipart/form-data; charset=utf-8; boundary=__X_PAW_BOUNDARY__' -F "file=@/home/sschroeder/fastAPIproject/upload_tests/o3_CO002_2012_2017_v1-0.dat" "http://127.0.0.1:8000/data/"
```

get variable information:  
```
curl "http://127.0.0.1:8000/variables/"
curl "http://127.0.0.1:8000/variables/o3"
```
get stationmeta_core information:  
```
curl "http://127.0.0.1:8000/stationmeta_core/"
curl "http://127.0.0.1:8000/stationmeta_core/China11"
```