Skip to content

Commit

Permalink
Revert "Revert "feat: Adjust repository to create single table for pa…
Browse files Browse the repository at this point in the history
…ges (#12…"

This reverts commit a3b4169.
  • Loading branch information
dricazenck authored Dec 9, 2024
1 parent e57294d commit c193d2f
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 67 deletions.
13 changes: 3 additions & 10 deletions src/main/java/com/wcc/platform/configuration/RepositoryConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,14 +22,9 @@ public ResourceContentRepository getResourceRepository(final SyncSurrealDriver d
return new SurrealDbResourceRepository(driver);
}

@Bean(name = "footerRepository")
public PageRepository<FooterPage> getFooterPageRepository(final SyncSurrealDriver driver) {
return new SurrealDbPageRepository<>(driver, FooterPage.class);
}

@Bean(name = "landingPageRepository")
public PageRepository<LandingPage> getLandingPageRepository(final SyncSurrealDriver driver) {
return new SurrealDbPageRepository<>(driver, LandingPage.class);
@Bean
public PageRepository getPageRepository(final SyncSurrealDriver driver) {
return new SurrealDbPageRepository(driver);
}

/** Create FileMemberRepository bean. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -45,4 +49,20 @@ public ResponseEntity<Object> savePage(@RequestBody final FooterPage footerPage)
public ResponseEntity<Object> saveLandingPage(@RequestBody final LandingPage page) {
return ResponseEntity.ok(service.savePage(page));
}

/** Delete Page By ID. */
@DeleteMapping
@Operation(summary = "Delete page by id")
public ResponseEntity<Void> 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<Collection<String>> getAllResources() {
return ResponseEntity.ok(service.getAllPages());
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<ProgrammeItem> programmes;
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.wcc.platform.repository;

/** Generic page repository interface. */
public interface PageRepository<T> extends CrudRepository<T, String> {}
public interface PageRepository extends CrudRepository<String, String> {}
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,31 @@
import java.util.Optional;

/** SurrealDB repository implementation for page. */
public class SurrealDbPageRepository<T> implements PageRepository<T> {
public class SurrealDbPageRepository implements PageRepository {

/* default */ static final String TABLE = "page";

private final SyncSurrealDriver driver;
private final Class<T> entityType;

public SurrealDbPageRepository(final SyncSurrealDriver driver, final Class<T> 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<T> findAll() {
return driver.select(TABLE, entityType);
public Collection<String> findAll() {
return driver.select(TABLE, String.class);
}

@Override
public Optional<T> findById(final String id) {
public Optional<String> 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();
Expand Down
36 changes: 23 additions & 13 deletions src/main/java/com/wcc/platform/service/CmsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<FooterPage> footerRepository;
private final PageRepository<LandingPage> lpRepository;
private final PageRepository pageRepository;

/** Init repositories with respective qualifiers. */
@Autowired
public CmsService(
final ObjectMapper objectMapper,
@Qualifier("footerRepository") final PageRepository<FooterPage> footerRepository,
@Qualifier("landingPageRepository") final PageRepository<LandingPage> lpRepository) {
public CmsService(final ObjectMapper objectMapper, final PageRepository pageRepository) {
this.objectMapper = objectMapper;
this.footerRepository = footerRepository;
this.lpRepository = lpRepository;
this.pageRepository = pageRepository;
}

/**
Expand All @@ -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() {
Expand All @@ -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() {
Expand Down
36 changes: 28 additions & 8 deletions src/main/java/com/wcc/platform/service/PlatformService.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -23,21 +27,21 @@
public class PlatformService {

private final ResourceContentRepository resource;
private final PageRepository<FooterPage> footerRepository;
private final PageRepository<LandingPage> 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<FooterPage> footerRepository,
@Qualifier("landingPageRepository") final PageRepository<LandingPage> 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) {
Expand All @@ -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<String> getAllPages() {
return pageRepository.findAll();
}

public Collection<ResourceContent> getAllResources() {
Expand Down Expand Up @@ -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<Member> memberOptional = emailExists(member.getEmail());
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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<FooterPage> 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<FooterPage> mockResult = List.of(createFooterPageTest());
List<String> mockResult = Collections.singletonList(PAGE);
when(mockDriver.select(TABLE, String.class)).thenReturn(mockResult);

when(mockDriver.select(TABLE, FooterPage.class)).thenReturn(mockResult);
Collection<String> result = repository.findAll();

Collection<FooterPage> 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<FooterPage> 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<FooterPage> 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());
}

Expand Down
Loading

0 comments on commit c193d2f

Please sign in to comment.