diff --git a/backend/controllers/articleController.py b/backend/controllers/articleController.py index 0f049ed..faa35bc 100644 --- a/backend/controllers/articleController.py +++ b/backend/controllers/articleController.py @@ -1,17 +1,23 @@ from flask import jsonify from .dummy_data import * +from database import db + +#https://pymongo.readthedocs.io/en/stable/tutorial.html#getting-a-collection +articles = db["articles"] + +articles.insert_one(dummy_article) +articles.insert_one(dummy_article_2) + # TODO: Implement auth middleware logic to check if user is logged in # TODO: fetch one article from database def get_article(id): - if id == "1": - return jsonify(dummy_article), 200 - elif id == "2": - return jsonify(dummy_article_2), 200 - else: - return "", 404 + article = articles.find_one(filter = {"_id" : id}) + if article is not None: + return jsonify(article), 200 + return "", 404 # TODO: create one article diff --git a/backend/database/__init__.py b/backend/database/__init__.py new file mode 100644 index 0000000..bbe8e39 --- /dev/null +++ b/backend/database/__init__.py @@ -0,0 +1,23 @@ + +from pymongo import MongoClient +from os import getenv + +class MissingEnvironmentVariable(Exception): + pass + +DB_PORT, DB_NAME = getenv("DB_PORT"), getenv("DB_NAME") + +if DB_NAME is None: + raise MissingEnvironmentVariable("DB_NAME is missing") + +if DB_PORT is None: + raise MissingEnvironmentVariable("DB_PORT is missing") +else: + DB_PORT = int(DB_PORT) + print("connecting to port : " ,DB_PORT) + + +client = MongoClient("mongodb", DB_PORT) + +#https://pymongo.readthedocs.io/en/stable/tutorial.html#getting-a-database +db = getattr(client, DB_NAME) \ No newline at end of file diff --git a/backend/database/__pycache__/__init__.cpython-310.pyc b/backend/database/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000..7d78ce0 Binary files /dev/null and b/backend/database/__pycache__/__init__.cpython-310.pyc differ diff --git a/backend/requirements.txt b/backend/requirements.txt index c05d987..ae2a918 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -1 +1,2 @@ flask==2.2.3 +pymongo \ No newline at end of file diff --git a/docker-compose-dev.yml b/docker-compose-dev.yml index a58f743..362b0a4 100644 --- a/docker-compose-dev.yml +++ b/docker-compose-dev.yml @@ -31,6 +31,7 @@ services: - ./backend/middleware:/backend/middleware:ro - ./backend/models:/backend/models:ro - ./backend/routes:/backend/routes:ro + - ./backend/database:/backend/database:ro react-app-dev: depends_on: