Skip to content

Commit 183f0cf

Browse files
committed
add infrastructure adapters with images
1 parent 5373888 commit 183f0cf

4 files changed

Lines changed: 215 additions & 28 deletions

File tree

Lines changed: 64 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.mastercloudapps.twitterscheduler.infrastructure.adapter;
22

33
import java.time.Instant;
4+
import java.util.ArrayList;
45
import java.util.Collection;
6+
import java.util.List;
57
import java.util.Optional;
68
import java.util.stream.Collectors;
79

@@ -11,6 +13,7 @@
1113
import com.mastercloudapps.twitterscheduler.domain.exception.RepositoryException;
1214
import com.mastercloudapps.twitterscheduler.domain.pending.PendingTweet;
1315
import com.mastercloudapps.twitterscheduler.domain.pending.PendingTweetPort;
16+
import com.mastercloudapps.twitterscheduler.infrastructure.jpa.pending.PendingTweetImageJpaMapper;
1417
import com.mastercloudapps.twitterscheduler.infrastructure.jpa.pending.PendingTweetJpaEntity;
1518
import com.mastercloudapps.twitterscheduler.infrastructure.jpa.pending.PendingTweetJpaMapper;
1619
import com.mastercloudapps.twitterscheduler.infrastructure.jpa.pending.PendingTweetJpaRepository;
@@ -21,28 +24,45 @@ public class PendingTweetAdapter implements PendingTweetPort {
2124
private static final String DATA_ACCESS_ERROR = "An unexpected error occurred executing a data access operation";
2225

2326
private PendingTweetJpaRepository pendingTweetJpaRepository;
27+
28+
private PendingTweetJpaMapper pendingTweetMapper;
29+
30+
private PendingTweetImageJpaMapper pendingTweetImageMapper;
2431

25-
private PendingTweetJpaMapper mapper;
26-
27-
public PendingTweetAdapter(PendingTweetJpaRepository pendingTweetJpaRepository,
28-
PendingTweetJpaMapper mapper) {
32+
public PendingTweetAdapter(final PendingTweetJpaRepository pendingTweetJpaRepository,
33+
final PendingTweetJpaMapper pendingTweetMapper,
34+
final PendingTweetImageJpaMapper pendingTweetImageMapper) {
2935

3036
this.pendingTweetJpaRepository = pendingTweetJpaRepository;
31-
this.mapper = mapper;
37+
this.pendingTweetMapper = pendingTweetMapper;
38+
this.pendingTweetImageMapper = pendingTweetImageMapper;
3239
}
3340

3441
@Override
3542
public PendingTweet create(PendingTweet pendingTweet) {
3643

3744
try {
38-
PendingTweetJpaEntity pendingTweetJpaEntity = mapper.mapCreatePendingTweet(pendingTweet);
45+
PendingTweetJpaEntity pendingTweetJpaEntity = pendingTweetMapper.mapCreatePendingTweet(pendingTweet);
46+
47+
final var dbImages = pendingTweet.getImages().stream()
48+
.map(image -> pendingTweetImageMapper.mapCreatePendingTweetImage(image, pendingTweetJpaEntity))
49+
.collect(Collectors.toList());
50+
51+
pendingTweetJpaEntity.setImages(dbImages);
3952
pendingTweetJpaRepository.save(pendingTweetJpaEntity);
4053

4154
PendingTweetJpaEntity pendingTweetJpaResponse = pendingTweetJpaRepository
4255
.findById(pendingTweetJpaEntity.getId())
4356
.orElseThrow();
4457

45-
return mapper.mapEntity(pendingTweetJpaResponse);
58+
final var createdPendingTweet = pendingTweetMapper.mapEntity(pendingTweetJpaResponse);
59+
60+
final var domainImages = pendingTweetJpaResponse.getImages().stream()
61+
.map(image -> pendingTweetImageMapper.mapEntity(image))
62+
.collect(Collectors.toList());
63+
createdPendingTweet.addImages(domainImages);
64+
65+
return createdPendingTweet;
4666

4767
} catch (DataAccessException ex) {
4868

@@ -60,28 +80,56 @@ public void delete(Long id) {
6080
@Override
6181
public Collection<PendingTweet> findAll() {
6282

63-
return pendingTweetJpaRepository.findAll().stream()
64-
.map(entity -> mapper.mapEntity(entity))
65-
.collect(Collectors.toList());
83+
final var pendingTweetsDb = pendingTweetJpaRepository.findAll();
84+
85+
List<PendingTweet> pendingTweets = new ArrayList<>();
86+
87+
for (PendingTweetJpaEntity pendingTweetDb : pendingTweetsDb) {
88+
final var pendingTweet = pendingTweetMapper.mapEntity(pendingTweetDb);
89+
final var domainImages = pendingTweetDb.getImages().stream()
90+
.map(image -> pendingTweetImageMapper.mapEntity(image))
91+
.collect(Collectors.toList());
92+
pendingTweet.addImages(domainImages);
93+
pendingTweets.add(pendingTweet);
94+
}
95+
96+
return pendingTweets;
6697
}
6798

6899
@Override
69100
public Optional<PendingTweet> findOne(Long id) {
70101

71102
final var pendingTweetJpa = pendingTweetJpaRepository.findById(id);
72103

73-
if (pendingTweetJpa.isPresent()) {
74-
return Optional.of(mapper.mapEntity(pendingTweetJpa.get()));
104+
if (pendingTweetJpa.isPresent()) {
105+
final var pendingTweet = pendingTweetMapper.mapEntity(pendingTweetJpa.get());
106+
final var domainImages = pendingTweetJpa.get().getImages().stream()
107+
.map(image -> pendingTweetImageMapper.mapEntity(image))
108+
.collect(Collectors.toList());
109+
pendingTweet.addImages(domainImages);
110+
111+
return Optional.of(pendingTweet);
75112
}
76113
return Optional.empty();
77114
}
78115

79116
@Override
80117
public Collection<PendingTweet> findPendingForPublish(Instant date) {
81118

82-
return pendingTweetJpaRepository.findPendingForPublish(date).stream()
83-
.map(entity -> mapper.mapEntity(entity))
84-
.collect(Collectors.toList());
119+
final var pendingTweetsDb = pendingTweetJpaRepository.findPendingForPublish(date);
120+
121+
List<PendingTweet> pendingTweets = new ArrayList<>();
122+
123+
for (PendingTweetJpaEntity pendingTweetDb : pendingTweetsDb) {
124+
final var pendingTweet = pendingTweetMapper.mapEntity(pendingTweetDb);
125+
final var domainImages = pendingTweetDb.getImages().stream()
126+
.map(image -> pendingTweetImageMapper.mapEntity(image))
127+
.collect(Collectors.toList());
128+
pendingTweet.addImages(domainImages);
129+
pendingTweets.add(pendingTweet);
130+
}
131+
132+
return pendingTweets;
85133
}
86134

87-
}
135+
}

src/main/java/com/mastercloudapps/twitterscheduler/infrastructure/adapter/TweetAdapter.java

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.mastercloudapps.twitterscheduler.infrastructure.adapter;
22

3+
import java.util.ArrayList;
34
import java.util.Collection;
5+
import java.util.List;
46
import java.util.Optional;
57
import java.util.stream.Collectors;
68

@@ -10,6 +12,7 @@
1012
import com.mastercloudapps.twitterscheduler.domain.exception.RepositoryException;
1113
import com.mastercloudapps.twitterscheduler.domain.tweet.Tweet;
1214
import com.mastercloudapps.twitterscheduler.domain.tweet.TweetPort;
15+
import com.mastercloudapps.twitterscheduler.infrastructure.jpa.tweet.TweetImageJpaMapper;
1316
import com.mastercloudapps.twitterscheduler.infrastructure.jpa.tweet.TweetJpaEntity;
1417
import com.mastercloudapps.twitterscheduler.infrastructure.jpa.tweet.TweetJpaMapper;
1518
import com.mastercloudapps.twitterscheduler.infrastructure.jpa.tweet.TweetJpaRepository;
@@ -18,30 +21,46 @@
1821
public class TweetAdapter implements TweetPort {
1922

2023
private static final String DATA_ACCESS_ERROR = "An unexpected error occurred executing a data access operation";
21-
24+
2225
private TweetJpaRepository tweetJpaRepository;
2326

24-
private TweetJpaMapper mapper;
27+
private TweetJpaMapper tweetMapper;
28+
29+
private TweetImageJpaMapper tweetImageMapper;
2530

26-
public TweetAdapter(TweetJpaRepository tweetJpaRepository,
27-
TweetJpaMapper mapper) {
31+
public TweetAdapter(final TweetJpaRepository tweetJpaRepository,
32+
final TweetJpaMapper tweetMapper,
33+
final TweetImageJpaMapper tweetImageMapper) {
2834

2935
this.tweetJpaRepository = tweetJpaRepository;
30-
this.mapper = mapper;
36+
this.tweetMapper = tweetMapper;
37+
this.tweetImageMapper = tweetImageMapper;
3138
}
3239

3340
@Override
3441
public Tweet create(Tweet tweet) {
3542

3643
try {
37-
TweetJpaEntity tweetJpaEntity = mapper.mapDomainObject(tweet);
44+
TweetJpaEntity tweetJpaEntity = tweetMapper.mapDomainObject(tweet);
45+
46+
final var dbImages = tweet.getImages().stream()
47+
.map(image -> tweetImageMapper.mapDomainObject(image, tweetJpaEntity))
48+
.collect(Collectors.toList());
49+
tweetJpaEntity.setImages(dbImages);
50+
3851
tweetJpaRepository.save(tweetJpaEntity);
3952

4053
TweetJpaEntity tweetJpaResponse = tweetJpaRepository
4154
.findById(tweetJpaEntity.getId())
4255
.orElseThrow();
4356

44-
return mapper.mapEntity(tweetJpaResponse);
57+
final var createdTweet = tweetMapper.mapEntity(tweetJpaResponse);
58+
final var domainImages = tweetJpaResponse.getImages().stream()
59+
.map(image -> tweetImageMapper.mapEntity(image))
60+
.collect(Collectors.toList());
61+
createdTweet.addImages(domainImages);
62+
63+
return tweetMapper.mapEntity(tweetJpaResponse);
4564

4665
} catch (DataAccessException ex) {
4766

@@ -59,9 +78,20 @@ public void delete(Long id) {
5978
@Override
6079
public Collection<Tweet> findAll() {
6180

62-
return tweetJpaRepository.findAll().stream()
63-
.map(entity -> mapper.mapEntity(entity))
64-
.collect(Collectors.toList());
81+
final var tweetsDb = tweetJpaRepository.findAll();
82+
83+
List<Tweet> tweets = new ArrayList<>();
84+
85+
for (TweetJpaEntity tweetDb : tweetsDb) {
86+
final var tweet = tweetMapper.mapEntity(tweetDb);
87+
final var domainImages = tweetDb.getImages().stream()
88+
.map(image -> tweetImageMapper.mapEntity(image))
89+
.collect(Collectors.toList());
90+
tweet.addImages(domainImages);
91+
tweets.add(tweet);
92+
}
93+
94+
return tweets;
6595
}
6696

6797
@Override
@@ -70,9 +100,15 @@ public Optional<Tweet> findOne(Long id) {
70100
final var tweetJpa = tweetJpaRepository.findById(id);
71101

72102
if (tweetJpa.isPresent()) {
73-
return Optional.of(mapper.mapEntity(tweetJpa.get()));
103+
final var tweet = tweetMapper.mapEntity(tweetJpa.get());
104+
final var domainImages = tweetJpa.get().getImages().stream()
105+
.map(image -> tweetImageMapper.mapEntity(image))
106+
.collect(Collectors.toList());
107+
tweet.addImages(domainImages);
108+
109+
return Optional.of(tweet);
74110
}
75111
return Optional.empty();
76112
}
77113

78-
}
114+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.mastercloudapps.twitterscheduler.infrastructure.jpa.pending;
2+
3+
import java.util.Optional;
4+
5+
import org.springframework.stereotype.Component;
6+
7+
import com.mastercloudapps.twitterscheduler.domain.exception.RepositoryException;
8+
import com.mastercloudapps.twitterscheduler.domain.pending.PendingTweetImage;
9+
10+
@Component
11+
public class PendingTweetImageJpaMapper {
12+
13+
public PendingTweetImage mapEntity(final PendingTweetImageJpaEntity pendingTweetImageEntity) {
14+
15+
if (Optional.ofNullable(pendingTweetImageEntity).isEmpty()) {
16+
throw new RepositoryException("Empty entity");
17+
}
18+
19+
final var builder = PendingTweetImage
20+
.builder()
21+
.id(pendingTweetImageEntity.getId())
22+
.url(pendingTweetImageEntity.getUrl());
23+
24+
return builder.build();
25+
}
26+
27+
public PendingTweetImageJpaEntity mapCreatePendingTweetImage(final PendingTweetImage pendingTweetImage,
28+
final PendingTweetJpaEntity pendingTweetJpaEntity) {
29+
30+
if (Optional.ofNullable(pendingTweetImage).isEmpty()) {
31+
throw new RepositoryException("Empty domain object");
32+
}
33+
34+
final var builder = PendingTweetImageJpaEntity
35+
.builder()
36+
.url(pendingTweetImage.url().url())
37+
.pendingTweet(pendingTweetJpaEntity);
38+
39+
return builder.build();
40+
}
41+
42+
public PendingTweetImageJpaEntity mapDomainObject(final PendingTweetImage pendingTweetImage) {
43+
44+
if (Optional.ofNullable(pendingTweetImage).isEmpty()) {
45+
throw new RepositoryException("Empty domain object");
46+
}
47+
48+
final var builder = PendingTweetImageJpaEntity
49+
.builder()
50+
.id(pendingTweetImage.id().id())
51+
.url(pendingTweetImage.url().url());
52+
53+
return builder.build();
54+
}
55+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.mastercloudapps.twitterscheduler.infrastructure.jpa.tweet;
2+
3+
import java.util.Optional;
4+
5+
import org.springframework.stereotype.Component;
6+
7+
import com.mastercloudapps.twitterscheduler.domain.exception.RepositoryException;
8+
import com.mastercloudapps.twitterscheduler.domain.tweet.TweetImage;
9+
10+
@Component
11+
public class TweetImageJpaMapper {
12+
13+
public TweetImage mapEntity(final TweetImageJpaEntity tweetImageEntity) {
14+
15+
if (Optional.ofNullable(tweetImageEntity).isEmpty()) {
16+
throw new RepositoryException("Empty entity");
17+
}
18+
19+
final var builder = TweetImage
20+
.builder()
21+
.id(tweetImageEntity.getId())
22+
.size(tweetImageEntity.getSize())
23+
.type(tweetImageEntity.getType())
24+
.width(tweetImageEntity.getWidth())
25+
.height(tweetImageEntity.getHeight());
26+
27+
return builder.build();
28+
}
29+
30+
public TweetImageJpaEntity mapDomainObject(final TweetImage tweetImage,
31+
final TweetJpaEntity tweetJpaEntity) {
32+
33+
if (Optional.ofNullable(tweetImage).isEmpty()) {
34+
throw new RepositoryException("Empty domain object");
35+
}
36+
37+
final var builder = TweetImageJpaEntity
38+
.builder()
39+
.id(tweetImage.id().id())
40+
.size(tweetImage.size().size())
41+
.type(tweetImage.type().type())
42+
.width(tweetImage.width().width())
43+
.height(tweetImage.height().height())
44+
.tweet(tweetJpaEntity);
45+
46+
return builder.build();
47+
}
48+
}

0 commit comments

Comments
 (0)