Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
dbartek committed Dec 19, 2024
2 parents b38860d + df1d065 commit 23a1c43
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 133 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ name: Python application

on:
push:
branches: [ "main" ]
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

Expand Down Expand Up @@ -37,3 +38,9 @@ jobs:
- name: Test with pytest
run: |
pytest
- name: Verify docker compose version
run: |
docker compose version
- name: Start app using Docker Compose
run: |
docker compose up -d
106 changes: 62 additions & 44 deletions backend/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import sys
import os
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from db.db import db_connect, db_insert_user, db_update_user, db_delete_user, db_get_user_by_username, db_insert_snippet, db_update_snippet, db_delete_snippet, db_get_snippet_by_id, db_filter_snippets, db_insert_comment, User, Comment, Snippet
from db.db import db_connect, db_insert_user, db_update_user, db_delete_user, db_get_user_by_username, db_insert_snippet, db_update_snippet, db_delete_snippet, db_get_snippet_by_id, db_filter_snippets, db_insert_comment, db_get_comments_by_snippet_id, db_insert_project, db_get_project_by_id, db_update_project, db_insert_user_project, db_get_all_comments, db_get_all_projects, db_get_user_projects_by_project_id, User, Comment, Snippet, Project, UserProject
app = Flask(__name__)
CORS(app) # Enable CORS

Expand Down Expand Up @@ -142,49 +142,67 @@ def get_comments(snippet_id):
return jsonify([]), 200
return jsonify(comments[snippet_id]), 200

# @app.route('/projects', methods=['POST'])
# def create_project():
# conn = db_connect()
# project = Project(
# id=request.json.get('id'),
# name=request.json.get('name'),
# description=request.json.get('description')
# )
# project_id = db_insert_project(conn, project)
# if project_id:
# return jsonify({'id': project_id, 'name': project.name, 'description': project.description}), 201
# else:
# return jsonify({'error': 'Failed to create project'}), 500

# @app.route('/projects/<project_id>', methods=['GET'])
# def get_project(project_id):
# conn = db_connect()
# project = db_get_project_by_id(conn, project_id)
# if project:
# return jsonify({'id': project.id, 'name': project.name, 'description': project.description})
# else:
# return jsonify({'error': 'Project not found'}), 404

# @app.route('/projects/<project_id>', methods=['PUT'])
# def update_project(project_id):
# conn = db_connect()
# project = Project(
# id=project_id,
# name=request.json.get('name'),
# description=request.json.get('description')
# )
# db_update_project(conn, project)
# return jsonify({'id': project.id, 'name': project.name, 'description': project.description})

# @app.route('/projects/<project_id>/members', methods=['POST'])
# def add_project_member(project_id):
# conn = db_connect()
# project_member = ProjectMember(
# project_id=project_id,
# user_id=request.json.get('id')
# )
# db_insert_project_member(conn, project_member)
# return jsonify({'project_id': project_id, 'user_id': project_member.user_id}), 201
@app.route('/comments', methods=['GET'])
def get_all_comments():
conn = db_connect()
comments = db_get_all_comments(conn)
return jsonify([{'id': comment.id, 'snippet_id': comment.snippet_id, 'user_name': comment.user_name, 'content': comment.content, 'created_at': comment.created_at} for comment in comments])

@app.route('/projects', methods=['POST'])
def create_project():
conn = db_connect()
project = Project(
id=None, # ID will be generated by the database
name=request.json.get('name'),
description=request.json.get('description')
)
project_id = db_insert_project(conn, project)
if project_id:
return jsonify({'id': project_id, 'name': project.name, 'description': project.description}), 201
else:
return jsonify({'error': 'Failed to create project'}), 500

@app.route('/projects/<project_id>', methods=['PUT'])
def update_project(project_id):
conn = db_connect()
project = Project(
id=project_id,
name=request.json.get('name'),
description=request.json.get('description')
)
db_update_project(conn, project)
return jsonify({'id': project.id, 'name': project.name, 'description': project.description})

@app.route('/projects/<project_id>', methods=['GET'])
def get_project(project_id):
conn = db_connect()
project = db_get_project_by_id(conn, project_id)
if project:
return jsonify({'id': project.id, 'name': project.name, 'description': project.description})
else:
return jsonify({'error': 'Project not found'}), 404

@app.route('/projects', methods=['GET'])
def get_all_projects():
conn = db_connect()
projects = db_get_all_projects(conn)
return jsonify([{'id': project.id, 'name': project.name, 'description': project.description} for project in projects])

@app.route('/projects/<project_id>/members', methods=['POST'])
def add_project_member(project_id):
conn = db_connect()
project_member = UserProject(
user_name=request.json.get('user_name'),
project_id=project_id
)
db_insert_user_project(conn, project_member)
return jsonify({'project_id': project_id, 'user_name': project_member.user_name}), 201

@app.route('/projects/<project_id>/members', methods=['GET'])
def get_project_members(project_id):
conn = db_connect()
project_members = db_get_user_projects_by_project_id(conn, project_id)
return jsonify([{'user_name': member.user_name, 'project_id': member.project_id} for member in project_members])

if __name__ == '__main__':
app.run(debug=True, port=55555)
59 changes: 29 additions & 30 deletions backend/app/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import sys
import os
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from db.db import db_connect, db_insert_user, db_update_user, db_delete_user, db_get_user_by_username, db_insert_snippet, db_update_snippet, db_delete_snippet, db_get_snippet_by_id, db_filter_snippets, db_insert_comment, User, Comment, Snippet
from db.db import db_connect, db_insert_user, db_update_user, db_delete_user, db_get_user_by_username, db_insert_snippet, db_update_snippet, db_delete_snippet, db_get_snippet_by_id, db_filter_snippets, db_insert_comment, db_insert_project, User, Comment, Snippet, Project, UserProject

@pytest.fixture
def client():
Expand Down Expand Up @@ -136,35 +136,34 @@ def test_get_comments(client):
assert response.status_code == 200
assert isinstance(response.json, list)

# def test_create_project(client):
# response = client.post('/projects', json={
# 'id': 'project1',
# 'name': 'Project One',
# 'description': 'A sample project'
# })
# assert response.status_code == 201
# assert response.json['name'] == 'Project One'
def test_create_project(client):
response = client.post('/projects', json={
'name': 'Project One',
'description': 'A sample project'
})
assert response.status_code == 201
assert response.json['name'] == 'Project One'

# def test_get_project(client):
# conn = db_connect()
# project = Project('project1', 'Project One', 'A sample project')
# db_insert_project(conn, project)
# response = client.get('/projects/project1')
# assert response.status_code == 200
# assert response.json['name'] == 'Project One'
def test_get_project(client):
conn = db_connect()
project = Project(None, 'Project One', 'A sample project')
project_id = db_insert_project(conn, project)
response = client.get(f'/projects/{project_id}')
assert response.status_code == 200
assert response.json['name'] == 'Project One'

# def test_update_project(client):
# conn = db_connect()
# project = Project('project1', 'Project One', 'A sample project')
# db_insert_project(conn, project)
# response = client.put('/projects/project1', json={'name': 'Updated Project One'})
# assert response.status_code == 200
# assert response.json['name'] == 'Updated Project One'
def test_update_project(client):
conn = db_connect()
project = Project(None, 'Project One', 'A sample project')
project_id = db_insert_project(conn, project)
response = client.put(f'/projects/{project_id}', json={'name': 'Updated Project One'})
assert response.status_code == 200
assert response.json['name'] == 'Updated Project One'

# def test_add_project_member(client):
# conn = db_connect()
# project = Project('project1', 'Project One', 'A sample project')
# db_insert_project(conn, project)
# response = client.post('/projects/project1/members', json={'id': 'member1'})
# assert response.status_code == 201
# assert 'member1' in response.json['user_id']
def test_add_project_member(client):
conn = db_connect()
project = Project(None, 'Project One', 'A sample project')
project_id = db_insert_project(conn, project)
response = client.post(f'/projects/{project_id}/members', json={'user_name': 'johndoe'})
assert response.status_code == 201
assert response.json['user_name'] == 'johndoe'
55 changes: 0 additions & 55 deletions backend/app/test_data.py

This file was deleted.

48 changes: 48 additions & 0 deletions backend/db/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,20 @@ def db_get_comments_by_snippet_id(conn, snippet_id):
print(f"Error fetching comments: {e}")
return []

def db_get_all_comments(conn):
try:
with conn.cursor() as cursor:
select_query = sql.SQL("""
SELECT id, snippet_id, user_name, content, created_at
FROM comments
""")
cursor.execute(select_query)
comments_data = cursor.fetchall()
return [Comment(*comment) for comment in comments_data]
except Exception as e:
print(f"Error fetching comments: {e}")
return []

def db_insert_project(conn, project):
try:
with conn.cursor() as cursor:
Expand Down Expand Up @@ -306,6 +320,40 @@ def db_delete_project(conn, project_id):
print(f"Error deleting project: {e}")
conn.rollback()

def db_get_all_projects(conn):
try:
with conn.cursor() as cursor:
select_query = sql.SQL("""
SELECT id, name, description
FROM projects
""")
cursor.execute(select_query)
projects_data = cursor.fetchall()
return [Project(*project) for project in projects_data]
except Exception as e:
print(f"Error fetching projects: {e}")
return []

def db_get_project_by_id(conn, project_id):
try:
with conn.cursor() as cursor:
select_query = sql.SQL("""
SELECT id, name, description
FROM projects
WHERE id = %s
""")
cursor.execute(select_query, (project_id,))
project_data = cursor.fetchone()
if project_data:
project = Project(*project_data)
return project
else:
print("Project not found")
return None
except Exception as e:
print(f"Error fetching project: {e}")
return None

def db_insert_user_project(conn, user_project):
try:
with conn.cursor() as cursor:
Expand Down
6 changes: 6 additions & 0 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Flask==2.0.1
werkzeug==2.0.1
flask-swagger-ui==4.11.1
pytest==8.3.4
uvicorn==0.34.0
flask-cors==3.0.10
4 changes: 2 additions & 2 deletions frontend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Use the official Node.js 14 image as the base image for the frontend build stage
FROM node:14.17.0 as frontend
FROM node:20 as frontend

# Set the working directory inside the container to /app
WORKDIR /app
Expand All @@ -8,7 +8,7 @@ WORKDIR /app
COPY package*.json ./

# Install the dependencies specified in package.json
RUN npm ci
RUN npm install

# Copy the rest of the application code to the working directory
COPY . .
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ werkzeug==2.0.1
flask-swagger-ui==4.11.1
pytest==8.3.4
psycopg2==2.9.3
flask-cors==3.0.10
flask-cors==3.0.10
requests

0 comments on commit 23a1c43

Please sign in to comment.