from database import db,Template,MarketPlace
import tools

def list():
    return db.query(Template).all()

def get(id):
    return db.query(Template.id == id).first()

def add(marketplace,template_id):
    url = db.query(MarketPlace).filter(MarketPlace.id == marketplace).first().url
    marketplace_content = tools.fetch_remote_json(url)
    template_content = marketplace_content[template_id]
    new_template = Template(template_id=template_id,marketplace=marketplace,content=template_content)
    db.add(new_template)
    db.commit()
    db.refresh(new_template)
    return new_template



from database import db,Template,MarketPlace
import tools

def list():
    return db.query(Template).all()

def get(id):
    return db.query(Template.id == id).first()

def add(marketplace,template_id):
    url = db.query(MarketPlace).filter(MarketPlace.id == marketplace).first().url
    marketplace_content = tools.fetch_remote_json(url)
    template_content = marketplace_content[template_id]
    new_template = Template(template_id=template_id,marketplace=marketplace,content=template_content)
    db.add(new_template)
    db.commit()
    db.refresh(new_template)
    return new_template


