Refactor/conver to controllers (#923)

* add dependency injection for get_repositories

* convert events api to controller

* update generic typing

* add abstract controllers

* update test naming

* migrate admin services to controllers

* add additional admin route tests

* remove print

* add public shared dependencies

* add types

* fix typo

* add static variables for recipe json keys

* add coverage gutters config

* update controller routers

* add generic success response

* add category/tag/tool tests

* add token refresh test

* add coverage utilities

* covert comments to controller

* add todo

* add helper properties

* delete old service

* update test notes

* add unit test for pretty_stats

* remove dead code from post_webhooks

* update group routes to use controllers

* add additional group test coverage

* abstract common permission checks

* convert ingredient parser to controller

* update recipe crud to use controller

* remove dead-code

* add class lifespan tracker for debugging

* convert bulk export to controller

* migrate tools router to controller

* update recipe share to controller

* move customer router to _base

* ignore prints in flake8

* convert units and foods to new controllers

* migrate user routes to controllers

* centralize error handling

* fix invalid ref

* reorder fields

* update routers to share common handling

* update tests

* remove prints

* fix cookbooks delete

* fix cookbook get

* add controller for mealplanner

* cover report routes to controller

* remove __future__ imports

* remove dead code

* remove all base_http children and remove dead code
This commit is contained in:
Hayden
2022-01-13 13:06:52 -09:00
committed by GitHub
parent 5823a32daf
commit c4540f1395
164 changed files with 3111 additions and 3213 deletions

View File

@@ -1,8 +1,74 @@
from fastapi import APIRouter
from functools import cached_property
from typing import Type
from mealie.services._base_http_service.router_factory import RouterFactory
from mealie.services.recipe.recipe_comments_service import RecipeCommentsService
from fastapi import APIRouter, Depends, HTTPException
from pydantic import UUID4
router = APIRouter()
from mealie.core.exceptions import mealie_registered_exceptions
from mealie.routes._base.abc_controller import BaseUserController
from mealie.routes._base.controller import controller
from mealie.routes._base.mixins import CrudMixins
from mealie.schema.query import GetAll
from mealie.schema.recipe.recipe_comments import (
RecipeCommentCreate,
RecipeCommentOut,
RecipeCommentSave,
RecipeCommentUpdate,
)
from mealie.schema.response.responses import ErrorResponse, SuccessResponse
router.include_router(RouterFactory(RecipeCommentsService, prefix="/comments", tags=["Recipe: Comments"]))
router = APIRouter(prefix="/comments", tags=["Recipe: Comments"])
@controller(router)
class RecipeCommentRoutes(BaseUserController):
@cached_property
def repo(self):
return self.deps.repos.comments
def registered_exceptions(self, ex: Type[Exception]) -> str:
registered = {
**mealie_registered_exceptions(self.deps.t),
}
return registered.get(ex, "An unexpected error occurred.")
# =======================================================================
# CRUD Operations
@property
def mixins(self) -> CrudMixins:
return CrudMixins(self.repo, self.deps.logger, self.registered_exceptions, "An unexpected error occurred.")
def _check_comment_belongs_to_user(self, item_id: UUID4) -> None:
comment = self.repo.get_one(item_id)
if comment.user_id != self.deps.acting_user.id and not self.deps.acting_user.admin:
raise HTTPException(
status_code=403,
detail=ErrorResponse.response(message="Comment does not belong to user"),
)
@router.get("", response_model=list[RecipeCommentOut])
def get_all(self, q: GetAll = Depends(GetAll)):
return self.repo.get_all(start=q.start, limit=q.limit, override_schema=RecipeCommentOut)
@router.post("", response_model=RecipeCommentOut, status_code=201)
def create_one(self, data: RecipeCommentCreate):
save_data = RecipeCommentSave(text=data.text, user_id=self.deps.acting_user.id, recipe_id=data.recipe_id)
return self.mixins.create_one(save_data)
@router.get("/{item_id}", response_model=RecipeCommentOut)
def get_one(self, item_id: UUID4):
return self.mixins.get_one(item_id)
@router.put("/{item_id}", response_model=RecipeCommentOut)
def update_one(self, item_id: UUID4, data: RecipeCommentUpdate):
self._check_comment_belongs_to_user(item_id)
return self.mixins.update_one(data, item_id)
@router.delete("/{item_id}", response_model=SuccessResponse)
def delete_one(self, item_id: UUID4):
self._check_comment_belongs_to_user(item_id)
self.mixins.delete_one(item_id)
return SuccessResponse.respond(message="Comment deleted")