From c1ab4e5b67fd2aa1fb9e275bdb620898a956218c Mon Sep 17 00:00:00 2001 From: Egor Gavrilov Date: Tue, 27 Feb 2024 21:13:24 +0300 Subject: [PATCH] Setup mapper for DTOs. --- .../dto/output/MainNewsEntryDto.java | 3 +- .../dto/output/MoreTextNewsEntryDto.java | 3 +- .../com/egor/rssaggregator/entity/News.java | 8 +-- .../com/egor/rssaggregator/entity/User.java | 1 + .../mapper/input/FeedInputMapper.java | 10 ++++ .../mapper/output/FeedOutputMapper.java | 10 ++++ .../mapper/output/NewsEntryOutputDto.java | 12 +++++ .../mapper/input/FeedInputMapperImpl.java | 29 +++++++++++ .../mapper/output/FeedOutputMapperImpl.java | 29 +++++++++++ .../mapper/output/NewsEntryOutputDtoImpl.java | 51 +++++++++++++++++++ 10 files changed, 148 insertions(+), 8 deletions(-) create mode 100644 backend/src/main/java/com/egor/rssaggregator/mapper/input/FeedInputMapper.java create mode 100644 backend/src/main/java/com/egor/rssaggregator/mapper/output/FeedOutputMapper.java create mode 100644 backend/src/main/java/com/egor/rssaggregator/mapper/output/NewsEntryOutputDto.java create mode 100644 target/generated-sources/annotations/com/egor/rssaggregator/mapper/input/FeedInputMapperImpl.java create mode 100644 target/generated-sources/annotations/com/egor/rssaggregator/mapper/output/FeedOutputMapperImpl.java create mode 100644 target/generated-sources/annotations/com/egor/rssaggregator/mapper/output/NewsEntryOutputDtoImpl.java diff --git a/backend/src/main/java/com/egor/rssaggregator/dto/output/MainNewsEntryDto.java b/backend/src/main/java/com/egor/rssaggregator/dto/output/MainNewsEntryDto.java index 4a1f93b..d9e9836 100644 --- a/backend/src/main/java/com/egor/rssaggregator/dto/output/MainNewsEntryDto.java +++ b/backend/src/main/java/com/egor/rssaggregator/dto/output/MainNewsEntryDto.java @@ -1,6 +1,5 @@ package com.egor.rssaggregator.dto.output; -import com.egor.rssaggregator.entity.Feed; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Size; @@ -20,5 +19,5 @@ public class MainNewsEntryDto { private LocalDate newsDate; @NotNull - private Feed source; + private GetFeedDto source; } diff --git a/backend/src/main/java/com/egor/rssaggregator/dto/output/MoreTextNewsEntryDto.java b/backend/src/main/java/com/egor/rssaggregator/dto/output/MoreTextNewsEntryDto.java index e73fe15..a6fb006 100644 --- a/backend/src/main/java/com/egor/rssaggregator/dto/output/MoreTextNewsEntryDto.java +++ b/backend/src/main/java/com/egor/rssaggregator/dto/output/MoreTextNewsEntryDto.java @@ -1,6 +1,5 @@ package com.egor.rssaggregator.dto.output; -import com.egor.rssaggregator.entity.Feed; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Size; @@ -22,5 +21,5 @@ public class MoreTextNewsEntryDto { private String newsText; @NotNull - private Feed source; + private GetFeedDto source; } diff --git a/backend/src/main/java/com/egor/rssaggregator/entity/News.java b/backend/src/main/java/com/egor/rssaggregator/entity/News.java index 7dca56b..aaf6970 100644 --- a/backend/src/main/java/com/egor/rssaggregator/entity/News.java +++ b/backend/src/main/java/com/egor/rssaggregator/entity/News.java @@ -32,8 +32,8 @@ public class News { @ToString.Exclude @ManyToOne(cascade = CascadeType.ALL) - @JoinColumn(name = "feed_id") - private Feed feed; + @JoinColumn(name = "source_id") + private Feed source; @Override public boolean equals(Object o) { @@ -43,11 +43,11 @@ public boolean equals(Object o) { return Objects.equals(id, news.id) && Objects.equals(newsHead, news.newsHead) && Objects.equals(newsDate, news.newsDate) && - Objects.equals(feed, news.feed); + Objects.equals(source, news.source); } @Override public int hashCode() { - return Objects.hash(id, newsHead, newsDate, feed); + return Objects.hash(id, newsHead, newsDate, source); } } diff --git a/backend/src/main/java/com/egor/rssaggregator/entity/User.java b/backend/src/main/java/com/egor/rssaggregator/entity/User.java index 4fdffbd..5e35b46 100644 --- a/backend/src/main/java/com/egor/rssaggregator/entity/User.java +++ b/backend/src/main/java/com/egor/rssaggregator/entity/User.java @@ -31,6 +31,7 @@ public class User implements UserDetails { @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true) @ToString.Exclude + @Builder.Default private List feeds = new ArrayList<>(); @Override diff --git a/backend/src/main/java/com/egor/rssaggregator/mapper/input/FeedInputMapper.java b/backend/src/main/java/com/egor/rssaggregator/mapper/input/FeedInputMapper.java new file mode 100644 index 0000000..ac3d6a7 --- /dev/null +++ b/backend/src/main/java/com/egor/rssaggregator/mapper/input/FeedInputMapper.java @@ -0,0 +1,10 @@ +package com.egor.rssaggregator.mapper.input; + +import com.egor.rssaggregator.dto.input.AddFeedDto; +import com.egor.rssaggregator.entity.Feed; +import org.mapstruct.Mapper; + +@Mapper(componentModel = "spring") +public interface FeedInputMapper { + Feed toEntity(AddFeedDto addFeedDto); +} diff --git a/backend/src/main/java/com/egor/rssaggregator/mapper/output/FeedOutputMapper.java b/backend/src/main/java/com/egor/rssaggregator/mapper/output/FeedOutputMapper.java new file mode 100644 index 0000000..215044f --- /dev/null +++ b/backend/src/main/java/com/egor/rssaggregator/mapper/output/FeedOutputMapper.java @@ -0,0 +1,10 @@ +package com.egor.rssaggregator.mapper.output; + +import com.egor.rssaggregator.dto.output.GetFeedDto; +import com.egor.rssaggregator.entity.Feed; +import org.mapstruct.Mapper; + +@Mapper(componentModel = "spring") +public interface FeedOutputMapper { + GetFeedDto toDto(Feed feed); +} diff --git a/backend/src/main/java/com/egor/rssaggregator/mapper/output/NewsEntryOutputDto.java b/backend/src/main/java/com/egor/rssaggregator/mapper/output/NewsEntryOutputDto.java new file mode 100644 index 0000000..5fe5147 --- /dev/null +++ b/backend/src/main/java/com/egor/rssaggregator/mapper/output/NewsEntryOutputDto.java @@ -0,0 +1,12 @@ +package com.egor.rssaggregator.mapper.output; + +import com.egor.rssaggregator.dto.output.MainNewsEntryDto; +import com.egor.rssaggregator.dto.output.MoreTextNewsEntryDto; +import com.egor.rssaggregator.entity.News; +import org.mapstruct.Mapper; + +@Mapper(componentModel = "spring", uses = FeedOutputMapper.class) +public interface NewsEntryOutputDto { + MainNewsEntryDto toMainDto(News news); + MoreTextNewsEntryDto toMoreDto(News news); +} diff --git a/target/generated-sources/annotations/com/egor/rssaggregator/mapper/input/FeedInputMapperImpl.java b/target/generated-sources/annotations/com/egor/rssaggregator/mapper/input/FeedInputMapperImpl.java new file mode 100644 index 0000000..b19319a --- /dev/null +++ b/target/generated-sources/annotations/com/egor/rssaggregator/mapper/input/FeedInputMapperImpl.java @@ -0,0 +1,29 @@ +package com.egor.rssaggregator.mapper.input; + +import com.egor.rssaggregator.dto.input.AddFeedDto; +import com.egor.rssaggregator.entity.Feed; +import javax.annotation.processing.Generated; +import org.springframework.stereotype.Component; + +@Generated( + value = "org.mapstruct.ap.MappingProcessor", + date = "2024-02-27T21:12:19+0300", + comments = "version: 1.5.5.Final, compiler: javac, environment: Java 17.0.9 (Red Hat, Inc.)" +) +@Component +public class FeedInputMapperImpl implements FeedInputMapper { + + @Override + public Feed toEntity(AddFeedDto addFeedDto) { + if ( addFeedDto == null ) { + return null; + } + + Feed.FeedBuilder feed = Feed.builder(); + + feed.name( addFeedDto.getName() ); + feed.url( addFeedDto.getUrl() ); + + return feed.build(); + } +} diff --git a/target/generated-sources/annotations/com/egor/rssaggregator/mapper/output/FeedOutputMapperImpl.java b/target/generated-sources/annotations/com/egor/rssaggregator/mapper/output/FeedOutputMapperImpl.java new file mode 100644 index 0000000..9e3fadd --- /dev/null +++ b/target/generated-sources/annotations/com/egor/rssaggregator/mapper/output/FeedOutputMapperImpl.java @@ -0,0 +1,29 @@ +package com.egor.rssaggregator.mapper.output; + +import com.egor.rssaggregator.dto.output.GetFeedDto; +import com.egor.rssaggregator.entity.Feed; +import javax.annotation.processing.Generated; +import org.springframework.stereotype.Component; + +@Generated( + value = "org.mapstruct.ap.MappingProcessor", + date = "2024-02-27T21:12:19+0300", + comments = "version: 1.5.5.Final, compiler: javac, environment: Java 17.0.9 (Red Hat, Inc.)" +) +@Component +public class FeedOutputMapperImpl implements FeedOutputMapper { + + @Override + public GetFeedDto toDto(Feed feed) { + if ( feed == null ) { + return null; + } + + GetFeedDto.GetFeedDtoBuilder getFeedDto = GetFeedDto.builder(); + + getFeedDto.name( feed.getName() ); + getFeedDto.url( feed.getUrl() ); + + return getFeedDto.build(); + } +} diff --git a/target/generated-sources/annotations/com/egor/rssaggregator/mapper/output/NewsEntryOutputDtoImpl.java b/target/generated-sources/annotations/com/egor/rssaggregator/mapper/output/NewsEntryOutputDtoImpl.java new file mode 100644 index 0000000..a8e4010 --- /dev/null +++ b/target/generated-sources/annotations/com/egor/rssaggregator/mapper/output/NewsEntryOutputDtoImpl.java @@ -0,0 +1,51 @@ +package com.egor.rssaggregator.mapper.output; + +import com.egor.rssaggregator.dto.output.MainNewsEntryDto; +import com.egor.rssaggregator.dto.output.MoreTextNewsEntryDto; +import com.egor.rssaggregator.entity.News; +import javax.annotation.processing.Generated; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Generated( + value = "org.mapstruct.ap.MappingProcessor", + date = "2024-02-27T21:12:18+0300", + comments = "version: 1.5.5.Final, compiler: javac, environment: Java 17.0.9 (Red Hat, Inc.)" +) +@Component +public class NewsEntryOutputDtoImpl implements NewsEntryOutputDto { + + @Autowired + private FeedOutputMapper feedOutputMapper; + + @Override + public MainNewsEntryDto toMainDto(News news) { + if ( news == null ) { + return null; + } + + MainNewsEntryDto.MainNewsEntryDtoBuilder mainNewsEntryDto = MainNewsEntryDto.builder(); + + mainNewsEntryDto.newsHead( news.getNewsHead() ); + mainNewsEntryDto.newsDate( news.getNewsDate() ); + mainNewsEntryDto.source( feedOutputMapper.toDto( news.getSource() ) ); + + return mainNewsEntryDto.build(); + } + + @Override + public MoreTextNewsEntryDto toMoreDto(News news) { + if ( news == null ) { + return null; + } + + MoreTextNewsEntryDto.MoreTextNewsEntryDtoBuilder moreTextNewsEntryDto = MoreTextNewsEntryDto.builder(); + + moreTextNewsEntryDto.newsHead( news.getNewsHead() ); + moreTextNewsEntryDto.newsDate( news.getNewsDate() ); + moreTextNewsEntryDto.newsText( news.getNewsText() ); + moreTextNewsEntryDto.source( feedOutputMapper.toDto( news.getSource() ) ); + + return moreTextNewsEntryDto.build(); + } +}