def create(user_id, pool): """ This function creates a new note related to the passed in person id. :param person_id: Id of the person the note is related to :param note: The JSON containing the note data :return: 201 on success """ # get the parent person user = User.query.filter(User.user_id == user_id).one_or_none() # Was a person found? if user is None: abort(404, "User not found for Id: {user_id}".format(user_id=user_id)) # Create a note schema instance schema = PoolSchema() new_pool = schema.load(pool, session=db.session).data # Add the note to the person and database user.pool.append(new_pool) db.session.commit() # Serialize and return the newly created note in the response data = schema.dump(new_pool).data return data, 201
def update(user_id, pool_id, pool): """ This function updates an existing note related to the passed in person id. :param person_id: Id of the person the note is related to :param note_id: Id of the note to update :param content: The JSON containing the note data :return: 200 on success """ update_pool = (Pool.query.filter(User.user_id == user_id).filter( Pool.pool_id == pool_id).one_or_none()) # Did we find an existing note? if update_pool is not None: # turn the passed in note into a db object schema = PoolSchema() update = schema.load(pool, session=db.session).data # Set the id's to the note we want to update update.user_id = update_pool.user_id update.pool_id = update_pool.pool_id # merge the new object into the old and commit it to the db db.session.merge(update) db.session.commit() # return updated note in the response data = schema.dump(update_pool).data return data, 200 # Otherwise, nope, didn't find that note else: abort(404, "Pool not found for Id: {pool_id}".format(pool_id=pool_id))