Доработки 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; 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.content.Message;
import dev.struchkov.godfather.main.domain.unit.UnitActiveType; import dev.struchkov.godfather.main.domain.unit.UnitActiveType;
import dev.struchkov.godfather.quarkus.context.service.ErrorHandler; 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))) { if (!unit.isNotSaveHistory() && (optDefaultUnit.isEmpty() || !optDefaultUnit.get().equals(unit))) {
return Uni.combine().all().unis( return Uni.combine().all().unis(
Uni.createFrom().item(newUnitRequest), 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(); ).asTuple();
} }
return Uni.combine().all().unis(Uni.createFrom().item(newUnitRequest), Uni.createFrom().voidItem()).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; 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.StorylineHistory;
import dev.struchkov.godfather.main.domain.UnitPointer; import dev.struchkov.godfather.main.domain.UnitPointer;
import dev.struchkov.godfather.main.domain.content.Mail; 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> { public class StorylineMailService implements StorylineService<Mail> {
private final UnitPointerService unitPointerService; private final UnitPointerService unitPointerService;
private final StorylineHistoryRepository storylineHistoryRepository; private StorylineHistoryRepository storylineRepository;
private final Storyline<Mail> storyLine; private final Storyline<Mail> storyLine;
private String defaultUnitName; private String defaultUnitName;
public StorylineMailService( public StorylineMailService(
UnitPointerService unitPointerService, UnitPointerService unitPointerService,
StorylineHistoryRepository storylineHistoryRepository,
List<Object> unitConfigurations List<Object> unitConfigurations
) { ) {
this.storyLine = new StorylineFactory<Mail>(unitConfigurations).createStoryLine(); this.storyLine = new StorylineFactory<Mail>(unitConfigurations).createStoryLine();
this.unitPointerService = unitPointerService; this.unitPointerService = unitPointerService;
this.storylineHistoryRepository = storylineHistoryRepository; }
public void setStorylineHistoryRepository(StorylineHistoryRepository storylineHistoryRepository) {
this.storylineRepository = storylineHistoryRepository;
} }
@Override @Override
public Uni<Void> save(@NotNull StorylineHistory storylineHistory) { public Uni<Void> save(@NotNull StorylineHistory storylineHistory) {
isNotNull(storylineHistory); isNotNull(storylineHistory, storylineHistory.getUnitName(), storylineHistory.getPersonId(), storylineHistory.getMessage());
return storylineHistoryRepository.save(storylineHistory); return unitPointerService.save(new UnitPointer(storylineHistory.getPersonId(), storylineHistory.getUnitName()))
.call(ignored -> {
if (checkNotNull(storylineRepository)) {
return storylineRepository.save(storylineHistory);
}
return Uni.createFrom().voidItem();
}).replaceWithVoid();
} }
@Override @Override
@ -69,8 +78,15 @@ public class StorylineMailService implements StorylineService<Mail> {
if (checkNotNull(unit) && checkNotEmpty(unit.getNextUnits())) { if (checkNotNull(unit) && checkNotEmpty(unit.getNextUnits())) {
return Uni.createFrom().item(unit.getNextUnits()); return Uni.createFrom().item(unit.getNextUnits());
} else { } else {
return storylineHistoryRepository.cleanHistoryByPersonId(personId) return Uni.createFrom().deferred(
.onItem().transform(v -> storyLine.getStartingUnits()); () -> {
if (checkNotNull(storylineRepository)) {
return storylineRepository.cleanHistoryByPersonId(personId);
}
return Uni.createFrom().voidItem();
}
)
.map(ignored -> storyLine.getStartingUnits());
} }
} }
).map( ).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 @Override
public Uni<StorylineHistory> replaceUserToBack(String personId, int countUnitsToBack) { 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 @Override
public Uni<StorylineHistory> replaceUserToBack(String personId, String unitName) { 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 @Override
@ -121,7 +130,14 @@ public class StorylineMailService implements StorylineService<Mail> {
@Override @Override
public Uni<Void> cleanHistoryByPersonId(@NotNull String personId) { 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)); .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<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, int countUnitsToBack);
Uni<StorylineHistory> replaceUserToBack(String personId, String unitName); Uni<StorylineHistory> replaceUserToBack(String personId, String unitName);

View File

@ -1,5 +1,6 @@
package dev.struchkov.godfather.simple.core; 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.content.Message;
import dev.struchkov.godfather.main.domain.unit.UnitActiveType; import dev.struchkov.godfather.main.domain.unit.UnitActiveType;
import dev.struchkov.godfather.simple.context.service.Accessibility; 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); UnitRequest<MainUnit, M> newUnitRequest = actionUnit.action(unitRequest);
final Optional<MainUnit<M>> optDefaultUnit = storyLineService.getDefaultUnit(); final Optional<MainUnit<M>> optDefaultUnit = storyLineService.getDefaultUnit();
if (!unit.isNotSaveHistory() && (optDefaultUnit.isEmpty() || !optDefaultUnit.get().equals(unit))) { 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(); final MainUnit<M> newUnit = newUnitRequest.getUnit();
return !unit.equals(newUnit) ? getAction(newUnitRequest) : unitRequest; return !unit.equals(newUnit) ? getAction(newUnitRequest) : unitRequest;

View File

@ -1,5 +1,6 @@
package dev.struchkov.godfather.simple.core.service; 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.StorylineHistory;
import dev.struchkov.godfather.main.domain.UnitPointer; import dev.struchkov.godfather.main.domain.UnitPointer;
import dev.struchkov.godfather.main.domain.content.Message; 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.Storyline;
import dev.struchkov.godfather.simple.core.StorylineFactory; import dev.struchkov.godfather.simple.core.StorylineFactory;
import dev.struchkov.godfather.simple.domain.unit.MainUnit; import dev.struchkov.godfather.simple.domain.unit.MainUnit;
import dev.struchkov.haiti.utils.Inspector;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import static dev.struchkov.haiti.utils.Checker.checkNotNull;
import static dev.struchkov.haiti.utils.Inspector.isNotNull; 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> { public class StorylineMailService<T extends Message> implements StorylineService<T> {
private final UnitPointerService unitPointerService; private final UnitPointerService unitPointerService;
private final StorylineHistoryRepository storylineHistoryRepository; private StorylineHistoryRepository storylineRepository;
private final Storyline<T> storyLine; private final Storyline<T> storyLine;
private String defaultUnitName; private String defaultUnitName;
public StorylineMailService( public StorylineMailService(
UnitPointerService unitPointerService, UnitPointerService unitPointerService,
StorylineHistoryRepository storylineHistoryRepository,
List<Object> unitConfigurations List<Object> unitConfigurations
) { ) {
this.storyLine = new StorylineFactory<T>(unitConfigurations).createStoryLine(); this.storyLine = new StorylineFactory<T>(unitConfigurations).createStoryLine();
this.unitPointerService = unitPointerService; this.unitPointerService = unitPointerService;
this.storylineHistoryRepository = storylineHistoryRepository; }
public void setStorylineRepository(StorylineHistoryRepository storylineRepository) {
this.storylineRepository = storylineRepository;
} }
@Override @Override
public void save(@NotNull StorylineHistory storylineHistory) { public void save(@NotNull StorylineHistory storylineHistory) {
isNotNull(storylineHistory); isNotNull(storylineHistory, storylineHistory.getUnitName(), storylineHistory.getPersonId(), storylineHistory.getMessage());
storylineHistoryRepository.save(storylineHistory); unitPointerService.save(new UnitPointer(storylineHistory.getPersonId(), storylineHistory.getUnitName()));
if (checkNotNull(storylineRepository)) {
storylineRepository.save(storylineHistory);
}
} }
@Override @Override
@ -54,34 +62,24 @@ public class StorylineMailService<T extends Message> implements StorylineService
final Optional<Set<MainUnit<T>>> optMainUnits = getUnitNameByPersonId(personId) final Optional<Set<MainUnit<T>>> optMainUnits = getUnitNameByPersonId(personId)
.map(MainUnit::getNextUnits) .map(MainUnit::getNextUnits)
.filter(mainUnits -> !mainUnits.isEmpty()); .filter(mainUnits -> !mainUnits.isEmpty());
if (optMainUnits.isEmpty()) { if (checkNotNull(storylineRepository) && optMainUnits.isEmpty()) {
storylineHistoryRepository.cleanHistoryByPersonId(personId); storylineRepository.cleanHistoryByPersonId(personId);
} }
final Set<MainUnit<T>> nextUnits = optMainUnits.orElse(storyLine.getStartingUnits()); final Set<MainUnit<T>> nextUnits = optMainUnits.orElse(storyLine.getStartingUnits());
nextUnits.addAll(storyLine.getGlobalUnits()); nextUnits.addAll(storyLine.getGlobalUnits());
return nextUnits; 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 @Override
public Optional<StorylineHistory> replaceUserToBack(String personId, int countUnitsToBack) { 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 @Override
public Optional<StorylineHistory> replaceUserToBack(String personId, String unitName) { 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 @Override
@ -97,7 +95,9 @@ public class StorylineMailService<T extends Message> implements StorylineService
@Override @Override
public void cleanHistoryByPersonId(@NotNull String personId) { public void cleanHistoryByPersonId(@NotNull String personId) {
storylineHistoryRepository.cleanHistoryByPersonId(personId); if (checkNotNull(storylineRepository)) {
storylineRepository.cleanHistoryByPersonId(personId);
}
unitPointerService.removeByPersonId(personId); unitPointerService.removeByPersonId(personId);
} }

View File

@ -16,8 +16,6 @@ public interface StorylineService<M extends Message> {
Set<MainUnit<M>> getNextUnitByPersonId(@NotNull String personId); 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, int countUnitsToBack);
Optional<StorylineHistory> replaceUserToBack(String personId, String unitName); 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 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 { public class StorylineHistory {
private String personId; private String personId;