Files

197 lines
7.4 KiB
Python

from fastapi import status, HTTPException, Response, APIRouter, Depends, Query, BackgroundTasks
from sqlalchemy.orm import Session
from typing import List, Annotated
from ..database import get_db
from .. import models, schemas, security, crud
router = APIRouter(
prefix="/students",
dependencies=[Depends(security.verify_api_key)],
tags=['Students']
)
@router.get("/", response_model=List[schemas.Student])
def get_students(db: Session = Depends(get_db)):
students = db.query(models.Students)
return students
@router.get("/{id}", response_model=schemas.Student)
def get_student(id: int, db: Session = Depends(get_db)):
student = db.query(models.Students).filter(models.Students.StudentID == id).first()
if not student:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
return student
@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.StudentID == id).all()
if not responses:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
return responses
@router.get("/matches/{id}", response_model=List[schemas.MatchStudent])
def get_matches(id: int, db: Session = Depends(get_db)):
matches = db.query(
models.Jobs.JobID,
models.Jobs.Company_name,
models.Jobs.Link_to_job,
models.Jobs.Job_name,
models.Jobs.Year,
models.Jobs.Qualification,
models.Jobs.Time,
models.Jobs.Salary_after_interview,
models.Jobs.Salary,
models.Jobs.Email,
models.Jobs.Responsibilities,
models.Matches.Match
).join(models.Matches, models.Jobs.JobID == models.Matches.JobID).\
filter(models.Matches.StudentID == 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.StudentsHardskill])
def get_students_hardskills(id: int, db: Session = Depends(get_db)):
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 == id).all()
if not hardskills:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
return hardskills
@router.get("/jobs-search/", response_model=List[schemas.Jobs_search])
def get_jobs(year: str = None, qualification: bool = None, time: Annotated[list[str], Query()] = [], salary: int = None, hardskills: Annotated[list[str], Query()] = [], search: str = None, db: Session = Depends(get_db)):
if hardskills:
jobs = db.query(
models.Jobs.Job_name,
models.Jobs.Company_name,
models.Jobs.Link_to_job,
models.Jobs.Year,
models.Jobs.Qualification,
models.Jobs.Time,
models.Jobs.Salary,
models.Jobs.Salary_after_interview,
models.Jobs.Email,
models.Jobs.Responsibilities,
models.Jobs.JobID,
models.JobsHard_skills.JobID,
models.Hard_skills.Title
).join(models.JobsHard_skills, models.Jobs.JobID == models.JobsHard_skills.JobID).\
join(models.Hard_skills, models.JobsHard_skills.Hard_skillID == models.Hard_skills.Hard_skillID).\
filter(models.Hard_skills.Title.in_(hardskills), models.Jobs.Qualification == qualification, models.Jobs.Archive == False).distinct(models.Jobs.JobID)
else:
jobs = db.query(models.Jobs).filter(models.Jobs.Qualification == qualification, models.Jobs.Archive == False)
if year:
jobs = jobs.filter(models.Jobs.Year >= year)
if salary:
jobs = jobs.filter(models.Jobs.Salary >= salary)
if time:
jobs = jobs.filter(models.Jobs.Time.op("&&")(time))
if search:
jobs = jobs.filter(models.Jobs.Job_name.match(search))
return jobs.all()
@router.post("/", status_code=status.HTTP_201_CREATED, response_model=schemas.Student)
def create_student(student: schemas.StudentCreate, background_tasks: BackgroundTasks, db: Session = Depends(get_db)):
new_student = models.Students(**student.model_dump(exclude='Hardskills'))
db.add(new_student)
db.flush()
crud.update_hardskills(new_student.StudentID, student.Hardskills, is_job=False, db=db)
crud.update_matches(new_student.StudentID, is_job=False, db=db, background_tasks=background_tasks)
crud.commit_transaction(db)
return new_student
@router.post("/responses/", status_code=status.HTTP_201_CREATED, response_model=schemas.Response)
def add_to_responses(response: schemas.ResponseCreate, db: Session = Depends(get_db)):
new_response = models.Responses(**response.model_dump())
db.add(new_response)
crud.commit_transaction(db)
return new_response
@router.put("/{id}", response_model=schemas.Student)
def update_student(id: int, updated_student: schemas.StudentUpdate, background_tasks: BackgroundTasks, db: Session = Depends(get_db)):
student_query = db.query(models.Students).filter(models.Students.StudentID == id)
student = student_query.first()
if not student:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
student_query.update(updated_student.model_dump(exclude='Hardskills'))
db.flush()
crud.update_hardskills(id, updated_student.Hardskills, is_job=False, db=db)
crud.update_matches(id, is_job=False, db=db, background_tasks=background_tasks)
crud.commit_transaction(db)
return student_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.patch("/{id}", status_code=status.HTTP_200_OK)
def update_students_link(id: int, Link: str, db: Session = Depends(get_db)):
student_query = db.query(models.Students).filter(models.Students.StudentID == id)
student = student_query.first()
if not student:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
student_query.update({models.Students.Link: Link})
db.flush()
crud.commit_transaction(db)
return student_query.first()
@router.delete("/{id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_student(id: int, db: Session = Depends(get_db)):
student_query = db.query(models.Students).filter(models.Students.StudentID == id)
student = student_query.first()
if not student:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
student_query.delete()
crud.commit_transaction(db)
return Response(status_code=status.HTTP_204_NO_CONTENT)
@router.delete("/responses/{id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_responses(id: int, db: Session = Depends(get_db)):
responses_query = db.query(models.Responses).filter(models.Responses.ResponseID == id)
response = responses_query.first()
if not response:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
responses_query.delete()
crud.commit_transaction(db)
return Response(status_code=status.HTTP_204_NO_CONTENT)