Пример #1
0
def get_user(username: str,
             provider: ProviderType,
             from_local: Optional[bool] = False,
             data_base: SESSION = Depends(get_con)):
    if from_local:
        user = ModelUser.get_user_by_username(username=username,
                                              db_session=data_base)
        # add provider
        if not user:
            return JSONResponse(status_code=status.HTTP_404_NOT_FOUND,
                                content='User not found in database')

        repositories = ModelRepository.get_all_from_local(db_session=data_base,
                                                          user_id=user.id)
        names = [repository.name for repository in repositories]
        return UserOutputRepos(id=user.id,
                               login=user.login,
                               email=user.email,
                               twitter_username=user.twitter_username,
                               repos=names)

    source = dealer.get_provider(provider=provider)
    user = source.get_user(username=username)
    if not user:
        return JSONResponse(status_code=status.HTTP_404_NOT_FOUND,
                            content='User not found remotely')

    repositories = source.get_all_repos(username=username)
    names = [repository.name for repository in repositories]

    return UserOutputRepos(id=user.id,
                           login=user.login,
                           email=user.email,
                           twitter_username=user.twitter_username,
                           repos=names)
Пример #2
0
def update_user(username: str,
                provider: ProviderType,
                data_base: SESSION = Depends(get_con)):
    source = dealer.get_provider(provider=provider)
    user = source.get_user(username=username)

    if not user:
        return JSONResponse(status_code=status.HTTP_404_NOT_FOUND,
                            content=dict(message='User not found remotely'))

    model_user = ModelUser.get_user_by_username(username=user.login,
                                                db_session=data_base)

    if not model_user:
        return JSONResponse(status_code=status.HTTP_404_NOT_FOUND,
                            content=dict(message='User not found in database'))

    status_user, message = model_user.update(
        connection=data_base,
        email=user.email,
        twitter_username=user.twitter_username)

    if not status_user:
        return JSONResponse(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
                            content=dict(message='Database error',
                                         details=message))

    return 'User updated successfully'
Пример #3
0
def get_all_repositories(username: str, provider: ProviderType):
    source = dealer.get_provider(provider=provider)

    repositories = source.get_all_repos(username=username)

    if not repositories:
        return JSONResponse(status_code=status.HTTP_404_NOT_FOUND,
                            content='User not found in remotely')
    return repositories
Пример #4
0
def delete_repo(username: str,
                repository_name: str,
                provider: ProviderType,
                data_base: SESSION = Depends(get_con)):
    source = dealer.get_provider(provider=provider)
    repository = source.get_repository(username=username,
                                       repository_name=repository_name)
    repository = ModelRepository.get_by_id(db_session=data_base,
                                           index=repository.id)
    if repository:
        repository.delete(connection=data_base)
    return 'Repository deleted'
Пример #5
0
def get_repository(username: str,
                   repository_name: str,
                   provider: ProviderType,
                   save_data: bool,
                   data_base: SESSION = Depends(get_con)):
    source = dealer.get_provider(provider=provider)

    repository = source.get_repository(username=username,
                                       repository_name=repository_name)
    if not repository:
        return JSONResponse(status_code=404,
                            content=dict(message='Repository not found'))
    if save_data:
        user = source.get_user(username=username)
        if not ModelUser.get_user_by_id(
                db_session=data_base, index=user.id, provider=provider):
            model_user = ModelUser(id=user.id,
                                   login=user.login,
                                   email=user.email,
                                   twitter_username=user.twitter_username,
                                   provider=provider)
            status_user, message = model_user.save(connection=data_base)

            if not status_user:
                return JSONResponse(
                    status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
                    content=dict(message='Database error', details=message))

        if not ModelRepository.get_by_id(db_session=data_base,
                                         index=repository.id):
            model_repository = ModelRepository(
                id=repository.id,
                user_id=user.id,
                name=repository.name,
                private=repository.private,
                created_at=repository.created_at,
                updated_at=repository.updated_at,
                size=repository.size,
                stars=repository.stars,
                watchers=repository.watchers)
            status_repository, message = model_repository.save(
                connection=data_base)
            if not status_repository:
                return JSONResponse(
                    status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
                    content=dict(message='Database error', details=message))
    return repository
Пример #6
0
def delete_user(username: str,
                provider: ProviderType,
                data_base: SESSION = Depends(get_con)):
    source = dealer.get_provider(provider=provider)
    user = source.get_user(username=username)
    user = ModelUser.get_user_by_id(db_session=data_base,
                                    index=user.id,
                                    provider=provider)

    if not user:
        return 'User deleted'

    if ModelRepository.get_all_from_local(db_session=data_base,
                                          user_id=user.id):
        return JSONResponse(status_code=status.HTTP_400_BAD_REQUEST,
                            content=dict(message='Constraint violation'))
    status_user, message = user.delete(connection=data_base)

    if not status_user:
        return JSONResponse(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
                            content=dict(message='Database error',
                                         details=message))
    return 'User deleted'