247 lines
10 KiB
Python
247 lines
10 KiB
Python
from fastapi import status, HTTPException, Response, APIRouter, Depends, BackgroundTasks, Query
|
||
from sqlalchemy.orm import Session
|
||
from sqlalchemy import not_, func
|
||
from typing import List, Annotated
|
||
from ..database import get_db
|
||
from .. import models, schemas, security, crud
|
||
|
||
router = APIRouter(
|
||
prefix="/jobs",
|
||
dependencies=[Depends(security.verify_access_token)],
|
||
responses={401: {"description": "Unauthorized"}},
|
||
tags=['Jobs']
|
||
)
|
||
|
||
@router.get("/", response_model=List[schemas.JobGet])
|
||
def get_jobs(db: Session = Depends(get_db)):
|
||
jobs = db.query(models.Jobs).all()
|
||
|
||
if not jobs:
|
||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||
|
||
response: List[schemas.JobGet] = []
|
||
|
||
for job in jobs:
|
||
hardskills = db.query(models.JobsHard_skills.Hard_skillID, models.Hard_skills.Title).\
|
||
join(models.Hard_skills, models.JobsHard_skills.Hard_skillID == models.Hard_skills.Hard_skillID).\
|
||
filter(models.JobsHard_skills.JobID == job.JobID).all()
|
||
|
||
Hardskills = [hardskill.Title for hardskill in hardskills]
|
||
response += [schemas.JobGet(JobID=job.JobID, UserID = job.UserID, Job_name=job.Job_name, Company_name=job.Company_name,
|
||
Link_to_job=job.Link_to_job, Year=job.Year, Qualification=job.Qualification, Time=job.Time,
|
||
Salary_after_interview=job.Salary_after_interview, Salary=job.Salary, Email=job.Email, Archive=job.Archive,
|
||
Responsibilities=job.Responsibilities, Hardskills=Hardskills)]
|
||
|
||
return response
|
||
|
||
@router.get("/{id}", response_model=schemas.JobGet)
|
||
def get_job(id: int, db: Session = Depends(get_db)):
|
||
job = db.query(models.Jobs).filter(models.Jobs.JobID == id).first()
|
||
|
||
if not job:
|
||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||
|
||
hardskills = db.query(models.JobsHard_skills.Hard_skillID, models.Hard_skills.Title).\
|
||
join(models.Hard_skills, models.JobsHard_skills.Hard_skillID == models.Hard_skills.Hard_skillID).\
|
||
filter(models.JobsHard_skills.JobID == id).all()
|
||
|
||
Hardskills = [hardskill.Title for hardskill in hardskills]
|
||
response = schemas.JobGet(JobID=job.JobID, UserID = job.UserID, Job_name=job.Job_name, Company_name=job.Company_name,
|
||
Link_to_job=job.Link_to_job, Year=job.Year, Qualification=job.Qualification, Time=job.Time,
|
||
Salary_after_interview=job.Salary_after_interview, Salary=job.Salary, Email=job.Email, Archive=job.Archive,
|
||
Responsibilities=job.Responsibilities, Hardskills=Hardskills)
|
||
|
||
return response
|
||
|
||
@router.get("/matches/{id}", response_model=List[schemas.MatchJob])
|
||
def get_matches(id: int, db: Session = Depends(get_db)):
|
||
matches = db.query(models.Matches).filter(models.Matches.JobID == id).order_by(models.Matches.Match.desc()).all()
|
||
matches = db.query(
|
||
models.Students.StudentID,
|
||
models.Students.Name,
|
||
models.Students.Type,
|
||
models.Students.Faculties,
|
||
models.Students.Group,
|
||
models.Students.Year,
|
||
models.Students.Experience_specialty,
|
||
models.Students.Time,
|
||
models.Students.Soft_skills,
|
||
models.Students.Link,
|
||
models.Students.Email,
|
||
models.Students.Phone_number,
|
||
models.Matches.Match
|
||
).join(models.Matches, models.Students.StudentID == models.Matches.StudentID).\
|
||
filter(models.Matches.JobID == id).\
|
||
order_by(models.Matches.Match.desc()).\
|
||
all()
|
||
|
||
if not matches:
|
||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||
|
||
return matches
|
||
|
||
@router.get("/hardskills/{id}", response_model=List[schemas.JobsHardskill])
|
||
def get_jobs_hardskills(id: int, db: Session = Depends(get_db)):
|
||
hardskills = db.query(models.JobsHard_skills.Hard_skillID, models.Hard_skills.Title).\
|
||
join(models.Hard_skills, models.JobsHard_skills.Hard_skillID == models.Hard_skills.Hard_skillID).\
|
||
filter(models.JobsHard_skills.JobID == id).all()
|
||
|
||
if not hardskills:
|
||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||
|
||
return hardskills
|
||
|
||
@router.get("/students-search/", response_model=List[schemas.StudentUpdate])
|
||
def get_students(
|
||
year: int = None,
|
||
time: Annotated[list[str], Query()] = [],
|
||
hardskills: Annotated[list[str], Query()] = [],
|
||
faculties: str = None,
|
||
experience_specialty: bool = None,
|
||
db: Session = Depends(get_db)
|
||
):
|
||
|
||
if hardskills:
|
||
students = db.query(
|
||
models.Students.StudentID,
|
||
models.Students.Name,
|
||
models.Students.Type,
|
||
models.Students.Faculties,
|
||
models.Students.Group,
|
||
models.Students.Уear,
|
||
models.Students.Experience_specialty,
|
||
models.Students.Time,
|
||
models.Students.Soft_skills,
|
||
models.Students.Link,
|
||
models.Students.Email,
|
||
models.Students.Phone_number,
|
||
).join(models.StudentsHard_skills, models.Students.StudentID == models.StudentsHard_skills.StudentID).\
|
||
join(models.Hard_skills, models.StudentsHard_skills.Hard_skillID == models.Hard_skills.Hard_skillID).\
|
||
filter(models.Hard_skills.Title.in_(hardskills)).distinct(models.Students.StudentID)
|
||
else:
|
||
students = db.query(models.Students).filter(models.Students.Experience_specialty == experience_specialty)
|
||
|
||
if time:
|
||
students = students.filter(models.Students.Time.op("&&")(time))
|
||
|
||
if year:
|
||
students = students.filter(models.Students.Year >= year)
|
||
|
||
if faculties:
|
||
students = students.filter(models.Students.Faculties.ilike("%" + faculties + "%"))
|
||
|
||
response: List[schemas.StudentUpdate] = []
|
||
|
||
for student in students:
|
||
hardskills = db.query(models.StudentsHard_skills.Hard_skillID, models.Hard_skills.Title).\
|
||
join(models.Hard_skills, models.StudentsHard_skills.Hard_skillID == models.Hard_skills.Hard_skillID).\
|
||
filter(models.StudentsHard_skills.StudentID == student.StudentID).all()
|
||
|
||
Hardskills = [hardskill.Title for hardskill in hardskills]
|
||
response += [schemas.StudentUpdate(Name=student.Name, Type=student.Type, Faculties=student.Faculties,
|
||
Group=student.Group, Year=student.Year, Experience_specialty=student.Experience_specialty,
|
||
Time=student.Time, Soft_skills=student.Soft_skills, Link=student.Link, Email=student.Email,
|
||
Phone_number=student.Phone_number, Hardskills=Hardskills)]
|
||
|
||
return response
|
||
|
||
@router.get("/responses/{id}", response_model=List[schemas.Response])
|
||
def get_responses(id: int, db: Session = Depends(get_db)):
|
||
responses = db.query(models.Responses).filter(models.Responses.JobID == id).all()
|
||
|
||
if not responses:
|
||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||
|
||
return responses
|
||
|
||
@router.get("/responses/", response_model=List[schemas.Response])
|
||
def get_responses_all_unprocessed(db: Session = Depends(get_db)):
|
||
responses = db.query(
|
||
models.Responses.ResponseID,
|
||
models.Responses.StudentID,
|
||
models.Responses.JobID,
|
||
models.Responses.Status,
|
||
models.Responses.Comment,
|
||
models.Responses.Link,
|
||
).join(models.Students, models.Students.StudentID == models.Responses.StudentID).\
|
||
filter(models.Responses.Status == "Ожидает рассмотрения").\
|
||
order_by(models.Students.Year.asc()).\
|
||
all() # order_by работате и на буквы в начале группы. Так, сейчас должен работать по идеи
|
||
|
||
if not responses:
|
||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||
|
||
return responses
|
||
|
||
@router.post("/", status_code=status.HTTP_201_CREATED, response_model=schemas.Job)
|
||
def create_job(
|
||
job: schemas.JobCreate,
|
||
background_tasks: BackgroundTasks,
|
||
db: Session = Depends(get_db),
|
||
current_user: models.Users = Depends(security.get_current_user)
|
||
):
|
||
|
||
new_job = models.Jobs(UserID=current_user.UserID, **job.model_dump(exclude='Hardskills'))
|
||
db.add(new_job)
|
||
db.flush()
|
||
|
||
crud.update_hardskills(new_job.JobID, job.Hardskills, is_job=True, db=db)
|
||
|
||
if not new_job.Archive:
|
||
crud.update_matches(new_job.JobID, is_job=True, db=db, background_tasks=background_tasks)
|
||
|
||
crud.commit_transaction(db)
|
||
|
||
return new_job
|
||
|
||
@router.put("/{id}", response_model=schemas.Job)
|
||
def update_job(
|
||
id: int,
|
||
updated_job: schemas.JobUpdate,
|
||
background_tasks: BackgroundTasks,
|
||
db: Session = Depends(get_db),
|
||
current_user: models.Users = Depends(security.get_current_user)
|
||
):
|
||
|
||
job_query = db.query(models.Jobs).filter(models.Jobs.JobID == id)
|
||
job = job_query.first()
|
||
|
||
if not job:
|
||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||
|
||
updated_data = updated_job.model_dump(exclude='Hardskills')
|
||
updated_data["UserID"] = current_user.UserID
|
||
|
||
job_query.update(updated_data, synchronize_session=False)
|
||
db.flush()
|
||
|
||
crud.update_hardskills(id, updated_job.Hardskills, is_job=True, db=db)
|
||
|
||
if not updated_job.Archive:
|
||
crud.update_matches(id, is_job=True, db=db, background_tasks=background_tasks)
|
||
else:
|
||
db.query(models.Matches).filter(models.Matches.JobID == id).delete()
|
||
db.flush()
|
||
|
||
crud.commit_transaction(db)
|
||
|
||
return job_query.first()
|
||
|
||
@router.put("/responses/{id}", status_code=status.HTTP_200_OK)
|
||
def update_response(id: int, updated_response: schemas.ResponseUpdate, background_tasks: BackgroundTasks, db: Session = Depends(get_db)):
|
||
updated_record = crud.update_record(models.Responses, id, updated_response.model_dump(), db, background_tasks)
|
||
return updated_record
|
||
|
||
@router.delete("/{id}", status_code=status.HTTP_204_NO_CONTENT)
|
||
def delete_job(id: int, db: Session = Depends(get_db)):
|
||
job_query = db.query(models.Jobs).filter(models.Jobs.JobID == id)
|
||
job = job_query.first()
|
||
|
||
if not job:
|
||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||
|
||
job_query.delete()
|
||
|
||
crud.commit_transaction(db)
|
||
|
||
return Response(status_code=status.HTTP_204_NO_CONTENT) |