async def update_cost(cost_id: UUID, data: CostUpdateRequest) -> Response: cost = await get_or_404(Cost.select(), id=cost_id) update_data = data.dict(exclude_unset=True) cost = set_attrs(cost, **update_data) await db_manager.update(cost) return APIResponse(CostResponse.from_orm(cost).dict())
async def update_user(user_id: UUID, data: UserUpdateRequest) -> Response: user = await get_or_404(User.select(), id=user_id) update_data = data.dict(exclude_unset=True) course = set_attrs(user, **update_data) await db_manager.update(course) return APIResponse(UserResponse.from_orm(course).dict())
async def update_action(action_id: UUID, data: ActionUpdateRequest) -> Response: action = await get_or_404(Action.select(), id=action_id) update_data = data.dict(exclude_unset=True) action = set_attrs(action, **update_data) await db_manager.update(action) return APIResponse(ActionResponse.from_orm(action).dict())
async def update_message(message_id: UUID, data: MessageUpdateRequest) -> Response: message = await get_or_404(Message.select(), id=message_id) update_data = data.dict(exclude_unset=True) message = set_attrs(message, **update_data) await db_manager.update(message) return APIResponse(MessageResponse.from_orm(message).dict())
async def create_photo( request: Request, user: UUID = Form(...), is_main: bool = Form(...), image: UploadFile = File(...) ) -> Response: contents = await image.read() image_path = Path(settings.image_storage_name, str(user), image.filename) storage_path = Path(settings.project_dir, image_path) storage_path.parent.mkdir(parents=True, exist_ok=True) with storage_path.open("wb") as f: f.write(contents) image_url = f'/{image_path}' user_id = await execute( Photo.insert(user=user, is_main=is_main, url=image_url)) photo = first(await execute(Photo.filter(id=user_id))) return APIResponse(PhotoResponse.from_orm(photo).dict(), status_code=HTTPStatus.CREATED)
async def delete_user(user_id: UUID) -> Response: await get_or_404(User.select(), id=user_id) await execute(User.delete().where(User.id == user_id)) return APIResponse(status_code=HTTPStatus.NO_CONTENT)
async def create_user(data: UserCreateRequest) -> Response: user_id = await execute(User.insert(**data.dict())) user = first(await execute(User.filter(id=user_id))) return APIResponse(UserResponse.from_orm(user).dict(), status_code=HTTPStatus.CREATED)
async def get_user(user_id: UUID = Depends(get_current_username)) -> Response: user = await get_or_404(User.select(), id=user_id) return APIResponse(UserResponse.from_orm(user).dict(), status_code=HTTPStatus.OK)
async def get_users() -> Response: users = await execute(User.select()) return APIResponse(invoke(map(UserResponse.from_orm, users), 'dict'))
async def create_action(data: ActionCreateRequest) -> Response: action_id = await execute(Action.insert(**data.dict())) action = first(await execute(Action.filter(id=action_id))) return APIResponse(ActionResponse.from_orm(action).dict(), status_code=HTTPStatus.CREATED)
async def create_message(data: MessageCreateRequest) -> Response: message_id = await execute(Message.insert(**data.dict())) message = first(await execute(Message.filter(id=message_id))) return APIResponse(MessageResponse.from_orm(message).dict(), status_code=HTTPStatus.CREATED)
async def get_messages() -> Response: messages = await execute(Message.select()) return APIResponse(invoke(map(MessageResponse.from_orm, messages), 'dict'))
async def delete_photo(photo_id: UUID) -> Response: await get_or_404(Photo.select(), True) await execute(Photo.delete().where(Photo.id == photo_id)) return APIResponse(status_code=HTTPStatus.NO_CONTENT)
async def get_photos() -> Response: photo = await execute(Photo.select()) return APIResponse(invoke(map(PhotoResponse.from_orm, photo), 'dict'))
async def delete_action(action_id: UUID) -> Response: await get_or_404(Action.select(), True) await execute(Action.delete().where(Action.id == action_id)) return APIResponse(status_code=HTTPStatus.NO_CONTENT)
async def get_costs() -> Response: costs = await execute(Cost.select()) return APIResponse(invoke(map(CostResponse.from_orm, costs), 'dict'))
async def create_cost(data: CostCreateRequest) -> Response: cost_id = await execute(Cost.insert(**data.dict())) cost = first(await execute(Cost.filter(id=cost_id))) return APIResponse( CostResponse.from_orm(cost).dict(), status_code=HTTPStatus.CREATED )
async def delete_message(message_id: UUID) -> Response: await get_or_404(Message.select(), True) await execute(Message.delete().where(Message.id == message_id)) return APIResponse(status_code=HTTPStatus.NO_CONTENT)
async def delete_cost(cost_id: UUID) -> Response: await get_or_404(Cost.select(), True) await execute(Cost.delete().where(Cost.id == cost_id)) return APIResponse(status_code=HTTPStatus.NO_CONTENT)
async def get_actions() -> Response: actions = await execute(Action.select()) return APIResponse(invoke(map(ActionResponse.from_orm, actions), 'dict'))