# Path: models/application.py
# class ApplicationModel(BaseModel):
#     table = "applications"
#     db = "console"
#     
#     fields={
#         "application_id":True,
#         "container_id":True,
#         "project_url":True,
#         "project_name":True,
#         "storage_path":True,
#         "app_name":True,
#         "run_host":True,
#         "inspect_container":True,
#         "user_id":True,
#         "status":True,
#         "logs":True,
#         "update_time":True,
#         'create_time':True
#     }


from models.application import ApplicationModel
from tornado import gen

class ApplicationService():
    m_application = ApplicationModel()
    
    @gen.coroutine
    def insert_application(self,application,callback=None):
        application_id = application.get("application_id",None)
        app = None
        if application_id is not None:
            app = yield self.m_application.find_one(application_id)
        model = {}
        if app is None:
            model = yield self.m_application.insert_one(application)
            app = yield self.m_application.find_one(model.inserted_id)
            application["_id"] = str(model.inserted_id)
        else:
            model = yield self.m_application.update_one({ "_id":application_id},{"$set":application})
        raise gen.Return(application)
    
    @gen.coroutine
    def exist_application(self,application_id,callback=None):
        result = yield self.m_application.find_one(application_id)
        if result==None or not isinstance(result,dict):
            raise gen.Return(False)
        else:

# Path: models/application.py
# class ApplicationModel(BaseModel):
#     table = "applications"
#     db = "console"
#     
#     fields={
#         "application_id":True,
#         "container_id":True,
#         "project_url":True,
#         "project_name":True,
#         "storage_path":True,
#         "app_name":True,
#         "run_host":True,
#         "inspect_container":True,
#         "user_id":True,
#         "status":True,
#         "logs":True,
#         "update_time":True,
#         'create_time':True
#     }


# Path: services/application.py
from models.application import ApplicationModel
from tornado import gen


class ApplicationService():
    m_application = ApplicationModel()
    
    @gen.coroutine
    def insert_application(self,application,callback=None):
        application_id = application.get("application_id",None)
        app = None
        if application_id is not None:
            app = yield self.m_application.find_one(application_id)
        model = {}
        if app is None:
            model = yield self.m_application.insert_one(application)
            app = yield self.m_application.find_one(model.inserted_id)
            application["_id"] = str(model.inserted_id)
        else:
            model = yield self.m_application.update_one({ "_id":application_id},{"$set":application})
        raise gen.Return(application)
    
    @gen.coroutine
    def exist_application(self,application_id,callback=None):
        result = yield self.m_application.find_one(application_id)
        if result==None or not isinstance(result,dict):
            raise gen.Return(False)
        else:

            raise gen.Return(True)