Ejemplo n.º 1
0
def get(person_id):
    with session() as sess:
        try:
            person = sess.query(Person).filter_by(id=person_id).one()
        except NoResultFound:
            return make_response(f"Person with id {person_id} does not exist",
                                 404)
        return make_response("Success", 200, person=person.to_dict())
Ejemplo n.º 2
0
def post():
    data = request.json
    with session() as sess:
        new_office = Office(name=data['name'])
        sess.add(new_office)
        sess.flush()
        return make_response(f"Created office with id {new_office.id}",
                             200,
                             office=new_office.to_dict())
Ejemplo n.º 3
0
def get(office_id):
    with session() as sess:
        try:
            office = sess.query(Office).filter_by(id=office_id).one()
        except NoResultFound:
            return make_response(f"Office with id {office_id} does not exist",
                                 404)
        return make_response(f"Office with id {office_id}",
                             200,
                             office=office.to_dict())
Ejemplo n.º 4
0
def put(person_id):
    modified_person = request.json
    increment_age = modified_person.get('age', False)
    name = modified_person.get('name')
    with session() as sess:
        try:
            person = sess.query(Person).filter_by(id=person_id).one()
            if increment_age:
                person.age = person.age + 1
            if name:
                person.name = name
        except NoResultFound:
            return make_response(f"Person with id {person_id} does not exist",
                                 404)
        return make_response("Success", 200, person=person.to_dict())
Ejemplo n.º 5
0
def remove_employee():
    data = request.json
    if 'office_id' not in data or 'person_id' not in data:
        return make_response(
            "Both office_id and person_id need to be supplied", 404)
    office_id = data['office_id']
    person_id = data['person_id']
    with session() as sess:
        try:
            sess.query(Person).filter_by(id=person_id).update(
                {"office_id": None})
        except NoResultFound:
            return make_response(f"No person with id {person_id} exists", 404)
        return make_response(
            f"Person with id {person_id} was removed from office with id {office_id}",
            200,
        )
Ejemplo n.º 6
0
def post():
    data = request.json
    office_id = data.get('office_id')
    office = None
    with session() as sess:
        if office_id:
            try:
                office = sess.query(Office).filter_by(id=office_id).one()
            except NoResultFound:
                return make_response(
                    f"Office with id {office_id} does not exist, did not create person entry",
                    404)
        new_person = Person(name=data['name'],
                            age=data['age'],
                            office_id=office,
                            office=office)
        sess.add(new_person)
        sess.flush()
        return make_response(f"Created person with id {new_person.id}",
                             200,
                             person=new_person.to_dict())