Добавил поддержку payload для BoxAnswer и Message
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Struchkov Mark 2023-02-15 22:38:14 +03:00
parent 5a630282f6
commit 89d7796402
Signed by: upagge
GPG Key ID: D3018BE7BA428CA6
2 changed files with 58 additions and 3 deletions

View File

@ -2,6 +2,11 @@ package dev.struchkov.godfather.main.domain;
import dev.struchkov.godfather.main.domain.keyboard.KeyBoard; import dev.struchkov.godfather.main.domain.keyboard.KeyBoard;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import static dev.struchkov.haiti.utils.Checker.checkNotNull;
import static dev.struchkov.haiti.utils.Checker.checkNull; import static dev.struchkov.haiti.utils.Checker.checkNull;
/** /**
@ -33,12 +38,15 @@ public class BoxAnswer {
*/ */
private String recipientPersonId; private String recipientPersonId;
protected Map<ContextKey, Object> payload;
private BoxAnswer(Builder builder) { private BoxAnswer(Builder builder) {
message = builder.message; message = builder.message;
keyBoard = builder.keyBoard; keyBoard = builder.keyBoard;
replace = builder.replace; replace = builder.replace;
replaceMessageId = builder.replaceMessageId; replaceMessageId = builder.replaceMessageId;
recipientPersonId = builder.recipientPersonId; recipientPersonId = builder.recipientPersonId;
payload = builder.payload;
} }
public static BoxAnswer boxAnswer(boolean replace, String message) { public static BoxAnswer boxAnswer(boolean replace, String message) {
@ -115,6 +123,21 @@ public class BoxAnswer {
} }
} }
public Map<ContextKey, Object> getPayload() {
return payload;
}
public <T> void setPayload(ContextKey<T> key, T value) {
if (checkNotNull(value)) {
payload.put(key, value);
}
}
public <T> Optional<T> getPayLoad(ContextKey<T> contextKey) {
return Optional.ofNullable(payload.get(contextKey))
.map(value -> (T) value);
}
@Override @Override
public String toString() { public String toString() {
return "BoxAnswer{" + return "BoxAnswer{" +
@ -131,6 +154,7 @@ public class BoxAnswer {
private boolean replace; private boolean replace;
private String replaceMessageId; private String replaceMessageId;
private String recipientPersonId; private String recipientPersonId;
private Map<ContextKey, Object> payload = new HashMap<>();
private Builder() { private Builder() {
} }
@ -160,6 +184,13 @@ public class BoxAnswer {
return this; return this;
} }
public <T> Builder payload(ContextKey<T> key, T value) {
if (checkNotNull(value)) {
payload.put(key, value);
}
return this;
}
public BoxAnswer build() { public BoxAnswer build() {
return new BoxAnswer(this); return new BoxAnswer(this);
} }

View File

@ -1,9 +1,15 @@
package dev.struchkov.godfather.main.domain.content; package dev.struchkov.godfather.main.domain.content;
import dev.struchkov.autoresponder.entity.DeliverableText; import dev.struchkov.autoresponder.entity.DeliverableText;
import dev.struchkov.godfather.main.domain.ContextKey;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import static dev.struchkov.haiti.utils.Checker.checkNotNull;
/** /**
* Абстрактная сущность - Сообщение от пользователя. * Абстрактная сущность - Сообщение от пользователя.
@ -22,17 +28,19 @@ public abstract class Message implements DeliverableText {
/** /**
* Дата создания. * Дата создания.
*/ */
private LocalDateTime createDate; protected LocalDateTime createDate;
/** /**
* Идентификатор пользователя, отправившего сообщение. * Идентификатор пользователя, отправившего сообщение.
*/ */
private String personId; protected String personId;
/** /**
* Текстовое сообщение. * Текстовое сообщение.
*/ */
private String text; protected String text;
protected Map<ContextKey, Object> payload = new HashMap<>();
protected Message(Message source) { protected Message(Message source) {
this.id = source.getId(); this.id = source.getId();
@ -40,6 +48,7 @@ public abstract class Message implements DeliverableText {
this.text = source.getText(); this.text = source.getText();
this.createDate = source.getCreateDate(); this.createDate = source.getCreateDate();
this.contentType = source.getContentType(); this.contentType = source.getContentType();
this.payload = source.getPayload();
} }
protected Message() { protected Message() {
@ -85,6 +94,21 @@ public abstract class Message implements DeliverableText {
this.id = id; this.id = id;
} }
public Map<ContextKey, Object> getPayload() {
return payload;
}
public <T> void setPayload(ContextKey<T> key, T value) {
if (checkNotNull(value)) {
payload.put(key, value);
}
}
public <T> Optional<T> getPayLoad(ContextKey<T> contextKey) {
return Optional.ofNullable(payload.get(contextKey))
.map(value -> (T) value);
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;