Skip to content

Commit

Permalink
Merge pull request #15 from CSE2102-Fall24/develop
Browse files Browse the repository at this point in the history
Incomplete-MIlestone6
  • Loading branch information
eoa21004 authored Nov 8, 2024
2 parents 745e7e0 + 5dcad0a commit 05d1f56
Show file tree
Hide file tree
Showing 17 changed files with 712 additions and 64 deletions.
109 changes: 52 additions & 57 deletions .github/workflows/ms5yml
Original file line number Diff line number Diff line change
@@ -1,59 +1,54 @@
name: ms5API Testing

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

name: API CI/CD
# This is a trigger for any push to the repo, and tells github when the actions have to be run
# Every time we do a push, the action will be executed
# The actions should be run only when there is a push from main and develop
on:
push:
branches:
- main
- develop

#Tells github actions what to execute when trigger condition is met
jobs:
api-testing:
runs-on: ubuntu-latest

# Each job runs in parallel
tests: #This is the job name

# runs-on indicates which GitHub "Runners" will run this CICD pipeline
# For all CSE-2102 repos, just use the following line as is
runs-on: self-hosted
# This next block allows you to run this ENTIRE job on different python versions
strategy:
matrix:
#python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
python-version: ["3.8"]

# Steps are run in sequence in this job. If one step fails, the entire job fails.
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


# Use this next line to pull your repo into the Docker container running the job
- uses: actions/checkout@v3
# This block sets up the python version
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
# Pylint is a static code analysis tool. Use this block as is to install pylint
# in the Docker container running the job
- name: Install pylint
run: |
python -m pip install --upgrade pip
pip install pylint
pip install requests

- 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: Run Pytest
run: |
pytest
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# SQLite database
#/Backend/testData.db
#*.db

#Log files for some reason lol
*.log
logs/
178 changes: 178 additions & 0 deletions Backend/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
"""
This module provides functions for HTTP communication and
route functions
"""
from flask import Flask, request, jsonify
from database_files import databasefunctions
app = Flask(__name__)

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

@app.route("/add_newuser", methods=["POST"])
def add_newuser():
"""
Adds new user to database
"""
data = request.get_json()
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

@app.route("/add_newpet", methods=["POST"])
def add_newpet():
"""
Adds new pet to database
"""
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("location"), data.get("photo_path"))

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



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

@app.route("/remove_user", methods=["DELETE"])
def removeuser():
"""
Remove user from database
"""
data = request.get_json()
userid = data.get("user_id")
roweffected = databasefunctions.remove_user(userid)

return (f"{roweffected}, row was deleted in user table"), 200


@app.route("/remove_pet", methods=["DELETE"])
def removepet():
"""
Remove pet from database
"""
data = request.get_json()
petid = data.get("pet_id")
roweffected = databasefunctions.remove_pet(petid)

return (f"{roweffected}, row was deleted in pet table"), 200


@app.route("/remove_favorite", methods=["DELETE"])
def removefavorite():
"""
Remove favorite from database
"""
data = request.get_json()
roweffected = databasefunctions.remove_favorite(data.get("user_id"),data.get("pet_id"))

return (f"{roweffected}, row was deleted in the favorite table"), 200

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



@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():
"""
fetch pet from database
"""
data = request.get_json()
petid = data.get("pet_id")
users_row = databasefunctions.fetch_pet_by_id(petid)

return jsonify(users_row), 200


@app.route("/fetch_user", methods=["POST"])
def fetch_user():
"""
fetch user from database
"""
data = request.get_json()
userid = data.get("user_id")
users_row = databasefunctions.fetch_user_by_id(userid)

return jsonify(users_row), 200


@app.route("/fetch_favorited_pets", methods=["POST"])
def fetch_favorite_pet():
"""
fetches all pets favorited by a user
"""
data = request.get_json()
userid = data.get("user_id")
favorited_pets = databasefunctions.fetch_favorites_by_user(userid)

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", debug=True)
Binary file not shown.
Loading

0 comments on commit 05d1f56

Please sign in to comment.