Skip to content

Commit

Permalink
Merge pull request #20 from CSE2102-Fall24/Milestone6-ericfeaturebranch
Browse files Browse the repository at this point in the history
Complete API/TEST/DOCKER
  • Loading branch information
eoa21004 authored Nov 8, 2024
2 parents 713cf4d + 2b2ba22 commit 6ef0ee2
Show file tree
Hide file tree
Showing 11 changed files with 331 additions and 137 deletions.
90 changes: 36 additions & 54 deletions .github/workflows/ms5yml
Original file line number Diff line number Diff line change
@@ -1,59 +1,41 @@
name: ms5API Testing
name: API CI/CD

on:
push: #we want only the milstone5
branches: [Milestone-5_feature_branch]
pull_request:
branches: [Milestone-5_feature_branch]
on:
push:
branches:
- Milestone6-ericfeaturebranch

jobs:
api-testing:
runs-on: ubuntu-latest
tests:
runs-on: self-hosted
strategy:
matrix:
#python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
python-version: ["3.8"]

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Python 3
uses: actions/setup-python@v2
with:
python-version: '3.8'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flask flask_cors flasgger pylint pytest

# Run pylint
- name: Run pylint
run: |
pylint ./backend/*.py
continue-on-error: false # Set to true to see warnings

#docker image build
- name: Build API Docker Image
run: |
docker build -t api-image -f Dockerfile .

#run docker container
- name: Start API Container
run: |
docker run -d -p 5000:5000 --name api-container api-image

#running main.py
- name: Start API Service
run: |
docker exec api-container python main.py

#test_main.py
- name: Run test_main.py
run: |
docker exec api-container pytest test_main.py --maxfail=1 --disable-warnings

#cut the docker
- name: Stop and Remove API Container
run: |
docker stop api-container
docker rm api-container


- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Install pylint
run: |
python -m pip install --upgrade pip
pip install pylint

- name: Install pytest
run: |
python -m pip install --upgrade pip
pip install pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi

- name: Run Api
run: |
python3 Backend/app.py &

- name: pytest
run: |
pytest

78 changes: 64 additions & 14 deletions Backend/main.py → Backend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
This module provides functions for HTTP communication and
route functions
"""
import sqlite3
import databasefunctions
from flask import Flask, request, jsonify
from petfuc import get_random_pet
from userfuc import add_user_faviorte, remove_user_faviorte, replace_user_location
from database_files import databasefunctions
app = Flask(__name__)

# -----------------------------------------------------------------------------------------
Expand All @@ -17,9 +14,11 @@ def add_newuser():
Adds new user to database
"""
data = request.get_json()
newuser_id = databasefunctions.insert_user(data.get("name"), data.get("age"), data.get("location"))
newuser_id = databasefunctions.insert_user(data.get("username"),data.get("password"),
data.get("name"), data.get("age"),
data.get("location"))

return jsonify({'user_id': newuser_id}), 200
return jsonify({'user_id': newuser_id}), 200

@app.route("/add_newpet", methods=["POST"])
def add_newpet():
Expand All @@ -28,21 +27,29 @@ def add_newpet():
"""
data = request.get_json()
newpet_id = databasefunctions.insert_pet(data.get("name"), data.get("age"),
data.get("gender"), data.get("breed"), data.get("type"),
data.get("gender"), data.get("breed"), data.get("type"),
data.get("location"), data.get("photo_path"))

return jsonify({'pet_id': newpet_id}), 200
return jsonify({'pet_id': newpet_id}), 200


@app.route("/add_newfavorite", methods=["POST"])
def add_newfavorite():
"""
Adds new user favorite to database
INSERT INTO pets (name, age, gender, breed, type, location, photo_path)
VALUES
('Bella', 3, 'female', 'Golden Retriever', 'dog', 'New York', 'images/Chuck.jpg'),
('Max', 5, 'male', 'Maine Coon', 'cat', 'Boston', 'images/gunner.jpg'),
('Luna', 2, 'female', 'Siamese', 'cat', 'Chicago', 'images/luna.jpg'),
('Charlie', 4, 'male', 'Beagle', 'dog', 'Los Angeles', 'images/lizzie_izzie.jpg'),
('Daisy', 1, 'female', 'Labrador Retriever', 'dog', 'Miami', 'images/monster.jpg');
"""
data = request.get_json()
newfavorite_id = databasefunctions.insert_favorite(data.get("user_id"), data.get("pet_id"))

return jsonify({'faviorte_id': newfavorite_id}), 200
return jsonify({'faviorte_id': newfavorite_id}), 200



Expand All @@ -66,7 +73,7 @@ def removepet():
Remove pet from database
"""
data = request.get_json()
petid = data.get("petid")
petid = data.get("pet_id")
roweffected = databasefunctions.remove_pet(petid)

return (f"{roweffected}, row was deleted in pet table"), 200
Expand All @@ -85,6 +92,25 @@ def removefavorite():
# ---------------------------------------------------------------------



@app.route("/fetch_userid", methods=["POST"])
def fetch_userid():
"""
fetch userid associated with username and password
"""
data = request.get_json()
username = data.get("username")
password = data.get("password")


userid = databasefunctions.fetch_userid_from_username_and_password(username, password)

return jsonify(userid), 200





@app.route("/fetch_pet", methods=["POST"])
def fetch_pet():
"""
Expand All @@ -94,7 +120,7 @@ def fetch_pet():
petid = data.get("pet_id")
users_row = databasefunctions.fetch_pet_by_id(petid)

return jsonify(users_row), 200
return jsonify(users_row), 200


@app.route("/fetch_user", methods=["POST"])
Expand All @@ -106,7 +132,7 @@ def fetch_user():
userid = data.get("user_id")
users_row = databasefunctions.fetch_user_by_id(userid)

return jsonify(users_row), 200
return jsonify(users_row), 200


@app.route("/fetch_favorited_pets", methods=["POST"])
Expand All @@ -118,11 +144,35 @@ def fetch_favorite_pet():
userid = data.get("user_id")
favorited_pets = databasefunctions.fetch_favorites_by_user(userid)

return jsonify(favorited_pets), 200
return jsonify(favorited_pets), 200


# ---------------------------------------------------------------

@app.route("/fetch_allusers", methods=["POST"])
def fetch_allusers():
"""
fetches all users in database
"""

allusers = databasefunctions.fetch_all_users()
return jsonify(allusers), 200


@app.route("/fetch_allpets", methods=["POST"])
def fetch_allpets():
"""
fetches all pets in database
"""

allpets = databasefunctions.fetch_all_pets()
return jsonify(allpets), 200







if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=True)
app.run(host="0.0.0.0", debug=True)
Binary file not shown.
Loading

0 comments on commit 6ef0ee2

Please sign in to comment.