import pymongo import os client = pymongo.MongoClient(os.getenv("MONGO_URL")) db = client.VE class Job: def __init__(self, title, education, pph): self.title = title self.education = education self.pph = pph def create_new_job(title, education, pph): coll = db.jobs job = coll.find_one({"title": title}) ### Check if there is a job with that title, if so ask the admin if they want to update that job with the new education requirement and pph if job is not None: do_update = input("A job with that title has already been created. Do you want to update it? (y/n): ") if do_update == "y": coll.update_one({"title": title}, {"$set": {"education": education, "pph": pph}}) return True else: return False ### Check if there is any job with the id 0, if not then add it first_job = coll.find_one({"_id": 0}) if first_job is None: coll.insert_one({"_id": 0, "title": title, "education": education, "pph": pph}) return True ### Get the all the IDs of the jobs in the db all_jobs = coll.find({}) jobs = [] for job in all_jobs: jobs.append(job["_id"]) ### Insert the job into the db, but get the last ID in the array and add 1 to it. coll.insert_one({"_id": (jobs[-1] + 1), "title": title, "education": education, "pph": pph}) return True def get_job_details(job_id): coll = db.jobs job = coll.find_one({"_id": job_id}) return Job(job['title'], job['education'], job['pph']) def get_all_jobs(): coll = db.jobs jobs = coll.find({}) for job in jobs: print(f""" Job ID: {job['_id']} Job Title: {job['title']} Education Requirements: {job['education']} Pay Per Hour: {job['pph']} """) return