diff --git a/backend/app/controllers/app.py b/backend/app/controllers/app.py index a907b68c..8c5cc625 100644 --- a/backend/app/controllers/app.py +++ b/backend/app/controllers/app.py @@ -16,6 +16,7 @@ def add(): _ = getI18n("controllers") name = request.json.get('app_name') tenant_id = request.json.get('app_tenant_id') + app_id = request.json.get('app_id') default_source_branch = request.json.get('app_default_source_branch') default_target_branch = request.json.get('app_default_target_branch') description = request.json.get('app_description') @@ -23,11 +24,17 @@ def add(): creater = session['username'] try: - app = Application.create(tenant_id, creater, name, description, default_source_branch, default_target_branch) - appID = app.app_id + if app_id: + app = Application.update_application(app_id, name=name, description=description, default_source_branch=default_source_branch, default_target_branch=default_target_branch) + ApplicationService.delete_service_by_app_id(app_id) + appID = app_id + else: + app = Application.create(tenant_id, creater, name, description, default_source_branch, default_target_branch) + appID = app.app_id for service in services: - newService = ApplicationService.create_service(appID, service["service_name"], service["service_git_path"], service["service_workflow"], service["service_role"], service["service_language"], service["service_framework"], service["service_database"], service["service_api_type"], service["service_api_location"], service["service_container_name"], service["service_container_group"], service["service_region"], service["service_public_ip"], service["service_security_group"], service["service_cd_subnet"], service["service_struct_cache"]) + if "service_name" in service: + newService = ApplicationService.create_service(appID, service["service_name"], service["service_git_path"], service["service_workflow"], service["service_role"], service["service_language"], service["service_framework"], service["service_database"], service["service_api_type"], service["service_api_location"], service["service_container_name"], service["service_container_group"], service["service_region"], service["service_public_ip"], service["service_security_group"], service["service_cd_subnet"], service["service_struct_cache"]) ApplicationServiceLib.create_libs(newService.service_id, service["service_libs_name"]) diff --git a/backend/app/models/application.py b/backend/app/models/application.py index d9d77c81..7e98e88e 100644 --- a/backend/app/models/application.py +++ b/backend/app/models/application.py @@ -61,4 +61,13 @@ def get_application_by_id(appID): 'default_target_branch': app.default_target_branch, 'service': ApplicationService.get_services_by_app_id(app.app_id) } - return app_dict \ No newline at end of file + return app_dict + + def update_application(app_id, **kwargs): + app = Application.query.get(app_id) + if app: + for key, value in kwargs.items(): + setattr(app, key, value) + db.session.commit() + return app + return None \ No newline at end of file diff --git a/backend/app/models/application_service.py b/backend/app/models/application_service.py index 6a16afe9..fad05598 100644 --- a/backend/app/models/application_service.py +++ b/backend/app/models/application_service.py @@ -5,6 +5,7 @@ class ApplicationService(db.Model): service_id = db.Column(db.Integer, primary_key=True) app_id = db.Column(db.Integer, db.ForeignKey('application.app_id'), nullable=False) name = db.Column(db.String(255), nullable=False) + status = db.Column(db.String(50)) git_path = db.Column(db.String(255)) git_workflow = db.Column(db.String(255)) role = db.Column(db.Text) @@ -23,12 +24,16 @@ class ApplicationService(db.Model): created_at = db.Column(db.TIMESTAMP, server_default=db.text('CURRENT_TIMESTAMP')) updated_at = db.Column(db.TIMESTAMP, server_default=db.text('CURRENT_TIMESTAMP')) + STATUS_DELETE = "DELETED" + STATUS_OK = "OK" + def create_service(app_id, name, git_path, git_workflow, role, language, framework, database, api_type, api_location, cd_container_name, cd_container_group, cd_region, cd_public_ip, cd_security_group, cd_subnet, struct_cache): service = ApplicationService( app_id=app_id, name=name, git_path=git_path, + status=ApplicationService.STATUS_OK, git_workflow=git_workflow, role=role, language=language, @@ -59,10 +64,6 @@ def get_service_by_id(cls, service_id): @staticmethod def get_service_by_name(appID, service_name): services = ApplicationService.query.filter_by(name=service_name, app_id=appID).all() - print("########") - print(appID) - print(service_name) - print(services) service_dict = {} for service in services: @@ -71,6 +72,7 @@ def get_service_by_name(appID, service_name): 'app_id': service.app_id, 'name': service.name, 'git_path': service.git_path, + 'status': service.status, 'git_workflow': service.git_workflow, 'role': service.role, 'language': service.language, @@ -90,17 +92,14 @@ def get_service_by_name(appID, service_name): return service_dict - def update_service(self, name, git_path, git_workflow, role, language, framework, database, api_type, api_location): - self.name = name - self.git_path = git_path - self.git_workflow = git_workflow - self.role = role - self.language = language - self.framework = framework - self.database = database - self.api_type = api_type - self.api_location = api_location - db.session.commit() + def update_service(self, service_id, **kwargs): + service = self.query.get(service_id) + if service: + for key, value in kwargs.items(): + setattr(service, key, value) + db.session.commit() + return service + return None @classmethod def delete_service(cls, service_id): @@ -110,10 +109,19 @@ def delete_service(cls, service_id): db.session.commit() return True return False + + @classmethod + def delete_service_by_app_id(cls, app_id): + services = cls.query.filter_by(app_id=app_id).all() + for service in services: + re = cls.update_service(cls, service.service_id, status=cls.STATUS_DELETE) + if not re: + return False + return True @classmethod def get_services_by_app_id(cls, app_id): - services = cls.query.filter_by(app_id=app_id).all() + services = cls.query.filter_by(app_id=app_id, status=cls.STATUS_OK).all() services_list = [] for service in services: @@ -122,6 +130,7 @@ def get_services_by_app_id(cls, app_id): 'app_id': service.app_id, 'name': service.name, 'git_path': service.git_path, + 'status': service.status, 'git_workflow': service.git_workflow, 'role': service.role, 'language': service.language, diff --git a/db/database.db b/db/database.db index b55fc119..c230c221 100644 Binary files a/db/database.db and b/db/database.db differ diff --git a/docs/files/WeChat-zhushou.png b/docs/files/WeChat-zhushou.png index 9b12053e..bdd3ca73 100644 Binary files a/docs/files/WeChat-zhushou.png and b/docs/files/WeChat-zhushou.png differ diff --git a/frontend/static/js/app.js b/frontend/static/js/app.js index 8699da54..a3e87681 100644 --- a/frontend/static/js/app.js +++ b/frontend/static/js/app.js @@ -7,11 +7,13 @@ $(document).ready(function () { }); $("#add-application").click(function () { + cleanUp() $('#app-edit').modal('show'); }); $("#app-edit-save").click(function () { var requestData = { + 'app_id': $("#app_id").val(), 'app_name': $("#app_name").val(), 'app_description': $("#app_description").val(), 'app_default_source_branch': $("#app_default_source_branch").val(), @@ -51,7 +53,7 @@ $(document).ready(function () { }); $("#app-edit-cancel").click(function () { - $('#app-edit').modal('hide'); + location.reload(); }); $("#add-service").click(function(){ @@ -141,8 +143,22 @@ $(document).ready(function () { }); }); +function removeSubservice(idx){ + $("#subservice_"+idx).remove() +} + +function cleanUp() { + $('.subservice').remove(); + $("#app_id").val('') + $("#app_default_source_branch").val('') + $("#app_default_target_branch").val('') + $("#app_description").val('') + $("#app_name").val('') +} + function showApp(appID) { $('#app-edit').modal('show'); + cleanUp() var requestData = { 'app_id': appID } @@ -158,90 +174,91 @@ function showApp(appID) { subserviceLen = subservice.length serviceID = subserviceLen+1 - data.service.forEach(function (service, element_index, element_array) { + data.service.forEach(function (service, ele_idx, element_array) { + idx = ele_idx+1 serviceID = service.service_id libsStr = "" service.libs.forEach(function (lib, element_index, element_array) { libsStr += lib.sys_lib_name+"," }); libsStr = libsStr.replace(/,$/, ''); - str = `` $("#add-service").after(str) - $("#subservice_"+serviceID).slideDown("500") + $("#subservice_"+idx).slideDown("500") setTimeout(function () { $('#app-edit').modal('refresh'); }, 550);