135 lines
4.4 KiB
Python
135 lines
4.4 KiB
Python
"""
|
|
Applications API routes
|
|
"""
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from typing import List
|
|
from pydantic import BaseModel
|
|
from sqlalchemy.orm import Session
|
|
|
|
from ..database import SessionLocal
|
|
from ..models import Application, ApplicationStatus, User, JobPosting, Resume
|
|
from ..config.settings import settings
|
|
|
|
router = APIRouter()
|
|
|
|
# Pydantic models for applications
|
|
class ApplicationCreate(BaseModel):
|
|
job_posting_id: int
|
|
resume_id: int
|
|
cover_letter: str = None
|
|
|
|
class ApplicationUpdate(BaseModel):
|
|
status: ApplicationStatus = None
|
|
cover_letter: str = None
|
|
|
|
class ApplicationResponse(BaseModel):
|
|
id: int
|
|
user_id: int
|
|
job_posting_id: int
|
|
resume_id: int
|
|
cover_letter: str
|
|
status: str
|
|
created_at: str
|
|
|
|
class Config:
|
|
from_attributes = True
|
|
|
|
@router.get("/", response_model=List[ApplicationResponse])
|
|
async def get_applications(skip: int = 0, limit: int = 100):
|
|
"""Get all applications"""
|
|
db = SessionLocal()
|
|
try:
|
|
applications = db.query(Application).offset(skip).limit(limit).all()
|
|
return applications
|
|
finally:
|
|
db.close()
|
|
|
|
@router.get("/{application_id}", response_model=ApplicationResponse)
|
|
async def get_application(application_id: int):
|
|
"""Get a specific application"""
|
|
db = SessionLocal()
|
|
try:
|
|
application = db.query(Application).filter(Application.id == application_id).first()
|
|
if not application:
|
|
raise HTTPException(status_code=404, detail="Application not found")
|
|
return application
|
|
finally:
|
|
db.close()
|
|
|
|
@router.post("/", response_model=ApplicationResponse)
|
|
async def create_application(application: ApplicationCreate, user_id: int = 1): # In real app, get from auth context
|
|
"""Create a new job application"""
|
|
db = SessionLocal()
|
|
try:
|
|
# Verify user exists and has permission to apply
|
|
user = db.query(User).filter(User.id == user_id).first()
|
|
if not user or user.role != "job_seeker":
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="Only job seekers can apply for jobs"
|
|
)
|
|
|
|
# Verify job posting exists and is active
|
|
job_posting = db.query(JobPosting).filter(
|
|
JobPosting.id == application.job_posting_id,
|
|
JobPosting.is_active == True
|
|
).first()
|
|
if not job_posting:
|
|
raise HTTPException(status_code=404, detail="Job posting not found or inactive")
|
|
|
|
# Verify resume exists and belongs to user
|
|
resume = db.query(Resume).filter(
|
|
Resume.id == application.resume_id,
|
|
Resume.user_id == user_id
|
|
).first()
|
|
if not resume:
|
|
raise HTTPException(status_code=404, detail="Resume not found")
|
|
|
|
db_application = Application(
|
|
user_id=user_id,
|
|
job_posting_id=application.job_posting_id,
|
|
resume_id=application.resume_id,
|
|
cover_letter=application.cover_letter
|
|
)
|
|
db.add(db_application)
|
|
db.commit()
|
|
db.refresh(db_application)
|
|
return db_application
|
|
finally:
|
|
db.close()
|
|
|
|
@router.put("/{application_id}", response_model=ApplicationResponse)
|
|
async def update_application(application_id: int, app_update: ApplicationUpdate):
|
|
"""Update an application"""
|
|
db = SessionLocal()
|
|
try:
|
|
db_application = db.query(Application).filter(Application.id == application_id).first()
|
|
if not db_application:
|
|
raise HTTPException(status_code=404, detail="Application not found")
|
|
|
|
# Update fields if provided
|
|
if app_update.status is not None:
|
|
db_application.status = app_update.status.value
|
|
if app_update.cover_letter is not None:
|
|
db_application.cover_letter = app_update.cover_letter
|
|
|
|
db.commit()
|
|
db.refresh(db_application)
|
|
return db_application
|
|
finally:
|
|
db.close()
|
|
|
|
@router.delete("/{application_id}")
|
|
async def delete_application(application_id: int):
|
|
"""Delete an application"""
|
|
db = SessionLocal()
|
|
try:
|
|
db_application = db.query(Application).filter(Application.id == application_id).first()
|
|
if not db_application:
|
|
raise HTTPException(status_code=404, detail="Application not found")
|
|
|
|
db.delete(db_application)
|
|
db.commit()
|
|
return {"message": "Application deleted successfully"}
|
|
finally:
|
|
db.close() |