-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from fioreale/develop
Develop
- Loading branch information
Showing
28 changed files
with
1,925 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Python files | ||
__pycache__/ | ||
*.pyc | ||
*.pyo | ||
*.pyd | ||
|
||
# Dependencies | ||
venv/ | ||
env/ | ||
env/ | ||
*.env | ||
*.venv | ||
*.env.bak | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
dist/ | ||
build/ | ||
*.egg-info/ | ||
.idea/ | ||
.vscode/ | ||
*.egg | ||
*.egg-info/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Python files | ||
__pycache__/ | ||
*.pyc | ||
*.pyo | ||
*.pyd | ||
|
||
# Dependencies | ||
venv/ | ||
env/ | ||
env/ | ||
*.env | ||
*.venv | ||
*.env.bak | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
dist/ | ||
build/ | ||
*.egg-info/ | ||
.idea/ | ||
.vscode/ | ||
*.egg | ||
*.egg-info/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from typing import List | ||
from pydantic import BaseModel, validator | ||
|
||
|
||
class Serie(BaseModel): | ||
reps: str | ||
carico: str | ||
|
||
|
||
class Esercizio(BaseModel): | ||
name: str | ||
serie: List[Serie] | ||
|
||
|
||
class Scheda(BaseModel): | ||
name: str | ||
esercizi: List[Esercizio] | ||
|
||
|
||
class Workout(BaseModel): | ||
name: str | ||
schede: List[Scheda] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
from pymongo import MongoClient | ||
from ..api.models.workout import Workout, Scheda | ||
import logging | ||
import json | ||
|
||
client = MongoClient( | ||
host="ferretdb.internal", | ||
port=27017, | ||
username="username", | ||
password="password", | ||
authMechanism="PLAIN", | ||
) | ||
db = client["myfitappdb"] | ||
collection = db["workouts"] | ||
|
||
logging.basicConfig(filename="app.log", level=logging.ERROR) | ||
|
||
|
||
def getWorkoutDB(name: str): | ||
try: | ||
return collection.find_one({"name": name}) | ||
|
||
except Exception as e: | ||
logging.error(f"Failed to get item {name}: {e}") | ||
return None | ||
|
||
|
||
def getWorkoutList(): | ||
try: | ||
return [doc["name"] for doc in collection.find({}, {"name": 1})] | ||
|
||
except Exception as e: | ||
logging.error(f"Failed to get workout list: {e}") | ||
return None | ||
|
||
|
||
def loadWorkoutDB(workout: Workout): | ||
workout_data = json.dumps(workout.dict()) | ||
try: | ||
if getWorkoutDB(workout.name) is None: | ||
collection.insert_one({"name": workout.name, "data": workout_data}) | ||
|
||
else: | ||
collection.update_one( | ||
{"name": workout.name}, {"$set": {"data": workout_data}} | ||
) | ||
|
||
return True | ||
except Exception as e: | ||
logging.error(f"Failed to write item {workout.name}: {e}") | ||
return False | ||
|
||
|
||
def updateWorkout(name: str, scheda: Scheda): | ||
try: | ||
workout_dump = getWorkoutDB(name) | ||
|
||
if workout_dump is not None: | ||
workout_dict = json.loads(workout_dump.get("data")) | ||
workout_data = Workout(**workout_dict) | ||
|
||
for idx, s in enumerate(workout_data.schede): | ||
if s.name == scheda.name: | ||
workout_data.schede[idx] = scheda | ||
|
||
if loadWorkoutDB(workout_data): | ||
return True | ||
else: | ||
logging.error( | ||
f"Failed to update workout {name}: Cannot write new one on DB!" | ||
) | ||
return False | ||
else: | ||
logging.error(f"Failed to update workout {name}: It does not exist!") | ||
return False | ||
|
||
except Exception as e: | ||
logging.error(f"Failed to update workout {name}: {e}") | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
from fastapi import FastAPI, HTTPException | ||
from fastapi.exceptions import RequestValidationError | ||
from fastapi.responses import JSONResponse | ||
from fastapi.staticfiles import StaticFiles | ||
from pydantic import ValidationError | ||
|
||
from .api.models.workout import Workout, Scheda | ||
from .db.workout_db import getWorkoutDB, getWorkoutList, loadWorkoutDB, updateWorkout | ||
|
||
import uvicorn | ||
|
||
import json | ||
|
||
app = FastAPI() | ||
|
||
|
||
@app.exception_handler(RequestValidationError) | ||
async def validation_exception_handler(request, exc): | ||
return JSONResponse( | ||
status_code=400, | ||
content={"message": "Data validation failed", "detail": exc.errors()}, | ||
) | ||
|
||
|
||
@app.get("/workout/{name}") | ||
async def get_workout(name: str): | ||
if not name: | ||
raise HTTPException(status_code=400, detail="Name parameter cannot be empty") | ||
|
||
try: | ||
workout_db = getWorkoutDB(name) | ||
|
||
if workout_db is not None: | ||
workout_dict = json.loads(workout_db.get("data")) | ||
|
||
workout_data = Workout(**workout_dict) | ||
|
||
return workout_data | ||
else: | ||
raise HTTPException(status_code=400, detail="No workout to load") | ||
except ValidationError as e: | ||
raise RequestValidationError(errors=e.errors()) | ||
|
||
|
||
@app.get("/workout") | ||
async def get_workout_list(): | ||
try: | ||
workout_list_data = getWorkoutList() | ||
|
||
return workout_list_data | ||
except ValidationError as e: | ||
raise RequestValidationError(errors=e.errors()) | ||
|
||
|
||
@app.post("/workout") | ||
async def load_workout(workout: Workout): | ||
try: | ||
if loadWorkoutDB(workout): | ||
return JSONResponse( | ||
status_code=200, content={"message": "Workout upload successful"} | ||
) | ||
else: | ||
return JSONResponse( | ||
status_code=500, content={"message": "Cannot upload workout"} | ||
) | ||
except ValidationError as e: | ||
raise RequestValidationError(errors=e.errors()) | ||
|
||
|
||
@app.patch("/workout/{name}") | ||
async def update_workout(name: str, scheda: Scheda): | ||
try: | ||
if updateWorkout(name, scheda): | ||
return JSONResponse( | ||
status_code=200, content={"message": "Workout upload successful"} | ||
) | ||
else: | ||
return JSONResponse( | ||
status_code=500, content={"message": "Cannot upload workout"} | ||
) | ||
except ValidationError as e: | ||
raise RequestValidationError(errors=e.errors()) | ||
|
||
|
||
app.mount("/", StaticFiles(directory="app/public", html=True)) | ||
|
||
if __name__ == "__main__": | ||
uvicorn.run(app, host="0.0.0.0", port=8000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
body { | ||
font-family: 'Nunito', sans-serif; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
.top-link { | ||
transition: all .25s ease-in-out; | ||
position: fixed; | ||
bottom: 0; | ||
right: 0; | ||
display: inline-flex; | ||
cursor: pointer; | ||
align-items: center; | ||
justify-content: center; | ||
margin: 0 1em 1em 0; | ||
border-radius: 50%; | ||
padding: .25em; | ||
width: 40px; | ||
height: 40px; | ||
background-color: #F8F8F8; | ||
} | ||
|
||
.show { | ||
visibility: visible; | ||
opacity: 1; | ||
} | ||
|
||
.hide { | ||
visibility: hidden; | ||
opacity: 0; | ||
} | ||
|
||
svg { | ||
fill: #000; | ||
width: 30px; | ||
height: 30px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
function changeWorkout() { | ||
var list_workouts = document.querySelectorAll(".listWorkouts"); | ||
|
||
list_workouts.forEach((workout_button) => { | ||
workout_button.onclick = (event) => { | ||
// from FillPage.js | ||
fillPage(event.target.textContent); | ||
$("#modalAddWorkout").modal("hide"); | ||
}; | ||
}); | ||
} |
Oops, something went wrong.