Skip to content

Commit

Permalink
Setup mapper for DTOs.
Browse files Browse the repository at this point in the history
  • Loading branch information
gavrilovegor519 committed Feb 27, 2024
1 parent 84b8817 commit c1ab4e5
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -20,5 +19,5 @@ public class MainNewsEntryDto {
private LocalDate newsDate;

@NotNull
private Feed source;
private GetFeedDto source;
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -22,5 +21,5 @@ public class MoreTextNewsEntryDto {
private String newsText;

@NotNull
private Feed source;
private GetFeedDto source;
}
8 changes: 4 additions & 4 deletions backend/src/main/java/com/egor/rssaggregator/entity/News.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class User implements UserDetails {

@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
@ToString.Exclude
@Builder.Default
private List<Feed> feeds = new ArrayList<>();

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit c1ab4e5

Please sign in to comment.