Пример #1
0
async def delete_job(job_id: int, token: str = Header(None)):
    authenticated_user = verify_token(token)
    if not authenticated_user:
        response = {'detail': 'UNAUTHORIZED ACCESS', 'status': 401}
        return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED,
                            content=response)

    query = Job.select().where(Job.c.id == job_id)
    is_exist = await database.fetch_one(query)
    if not is_exist:
        response = {'detail': 'JOB NOT FOUND', 'status': 404}
        return JSONResponse(status_code=status.HTTP_404_NOT_FOUND,
                            content=response)

    if is_exist.created_by != authenticated_user['user_id']:
        response = {
            'detail': 'UNAUTHORIZED ACCESS(YOU CAN DELETE YOUR JOB ONLY)',
            'status': 401
        }
        return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED,
                            content=response)

    query = Job.update().where(Job.c.id == job_id).values(status='d')
    await database.execute(query)
    response = {
        'detail': "JOB ID: {} DELETED SUCCESSFULLY".format(job_id),
        'status': 200
    }
    return JSONResponse(status_code=status.HTTP_200_OK, content=response)
Пример #2
0
async def update_job(job_id: int, job: JobValidator,
                     token: str = Header(None)):
    query = Job.select().where(Job.c.id == job_id)
    is_exist = await database.fetch_one(query)
    if not is_exist:
        response = {'detail': 'JOB NOT FOUND', 'status': 404}
        return JSONResponse(status_code=status.HTTP_404_NOT_FOUND,
                            content=response)

    authenticated_user = verify_token_with_role(token,
                                                expected_role='recruiter')
    if not authenticated_user or is_exist.created_by != authenticated_user[
            'user_id']:
        response = {
            'detail': 'UNAUTHORIZED ACCESS(ONLY JOB OWNER CAN UPDATE THE JOB)',
            'status': 401
        }
        return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED,
                            content=response)

    query = Job.update().where(Job.c.id == job_id).values(
        company_name=job.company_name
        if job.company_name else is_exist.company_name,
        job_title=job.job_title if job.job_title else is_exist.job_title,
        job_type=job.job_type if job.job_type else is_exist.job_type,
        experiance_min=job.experiance_min
        if job.experiance_min else is_exist.experiance_min,
        experiance_max=job.experiance_max
        if job.experiance_max else is_exist.experiance_max,
        job_count=job.job_count if job.job_count else is_exist.job_count,
        location=job.location if job.location else is_exist.location,
        status=job.status if job.status else is_exist.status,
        description_short=job.description_short
        if job.description_short else is_exist.description_short,
        description_long=job.description_long
        if job.description_long else is_exist.description_long,
    )
    last_record_id = await database.execute(query)
    await database.execute(query)
    return {**job.dict(), "ID": last_record_id}