Доработки StorylineService

This commit is contained in:
Struchkov Mark 2023-04-26 20:16:00 +03:00
parent f1b1cc1afe
commit deae70fd94
Signed by: upagge
GPG Key ID: D3018BE7BA428CA6
7 changed files with 84 additions and 51 deletions

View File

@ -1,5 +1,6 @@
package dev.struchkov.godfather.quarkus.core;
import dev.struchkov.godfather.main.domain.StorylineHistory;
import dev.struchkov.godfather.main.domain.content.Message;
import dev.struchkov.godfather.main.domain.unit.UnitActiveType;
import dev.struchkov.godfather.quarkus.context.service.ErrorHandler;
@ -140,7 +141,11 @@ public class GeneralAutoResponder<M extends Message> {
if (!unit.isNotSaveHistory() && (optDefaultUnit.isEmpty() || !optDefaultUnit.get().equals(unit))) {
return Uni.combine().all().unis(
Uni.createFrom().item(newUnitRequest),
storyLineService.save(message.getFromPersonId(), unit.getName(), message)
storyLineService.save(StorylineHistory.builder()
.personId(message.getFromPersonId())
.unitName(unit.getName())
.message(message)
.build())
).asTuple();
}
return Uni.combine().all().unis(Uni.createFrom().item(newUnitRequest), Uni.createFrom().voidItem()).asTuple();

View File

@ -1,5 +1,6 @@
package dev.struchkov.godfather.quarkus.core.service;
import dev.struchkov.godfather.exception.ConfigAppException;
import dev.struchkov.godfather.main.domain.StorylineHistory;
import dev.struchkov.godfather.main.domain.UnitPointer;
import dev.struchkov.godfather.main.domain.content.Mail;
@ -26,24 +27,32 @@ import static dev.struchkov.haiti.utils.Inspector.isNotNull;
public class StorylineMailService implements StorylineService<Mail> {
private final UnitPointerService unitPointerService;
private final StorylineHistoryRepository storylineHistoryRepository;
private StorylineHistoryRepository storylineRepository;
private final Storyline<Mail> storyLine;
private String defaultUnitName;
public StorylineMailService(
UnitPointerService unitPointerService,
StorylineHistoryRepository storylineHistoryRepository,
List<Object> unitConfigurations
) {
this.storyLine = new StorylineFactory<Mail>(unitConfigurations).createStoryLine();
this.unitPointerService = unitPointerService;
this.storylineHistoryRepository = storylineHistoryRepository;
}
public void setStorylineHistoryRepository(StorylineHistoryRepository storylineHistoryRepository) {
this.storylineRepository = storylineHistoryRepository;
}
@Override
public Uni<Void> save(@NotNull StorylineHistory storylineHistory) {
isNotNull(storylineHistory);
return storylineHistoryRepository.save(storylineHistory);
isNotNull(storylineHistory, storylineHistory.getUnitName(), storylineHistory.getPersonId(), storylineHistory.getMessage());
return unitPointerService.save(new UnitPointer(storylineHistory.getPersonId(), storylineHistory.getUnitName()))
.call(ignored -> {
if (checkNotNull(storylineRepository)) {
return storylineRepository.save(storylineHistory);
}
return Uni.createFrom().voidItem();
}).replaceWithVoid();
}
@Override
@ -69,8 +78,15 @@ public class StorylineMailService implements StorylineService<Mail> {
if (checkNotNull(unit) && checkNotEmpty(unit.getNextUnits())) {
return Uni.createFrom().item(unit.getNextUnits());
} else {
return storylineHistoryRepository.cleanHistoryByPersonId(personId)
.onItem().transform(v -> storyLine.getStartingUnits());
return Uni.createFrom().deferred(
() -> {
if (checkNotNull(storylineRepository)) {
return storylineRepository.cleanHistoryByPersonId(personId);
}
return Uni.createFrom().voidItem();
}
)
.map(ignored -> storyLine.getStartingUnits());
}
}
).map(
@ -85,27 +101,20 @@ public class StorylineMailService implements StorylineService<Mail> {
);
}
@Override
public Uni<Void> save(String personId, String unitName, Mail mail) {
isNotNull(personId, unitName, mail);
return unitPointerService.save(new UnitPointer(personId, unitName))
.onItem().transformToUni(u -> {
final StorylineHistory storylineHistory = new StorylineHistory();
storylineHistory.setPersonId(personId);
storylineHistory.setUnitName(unitName);
storylineHistory.setMessage(mail);
return storylineHistoryRepository.save(storylineHistory);
}).replaceWithVoid();
}
@Override
public Uni<StorylineHistory> replaceUserToBack(String personId, int countUnitsToBack) {
return storylineHistoryRepository.findByCountLast(personId, countUnitsToBack);
if (checkNotNull(storylineRepository)) {
return storylineRepository.findByCountLast(personId, countUnitsToBack);
}
return Uni.createFrom().failure(new ConfigAppException("Ошибка. Сервис StorylineHistoryRepository не сконфигурирован."));
}
@Override
public Uni<StorylineHistory> replaceUserToBack(String personId, String unitName) {
return storylineHistoryRepository.findByCountLast(personId, unitName);
if (checkNotNull(storylineRepository)) {
return storylineRepository.findByCountLast(personId, unitName);
}
return Uni.createFrom().failure(new ConfigAppException("Ошибка. Сервис StorylineHistoryRepository не сконфигурирован."));
}
@Override
@ -121,7 +130,14 @@ public class StorylineMailService implements StorylineService<Mail> {
@Override
public Uni<Void> cleanHistoryByPersonId(@NotNull String personId) {
return storylineHistoryRepository.cleanHistoryByPersonId(personId)
return Uni.createFrom().deferred(
() -> {
if (checkNotNull(storylineRepository)) {
return storylineRepository.cleanHistoryByPersonId(personId);
}
return Uni.createFrom().voidItem();
}
)
.call(() -> unitPointerService.removeByPersonId(personId));
}

View File

@ -17,8 +17,6 @@ public interface StorylineService<M extends Message> {
Uni<Set<MainUnit<M>>> getNextUnitByPersonId(@NotNull String personId);
Uni<Void> save(String personId, String name, M message);
Uni<StorylineHistory> replaceUserToBack(String personId, int countUnitsToBack);
Uni<StorylineHistory> replaceUserToBack(String personId, String unitName);

View File

@ -1,5 +1,6 @@
package dev.struchkov.godfather.simple.core;
import dev.struchkov.godfather.main.domain.StorylineHistory;
import dev.struchkov.godfather.main.domain.content.Message;
import dev.struchkov.godfather.main.domain.unit.UnitActiveType;
import dev.struchkov.godfather.simple.context.service.Accessibility;
@ -150,7 +151,11 @@ public class GeneralAutoResponder<M extends Message> {
UnitRequest<MainUnit, M> newUnitRequest = actionUnit.action(unitRequest);
final Optional<MainUnit<M>> optDefaultUnit = storyLineService.getDefaultUnit();
if (!unit.isNotSaveHistory() && (optDefaultUnit.isEmpty() || !optDefaultUnit.get().equals(unit))) {
storyLineService.save(message.getFromPersonId(), unit.getName(), message);
storyLineService.save(StorylineHistory.builder()
.personId(message.getFromPersonId())
.unitName(unit.getName())
.message(message)
.build());
}
final MainUnit<M> newUnit = newUnitRequest.getUnit();
return !unit.equals(newUnit) ? getAction(newUnitRequest) : unitRequest;

View File

@ -1,5 +1,6 @@
package dev.struchkov.godfather.simple.core.service;
import dev.struchkov.godfather.exception.ConfigAppException;
import dev.struchkov.godfather.main.domain.StorylineHistory;
import dev.struchkov.godfather.main.domain.UnitPointer;
import dev.struchkov.godfather.main.domain.content.Message;
@ -8,12 +9,14 @@ import dev.struchkov.godfather.simple.context.service.UnitPointerService;
import dev.struchkov.godfather.simple.core.Storyline;
import dev.struchkov.godfather.simple.core.StorylineFactory;
import dev.struchkov.godfather.simple.domain.unit.MainUnit;
import dev.struchkov.haiti.utils.Inspector;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import static dev.struchkov.haiti.utils.Checker.checkNotNull;
import static dev.struchkov.haiti.utils.Inspector.isNotNull;
/**
@ -22,24 +25,29 @@ import static dev.struchkov.haiti.utils.Inspector.isNotNull;
public class StorylineMailService<T extends Message> implements StorylineService<T> {
private final UnitPointerService unitPointerService;
private final StorylineHistoryRepository storylineHistoryRepository;
private StorylineHistoryRepository storylineRepository;
private final Storyline<T> storyLine;
private String defaultUnitName;
public StorylineMailService(
UnitPointerService unitPointerService,
StorylineHistoryRepository storylineHistoryRepository,
List<Object> unitConfigurations
) {
this.storyLine = new StorylineFactory<T>(unitConfigurations).createStoryLine();
this.unitPointerService = unitPointerService;
this.storylineHistoryRepository = storylineHistoryRepository;
}
public void setStorylineRepository(StorylineHistoryRepository storylineRepository) {
this.storylineRepository = storylineRepository;
}
@Override
public void save(@NotNull StorylineHistory storylineHistory) {
isNotNull(storylineHistory);
storylineHistoryRepository.save(storylineHistory);
isNotNull(storylineHistory, storylineHistory.getUnitName(), storylineHistory.getPersonId(), storylineHistory.getMessage());
unitPointerService.save(new UnitPointer(storylineHistory.getPersonId(), storylineHistory.getUnitName()));
if (checkNotNull(storylineRepository)) {
storylineRepository.save(storylineHistory);
}
}
@Override
@ -54,34 +62,24 @@ public class StorylineMailService<T extends Message> implements StorylineService
final Optional<Set<MainUnit<T>>> optMainUnits = getUnitNameByPersonId(personId)
.map(MainUnit::getNextUnits)
.filter(mainUnits -> !mainUnits.isEmpty());
if (optMainUnits.isEmpty()) {
storylineHistoryRepository.cleanHistoryByPersonId(personId);
if (checkNotNull(storylineRepository) && optMainUnits.isEmpty()) {
storylineRepository.cleanHistoryByPersonId(personId);
}
final Set<MainUnit<T>> nextUnits = optMainUnits.orElse(storyLine.getStartingUnits());
nextUnits.addAll(storyLine.getGlobalUnits());
return nextUnits;
}
@Override
public void save(String personId, String unitName, T mail) {
isNotNull(personId, unitName, mail);
unitPointerService.save(new UnitPointer(personId, unitName));
final StorylineHistory storylineHistory = new StorylineHistory();
storylineHistory.setPersonId(personId);
storylineHistory.setUnitName(unitName);
storylineHistory.setMessage(mail);
storylineHistoryRepository.save(storylineHistory);
}
@Override
public Optional<StorylineHistory> replaceUserToBack(String personId, int countUnitsToBack) {
return storylineHistoryRepository.findByCountLast(personId, countUnitsToBack);
Inspector.isNotNull(() -> new ConfigAppException("Ошибка. Сервис StorylineHistoryRepository не сконфигурирован."), storylineRepository);
return storylineRepository.findByCountLast(personId, countUnitsToBack);
}
@Override
public Optional<StorylineHistory> replaceUserToBack(String personId, String unitName) {
return storylineHistoryRepository.findByCountLast(personId, unitName);
Inspector.isNotNull(() -> new ConfigAppException("Ошибка. Сервис StorylineHistoryRepository не сконфигурирован."), storylineRepository);
return storylineRepository.findByCountLast(personId, unitName);
}
@Override
@ -97,7 +95,9 @@ public class StorylineMailService<T extends Message> implements StorylineService
@Override
public void cleanHistoryByPersonId(@NotNull String personId) {
storylineHistoryRepository.cleanHistoryByPersonId(personId);
if (checkNotNull(storylineRepository)) {
storylineRepository.cleanHistoryByPersonId(personId);
}
unitPointerService.removeByPersonId(personId);
}

View File

@ -16,8 +16,6 @@ public interface StorylineService<M extends Message> {
Set<MainUnit<M>> getNextUnitByPersonId(@NotNull String personId);
void save(String personId, String name, M message);
Optional<StorylineHistory> replaceUserToBack(String personId, int countUnitsToBack);
Optional<StorylineHistory> replaceUserToBack(String personId, String unitName);

View File

@ -2,7 +2,18 @@ package dev.struchkov.godfather.main.domain;
import dev.struchkov.godfather.main.domain.content.Message;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class StorylineHistory {
private String personId;