mirror of
https://github.com/hay-kot/mealie.git
synced 2025-08-22 06:23:34 -07:00
refactor/ router endpoint
This commit is contained in:
parent
b6111afe69
commit
cc35a4be19
16 changed files with 202 additions and 165 deletions
|
@ -27,6 +27,17 @@ const apiReq = {
|
|||
return response;
|
||||
},
|
||||
|
||||
put: async function (url, data) {
|
||||
let response = await axios.put(url, data).catch(function (error) {
|
||||
if (error.response) {
|
||||
processResponse(error.response);
|
||||
return response;
|
||||
} else return;
|
||||
});
|
||||
// processResponse(response);
|
||||
return response;
|
||||
},
|
||||
|
||||
get: async function (url, data) {
|
||||
let response = await axios.get(url, data).catch(function (error) {
|
||||
if (error.response) {
|
||||
|
|
|
@ -6,11 +6,11 @@ const backupBase = baseURL + "backups/";
|
|||
|
||||
const backupURLs = {
|
||||
// Backup
|
||||
available: `${backupBase}available/`,
|
||||
createBackup: `${backupBase}export/database/`,
|
||||
importBackup: (fileName) => `${backupBase}${fileName}/import/`,
|
||||
deleteBackup: (fileName) => `${backupBase}${fileName}/delete/`,
|
||||
downloadBackup: (fileName) => `${backupBase}${fileName}/download/`,
|
||||
available: `${backupBase}available`,
|
||||
createBackup: `${backupBase}export/database`,
|
||||
importBackup: (fileName) => `${backupBase}${fileName}/import`,
|
||||
deleteBackup: (fileName) => `${backupBase}${fileName}/delete`,
|
||||
downloadBackup: (fileName) => `${backupBase}${fileName}/download`,
|
||||
};
|
||||
|
||||
export default {
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
import { baseURL } from "./api-utils";
|
||||
import { apiReq } from "./api-utils";
|
||||
|
||||
const categoryBase = baseURL + "/recipes/categories";
|
||||
const prefix = baseURL + "/recipes/categories";
|
||||
|
||||
const categoryURLs = {
|
||||
get_all: `${categoryBase}/all/`,
|
||||
get_category: (category) => `${categoryBase}/${category}/`,
|
||||
get_all: `${prefix}/all`,
|
||||
get_category: (category) => `${prefix}/${category}`,
|
||||
delete_category: (category) => `${prefix}/${category}`,
|
||||
};
|
||||
|
||||
export default {
|
||||
|
@ -17,4 +18,8 @@ export default {
|
|||
let response = await apiReq.get(categoryURLs.get_category(category));
|
||||
return response.data;
|
||||
},
|
||||
async delete(category) {
|
||||
let response = await apiReq.delete(categoryURLs.delete_category(category));
|
||||
return response.data;
|
||||
},
|
||||
};
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
import { baseURL } from "./api-utils";
|
||||
import { apiReq } from "./api-utils";
|
||||
|
||||
const mealplanBase = baseURL + "meal-plan/";
|
||||
const prefix = baseURL + "meal-plans/";
|
||||
|
||||
const mealPlanURLs = {
|
||||
// Meals
|
||||
create: `${mealplanBase}create/`,
|
||||
today: `${mealplanBase}today/`,
|
||||
thisWeek: `${mealplanBase}this-week/`,
|
||||
all: `${mealplanBase}all/`,
|
||||
delete: (planID) => `${mealplanBase}${planID}/delete/`,
|
||||
update: (planID) => `${mealplanBase}${planID}/update/`,
|
||||
all: `${prefix}all`,
|
||||
create: `${prefix}create`,
|
||||
thisWeek: `${prefix}this-week`,
|
||||
update: (planID) => `${prefix}${planID}`,
|
||||
delete: (planID) => `${prefix}${planID}`,
|
||||
today: `${prefix}today`,
|
||||
};
|
||||
|
||||
export default {
|
||||
|
@ -40,7 +40,7 @@ export default {
|
|||
},
|
||||
|
||||
async update(id, body) {
|
||||
let response = await apiReq.post(mealPlanURLs.update(id), body);
|
||||
let response = await apiReq.put(mealPlanURLs.update(id), body);
|
||||
return response;
|
||||
},
|
||||
};
|
||||
|
|
|
@ -4,18 +4,17 @@ import { store } from "../store/store";
|
|||
import { router } from "../main";
|
||||
import qs from "qs";
|
||||
|
||||
const recipeBase = baseURL + "recipe/";
|
||||
const prefix = baseURL + "recipes/";
|
||||
|
||||
const recipeURLs = {
|
||||
// Recipes
|
||||
allRecipes: baseURL + "all-recipes/",
|
||||
recipe: (slug) => recipeBase + slug + "/",
|
||||
recipeImage: (slug) => recipeBase + "image/" + slug + "/",
|
||||
createByURL: recipeBase + "create-url/",
|
||||
create: recipeBase + "create/",
|
||||
updateImage: (slug) => `${recipeBase}${slug}/update/image/`,
|
||||
update: (slug) => `${recipeBase}${slug}/update/`,
|
||||
delete: (slug) => `${recipeBase}${slug}/delete/`,
|
||||
allRecipes: baseURL + "recipes",
|
||||
create: prefix + "create",
|
||||
createByURL: prefix + "create-url",
|
||||
recipe: (slug) => prefix + slug,
|
||||
update: (slug) => prefix + slug,
|
||||
delete: (slug) => prefix + slug,
|
||||
recipeImage: (slug) => `${prefix}${slug}/image`,
|
||||
updateImage: (slug) => `${prefix}${slug}/image`,
|
||||
};
|
||||
|
||||
export default {
|
||||
|
@ -43,7 +42,7 @@ export default {
|
|||
fd.append("image", fileObject);
|
||||
fd.append("extension", fileObject.name.split(".").pop());
|
||||
|
||||
let response = apiReq.post(recipeURLs.updateImage(recipeSlug), fd);
|
||||
let response = apiReq.put(recipeURLs.updateImage(recipeSlug), fd);
|
||||
|
||||
return response;
|
||||
},
|
||||
|
@ -51,7 +50,7 @@ export default {
|
|||
async update(data) {
|
||||
const recipeSlug = data.slug;
|
||||
|
||||
let response = await apiReq.post(recipeURLs.update(recipeSlug), data);
|
||||
let response = await apiReq.put(recipeURLs.update(recipeSlug), data);
|
||||
store.dispatch("requestRecentRecipes");
|
||||
return response.data;
|
||||
},
|
||||
|
|
|
@ -5,8 +5,8 @@ const settingsBase = baseURL + "site-settings/";
|
|||
|
||||
const settingsURLs = {
|
||||
siteSettings: `${settingsBase}`,
|
||||
updateSiteSettings: `${settingsBase}update/`,
|
||||
testWebhooks: `${settingsBase}webhooks/test/`,
|
||||
updateSiteSettings: `${settingsBase}`,
|
||||
testWebhooks: `${settingsBase}webhooks/test`,
|
||||
};
|
||||
|
||||
export default {
|
||||
|
@ -21,7 +21,7 @@ export default {
|
|||
},
|
||||
|
||||
async update(body) {
|
||||
let response = await apiReq.post(settingsURLs.updateSiteSettings, body);
|
||||
let response = await apiReq.put(settingsURLs.updateSiteSettings, body);
|
||||
return response.data;
|
||||
},
|
||||
};
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
import { baseURL } from "./api-utils";
|
||||
import { apiReq } from "./api-utils";
|
||||
|
||||
const themesBase = baseURL + "site-settings/";
|
||||
const prefix = baseURL + "themes/";
|
||||
|
||||
const settingsURLs = {
|
||||
allThemes: `${themesBase}themes/`,
|
||||
specificTheme: (themeName) => `${themesBase}themes/${themeName}/`,
|
||||
createTheme: `${themesBase}themes/create/`,
|
||||
updateTheme: (themeName) => `${themesBase}themes/${themeName}/update/`,
|
||||
deleteTheme: (themeName) => `${themesBase}themes/${themeName}/delete/`,
|
||||
allThemes: `${baseURL}themes`,
|
||||
specificTheme: (themeName) => `${prefix}themes/${themeName}`,
|
||||
createTheme: `${prefix}themes/create`,
|
||||
updateTheme: (themeName) => `${prefix}themes/${themeName}`,
|
||||
deleteTheme: (themeName) => `${prefix}themes/${themeName}`,
|
||||
};
|
||||
|
||||
export default {
|
||||
|
@ -32,7 +32,7 @@ export default {
|
|||
name: themeName,
|
||||
colors: colors,
|
||||
};
|
||||
let response = await apiReq.post(settingsURLs.updateTheme(themeName), body);
|
||||
let response = await apiReq.put(settingsURLs.updateTheme(themeName), body);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@ from utils.snackbar import SnackResponse
|
|||
router = APIRouter(prefix="/api/backups", tags=["Import / Export"])
|
||||
|
||||
|
||||
@router.get("/available/", response_model=Imports)
|
||||
@router.get("/available", response_model=Imports)
|
||||
def available_imports():
|
||||
"""Returns a list of avaiable .zip files for import into Mealie."""
|
||||
imports = []
|
||||
|
@ -31,7 +31,7 @@ def available_imports():
|
|||
return Imports(imports=imports, templates=templates)
|
||||
|
||||
|
||||
@router.post("/export/database/", status_code=201)
|
||||
@router.post("/export/database", status_code=201)
|
||||
def export_database(data: BackupJob, db: Session = Depends(generate_session)):
|
||||
"""Generates a backup of the recipe database in json format."""
|
||||
export_path = backup_all(
|
||||
|
@ -51,7 +51,7 @@ def export_database(data: BackupJob, db: Session = Depends(generate_session)):
|
|||
)
|
||||
|
||||
|
||||
@router.post("/upload/")
|
||||
@router.post("/upload")
|
||||
def upload_backup_zipfile(archive: UploadFile = File(...)):
|
||||
""" Upload a .zip File to later be imported into Mealie """
|
||||
dest = BACKUP_DIR.joinpath(archive.filename)
|
||||
|
@ -65,7 +65,7 @@ def upload_backup_zipfile(archive: UploadFile = File(...)):
|
|||
return SnackResponse.error("Failure uploading file")
|
||||
|
||||
|
||||
@router.get("/{file_name}/download/")
|
||||
@router.get("/{file_name}/download")
|
||||
def upload_nextcloud_zipfile(file_name: str):
|
||||
""" Upload a .zip File to later be imported into Mealie """
|
||||
file = BACKUP_DIR.joinpath(file_name)
|
||||
|
@ -78,7 +78,7 @@ def upload_nextcloud_zipfile(file_name: str):
|
|||
return SnackResponse.error("No File Found")
|
||||
|
||||
|
||||
@router.post("/{file_name}/import/", status_code=200)
|
||||
@router.post("/{file_name}/import", status_code=200)
|
||||
def import_database(
|
||||
file_name: str, import_data: ImportJob, db: Session = Depends(generate_session)
|
||||
):
|
||||
|
@ -98,7 +98,7 @@ def import_database(
|
|||
return imported
|
||||
|
||||
|
||||
@router.delete("/{file_name}/delete/", tags=["Import / Export"], status_code=200)
|
||||
@router.delete("/{file_name}/delete", tags=["Import / Export"], status_code=200)
|
||||
def delete_backup(file_name: str):
|
||||
""" Removes a database backup from the file system """
|
||||
|
||||
|
|
|
@ -6,17 +6,17 @@ from services.meal_services import MealPlan
|
|||
from sqlalchemy.orm.session import Session
|
||||
from utils.snackbar import SnackResponse
|
||||
|
||||
router = APIRouter(prefix="/api/meal-plan", tags=["Meal Plan"])
|
||||
router = APIRouter(prefix="/api/meal-plans", tags=["Meal Plan"])
|
||||
|
||||
|
||||
@router.get("/all/", response_model=List[MealPlan])
|
||||
@router.get("/all", response_model=List[MealPlan])
|
||||
def get_all_meals(db: Session = Depends(generate_session)):
|
||||
""" Returns a list of all available Meal Plan """
|
||||
|
||||
return MealPlan.get_all(db)
|
||||
|
||||
|
||||
@router.post("/create/")
|
||||
@router.post("/create")
|
||||
def set_meal_plan(data: MealPlan, db: Session = Depends(generate_session)):
|
||||
""" Creates a meal plan database entry """
|
||||
data.process_meals(db)
|
||||
|
@ -30,7 +30,14 @@ def set_meal_plan(data: MealPlan, db: Session = Depends(generate_session)):
|
|||
return SnackResponse.success("Mealplan Created")
|
||||
|
||||
|
||||
@router.post("/{plan_id}/update/")
|
||||
@router.get("/this-week", response_model=MealPlan)
|
||||
def get_this_week(db: Session = Depends(generate_session)):
|
||||
""" Returns the meal plan data for this week """
|
||||
|
||||
return MealPlan.this_week(db)
|
||||
|
||||
|
||||
@router.put("/{plan_id}")
|
||||
def update_meal_plan(
|
||||
plan_id: str, meal_plan: MealPlan, db: Session = Depends(generate_session)
|
||||
):
|
||||
|
@ -49,7 +56,7 @@ def update_meal_plan(
|
|||
return SnackResponse.success("Mealplan Updated")
|
||||
|
||||
|
||||
@router.delete("/{plan_id}/delete/")
|
||||
@router.delete("/{plan_id}")
|
||||
def delete_meal_plan(plan_id, db: Session = Depends(generate_session)):
|
||||
""" Removes a meal plan from the database """
|
||||
|
||||
|
@ -66,10 +73,3 @@ def get_today(db: Session = Depends(generate_session)):
|
|||
"""
|
||||
|
||||
return MealPlan.today(db)
|
||||
|
||||
|
||||
@router.get("/this-week/", response_model=MealPlan)
|
||||
def get_this_week(db: Session = Depends(generate_session)):
|
||||
""" Returns the meal plan data for this week """
|
||||
|
||||
return MealPlan.this_week(db)
|
||||
|
|
|
@ -14,7 +14,7 @@ from utils.snackbar import SnackResponse
|
|||
router = APIRouter(prefix="/api/migrations", tags=["Migration"])
|
||||
|
||||
|
||||
@router.get("/", response_model=List[Migrations])
|
||||
@router.get("", response_model=List[Migrations])
|
||||
def get_avaiable_nextcloud_imports():
|
||||
""" Returns a list of avaiable directories that can be imported into Mealie """
|
||||
response_data = []
|
||||
|
@ -35,7 +35,7 @@ def get_avaiable_nextcloud_imports():
|
|||
return response_data
|
||||
|
||||
|
||||
@router.post("/{type}/{file_name}/import/")
|
||||
@router.post("/{type}/{file_name}/import")
|
||||
def import_nextcloud_directory(
|
||||
type: str, file_name: str, db: Session = Depends(generate_session)
|
||||
):
|
||||
|
@ -49,7 +49,7 @@ def import_nextcloud_directory(
|
|||
return SnackResponse.error("Incorrect Migration Type Selected")
|
||||
|
||||
|
||||
@router.delete("/{type}/{file_name}/delete/")
|
||||
@router.delete("/{type}/{file_name}/delete")
|
||||
def delete_migration_data(type: str, file_name: str):
|
||||
""" Removes migration data from the file system """
|
||||
|
||||
|
@ -65,7 +65,7 @@ def delete_migration_data(type: str, file_name: str):
|
|||
return SnackResponse.info(f"Migration Data Remove: {remove_path.absolute()}")
|
||||
|
||||
|
||||
@router.post("/{type}/upload/")
|
||||
@router.post("/{type}/upload")
|
||||
def upload_nextcloud_zipfile(type: str, archive: UploadFile = File(...)):
|
||||
""" Upload a .zip File to later be imported into Mealie """
|
||||
dir = MIGRATION_DIR.joinpath(type)
|
||||
|
|
|
@ -6,10 +6,10 @@ from fastapi import APIRouter, Depends, Query
|
|||
from models.recipe_models import AllRecipeRequest
|
||||
from sqlalchemy.orm.session import Session
|
||||
|
||||
router = APIRouter(tags=["Recipes"])
|
||||
router = APIRouter(tags=["Query All Recipes"])
|
||||
|
||||
|
||||
@router.get("/api/all-recipes/")
|
||||
@router.get("/api/recipes")
|
||||
def get_all_recipes(
|
||||
keys: Optional[List[str]] = Query(...),
|
||||
num: Optional[int] = 100,
|
||||
|
@ -42,7 +42,7 @@ def get_all_recipes(
|
|||
return db.recipes.get_all_limit_columns(session, keys, limit=num)
|
||||
|
||||
|
||||
@router.post("/api/all-recipes/")
|
||||
@router.post("/api/recipes")
|
||||
def get_all_recipes_post(
|
||||
body: AllRecipeRequest, session: Session = Depends(generate_session)
|
||||
):
|
||||
|
|
|
@ -5,18 +5,18 @@ from models.category_models import RecipeCategoryResponse
|
|||
from sqlalchemy.orm.session import Session
|
||||
|
||||
router = APIRouter(
|
||||
prefix="/api/recipes/categories",
|
||||
tags=["Recipes"],
|
||||
prefix="/api/categories",
|
||||
tags=["Recipe Categories"],
|
||||
)
|
||||
|
||||
|
||||
@router.get("/all/")
|
||||
@router.get("")
|
||||
async def get_all_recipe_categories(session: Session = Depends(generate_session)):
|
||||
""" Returns a list of available categories in the database """
|
||||
return db.categories.get_all_limit_columns(session, ["slug", "name"])
|
||||
|
||||
|
||||
@router.get("/{category}/", response_model=RecipeCategoryResponse)
|
||||
@router.get("/{category}", response_model=RecipeCategoryResponse)
|
||||
def get_all_recipes_by_category(
|
||||
category: str, session: Session = Depends(generate_session)
|
||||
):
|
||||
|
@ -24,10 +24,12 @@ def get_all_recipes_by_category(
|
|||
return db.categories.get(session, category)
|
||||
|
||||
|
||||
@router.delete("/{category}/")
|
||||
@router.delete("/{category}")
|
||||
async def delete_recipe_category(
|
||||
category: str, session: Session = Depends(generate_session)
|
||||
):
|
||||
""" Removes a recipe category from the database """
|
||||
""" Removes a recipe category from the database. Deleting a
|
||||
category does not impact a recipe. The category will be removed
|
||||
from any recipes that contain it """
|
||||
|
||||
db.categories.delete(session, category)
|
||||
|
|
|
@ -9,28 +9,20 @@ from sqlalchemy.orm.session import Session
|
|||
from utils.snackbar import SnackResponse
|
||||
|
||||
router = APIRouter(
|
||||
prefix="/api/recipe",
|
||||
tags=["Recipes"],
|
||||
prefix="/api/recipes",
|
||||
tags=["Recipe CRUD"],
|
||||
)
|
||||
|
||||
|
||||
@router.get("/{recipe_slug}/", response_model=Recipe)
|
||||
def get_recipe(recipe_slug: str, db: Session = Depends(generate_session)):
|
||||
""" Takes in a recipe slug, returns all data for a recipe """
|
||||
recipe = Recipe.get_by_slug(db, recipe_slug)
|
||||
@router.post("/create", status_code=201, response_model=str)
|
||||
def create_from_json(data: Recipe, db: Session = Depends(generate_session)) -> str:
|
||||
""" Takes in a JSON string and loads data into the database as a new entry"""
|
||||
new_recipe_slug = data.save_to_db(db)
|
||||
|
||||
return recipe
|
||||
return new_recipe_slug
|
||||
|
||||
|
||||
@router.get("/image/{recipe_slug}/")
|
||||
def get_recipe_img(recipe_slug: str):
|
||||
""" Takes in a recipe slug, returns the static image """
|
||||
recipe_image = read_image(recipe_slug)
|
||||
|
||||
return FileResponse(recipe_image)
|
||||
|
||||
|
||||
@router.post("/create-url/", status_code=201, response_model=str)
|
||||
@router.post("/create-url", status_code=201, response_model=str)
|
||||
def parse_recipe_url(url: RecipeURLIn, db: Session = Depends(generate_session)):
|
||||
""" Takes in a URL and attempts to scrape data and load it into the database """
|
||||
|
||||
|
@ -40,26 +32,15 @@ def parse_recipe_url(url: RecipeURLIn, db: Session = Depends(generate_session)):
|
|||
return recipe.slug
|
||||
|
||||
|
||||
@router.post("/create/")
|
||||
def create_from_json(data: Recipe, db: Session = Depends(generate_session)) -> str:
|
||||
""" Takes in a JSON string and loads data into the database as a new entry"""
|
||||
new_recipe_slug = data.save_to_db(db)
|
||||
@router.get("/{recipe_slug}", response_model=Recipe)
|
||||
def get_recipe(recipe_slug: str, db: Session = Depends(generate_session)):
|
||||
""" Takes in a recipe slug, returns all data for a recipe """
|
||||
recipe = Recipe.get_by_slug(db, recipe_slug)
|
||||
|
||||
return new_recipe_slug
|
||||
return recipe
|
||||
|
||||
|
||||
@router.post("/{recipe_slug}/update/image/")
|
||||
def update_recipe_image(
|
||||
recipe_slug: str, image: bytes = File(...), extension: str = Form(...)
|
||||
):
|
||||
""" Removes an existing image and replaces it with the incoming file. """
|
||||
response = write_image(recipe_slug, image, extension)
|
||||
Recipe.update_image(recipe_slug, extension)
|
||||
|
||||
return response
|
||||
|
||||
|
||||
@router.post("/{recipe_slug}/update/")
|
||||
@router.put("/{recipe_slug}")
|
||||
def update_recipe(
|
||||
recipe_slug: str, data: Recipe, db: Session = Depends(generate_session)
|
||||
):
|
||||
|
@ -70,7 +51,7 @@ def update_recipe(
|
|||
return new_slug
|
||||
|
||||
|
||||
@router.delete("/{recipe_slug}/delete/")
|
||||
@router.delete("/{recipe_slug}")
|
||||
def delete_recipe(recipe_slug: str, db: Session = Depends(generate_session)):
|
||||
""" Deletes a recipe by slug """
|
||||
|
||||
|
@ -82,3 +63,22 @@ def delete_recipe(recipe_slug: str, db: Session = Depends(generate_session)):
|
|||
)
|
||||
|
||||
return SnackResponse.success("Recipe Deleted")
|
||||
|
||||
|
||||
@router.get("/{recipe_slug}/image")
|
||||
def get_recipe_img(recipe_slug: str):
|
||||
""" Takes in a recipe slug, returns the static image """
|
||||
recipe_image = read_image(recipe_slug)
|
||||
|
||||
return FileResponse(recipe_image)
|
||||
|
||||
|
||||
@router.put("/{recipe_slug}/image")
|
||||
def update_recipe_image(
|
||||
recipe_slug: str, image: bytes = File(...), extension: str = Form(...)
|
||||
):
|
||||
""" Removes an existing image and replaces it with the incoming file. """
|
||||
response = write_image(recipe_slug, image, extension)
|
||||
Recipe.update_image(recipe_slug, extension)
|
||||
|
||||
return response
|
||||
|
|
|
@ -7,17 +7,26 @@ router = APIRouter(tags=["Recipes"])
|
|||
|
||||
router = APIRouter(
|
||||
prefix="/api/recipes/tags",
|
||||
tags=["Recipes"],
|
||||
tags=["Recipe Tags"],
|
||||
)
|
||||
|
||||
|
||||
@router.get("/all/")
|
||||
@router.get("/all")
|
||||
async def get_all_recipe_tags(session: Session = Depends(generate_session)):
|
||||
""" Returns a list of available tags in the database """
|
||||
return db.tags.get_all_primary_keys(session)
|
||||
|
||||
|
||||
@router.get("/{tag}/")
|
||||
@router.get("/{tag}")
|
||||
def get_all_recipes_by_tag(tag: str, session: Session = Depends(generate_session)):
|
||||
""" Returns a list of recipes associated with the provided tag. """
|
||||
return db.tags.get(session, tag)
|
||||
|
||||
|
||||
@router.delete("/{tag}")
|
||||
async def delete_recipe_tag(tag: str, session: Session = Depends(generate_session)):
|
||||
"""Removes a recipe tag from the database. Deleting a
|
||||
tag does not impact a recipe. The tag will be removed
|
||||
from any recipes that contain it"""
|
||||
|
||||
db.tags.delete(session, tag)
|
||||
|
|
|
@ -1,27 +1,28 @@
|
|||
from db.db_setup import generate_session
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from services.settings_services import SiteSettings, SiteTheme
|
||||
from fastapi import APIRouter, Depends
|
||||
from services.settings_services import SiteSettings
|
||||
from sqlalchemy.orm.session import Session
|
||||
from utils.post_webhooks import post_webhooks
|
||||
from utils.snackbar import SnackResponse
|
||||
|
||||
router = APIRouter(prefix="/api/site-settings", tags=["Settings"])
|
||||
|
||||
@router.get("/")
|
||||
|
||||
@router.get("")
|
||||
def get_main_settings(db: Session = Depends(generate_session)):
|
||||
""" Returns basic site settings """
|
||||
|
||||
return SiteSettings.get_site_settings(db)
|
||||
|
||||
|
||||
@router.post("/webhooks/test/")
|
||||
@router.post("/webhooks/test")
|
||||
def test_webhooks():
|
||||
""" Run the function to test your webhooks """
|
||||
|
||||
return post_webhooks()
|
||||
|
||||
|
||||
@router.post("/update/")
|
||||
@router.put("")
|
||||
def update_settings(data: SiteSettings, db: Session = Depends(generate_session)):
|
||||
""" Returns Site Settings """
|
||||
data.update(db)
|
||||
|
@ -35,58 +36,4 @@ def update_settings(data: SiteSettings, db: Session = Depends(generate_session))
|
|||
return SnackResponse.success("Settings Updated")
|
||||
|
||||
|
||||
@router.get("/themes/", tags=["Themes"])
|
||||
def get_all_themes(db: Session = Depends(generate_session)):
|
||||
""" Returns all site themes """
|
||||
|
||||
return SiteTheme.get_all(db)
|
||||
|
||||
|
||||
@router.get("/themes/{theme_name}/", tags=["Themes"])
|
||||
def get_single_theme(theme_name: str, db: Session = Depends(generate_session)):
|
||||
""" Returns a named theme """
|
||||
return SiteTheme.get_by_name(db, theme_name)
|
||||
|
||||
|
||||
@router.post("/themes/create/", tags=["Themes"])
|
||||
def create_theme(data: SiteTheme, db: Session = Depends(generate_session)):
|
||||
""" Creates a site color theme database entry """
|
||||
data.save_to_db(db)
|
||||
# try:
|
||||
# data.save_to_db()
|
||||
# except:
|
||||
# raise HTTPException(
|
||||
# status_code=400, detail=SnackResponse.error("Unable to Save Theme")
|
||||
# )
|
||||
|
||||
return SnackResponse.success("Theme Saved")
|
||||
|
||||
|
||||
@router.post("/themes/{theme_name}/update/", tags=["Themes"])
|
||||
def update_theme(
|
||||
theme_name: str, data: SiteTheme, db: Session = Depends(generate_session)
|
||||
):
|
||||
""" Update a theme database entry """
|
||||
data.update_document(db)
|
||||
|
||||
# try:
|
||||
# except:
|
||||
# raise HTTPException(
|
||||
# status_code=400, detail=SnackResponse.error("Unable to Update Theme")
|
||||
# )
|
||||
|
||||
return SnackResponse.success("Theme Updated")
|
||||
|
||||
|
||||
@router.delete("/themes/{theme_name}/delete/", tags=["Themes"])
|
||||
def delete_theme(theme_name: str, db: Session = Depends(generate_session)):
|
||||
""" Deletes theme from the database """
|
||||
SiteTheme.delete_theme(db, theme_name)
|
||||
# try:
|
||||
# SiteTheme.delete_theme(theme_name)
|
||||
# except:
|
||||
# raise HTTPException(
|
||||
# status_code=400, detail=SnackResponse.error("Unable to Delete Theme")
|
||||
# )
|
||||
|
||||
return SnackResponse.success("Theme Deleted")
|
||||
|
|
64
mealie/routes/theme_routes.py
Normal file
64
mealie/routes/theme_routes.py
Normal file
|
@ -0,0 +1,64 @@
|
|||
from db.db_setup import generate_session
|
||||
from fastapi import APIRouter, Depends
|
||||
from services.settings_services import SiteTheme
|
||||
from sqlalchemy.orm.session import Session
|
||||
from utils.snackbar import SnackResponse
|
||||
|
||||
router = APIRouter(prefix="/api", tags=["Themes"])
|
||||
|
||||
|
||||
@router.get("/themes")
|
||||
def get_all_themes(db: Session = Depends(generate_session)):
|
||||
""" Returns all site themes """
|
||||
|
||||
return SiteTheme.get_all(db)
|
||||
|
||||
|
||||
@router.post("/themes/create")
|
||||
def create_theme(data: SiteTheme, db: Session = Depends(generate_session)):
|
||||
""" Creates a site color theme database entry """
|
||||
data.save_to_db(db)
|
||||
# try:
|
||||
# data.save_to_db()
|
||||
# except:
|
||||
# raise HTTPException(
|
||||
# status_code=400, detail=SnackResponse.error("Unable to Save Theme")
|
||||
# )
|
||||
|
||||
return SnackResponse.success("Theme Saved")
|
||||
|
||||
|
||||
@router.get("/themes/{theme_name}")
|
||||
def get_single_theme(theme_name: str, db: Session = Depends(generate_session)):
|
||||
""" Returns a named theme """
|
||||
return SiteTheme.get_by_name(db, theme_name)
|
||||
|
||||
|
||||
@router.put("/themes/{theme_name}")
|
||||
def update_theme(
|
||||
theme_name: str, data: SiteTheme, db: Session = Depends(generate_session)
|
||||
):
|
||||
""" Update a theme database entry """
|
||||
data.update_document(db)
|
||||
|
||||
# try:
|
||||
# except:
|
||||
# raise HTTPException(
|
||||
# status_code=400, detail=SnackResponse.error("Unable to Update Theme")
|
||||
# )
|
||||
|
||||
return SnackResponse.success("Theme Updated")
|
||||
|
||||
|
||||
@router.delete("/themes/{theme_name}")
|
||||
def delete_theme(theme_name: str, db: Session = Depends(generate_session)):
|
||||
""" Deletes theme from the database """
|
||||
SiteTheme.delete_theme(db, theme_name)
|
||||
# try:
|
||||
# SiteTheme.delete_theme(theme_name)
|
||||
# except:
|
||||
# raise HTTPException(
|
||||
# status_code=400, detail=SnackResponse.error("Unable to Delete Theme")
|
||||
# )
|
||||
|
||||
return SnackResponse.success("Theme Deleted")
|
Loading…
Add table
Add a link
Reference in a new issue