Добавил поддержку 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 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;
/**
@ -33,12 +38,15 @@ public class BoxAnswer {
*/
private String recipientPersonId;
protected Map<ContextKey, Object> payload;
private BoxAnswer(Builder builder) {
message = builder.message;
keyBoard = builder.keyBoard;
replace = builder.replace;
replaceMessageId = builder.replaceMessageId;
recipientPersonId = builder.recipientPersonId;
payload = builder.payload;
}
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
public String toString() {
return "BoxAnswer{" +
@ -131,6 +154,7 @@ public class BoxAnswer {
private boolean replace;
private String replaceMessageId;
private String recipientPersonId;
private Map<ContextKey, Object> payload = new HashMap<>();
private Builder() {
}
@ -160,6 +184,13 @@ public class BoxAnswer {
return this;
}
public <T> Builder payload(ContextKey<T> key, T value) {
if (checkNotNull(value)) {
payload.put(key, value);
}
return this;
}
public BoxAnswer build() {
return new BoxAnswer(this);
}

View File

@ -1,9 +1,15 @@
package dev.struchkov.godfather.main.domain.content;
import dev.struchkov.autoresponder.entity.DeliverableText;
import dev.struchkov.godfather.main.domain.ContextKey;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
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) {
this.id = source.getId();
@ -40,6 +48,7 @@ public abstract class Message implements DeliverableText {
this.text = source.getText();
this.createDate = source.getCreateDate();
this.contentType = source.getContentType();
this.payload = source.getPayload();
}
protected Message() {
@ -85,6 +94,21 @@ public abstract class Message implements DeliverableText {
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
public boolean equals(Object o) {
if (this == o) return true;