# Path: example/generic_social_network/app/models/query_builders/follows_dbm.py
# class FollowsDBM:
#     def __init__(self, db):
#         self.db = db
# 
#     def reset(self):
#         self.db.drop_follows()
# 
#     def all(self):
#         return Follow.query.all()
# 
#     def find(self, follower_id=None, followed_id=None):
#         query = Follow.query
#         if follower_id is not None:
#             query = query.filter(Follow.follower_id == follower_id)
#         if followed_id is not None:
#             query = query.filter(Follow.followed_id == followed_id)
#         results = query.all()
# 
#         if follower_id is not None and followed_id is not None:
#             if len(results) == 0:
#                 return None
#             if len(results) == 1:
#                 return results[0]
#             raise Exception
# 
#         return results
# 
#     def find_by_follower_id(self, follower_id):
#         return self.find(follower_id=follower_id)
# 
#     def find_by_followed_id(self, followed_id):
#         return self.find(followed_id=followed_id)
# 
#     def create_with_ids(self, follower_id, followed_id):
#         Follow.insert({
#             'follower_id': follower_id,
#             'followed_id': followed_id
#         })
#         self.db.session.commit()
# 
#     def delete(self, follower_id=None, followed_id=None):
#         if not followed_id or not followed_id:
#             raise Exception
#         Follow.query.filter_by(follower_id=follower_id, followed_id=followed_id).delete()
#         self.db.session.commit()
# 
#     def delete_matching_follower_id(self, follower_id):
#         Follow.query.filter_by(follower_id=follower_id).delete()
#         self.db.session.commit()
# 
#     def delete_matching_followed_id(self, followed_id):
#         Follow.query.filter_by(followed_id=followed_id).delete()
#         self.db.session.commit()
# 
# Path: example/generic_social_network/app/models/query_builders/posts_dbm.py
# class PostsDBM:
#     def __init__(self, db):
#         self.db = db
# 
#     def reset(self):
#         self.db.drop_posts()
# 
#     def all(self):
#         return Post.query.all()
# 
#     def find_by_id(self, id_):
#         matching = Post.query.filter_by(post_id=id_).all()
#         if len(matching) == 0:
#             return None
#         if len(matching) == 1:
#             return matching[0]
#         raise Exception
# 
#     def find_by_title(self, title):
#         return Post.query.filter_by(title=title).all()
# 
#     def find_by_author_id(self, author_id):
#         return Post.query.filter_by(author_id=author_id).all()
# 
#     def find_posts_for_follower(self, user_id):
#         return Post.query \
#             .join(Follow, Post.author_id == Follow.followed_id) \
#             .filter(Follow.follower_id == user_id) \
#             .all()
# 
#     def create_from_json(self, json):
#         Post.insert(json)
#         self.db.session.commit()
# 
#     def update_from_json(self, json):
#         Post.insert_on_duplicate_key_update(json)
#         self.db.session.commit()
# 
#     def delete(self, post=None, post_id=None):
#         if post_id is None:
#             post_id = post.post_id
#         Post.query.filter_by(post_id=post_id).delete()
#         self.db.session.commit()
# 
#     def delete_matching_author_id(self, author_id):
#         Post.query.filter_by(author_id=author_id).delete()
#         self.db.session.commit()


from generic_social_network.app.models.tables.user import User
from .follows_dbm import FollowsDBM
from .posts_dbm import PostsDBM
        self.db = db

    def reset(self):
        self.db.drop_users()

    def all(self):
        return User.query.all()

    def find_by_id(self, id_):
        matching = User.query.filter_by(user_id=id_).all()
        if len(matching) == 0:
            return None
        if len(matching) == 1:
            return matching[0]
        raise Exception

    def create_from_json(self, json):
        User.insert(json)
        self.db.session.commit()

    def update_from_json(self, json):
        User.insert_on_duplicate_key_update(json)
        self.db.session.commit()

    def delete(self, user=None, user_id=None):
        if user_id is None:
            user_id = user.user_id
        User.query.filter_by(user_id=user_id).delete()
        self.db.session.commit()
        FollowsDBM(self.db).delete_matching_follower_id(user_id)

# Path: example/generic_social_network/app/models/query_builders/follows_dbm.py
# class FollowsDBM:
#     def __init__(self, db):
#         self.db = db
# 
#     def reset(self):
#         self.db.drop_follows()
# 
#     def all(self):
#         return Follow.query.all()
# 
#     def find(self, follower_id=None, followed_id=None):
#         query = Follow.query
#         if follower_id is not None:
#             query = query.filter(Follow.follower_id == follower_id)
#         if followed_id is not None:
#             query = query.filter(Follow.followed_id == followed_id)
#         results = query.all()
# 
#         if follower_id is not None and followed_id is not None:
#             if len(results) == 0:
#                 return None
#             if len(results) == 1:
#                 return results[0]
#             raise Exception
# 
#         return results
# 
#     def find_by_follower_id(self, follower_id):
#         return self.find(follower_id=follower_id)
# 
#     def find_by_followed_id(self, followed_id):
#         return self.find(followed_id=followed_id)
# 
#     def create_with_ids(self, follower_id, followed_id):
#         Follow.insert({
#             'follower_id': follower_id,
#             'followed_id': followed_id
#         })
#         self.db.session.commit()
# 
#     def delete(self, follower_id=None, followed_id=None):
#         if not followed_id or not followed_id:
#             raise Exception
#         Follow.query.filter_by(follower_id=follower_id, followed_id=followed_id).delete()
#         self.db.session.commit()
# 
#     def delete_matching_follower_id(self, follower_id):
#         Follow.query.filter_by(follower_id=follower_id).delete()
#         self.db.session.commit()
# 
#     def delete_matching_followed_id(self, followed_id):
#         Follow.query.filter_by(followed_id=followed_id).delete()
#         self.db.session.commit()
# 
# Path: example/generic_social_network/app/models/query_builders/posts_dbm.py
# class PostsDBM:
#     def __init__(self, db):
#         self.db = db
# 
#     def reset(self):
#         self.db.drop_posts()
# 
#     def all(self):
#         return Post.query.all()
# 
#     def find_by_id(self, id_):
#         matching = Post.query.filter_by(post_id=id_).all()
#         if len(matching) == 0:
#             return None
#         if len(matching) == 1:
#             return matching[0]
#         raise Exception
# 
#     def find_by_title(self, title):
#         return Post.query.filter_by(title=title).all()
# 
#     def find_by_author_id(self, author_id):
#         return Post.query.filter_by(author_id=author_id).all()
# 
#     def find_posts_for_follower(self, user_id):
#         return Post.query \
#             .join(Follow, Post.author_id == Follow.followed_id) \
#             .filter(Follow.follower_id == user_id) \
#             .all()
# 
#     def create_from_json(self, json):
#         Post.insert(json)
#         self.db.session.commit()
# 
#     def update_from_json(self, json):
#         Post.insert_on_duplicate_key_update(json)
#         self.db.session.commit()
# 
#     def delete(self, post=None, post_id=None):
#         if post_id is None:
#             post_id = post.post_id
#         Post.query.filter_by(post_id=post_id).delete()
#         self.db.session.commit()
# 
#     def delete_matching_author_id(self, author_id):
#         Post.query.filter_by(author_id=author_id).delete()
#         self.db.session.commit()


# Path: example/generic_social_network/app/models/query_builders/users_dbm.py
from generic_social_network.app.models.tables.user import User
from .follows_dbm import FollowsDBM
from .posts_dbm import PostsDBM

        self.db = db

    def reset(self):
        self.db.drop_users()

    def all(self):
        return User.query.all()

    def find_by_id(self, id_):
        matching = User.query.filter_by(user_id=id_).all()
        if len(matching) == 0:
            return None
        if len(matching) == 1:
            return matching[0]
        raise Exception

    def create_from_json(self, json):
        User.insert(json)
        self.db.session.commit()

    def update_from_json(self, json):
        User.insert_on_duplicate_key_update(json)
        self.db.session.commit()

    def delete(self, user=None, user_id=None):
        if user_id is None:
            user_id = user.user_id
        User.query.filter_by(user_id=user_id).delete()
        self.db.session.commit()
        FollowsDBM(self.db).delete_matching_follower_id(user_id)

        FollowsDBM(self.db).delete_matching_followed_id(user_id)