From 02ef939fbd680e88a2b6f0d8c6b088cbb9634d19 Mon Sep 17 00:00:00 2001 From: Antoine C Date: Mon, 26 Jun 2023 14:30:27 +0200 Subject: [PATCH 1/3] add pymongo to requirements --- backend/requirements.txt | 1 + 1 file changed, 1 insertion(+) 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 From 810cf333dca98772080983d1b13427b7a889c583 Mon Sep 17 00:00:00 2001 From: Antoine C Date: Mon, 26 Jun 2023 14:31:18 +0200 Subject: [PATCH 2/3] connect to mongodb container from flask app --- backend/database/__init__.py | 23 ++++++++++++++++++ .../__pycache__/__init__.cpython-310.pyc | Bin 0 -> 657 bytes docker-compose-dev.yml | 1 + 3 files changed, 24 insertions(+) create mode 100644 backend/database/__init__.py create mode 100644 backend/database/__pycache__/__init__.cpython-310.pyc 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 0000000000000000000000000000000000000000..7d78ce0b57767164bb2b0b10085ed57772c96cfd GIT binary patch literal 657 zcmYjP&5qMB5cW@+q-l!KN=S%PE=csi=8ga@>{gsw5Fie@SZO>Ii^MLryV~VQ_a%4) zUddNZyaFf2>4A=&CBDp|!B?cf3 zm84;kVkZ_Jt(0?0ZF$2`C_C{%_uS~a(EnTf#=eqOHw%)YeSJ1MSt#3E2gIWlsl^5cCcWa~8 zlQ?MvIZ5c43E9Lx$3~ p-rKwPy=STHk#>a3EF_g5V_t_cGSx%2$(1PFN{{Synq#ghO literal 0 HcmV?d00001 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: From 38733851fe09f0b1c60d84403bd359423b70fa8d Mon Sep 17 00:00:00 2001 From: Antoine C Date: Mon, 26 Jun 2023 14:32:51 +0200 Subject: [PATCH 3/3] update get_article controller with mongodb --- backend/controllers/articleController.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) 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