Beispiel #1
0
async def delete_todo(
    todo_id: int,
    current_user: auth.User = fastapi.Depends(get_current_active_user),
    todo_service: todo.TodoService = fastapi.Depends(
        service_locator.default().todo_service),
) -> None:
    todo_service.delete_todo(user_id=current_user.user_id, todo_id=todo_id)
Beispiel #2
0
def update_todo(
    *,
    user_id: int,
    todo_id: int,
    todo_service: todo.TodoService,
    updates: typing.Dict[str, typing.Any],
) -> response.TodoResponse:
    original_todo = todo_service.get_by_id(user_id=user_id, todo_id=todo_id)
    if original_todo:
        data = original_todo.dict()
        data.update(updates)
        _, _, errors = pydantic.validate_model(todo.Todo, data)
        if errors:
            raise fastapi.HTTPException(
                status_code=fastapi.status.HTTP_400_BAD_REQUEST,
                detail=errors.json(),
            )

        updated_todo = original_todo.copy(update=updates)
        updated_todo_from_db = todo_service.update_todo(user_id=user_id,
                                                        todo=updated_todo)
        return response.TodoResponse.from_domain(updated_todo_from_db)
    else:
        raise fastapi.HTTPException(
            status_code=fastapi.status.HTTP_404_NOT_FOUND,
            detail="Todo does not exist.")
Beispiel #3
0
async def all_todos(
    current_user: auth.User = fastapi.Depends(get_current_active_user),
    todo_service: todo.TodoService = fastapi.Depends(
        service_locator.default().todo_service),
) -> typing.List[response.TodoResponse]:
    todos = todo_service.all(current_user.user_id)
    return [response.TodoResponse.from_domain(t) for t in todos]
Beispiel #4
0
async def add_daily_todo(
    description: str,
    note: typing.Optional[str] = None,
    start_date: typing.Optional[datetime.date] = None,
    current_user: auth.User = fastapi.Depends(get_current_active_user),
    todo_service: todo.TodoService = fastapi.Depends(
        service_locator.default().todo_service),
) -> response.TodoResponse:
    if start_date is None:
        start_date = datetime.date.today()
    if note is None:
        note = ""
    daily_todo = todo.Daily(
        advance_days=0,
        category=core.TodoCategory.Todo,
        date_added=datetime.date.today(),
        date_completed=None,
        description=description,
        note=note,
        start_date=start_date,
        todo_id=-1,
        user_id=current_user.user_id,
    )
    new_todo = todo_service.add_todo(user_id=current_user.user_id,
                                     todo=daily_todo)
    return response.TodoResponse.from_domain(new_todo)
Beispiel #5
0
async def add_monthly_todo(
    description: str,
    advance_days: int,
    month_day: int,
    note: typing.Optional[str] = None,
    start_date: typing.Optional[datetime.date] = None,
    current_user: auth.User = fastapi.Depends(get_current_active_user),
    todo_service: todo.TodoService = fastapi.Depends(
        service_locator.default().todo_service),
) -> response.TodoResponse:
    if month_day not in range(1, 28):
        raise fastapi.HTTPException(
            status_code=HTTP_400_BAD_REQUEST,
            detail="month_day must be between 1 and 28",
        )

    if start_date is None:
        start_date = datetime.date.today()
    if note is None:
        note = ""
    monthly_todo = todo.Monthly(
        advance_days=advance_days,
        category=core.TodoCategory.Todo,
        date_added=datetime.date.today(),
        date_completed=None,
        description=description,
        note=note,
        start_date=start_date,
        todo_id=-1,
        user_id=current_user.user_id,
        month_day=month_day,
    )
    new_todo = todo_service.add_todo(user_id=current_user.user_id,
                                     todo=monthly_todo)
    return response.TodoResponse.from_domain(new_todo)
Beispiel #6
0
async def add_irregular_todo(
    description: str,
    advance_days: int,
    month: int,
    week_day: int,
    week: int,
    note: typing.Optional[str] = None,
    start_date: typing.Optional[datetime.date] = None,
    current_user: auth.User = fastapi.Depends(get_current_active_user),
    todo_service: todo.TodoService = fastapi.Depends(
        service_locator.default().todo_service),
) -> response.TodoResponse:
    validation_errors: typing.List[str] = []
    if month not in range(1, 12):
        validation_errors.append("month must be between 1 and 12")
    if week_day not in range(1, 7):
        validation_errors.append("week_day must be between 1 and 7")
    if week not in range(1, 5):
        validation_errors.append("week must be between 1 and 5")
    if validation_errors:
        errors_str = "; ".join(validation_errors)
        msg = f"Validation errors: {errors_str}"
        raise fastapi.HTTPException(status_code=HTTP_400_BAD_REQUEST,
                                    detail=msg)
    if start_date is None:
        start_date = datetime.date.today()
    if note is None:
        note = ""
    irregular_todo = todo.Irregular(
        advance_days=advance_days,
        category=core.TodoCategory.Todo,
        date_added=datetime.date.today(),
        date_completed=None,
        description=description,
        note=note,
        start_date=start_date,
        todo_id=-1,
        user_id=current_user.user_id,
        month=todo.Month(month),
        week_day=todo.Weekday(week_day),
        week_number=week,
    )
    new_todo = todo_service.add_todo(user_id=current_user.user_id,
                                     todo=irregular_todo)
    return response.TodoResponse.from_domain(new_todo)