diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/ApplicationController.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/ApplicationController.java index 5a009d3d89..8b919f479c 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/ApplicationController.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/ApplicationController.java @@ -9,6 +9,7 @@ import com.appsmith.server.services.ApplicationService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; @@ -52,4 +53,11 @@ public class ApplicationController extends BaseController new ResponseDTO<>(HttpStatus.OK.value(), updatedApplication, null)); } + @DeleteMapping("/{id}") + public Mono> delete(@PathVariable String id) { + log.debug("Going to delete application with id: {}", id); + return applicationPageService.deleteApplication(id) + .map(deletedResource -> new ResponseDTO<>(HttpStatus.OK.value(), deletedResource, null)); + } + } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/PageController.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/PageController.java index 1fa4aa78f5..9378db06a7 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/PageController.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/PageController.java @@ -10,6 +10,7 @@ import com.appsmith.server.services.PageService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; @@ -68,4 +69,11 @@ public class PageController extends BaseController { return applicationPageService.getPageByName(applicationName, pageName, true) .map(page -> new ResponseDTO<>(HttpStatus.OK.value(), page, null)); } + + @DeleteMapping("/{id}") + public Mono> delete(@PathVariable String id) { + log.debug("Going to delete page with id: {}", id); + return service.delete(id) + .map(deletedResource -> new ResponseDTO<>(HttpStatus.OK.value(), deletedResource, null)); + } } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/BaseRepository.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/BaseRepository.java index eef3852672..1abb7cd7af 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/BaseRepository.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/BaseRepository.java @@ -16,7 +16,7 @@ public interface BaseRepository extends ReactiveMong * @param T The entity which needs to be archived * @return Mono */ - Mono archiveById(T entity); + Mono archive(T entity); /** * This function directly updates the document by setting the deleted flag to true for the entity with the given id diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/BaseRepositoryImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/BaseRepositoryImpl.java index 16336d0654..51692b8223 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/BaseRepositoryImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/BaseRepositoryImpl.java @@ -76,7 +76,7 @@ public class BaseRepositoryImpl e } @Override - public Mono archiveById(T entity) { + public Mono archive(T entity) { Assert.notNull(entity, "The given entity must not be null!"); Assert.notNull(entity.getId(), "The given entity's id must not be null!"); Assert.isTrue(!entity.getDeleted(), "The given entity is already deleted"); diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ApplicationPageService.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ApplicationPageService.java index cadad9097d..d5fa335c2d 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ApplicationPageService.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ApplicationPageService.java @@ -18,4 +18,6 @@ public interface ApplicationPageService { Mono getPageByName(String applicationName, String pageName, Boolean viewMode); Mono makePageDefault(String applicationId, String pageId); + + Mono deleteApplication(String id); } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ApplicationPageServiceImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ApplicationPageServiceImpl.java index 69040e59b2..7f06890f2e 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ApplicationPageServiceImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ApplicationPageServiceImpl.java @@ -1,5 +1,6 @@ package com.appsmith.server.services; +import com.appsmith.server.constants.AnalyticsEvents; import com.appsmith.server.constants.FieldName; import com.appsmith.server.domains.Application; import com.appsmith.server.domains.ApplicationPage; @@ -8,23 +9,30 @@ import com.appsmith.server.domains.Page; import com.appsmith.server.domains.User; import com.appsmith.server.exceptions.AppsmithError; import com.appsmith.server.exceptions.AppsmithException; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import reactor.core.publisher.Mono; import java.util.ArrayList; import java.util.List; +@Slf4j @Service public class ApplicationPageServiceImpl implements ApplicationPageService { private final ApplicationService applicationService; private final PageService pageService; private final SessionUserService sessionUserService; + private final AnalyticsService analyticsService; + public ApplicationPageServiceImpl(ApplicationService applicationService, - PageService pageService, SessionUserService sessionUserService) { + PageService pageService, + SessionUserService sessionUserService, + AnalyticsService analyticsService) { this.applicationService = applicationService; this.pageService = pageService; this.sessionUserService = sessionUserService; + this.analyticsService = analyticsService; } public Mono createPage(Page page) { @@ -188,4 +196,33 @@ public class ApplicationPageServiceImpl implements ApplicationPageService { .flatMap(savedPage -> addPageToApplication(Mono.just(savedApplication), savedPage, true)); }); } + + /** + * This function performs a soft delete for the application along with it's associated pages and actions. + * + * @param id The application id to delete + * @return The modified application object with the deleted flag set + */ + @Override + public Mono deleteApplication(String id) { + log.debug("Archiving application with id: {}", id); + + Mono applicationMono = applicationService.findById(id) + .switchIfEmpty(Mono.error(new AppsmithException(AppsmithError.NO_RESOURCE_FOUND, "application", id))) + .flatMap(application -> { + log.debug("Archiving pages for applicationId: {}", id); + return pageService.findByApplicationId(id) + .flatMap(page -> pageService.delete(page.getId())) + .collectList() + .thenReturn(application); + }) + .flatMap(application -> applicationService.archive(application)); + + return applicationMono + .map(deletedObj -> { + analyticsService.sendEvent(AnalyticsEvents.DELETE + "_" + deletedObj.getClass().getSimpleName().toUpperCase(), (Application) deletedObj); + return (Application) deletedObj; + }); + } + } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ApplicationService.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ApplicationService.java index bf9d94c083..082f1e2cd1 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ApplicationService.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ApplicationService.java @@ -14,4 +14,5 @@ public interface ApplicationService extends CrudService { Mono save(Application application); + Mono archive(Application application); } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ApplicationServiceImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ApplicationServiceImpl.java index e0ac89801b..58ae93a8f4 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ApplicationServiceImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ApplicationServiceImpl.java @@ -1,12 +1,9 @@ package com.appsmith.server.services; -import com.appsmith.server.constants.AnalyticsEvents; import com.appsmith.server.constants.FieldName; -import com.appsmith.server.domains.Action; import com.appsmith.server.domains.Application; import com.appsmith.server.domains.ApplicationPage; import com.appsmith.server.domains.Layout; -import com.appsmith.server.domains.Page; import com.appsmith.server.domains.User; import com.appsmith.server.exceptions.AppsmithError; import com.appsmith.server.exceptions.AppsmithException; @@ -103,6 +100,11 @@ public class ApplicationServiceImpl extends BaseService archive(Application application) { + return repository.archive(application); + } + /** * This function walks through all the pages in the application. In each page, it walks through all the layouts. * In a layout, dsl and publishedDsl JSONObjects exist. Publish function is responsible for copying the dsl into @@ -144,44 +146,4 @@ public class ApplicationServiceImpl extends BaseService true); } - - /** - * This function performs a soft delete for the application along with it's associated pages and actions. - * - * @param id The application id to delete - * @return The modified application object with the deleted flag set - */ - @Override - public Mono delete(String id) { - log.debug("Archiving application with id: {}", id); - - Mono applicationMono = repository.findById(id) - .switchIfEmpty(Mono.error(new AppsmithException(AppsmithError.NO_RESOURCE_FOUND, "application", id))) - .flatMap(application -> pageRepository.findByApplicationId(id) - .flatMap(page -> { - log.debug("Going to archive pageId: {} for applicationId: {}", page.getId(), id); - Mono archivedPageMono = pageRepository.archiveById(page); - Mono> archivedActionsMono = actionRepository.findByPageId(page.getId()) - .flatMap(action -> { - log.debug("Going to archive actionId: {} for applicationId: {}", action.getId(), id); - return actionRepository.archiveById(action); - }) - .collectList(); - return Mono.zip(archivedPageMono, archivedActionsMono) - .map(tuple -> { - Page page1 = tuple.getT1(); - List actions = tuple.getT2(); - log.debug("Archived pageId: {} and {} actions for applicationId: {}", page1.getId(), actions.size(), id); - return page1; - }); - }) - .collectList() - .flatMap(pages -> repository.archiveById(application))); - - return applicationMono - .map(deletedObj -> { - analyticsService.sendEvent(AnalyticsEvents.DELETE + "_" + deletedObj.getClass().getSimpleName().toUpperCase(), (Application) deletedObj); - return (Application) deletedObj; - }); - } } \ No newline at end of file diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/PageService.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/PageService.java index edb8c0bd62..a45d636c2b 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/PageService.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/PageService.java @@ -10,6 +10,8 @@ public interface PageService extends CrudService { Mono findById(String pageId); + Flux findByApplicationId(String applicationId); + Mono save(Page page); Mono findByIdAndLayoutsId(String pageId, String layoutId); diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/PageServiceImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/PageServiceImpl.java index b01aaea3e2..ee1613ebea 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/PageServiceImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/PageServiceImpl.java @@ -1,6 +1,8 @@ package com.appsmith.server.services; +import com.appsmith.server.constants.AnalyticsEvents; import com.appsmith.server.constants.FieldName; +import com.appsmith.server.domains.Action; import com.appsmith.server.domains.Application; import com.appsmith.server.domains.ApplicationPage; import com.appsmith.server.domains.Layout; @@ -8,6 +10,7 @@ import com.appsmith.server.domains.Page; import com.appsmith.server.dtos.PageNameIdDTO; import com.appsmith.server.exceptions.AppsmithError; import com.appsmith.server.exceptions.AppsmithException; +import com.appsmith.server.repositories.ActionRepository; import com.appsmith.server.repositories.PageRepository; import lombok.extern.slf4j.Slf4j; import org.bson.types.ObjectId; @@ -27,6 +30,7 @@ import java.util.List; public class PageServiceImpl extends BaseService implements PageService { private final ApplicationService applicationService; + private final ActionRepository actionRepository; @Autowired public PageServiceImpl(Scheduler scheduler, @@ -35,9 +39,11 @@ public class PageServiceImpl extends BaseService i ReactiveMongoTemplate reactiveMongoTemplate, PageRepository repository, ApplicationService applicationService, - AnalyticsService analyticsService) { + AnalyticsService analyticsService, + ActionRepository actionRepository) { super(scheduler, validator, mongoConverter, reactiveMongoTemplate, repository, analyticsService); this.applicationService = applicationService; + this.actionRepository = actionRepository; } @Override @@ -45,6 +51,11 @@ public class PageServiceImpl extends BaseService i return repository.findById(pageId); } + @Override + public Flux findByApplicationId(String applicationId) { + return repository.findByApplicationId(applicationId); + } + @Override public Mono save(Page page) { return repository.save(page); @@ -72,6 +83,39 @@ public class PageServiceImpl extends BaseService i return repository.deleteAll(); } + /** + * This function archives the page and all the actions associated with that page. + * + * @param id The pageId which needs to be archived. + * @return + */ + @Override + public Mono delete(String id) { + Mono pageMono = repository.findById(id) + .switchIfEmpty(Mono.error(new AppsmithException(AppsmithError.NO_RESOURCE_FOUND, FieldName.PAGE_ID, id))) + .flatMap(page -> { + log.debug("Going to archive pageId: {} for applicationId: {}", page.getId(), page.getApplicationId()); + Mono archivedPageMono = repository.archive(page); + Mono> archivedActionsMono = actionRepository.findByPageId(page.getId()) + .flatMap(action -> { + log.debug("Going to archive actionId: {} for applicationId: {}", action.getId(), id); + return actionRepository.archive(action); + }).collectList(); + return Mono.zip(archivedPageMono, archivedActionsMono) + .map(tuple -> { + Page page1 = tuple.getT1(); + List actions = tuple.getT2(); + log.debug("Archived pageId: {} and {} actions for applicationId: {}", page1.getId(), actions.size(), id); + return page1; + }); + }); + + return pageMono.map(deletedObj -> { + analyticsService.sendEvent(AnalyticsEvents.DELETE + "_" + deletedObj.getClass().getSimpleName().toUpperCase(), (Page) deletedObj); + return (Page) deletedObj; + }); + } + @Override public Flux findNamesByApplicationId(String applicationId) { return applicationService