diff --git a/src/main/java/com/wcc/platform/configuration/RepositoryConfig.java b/src/main/java/com/wcc/platform/configuration/RepositoryConfig.java index e94a26c4..5bc06a1a 100644 --- a/src/main/java/com/wcc/platform/configuration/RepositoryConfig.java +++ b/src/main/java/com/wcc/platform/configuration/RepositoryConfig.java @@ -2,8 +2,6 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.surrealdb.driver.SyncSurrealDriver; -import com.wcc.platform.domain.cms.pages.FooterPage; -import com.wcc.platform.domain.cms.pages.LandingPage; import com.wcc.platform.repository.MemberRepository; import com.wcc.platform.repository.PageRepository; import com.wcc.platform.repository.ResourceContentRepository; @@ -24,14 +22,9 @@ public ResourceContentRepository getResourceRepository(final SyncSurrealDriver d return new SurrealDbResourceRepository(driver); } - @Bean(name = "footerRepository") - public PageRepository getFooterPageRepository(final SyncSurrealDriver driver) { - return new SurrealDbPageRepository<>(driver, FooterPage.class); - } - - @Bean(name = "landingPageRepository") - public PageRepository getLandingPageRepository(final SyncSurrealDriver driver) { - return new SurrealDbPageRepository<>(driver, LandingPage.class); + @Bean + public PageRepository getPageRepository(final SyncSurrealDriver driver) { + return new SurrealDbPageRepository(driver); } /** Create FileMemberRepository bean. */ diff --git a/src/main/java/com/wcc/platform/controller/platform/PageController.java b/src/main/java/com/wcc/platform/controller/platform/PageController.java index bd697b2f..6ebb854e 100644 --- a/src/main/java/com/wcc/platform/controller/platform/PageController.java +++ b/src/main/java/com/wcc/platform/controller/platform/PageController.java @@ -5,12 +5,16 @@ import com.wcc.platform.service.PlatformService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; +import java.util.Collection; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; @@ -45,4 +49,20 @@ public ResponseEntity savePage(@RequestBody final FooterPage footerPage) public ResponseEntity saveLandingPage(@RequestBody final LandingPage page) { return ResponseEntity.ok(service.savePage(page)); } + + /** Delete Page By ID. */ + @DeleteMapping + @Operation(summary = "Delete page by id") + public ResponseEntity deletePage(@RequestParam(name = "id") final String pageId) { + service.deletePageById(pageId); + return ResponseEntity.noContent().build(); + } + + /** Get all pages content. */ + @GetMapping + @Operation(summary = "Get all pages") + @ResponseStatus(HttpStatus.OK) + public ResponseEntity> getAllResources() { + return ResponseEntity.ok(service.getAllPages()); + } } diff --git a/src/main/java/com/wcc/platform/domain/cms/pages/LandingPage.java b/src/main/java/com/wcc/platform/domain/cms/pages/LandingPage.java index 4edc30cd..c02ea3f9 100644 --- a/src/main/java/com/wcc/platform/domain/cms/pages/LandingPage.java +++ b/src/main/java/com/wcc/platform/domain/cms/pages/LandingPage.java @@ -1,6 +1,5 @@ package com.wcc.platform.domain.cms.pages; -import com.fasterxml.jackson.annotation.JsonIgnore; import com.wcc.platform.domain.cms.pages.programme.ProgrammeItem; import com.wcc.platform.domain.platform.Event; import jakarta.validation.constraints.NotNull; @@ -19,7 +18,7 @@ @AllArgsConstructor @Builder public class LandingPage { - @JsonIgnore private String id; + @NotNull private String id; @NotNull private Page heroSection; @NotNull private Page fullBannerSection; @NotNull private Section programmes; diff --git a/src/main/java/com/wcc/platform/domain/exceptions/PlatformInternalException.java b/src/main/java/com/wcc/platform/domain/exceptions/PlatformInternalException.java index 688c69c6..901d492e 100644 --- a/src/main/java/com/wcc/platform/domain/exceptions/PlatformInternalException.java +++ b/src/main/java/com/wcc/platform/domain/exceptions/PlatformInternalException.java @@ -1,9 +1,14 @@ package com.wcc.platform.domain.exceptions; +import com.wcc.platform.domain.cms.PageType; + /** Platform generic exception. */ public class PlatformInternalException extends RuntimeException { - public PlatformInternalException(final String message, final Throwable cause) { super(message, cause); } + + public PlatformInternalException(final PageType pageType, final Throwable cause) { + super("Invalid Page type " + pageType, cause); + } } diff --git a/src/main/java/com/wcc/platform/repository/PageRepository.java b/src/main/java/com/wcc/platform/repository/PageRepository.java index e297883e..f3cebd1d 100644 --- a/src/main/java/com/wcc/platform/repository/PageRepository.java +++ b/src/main/java/com/wcc/platform/repository/PageRepository.java @@ -1,4 +1,4 @@ package com.wcc.platform.repository; /** Generic page repository interface. */ -public interface PageRepository extends CrudRepository {} +public interface PageRepository extends CrudRepository {} diff --git a/src/main/java/com/wcc/platform/repository/surrealdb/SurrealDbPageRepository.java b/src/main/java/com/wcc/platform/repository/surrealdb/SurrealDbPageRepository.java index 0df74d89..7f81c847 100644 --- a/src/main/java/com/wcc/platform/repository/surrealdb/SurrealDbPageRepository.java +++ b/src/main/java/com/wcc/platform/repository/surrealdb/SurrealDbPageRepository.java @@ -7,33 +7,31 @@ import java.util.Optional; /** SurrealDB repository implementation for page. */ -public class SurrealDbPageRepository implements PageRepository { +public class SurrealDbPageRepository implements PageRepository { /* default */ static final String TABLE = "page"; private final SyncSurrealDriver driver; - private final Class entityType; - public SurrealDbPageRepository(final SyncSurrealDriver driver, final Class entityType) { + public SurrealDbPageRepository(final SyncSurrealDriver driver) { this.driver = driver; - this.entityType = entityType; } @Override - public T save(final T entity) { + public String save(final String entity) { return driver.create(TABLE, entity); } @Override - public Collection findAll() { - return driver.select(TABLE, entityType); + public Collection findAll() { + return driver.select(TABLE, String.class); } @Override - public Optional findById(final String id) { + public Optional findById(final String id) { final var key = TABLE + ":" + id; final var query = - driver.query("SELECT * FROM " + TABLE + " WHERE id = $id", Map.of("id", key), entityType); + driver.query("SELECT * FROM " + TABLE + " WHERE id = $id", Map.of("id", key), String.class); if (query.isEmpty()) { return Optional.empty(); diff --git a/src/main/java/com/wcc/platform/service/CmsService.java b/src/main/java/com/wcc/platform/service/CmsService.java index 978a3864..25c421d8 100644 --- a/src/main/java/com/wcc/platform/service/CmsService.java +++ b/src/main/java/com/wcc/platform/service/CmsService.java @@ -14,25 +14,19 @@ import com.wcc.platform.repository.PageRepository; import com.wcc.platform.utils.FileUtil; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; /** CMS service responsible for simple pages. */ @Service public class CmsService { private final ObjectMapper objectMapper; - private final PageRepository footerRepository; - private final PageRepository lpRepository; + private final PageRepository pageRepository; /** Init repositories with respective qualifiers. */ @Autowired - public CmsService( - final ObjectMapper objectMapper, - @Qualifier("footerRepository") final PageRepository footerRepository, - @Qualifier("landingPageRepository") final PageRepository lpRepository) { + public CmsService(final ObjectMapper objectMapper, final PageRepository pageRepository) { this.objectMapper = objectMapper; - this.footerRepository = footerRepository; - this.lpRepository = lpRepository; + this.pageRepository = pageRepository; } /** @@ -55,8 +49,17 @@ public TeamPage getTeam() { * @return Footer page */ public FooterPage getFooter() { - final var page = footerRepository.findById(PageType.FOOTER.name()); - return page.orElseGet(this::getFooterFallback); + final var page = pageRepository.findById(PageType.FOOTER.name()); + + if (page.isPresent()) { + try { + return objectMapper.readValue(page.get(), FooterPage.class); + } catch (JsonProcessingException e) { + throw new PlatformInternalException(e.getMessage(), e); + } + } + + return getFooterFallback(); } private FooterPage getFooterFallback() { @@ -74,9 +77,16 @@ private FooterPage getFooterFallback() { * @return Landing page of the community. */ public LandingPage getLandingPage() { - final var page = lpRepository.findById(PageType.LANDING_PAGE.name()); + final var page = pageRepository.findById(PageType.LANDING_PAGE.name()); + if (page.isPresent()) { + try { + return objectMapper.readValue(page.get(), LandingPage.class); + } catch (JsonProcessingException e) { + throw new PlatformInternalException(e.getMessage(), e); + } + } - return page.orElseGet(this::getLandingPageFallback); + return getLandingPageFallback(); } private LandingPage getLandingPageFallback() { diff --git a/src/main/java/com/wcc/platform/service/PlatformService.java b/src/main/java/com/wcc/platform/service/PlatformService.java index 40bff3af..ec8cc995 100644 --- a/src/main/java/com/wcc/platform/service/PlatformService.java +++ b/src/main/java/com/wcc/platform/service/PlatformService.java @@ -1,10 +1,14 @@ package com.wcc.platform.service; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.wcc.platform.domain.cms.PageType; import com.wcc.platform.domain.cms.pages.FooterPage; import com.wcc.platform.domain.cms.pages.LandingPage; import com.wcc.platform.domain.exceptions.ContentNotFoundException; import com.wcc.platform.domain.exceptions.DuplicatedMemberException; import com.wcc.platform.domain.exceptions.MemberNotFoundException; +import com.wcc.platform.domain.exceptions.PlatformInternalException; import com.wcc.platform.domain.platform.Member; import com.wcc.platform.domain.platform.MemberDto; import com.wcc.platform.domain.platform.ResourceContent; @@ -23,21 +27,21 @@ public class PlatformService { private final ResourceContentRepository resource; - private final PageRepository footerRepository; - private final PageRepository lpRepository; + private final PageRepository pageRepository; private final MemberRepository memberRepository; + private final ObjectMapper objectMapper; /** Constructor . */ @Autowired public PlatformService( @Qualifier("getResourceRepository") final ResourceContentRepository resource, final MemberRepository memberRepository, - @Qualifier("footerRepository") final PageRepository footerRepository, - @Qualifier("landingPageRepository") final PageRepository lpRepository) { + final PageRepository pageRepository, + final ObjectMapper objectMapper) { this.resource = resource; this.memberRepository = memberRepository; - this.footerRepository = footerRepository; - this.lpRepository = lpRepository; + this.pageRepository = pageRepository; + this.objectMapper = objectMapper; } public ResourceContent saveResourceContent(final ResourceContent resourceContent) { @@ -46,12 +50,24 @@ public ResourceContent saveResourceContent(final ResourceContent resourceContent /** Save any type of page based on page Type. */ public Object savePage(final LandingPage page) { - return lpRepository.save(page); + try { + return pageRepository.save(objectMapper.writeValueAsString(page)); + } catch (JsonProcessingException e) { + throw new PlatformInternalException(PageType.LANDING_PAGE, e); + } } /** Save any type of page based on page Type. */ public Object savePage(final FooterPage page) { - return footerRepository.save(page); + try { + return pageRepository.save(objectMapper.writeValueAsString(page)); + } catch (JsonProcessingException e) { + throw new PlatformInternalException(PageType.FOOTER, e); + } + } + + public Collection getAllPages() { + return pageRepository.findAll(); } public Collection getAllResources() { @@ -81,6 +97,10 @@ public void deleteById(final String id) { resource.deleteById(result.getId()); } + public void deletePageById(final String id) { + pageRepository.findById(id).ifPresent(pageRepository::deleteById); + } + /** Save Member into storage. */ public Member createMember(final Member member) { final Optional memberOptional = emailExists(member.getEmail()); diff --git a/src/test/java/com/wcc/platform/repository/surrealdb/SurrealDbPageRepositoryTest.java b/src/test/java/com/wcc/platform/repository/surrealdb/SurrealDbPageRepositoryTest.java index fbcdcf16..de6c0658 100644 --- a/src/test/java/com/wcc/platform/repository/surrealdb/SurrealDbPageRepositoryTest.java +++ b/src/test/java/com/wcc/platform/repository/surrealdb/SurrealDbPageRepositoryTest.java @@ -1,6 +1,5 @@ package com.wcc.platform.repository.surrealdb; -import static com.wcc.platform.factories.SetupFactories.createFooterPageTest; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.anyMap; @@ -12,10 +11,9 @@ import com.surrealdb.driver.SyncSurrealDriver; import com.wcc.platform.domain.cms.PageType; -import com.wcc.platform.domain.cms.pages.FooterPage; import java.util.Collection; +import java.util.Collections; import java.util.List; -import java.util.Optional; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mock; @@ -24,56 +22,55 @@ class SurrealDbPageRepositoryTest { private static final String TABLE = SurrealDbPageRepository.TABLE; + private static final String PAGE = "{ 'page': { 'title': 'title 1' } }"; @Mock private SyncSurrealDriver mockDriver; - private SurrealDbPageRepository repository; + private SurrealDbPageRepository repository; @BeforeEach void setUp() { MockitoAnnotations.openMocks(this); - repository = new SurrealDbPageRepository<>(mockDriver, FooterPage.class); + repository = new SurrealDbPageRepository(mockDriver); } @Test void testSave() { - FooterPage entity = createFooterPageTest(); - when(mockDriver.create(TABLE, entity)).thenReturn(entity); + when(mockDriver.create(TABLE, PAGE)).thenReturn(PAGE); - var savedEntity = repository.save(entity); + var savedEntity = repository.save(PAGE); - verify(mockDriver, times(1)).create(TABLE, entity); - assertEquals(entity, savedEntity); + verify(mockDriver, times(1)).create(TABLE, PAGE); + assertEquals(PAGE, savedEntity); } @Test void testFindAll() { - List mockResult = List.of(createFooterPageTest()); + List mockResult = Collections.singletonList(PAGE); + when(mockDriver.select(TABLE, String.class)).thenReturn(mockResult); - when(mockDriver.select(TABLE, FooterPage.class)).thenReturn(mockResult); + Collection result = repository.findAll(); - Collection result = repository.findAll(); - - verify(mockDriver, times(1)).select(TABLE, FooterPage.class); + verify(mockDriver, times(1)).select(TABLE, String.class); assertEquals(mockResult, result); } @Test void testFindByIdNotFoundCase1() { - Optional result = repository.findById(PageType.FOOTER.name()); + var result = repository.findById(PageType.FOOTER.name()); - verify(mockDriver, times(1)).query(anyString(), anyMap(), eq(FooterPage.class)); + verify(mockDriver, times(1)).query(anyString(), anyMap(), eq(String.class)); assertTrue(result.isEmpty()); } @Test void testFindByIdNotFoundCase2() { - when(mockDriver.query(anyString(), anyMap(), eq(FooterPage.class))).thenReturn(List.of()); + when(mockDriver.query(anyString(), anyMap(), eq(String.class))).thenReturn(List.of()); - Optional result = repository.findById("test-id-2"); + var result = repository.findById("test-id-2"); - verify(mockDriver, times(1)).query(anyString(), anyMap(), eq(FooterPage.class)); + verify(mockDriver, times(1)).query(anyString(), anyMap(), eq(String.class)); assertTrue(result.isEmpty()); } diff --git a/src/test/java/com/wcc/platform/service/CmsServiceTest.java b/src/test/java/com/wcc/platform/service/CmsServiceTest.java index 3fd46f9f..e1d6ceec 100644 --- a/src/test/java/com/wcc/platform/service/CmsServiceTest.java +++ b/src/test/java/com/wcc/platform/service/CmsServiceTest.java @@ -24,8 +24,7 @@ import org.mockito.MockitoAnnotations; class CmsServiceTest { - @Mock private PageRepository footerRepository; - @Mock private PageRepository landingPageRepository; + @Mock private PageRepository pageRepository; @Mock private ObjectMapper objectMapper; private CmsService service; @@ -34,7 +33,7 @@ class CmsServiceTest { void setUp() { MockitoAnnotations.openMocks(this); - service = new CmsService(objectMapper, footerRepository, landingPageRepository); + service = new CmsService(objectMapper, pageRepository); } @Test