Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mapping Solution #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion Backend/mapping/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
buildscript {
ext {
springBootVersion = '2.0.5.RELEASE'
mapstructVersion = "1.5.5.Final"
}
repositories {
mavenCentral()
Expand All @@ -23,8 +24,22 @@ repositories {
mavenCentral()
}


dependencies {
implementation "org.mapstruct:mapstruct:${mapstructVersion}"
implementation "io.springfox:springfox-swagger2:2.7.0"
implementation "io.springfox:springfox-swagger-ui:2.7.0"
testImplementation "org.testng:testng:6.10", "org.easytesting:fest-assert:1.4", "org.assertj:assertj-core:3.24.2"
annotationProcessor "org.mapstruct:mapstruct-processor:1.5.5.Final"
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
}

tasks.withType(JavaCompile) {
options.compilerArgs = [
'-Amapstruct.suppressGeneratorTimestamp=true'
]
}

test {
useTestNG()
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,27 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableSwagger2 //http://localhost:8080/swagger-ui.html
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

@Bean
public Docket mappingApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.staffinghub.coding.challenges.mapping.mappers;

import com.staffinghub.coding.challenges.mapping.mappers.blocks.IMappableBlock;
import com.staffinghub.coding.challenges.mapping.mappers.blocks.IMappableBlockDto;
import com.staffinghub.coding.challenges.mapping.mappers.blocks.MasterMapper;
import com.staffinghub.coding.challenges.mapping.models.db.Article;
import com.staffinghub.coding.challenges.mapping.models.db.blocks.ArticleBlock;
import com.staffinghub.coding.challenges.mapping.models.dto.ArticleDto;
import com.staffinghub.coding.challenges.mapping.models.dto.blocks.*;
import org.mapstruct.InheritInverseConfiguration;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collection;
import java.util.Comparator;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

@Mapper(componentModel = "spring")
public abstract class IArticleMapper {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does the name of the Abstract class start with an I.

I is traditionally used for Interface and Java devs normally prefix abstract classes with Abstract, thin it comes from the days before Intellisense


@Autowired
protected MasterMapper masterMapper;

@Mapping(target="blocks", expression = "java(getArticleBlocks(dto.getBlocks()))")
@Mapping(target = "lastModified", ignore = true)
@Mapping(target = "lastModifiedBy", ignore = true)
public abstract Article toArticle(ArticleDto dto);

@InheritInverseConfiguration
@Mapping(target="blocks", expression = "java(getArticleBlocksDto(article.getBlocks()))")
public abstract ArticleDto toArticleDto(Article article);

@Named("getArticleBlocks")
protected Set<ArticleBlock> getArticleBlocks(Collection<ArticleBlockDto> articleBlockDtos) {
return articleBlockDtos
.stream()
.map(block -> {
if (Objects.isNull(block)) {
return null;
}

if (block instanceof IMappableBlock) {
return block.map(masterMapper);
}

// TODO: Add exception handler to mitigate the need to use a try catch.
throw new UnsupportedOperationException(String.format("Failed to map %s as it's not implementing Mappable Article Dto Block", block.getClass()));
})
.collect(Collectors.toSet());
}

@Named("getArticleBlocksDto")
protected Collection<ArticleBlockDto> getArticleBlocksDto(Set<ArticleBlock> article) {
return article
.stream()
.map(block -> {
if (Objects.isNull(block)) {
return null;
}

if (block instanceof IMappableBlockDto) {
return block.map(masterMapper);
}

// TODO: Add exception handler to mitigate the need to use a try catch.
throw new UnsupportedOperationException(String.format("Failed to map %s as it's not implementing Mappable Article Block", block.getClass()));
})
.sorted(Comparator.comparingLong(articleBlock -> articleBlock.getSortIndex()))
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.staffinghub.coding.challenges.mapping.mappers.blocks;

import com.staffinghub.coding.challenges.mapping.models.db.blocks.*;
import com.staffinghub.coding.challenges.mapping.models.dto.blocks.*;

public interface IMainArticleBlockDtoMapper {

ArticleBlockDto map(ArticleBlock entity);

GalleryBlockDto map(GalleryBlock entity);

ImageBlockDto map(ImageBlock entity);

TextBlockDto map(TextBlock entity);

VideoBlockDto map(VideoBlock entity);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.staffinghub.coding.challenges.mapping.mappers.blocks;

import com.staffinghub.coding.challenges.mapping.models.db.blocks.*;
import com.staffinghub.coding.challenges.mapping.models.dto.blocks.*;

public interface IMainArticleBlockMapper {

ArticleBlock map(ArticleBlockDto entity);

GalleryBlock map(GalleryBlockDto entity);

ImageBlock map(ImageBlockDto entity);

TextBlock map(TextBlockDto entity);

VideoBlock map(VideoBlockDto entity);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.staffinghub.coding.challenges.mapping.mappers.blocks;

import com.staffinghub.coding.challenges.mapping.models.db.blocks.ArticleBlock;

public interface IMappableBlock {
ArticleBlock map(IMainArticleBlockMapper mapper);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.staffinghub.coding.challenges.mapping.mappers.blocks;

import com.staffinghub.coding.challenges.mapping.models.dto.blocks.ArticleBlockDto;

public interface IMappableBlockDto {
ArticleBlockDto map(IMainArticleBlockDtoMapper mapper);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.staffinghub.coding.challenges.mapping.mappers.blocks;

import com.staffinghub.coding.challenges.mapping.mappers.IArticleMapper;
import com.staffinghub.coding.challenges.mapping.mappers.blocks.mappers.*;
import com.staffinghub.coding.challenges.mapping.models.db.blocks.*;
import com.staffinghub.coding.challenges.mapping.models.dto.blocks.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MasterMapper implements IMainArticleBlockDtoMapper, IMainArticleBlockMapper {

@Autowired
private IGalleryBlockMapper galleryBlockMapper;

@Autowired
private IImageBlockMapper imageBlockMapper;

@Autowired
private ITextBlockMapper textBlockMapper;

@Autowired
private IVideoBlockMapper videoBlockMapper;

//<editor-fold desc="To Dto">

@Override
public ArticleBlockDto map(ArticleBlock entity) {
return null;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is there not toDto for this?

}

@Override
public GalleryBlockDto map(GalleryBlock entity) {
return galleryBlockMapper.toGalleryBlockDto(entity);
}

@Override
public ImageBlockDto map(ImageBlock entity) {
return imageBlockMapper.toImageBlockDto(entity);
}

@Override
public TextBlockDto map(TextBlock entity) {
return textBlockMapper.toTextBlockDto(entity);
}

@Override
public VideoBlockDto map(VideoBlock entity) {
return videoBlockMapper.toVideoBlockDto(entity);
}
//</editor-fold>

//<editor-fold desc="From Dto">

@Override
public ArticleBlock map(ArticleBlockDto entity) {
return null;
Copy link

@wscheffer wscheffer Jul 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is there no toBlock for this method?

}

@Override
public GalleryBlock map(GalleryBlockDto entity) {
return galleryBlockMapper.toGalleryBlock(entity);
}

@Override
public ImageBlock map(ImageBlockDto entity) {
return imageBlockMapper.toImageBlock(entity);
}

@Override
public TextBlock map(TextBlockDto entity) {
return textBlockMapper.toTextBlock(entity);
}

@Override
public VideoBlock map(VideoBlockDto entity) {
return videoBlockMapper.toVideoBlock(entity);
}
//</editor-fold>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.staffinghub.coding.challenges.mapping.mappers.blocks.mappers;

import com.staffinghub.coding.challenges.mapping.models.db.blocks.GalleryBlock;
import com.staffinghub.coding.challenges.mapping.models.dto.blocks.GalleryBlockDto;
import org.mapstruct.Mapper;

@Mapper(componentModel = "spring")
public interface IGalleryBlockMapper {

GalleryBlock toGalleryBlock(GalleryBlockDto galleryBlockDto);

GalleryBlockDto toGalleryBlockDto(GalleryBlock galleryBlock);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.staffinghub.coding.challenges.mapping.mappers.blocks.mappers;

import com.staffinghub.coding.challenges.mapping.models.db.blocks.ImageBlock;
import com.staffinghub.coding.challenges.mapping.models.dto.blocks.ImageBlockDto;
import org.mapstruct.Mapper;

@Mapper(componentModel = "spring")
public interface IImageBlockMapper {

ImageBlock toImageBlock(ImageBlockDto imageBlockDto);

ImageBlockDto toImageBlockDto(ImageBlock imageBlock);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.staffinghub.coding.challenges.mapping.mappers.blocks.mappers;

import com.staffinghub.coding.challenges.mapping.models.db.blocks.TextBlock;
import com.staffinghub.coding.challenges.mapping.models.dto.blocks.TextBlockDto;
import org.mapstruct.Mapper;

@Mapper(componentModel = "spring")
public interface ITextBlockMapper {

TextBlock toTextBlock(TextBlockDto textBlockDto);

TextBlockDto toTextBlockDto(TextBlock textBlock);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.staffinghub.coding.challenges.mapping.mappers.blocks.mappers;

import com.staffinghub.coding.challenges.mapping.models.db.blocks.VideoBlock;
import com.staffinghub.coding.challenges.mapping.models.dto.blocks.VideoBlockDto;
import org.mapstruct.Mapper;

@Mapper(componentModel = "spring")
public interface IVideoBlockMapper {

VideoBlock toVideoBlock(VideoBlockDto videoBlockDto);

VideoBlockDto toVideoBlockDto(VideoBlock videoBlock);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.staffinghub.coding.challenges.mapping.models.db.blocks;

public abstract class ArticleBlock {
import com.staffinghub.coding.challenges.mapping.mappers.blocks.IMainArticleBlockDtoMapper;
import com.staffinghub.coding.challenges.mapping.mappers.blocks.IMappableBlockDto;
import com.staffinghub.coding.challenges.mapping.models.dto.blocks.ArticleBlockDto;

public abstract class ArticleBlock implements IMappableBlockDto {

private int sortIndex;

Expand All @@ -11,4 +15,8 @@ public int getSortIndex() {
public void setSortIndex(int sortIndex) {
this.sortIndex = sortIndex;
}

public ArticleBlockDto map(IMainArticleBlockDtoMapper mapper) {
return mapper.map(this);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.staffinghub.coding.challenges.mapping.models.db.blocks;

import com.staffinghub.coding.challenges.mapping.mappers.blocks.IMainArticleBlockDtoMapper;
import com.staffinghub.coding.challenges.mapping.models.db.Image;
import com.staffinghub.coding.challenges.mapping.models.dto.blocks.GalleryBlockDto;

import java.util.List;

Expand All @@ -15,4 +17,8 @@ public List<Image> getImages() {
public void setImages(List<Image> images) {
this.images = images;
}

public GalleryBlockDto map(IMainArticleBlockDtoMapper mapper) {
return mapper.map(this);
}
}
Loading