Skip to content

Commit fe31ab3

Browse files
committed
created a new aggregate with events and string uuid
1 parent e57422a commit fe31ab3

File tree

10 files changed

+139
-55
lines changed

10 files changed

+139
-55
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.apssouza.eventsourcing.aggregates;
7+
8+
import com.apssouza.infra.AppEvent;
9+
import java.util.List;
10+
import java.util.concurrent.CopyOnWriteArrayList;
11+
12+
/**
13+
*
14+
* @author apssouza
15+
*/
16+
public abstract class AbstractAggregate implements Aggregate {
17+
18+
private String uuid;
19+
private List<AppEvent> changes;
20+
21+
22+
protected CopyOnWriteArrayList<AppEvent> appendChange(EmailAggregate item, AppEvent event) {
23+
CopyOnWriteArrayList<AppEvent> listChanges = new CopyOnWriteArrayList(changes);
24+
listChanges.add(event);
25+
return listChanges;
26+
}
27+
28+
@Override
29+
public List<AppEvent> getUncommittedChanges() {
30+
return changes;
31+
}
32+
33+
@Override
34+
public String getUuid() {
35+
return uuid;
36+
}
37+
38+
}

mail-service/src/main/java/com/apssouza/eventsourcing/aggregates/Aggregate.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,5 @@ public interface Aggregate {
1616

1717
Aggregate markChangesAsCommitted();
1818

19-
ObjectState getState();
2019

2120
}

mail-service/src/main/java/com/apssouza/eventsourcing/aggregates/EmailAggregate.java

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88
import com.apssouza.eventsourcing.events.EmailCreatedEvent;
99
import com.apssouza.eventsourcing.events.EmailDeliveredEvent;
1010
import com.apssouza.eventsourcing.events.EmailDeletedEvent;
11+
import com.apssouza.eventsourcing.events.EmailEvent;
1112
import com.apssouza.eventsourcing.events.EmailSentEvent;
1213
import com.apssouza.infra.AppEvent;
13-
import java.util.Arrays;
1414
import java.util.List;
15-
import java.util.UUID;
1615
import java.util.concurrent.CopyOnWriteArrayList;
1716

1817
/**
@@ -21,46 +20,53 @@
2120
*
2221
* @author apssouza
2322
*/
24-
public class EmailAggregate implements Aggregate {
23+
public class EmailAggregate extends AbstractAggregate implements Aggregate {
2524

2625
private final String uuid;
2726
private final List<AppEvent> changes;
28-
private final EmailState state;
27+
private final Email state;
2928

3029
public EmailAggregate(String uuid, List<AppEvent> changes) {
31-
this(uuid, changes, EmailState.CREATED);
30+
this(uuid, changes, new Email());
3231
}
3332

34-
public EmailAggregate(String uuid, List<AppEvent> changes, EmailState state) {
33+
public EmailAggregate(String uuid, List<AppEvent> changes, Email state) {
3534
this.uuid = uuid;
3635
this.changes = changes;
3736
this.state = state;
3837
}
3938

4039
public EmailAggregate create(EmailCreateCommand command) {
41-
Email email = new Email(command.getName(), command.getEmail());
40+
Email email = new Email(command.getName(), command.getEmail(), EmailState.CREATED);
4241
return applyChange(new EmailCreatedEvent(uuid, email));
4342
}
4443

4544
public EmailAggregate send(EmailSendCommand command) throws Exception {
46-
if (state == EmailState.DELETED) {
45+
if (state.getState() == EmailState.DELETED) {
4746
throw new Exception("Is not possible to edit a deleted user");
4847
}
49-
return applyChange(new EmailSentEvent(command.getUuid()));
48+
return applyChange(new EmailSentEvent(
49+
command.getUuid(),
50+
getState().setState(EmailState.SENT)));
5051
}
5152

5253
public EmailAggregate delivery(EmailDeliveryCommand command) throws Exception {
53-
if (state == EmailState.DELETED) {
54+
if (state.getState() == EmailState.DELETED) {
5455
throw new Exception("Is not possible to edit a deleted user");
5556
}
56-
return applyChange(new EmailDeliveredEvent(command.getUuid()));
57+
return applyChange(new EmailDeliveredEvent(
58+
command.getUuid(),
59+
getState().setState(EmailState.DELIVERED)
60+
));
5761
}
5862

5963
public EmailAggregate delete(EmailDeleteCommand command) throws Exception {
60-
if (state == EmailState.DELETED) {
64+
if (state.getState() == EmailState.DELETED) {
6165
throw new Exception("Is not possible to delete a deleted user");
6266
}
63-
return applyChange(new EmailDeletedEvent(uuid, command));
67+
return applyChange(new EmailDeletedEvent(uuid,
68+
getState().setState(EmailState.DELETED)
69+
));
6470
}
6571

6672
private EmailAggregate apply(AppEvent event) {
@@ -81,24 +87,24 @@ private EmailAggregate apply(AppEvent event) {
8187
}
8288

8389
private EmailAggregate apply(EmailCreatedEvent event) {
84-
return new EmailAggregate(uuid, changes, EmailState.CREATED);
90+
return new EmailAggregate(uuid, changes, event.getEmail());
8591
}
8692

8793
private EmailAggregate apply(EmailDeletedEvent event) {
88-
return new EmailAggregate(uuid, changes, EmailState.DELETED);
94+
return new EmailAggregate(uuid, changes, event.getEmail());
8995
}
9096

9197
private EmailAggregate apply(EmailSentEvent event) {
92-
return new EmailAggregate(uuid, changes, EmailState.SENT);
98+
return new EmailAggregate(uuid, changes, event.getEmail());
9399
}
94100

95101
private EmailAggregate apply(EmailDeliveredEvent event) {
96-
return new EmailAggregate(uuid, changes, EmailState.DELIVERED);
102+
return new EmailAggregate(uuid, changes, event.getEmail());
97103
}
98104

99105
public static EmailAggregate from(String uuid, List<AppEvent> history) {
100106

101-
EmailAggregate emailAggregate = new EmailAggregate(uuid, new CopyOnWriteArrayList(), EmailState.CREATED);
107+
EmailAggregate emailAggregate = new EmailAggregate(uuid, new CopyOnWriteArrayList(), new Email());
102108

103109
return history
104110
.stream()
@@ -119,33 +125,18 @@ private EmailAggregate applyChange(AppEvent event, boolean isNew) {
119125
}
120126
}
121127

122-
private CopyOnWriteArrayList<AppEvent> appendChange(EmailAggregate item, AppEvent event) {
123-
CopyOnWriteArrayList<AppEvent> listChanges = new CopyOnWriteArrayList(changes);
124-
listChanges.add(event);
125-
return listChanges;
126-
}
127128

128-
private EmailAggregate applyChange(AppEvent event) {
129+
private EmailAggregate applyChange(EmailEvent event) {
129130
return applyChange(event, true);
130131
}
131132

132-
@Override
133-
public List<AppEvent> getUncommittedChanges() {
134-
return changes;
135-
}
136133

137134
@Override
138135
public EmailAggregate markChangesAsCommitted() {
139136
return new EmailAggregate(uuid, new CopyOnWriteArrayList(), state);
140137
}
141138

142-
@Override
143-
public String getUuid() {
144-
return uuid;
145-
}
146-
147-
@Override
148-
public EmailState getState() {
139+
public Email getState() {
149140
return state;
150141
}
151142

mail-service/src/main/java/com/apssouza/eventsourcing/entities/Email.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.apssouza.eventsourcing.entities;
22

3+
import com.apssouza.eventsourcing.aggregates.EmailState;
34
import javax.persistence.Column;
45
import javax.persistence.Entity;
56
import javax.persistence.GeneratedValue;
@@ -31,23 +32,31 @@ public class Email {
3132
@NotNull
3233
@Column(unique = true)
3334
private String email;
35+
36+
private EmailState state;
3437

3538
@Version
3639
private long version;
3740

3841
public Email() {
3942
}
4043

41-
public Email(String name, String email) {
44+
public Email(String name, String email, EmailState state) {
4245
this.name = name;
4346
this.email = email;
47+
this.state = state;
4448
}
4549

46-
public Email(long id, String name, String email) {
47-
this(name, email);
50+
public Email(long id, String name, String email, EmailState state) {
51+
this(name, email,state);
4852
this.id = id;
4953
}
5054

55+
public EmailState getState() {
56+
return state;
57+
}
58+
59+
5160
public long getId() {
5261
return id;
5362
}
@@ -93,4 +102,9 @@ public String toString() {
93102
return "Email{" + "id=" + id + ", authId=" + authId + ", name=" + name + ", email=" + email + ", version=" + version + '}';
94103
}
95104

105+
public Email setState(EmailState emailState) {
106+
state = emailState;
107+
return this;
108+
}
109+
96110
}

mail-service/src/main/java/com/apssouza/eventsourcing/events/EmailCreatedEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
* @author apssouza
1212
*/
13-
public class EmailCreatedEvent extends AbstractDomainEvent implements AppEvent {
13+
public class EmailCreatedEvent extends AbstractDomainEvent implements EmailEvent {
1414

1515
private String uuid;
1616
private String type = "Created";
Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.apssouza.eventsourcing.events;
22

33
import com.apssouza.eventsourcing.commands.EmailDeleteCommand;
4+
import com.apssouza.eventsourcing.entities.Email;
45
import com.apssouza.infra.AbstractDomainEvent;
56
import com.apssouza.infra.AppEvent;
67
import java.time.Instant;
@@ -10,28 +11,29 @@
1011
*
1112
* @author apssouza
1213
*/
13-
public class EmailDeletedEvent extends AbstractDomainEvent implements AppEvent {
14+
public class EmailDeletedEvent extends AbstractDomainEvent implements EmailEvent {
1415

1516
private final String uuid;
1617
private final String type = "Deleted";
1718
private final Instant when = Instant.now();
18-
19-
private final EmailDeleteCommand command;
20-
21-
public EmailDeletedEvent(String uuid, EmailDeleteCommand command) {
19+
20+
private Email email;
21+
22+
/**
23+
*
24+
* @param uuid
25+
* @param email
26+
*/
27+
public EmailDeletedEvent(String uuid, Email email) {
2228
this.uuid = uuid;
23-
this.command = command;
29+
this.email = email;
2430
}
2531

2632
@Override
2733
public String uuid() {
2834
return uuid;
2935
}
3036

31-
public EmailDeleteCommand getCommand() {
32-
return command;
33-
}
34-
3537
public String type() {
3638
return type;
3739
}
@@ -41,4 +43,8 @@ public Instant when() {
4143
return when;
4244
}
4345

46+
public Email getEmail() {
47+
return email;
48+
}
49+
4450
}

mail-service/src/main/java/com/apssouza/eventsourcing/events/EmailDeliveredEvent.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.apssouza.eventsourcing.events;
22

3+
import com.apssouza.eventsourcing.entities.Email;
34
import com.apssouza.infra.AbstractDomainEvent;
45
import com.apssouza.infra.AppEvent;
56
import java.time.Instant;
@@ -10,14 +11,17 @@
1011
*
1112
* @author apssouza
1213
*/
13-
public class EmailDeliveredEvent extends AbstractDomainEvent implements AppEvent {
14+
public class EmailDeliveredEvent extends AbstractDomainEvent implements EmailEvent {
1415

1516
private final String uuid;
1617
private final Instant when = Instant.now();
1718
private final String type = "sent";
19+
20+
private Email email;
1821

19-
public EmailDeliveredEvent(String uuid) {
22+
public EmailDeliveredEvent(String uuid, Email email) {
2023
this.uuid = uuid;
24+
this.email = email;
2125
}
2226

2327
@Override
@@ -34,4 +38,8 @@ public Instant when() {
3438
return when;
3539
}
3640

41+
public Email getEmail() {
42+
return email;
43+
}
44+
3745
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.apssouza.eventsourcing.events;
7+
8+
import com.apssouza.eventsourcing.entities.Email;
9+
import com.apssouza.infra.AppEvent;
10+
11+
/**
12+
*
13+
* @author apssouza
14+
*/
15+
public interface EmailEvent extends AppEvent{
16+
17+
Email getEmail();
18+
19+
}

mail-service/src/main/java/com/apssouza/eventsourcing/events/EmailSentEvent.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.apssouza.eventsourcing.events;
22

3+
import com.apssouza.eventsourcing.entities.Email;
34
import com.apssouza.infra.AbstractDomainEvent;
45
import com.apssouza.infra.AppEvent;
56
import java.time.Instant;
@@ -10,14 +11,17 @@
1011
*
1112
* @author apssouza
1213
*/
13-
public class EmailSentEvent extends AbstractDomainEvent implements AppEvent {
14+
public class EmailSentEvent extends AbstractDomainEvent implements EmailEvent {
1415

1516
private final String uuid;
1617
private final Instant when = Instant.now();
1718
private final String type = "sent";
19+
20+
private Email email;
1821

19-
public EmailSentEvent(String uuid) {
22+
public EmailSentEvent(String uuid, Email email) {
2023
this.uuid = uuid;
24+
this.email = email;
2125
}
2226

2327
@Override
@@ -34,4 +38,8 @@ public Instant when() {
3438
return when;
3539
}
3640

41+
public Email getEmail() {
42+
return email;
43+
}
44+
3745
}

0 commit comments

Comments
 (0)