forked from digital2morrow/coding-challenge
-
Notifications
You must be signed in to change notification settings - Fork 1
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
WesleyBuck
wants to merge
1
commit into
RetroRabbit:master
Choose a base branch
from
WesleyBuck:mapping_solution
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 0 additions & 19 deletions
19
...apping/src/main/java/com/staffinghub/coding/challenges/mapping/mappers/ArticleMapper.java
This file was deleted.
Oops, something went wrong.
75 changes: 75 additions & 0 deletions
75
...pping/src/main/java/com/staffinghub/coding/challenges/mapping/mappers/IArticleMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
@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()); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
.../com/staffinghub/coding/challenges/mapping/mappers/blocks/IMainArticleBlockDtoMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
17 changes: 17 additions & 0 deletions
17
...ava/com/staffinghub/coding/challenges/mapping/mappers/blocks/IMainArticleBlockMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
7 changes: 7 additions & 0 deletions
7
...rc/main/java/com/staffinghub/coding/challenges/mapping/mappers/blocks/IMappableBlock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
7 changes: 7 additions & 0 deletions
7
...main/java/com/staffinghub/coding/challenges/mapping/mappers/blocks/IMappableBlockDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
80 changes: 80 additions & 0 deletions
80
.../src/main/java/com/staffinghub/coding/challenges/mapping/mappers/blocks/MasterMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
} |
13 changes: 13 additions & 0 deletions
13
...com/staffinghub/coding/challenges/mapping/mappers/blocks/mappers/IGalleryBlockMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
13 changes: 13 additions & 0 deletions
13
...a/com/staffinghub/coding/challenges/mapping/mappers/blocks/mappers/IImageBlockMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
13 changes: 13 additions & 0 deletions
13
...va/com/staffinghub/coding/challenges/mapping/mappers/blocks/mappers/ITextBlockMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
13 changes: 13 additions & 0 deletions
13
...a/com/staffinghub/coding/challenges/mapping/mappers/blocks/mappers/IVideoBlockMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 withAbstract
, thin it comes from the days before Intellisense