示例#1
0
async def delete_feedback(event_id, feedback_id, db):

    # Remove feedback from event array
    column = db[get_db_name()]["events"]

    found_event = column.find_one({"_id": event_id})

    if not found_event:
        raise HTTPException(status_code=404, detail="Event ID is invalid")

    if feedback_id in found_event["comment_ids"]:
        found_event["comment_ids"].remove(feedback_id)
    else:
        raise HTTPException(
            status_code=404,
            detail="Feedback ID not found in the provided event")

    column.update_one({"_id": event_id}, {"$set": found_event})

    # Remove feedback from the feedback collection
    column = db[get_db_name()]["feedback"]
    found_feedback = column.find_one({"_id": feedback_id})

    if not found_feedback:
        raise HTTPException(status_code=404, detail="Feedback ID is invalid")

    column.delete_one({"_id": feedback_id})
示例#2
0
async def register_event(form, db):
    # cast input form (python class) -> dictionary (become JSON eventually)
    form_dict = form.dict()

    # generating event_id (UUID)
    event_id = await generate_id()

    # insert the event_id to the dictionary for insertion
    form_dict["_id"] = event_id

    # create column for insertion in db
    column = db[get_db_name()]["events"]

    # XXX BAD CODE ALERT!!!!
    # This will turn every instance of a enum into a string of itself
    # Bad time complexity and pretty stupid
    # Fix this in models!!!
    # TODO: this code will break the "query by status" endpoint when it is implemented
    # fix before testing that!!!
    for key, val in form_dict.items():
        if isinstance(val, Enum):
            form_dict[key] = val.name

    # insert id into column
    column.insert_one(form_dict)

    # return user_id if success
    return event_id
示例#3
0
async def get_feedback(feedback_id, db):
    column = db[get_db_name()]["feedback"]
    feedback = column.find_one({"_id": feedback_id})

    if not feedback:
        raise HTTPException(status_code=404, detail="Feedback ID is invalid")

    return feedback
示例#4
0
async def get_event_by_status(event_id, db):
    column = db[get_db_name()]["events"]
    all_events = column.find()
    all_events_list = [event for event in all_events]
    if not all_events_list:
        raise HTTPException(status_code=404,
                            detail="Event with given ID not found")
    return {"events": all_events_list}
示例#5
0
async def delete_user(email, db):

    # create column for insertion in db
    column = db[get_db_name()]["users"]

    response = column.delete_one({"email": email})
    if response.deleted_count == 0:
        raise HTTPException(status_code=404,
                            detail="User not found and could not be deleted")
示例#6
0
async def get_all_events(db):
    collection = db[get_db_name()]["events"]
    events = list(collection.find())

    # change the "_id" field to a "event_id" field
    for event in events:
        event["event_id"] = event["_id"]
        del event["_id"]

    return {"events": events}
示例#7
0
async def add_feedback(form, db):
    form_dict = form.dict()
    feedback_id = await generate_id()
    form_dict["_id"] = feedback_id

    event_id = form_dict["event_id"]

    # attempt to find event and add feedback
    column = db[get_db_name()]["events"]
    found_event = column.find_one({"_id": event_id})

    if not found_event:
        raise HTTPException(status_code=404, detail="Event ID is invalid")

    found_event["comment_ids"].append(feedback_id)
    column.update_one({"_id": event_id}, {"$set": found_event})

    column = db[get_db_name()]["feedback"]
    column.insert_one(form_dict)

    return feedback_id
示例#8
0
async def get_user_info(email, db):
    #why is it email, db ... commas?
    column = db[get_db_name()]["users"]

    #make query from identifier input
    query = {"email": email}

    #query to database
    response = column.find_one(query)

    if not response:
        raise HTTPException(status_code=404, detail="User does not exist")

    return response
示例#9
0
async def register_user(form, db):
    # cast input form (python class) -> dictionary (become JSON eventually)
    form_dict = form.dict()

    # generating user_id (UUID)
    user_id = await generate_id()

    # insert the user_id to the dictionary for insertion
    form_dict["_id"] = user_id

    # create column for insertion in db
    column = db[get_db_name()]["users"]

    # insert id into column
    column.insert_one(form_dict)

    # return user_id if success
    return user_id
示例#10
0
async def events_by_location(origin, radius, db):
    def within_radius(event):
        event_location = event.get("location", {})

        event_lat = event_location.get("latitude", 0)
        event_lon = event_location.get("longitude", 0)

        destination = (event_lat, event_lon)

        distance_mi = distance.distance(origin, destination).miles

        return distance_mi <= radius

    column = db[get_db_name()]["events"]

    events = column.find()
    all_events = [event for event in events]

    valid_events = list(filter(within_radius, all_events))

    return valid_events
示例#11
0
async def get_user(user_id, db):
    column = db[get_db_name()]["users"]
    user = column.find_one({"_id": user_id})
    return user
示例#12
0
async def get_event(event_id, db):
    column = db[get_db_name()]["events"]
    event = column.find_one({"_id": event_id})

    return event