-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Splitted signal service impl with its interface
- Loading branch information
pasqualino.cristaudo
committed
Oct 20, 2023
1 parent
c8cc11f
commit 3cf70e8
Showing
4 changed files
with
61 additions
and
74 deletions.
There are no files selected for viewing
50 changes: 3 additions & 47 deletions
50
src/main/java/it/pagopa/interop/signalhub/persister/service/SignalService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,8 @@ | ||
package it.pagopa.interop.signalhub.persister.service; | ||
|
||
import it.pagopa.interop.signalhub.persister.entity.DeadSignal; | ||
import it.pagopa.interop.signalhub.persister.entity.Signal; | ||
import it.pagopa.interop.signalhub.persister.mapper.DeadSignalMapper; | ||
import it.pagopa.interop.signalhub.persister.repository.DeadSignalRepository; | ||
import it.pagopa.interop.signalhub.persister.repository.SignalRepository; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import reactor.core.publisher.Mono; | ||
import static it.pagopa.interop.signalhub.persister.exception.ExceptionTypeEnum.DUPLICATE_SIGNAL_ERROR; | ||
|
||
|
||
@Service | ||
@Slf4j | ||
public class SignalService { | ||
@Autowired | ||
private SignalRepository signalRepository; | ||
@Autowired | ||
private DeadSignalRepository deadSignalRepository; | ||
@Autowired | ||
private DeadSignalMapper deadSignalMapper; | ||
|
||
|
||
@Transactional | ||
public Mono<Signal> signalServiceFlow(Signal signal) { | ||
|
||
return getSignalById(signal.getSignalId(), signal.getEserviceId()) | ||
.switchIfEmpty(Mono.just(signal)) | ||
.filter(entity -> entity.getId() == null) | ||
.flatMap(this::createSignal) | ||
.switchIfEmpty(Mono.defer(() -> saveToDeadSignal(signal))); | ||
} | ||
|
||
private Mono<Signal> getSignalById(Long signalId, String eserviceId) { return this.signalRepository.findByIndexSignalAndEserviceId(signalId, eserviceId); } | ||
private Mono<Signal> createSignal(Signal signal) { return this.signalRepository.save(signal); } | ||
private Mono<DeadSignal> createDeadSignal(DeadSignal signal) { return this.deadSignalRepository.save(signal); } | ||
|
||
private Mono<DeadSignal> getDeadSignal(Signal signal) { | ||
DeadSignal deadSignal = deadSignalMapper.signalToDeadSignal(signal); | ||
deadSignal.setErrorReason(DUPLICATE_SIGNAL_ERROR.toString()); | ||
return Mono.just(deadSignal); | ||
} | ||
|
||
private Mono<Signal> saveToDeadSignal(Signal signal) { | ||
return getDeadSignal(signal) | ||
.flatMap(this::createDeadSignal) | ||
.then(Mono.just(signal)); | ||
} | ||
} | ||
public interface SignalService { | ||
Mono<Signal> signalServiceFlow(Signal signal); | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/it/pagopa/interop/signalhub/persister/service/impl/SignalServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package it.pagopa.interop.signalhub.persister.service.impl; | ||
|
||
import it.pagopa.interop.signalhub.persister.entity.DeadSignal; | ||
import it.pagopa.interop.signalhub.persister.entity.Signal; | ||
import it.pagopa.interop.signalhub.persister.mapper.DeadSignalMapper; | ||
import it.pagopa.interop.signalhub.persister.repository.DeadSignalRepository; | ||
import it.pagopa.interop.signalhub.persister.repository.SignalRepository; | ||
import it.pagopa.interop.signalhub.persister.service.SignalService; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import reactor.core.publisher.Mono; | ||
import static it.pagopa.interop.signalhub.persister.exception.ExceptionTypeEnum.DUPLICATE_SIGNAL_ERROR; | ||
|
||
|
||
@Service | ||
@Slf4j | ||
public class SignalServiceImpl implements SignalService { | ||
@Autowired | ||
private SignalRepository signalRepository; | ||
@Autowired | ||
private DeadSignalRepository deadSignalRepository; | ||
@Autowired | ||
private DeadSignalMapper deadSignalMapper; | ||
|
||
|
||
@Transactional | ||
public Mono<Signal> signalServiceFlow(Signal signal) { | ||
|
||
return getSignalById(signal.getSignalId(), signal.getEserviceId()) | ||
.switchIfEmpty(Mono.just(signal)) | ||
.filter(entity -> entity.getId() == null) | ||
.flatMap(this::createSignal) | ||
.switchIfEmpty(Mono.defer(() -> saveToDeadSignal(signal))); | ||
} | ||
|
||
private Mono<Signal> getSignalById(Long signalId, String eserviceId) { return this.signalRepository.findByIndexSignalAndEserviceId(signalId, eserviceId); } | ||
private Mono<Signal> createSignal(Signal signal) { return this.signalRepository.save(signal); } | ||
private Mono<DeadSignal> createDeadSignal(DeadSignal signal) { return this.deadSignalRepository.save(signal); } | ||
|
||
private Mono<DeadSignal> getDeadSignal(Signal signal) { | ||
DeadSignal deadSignal = deadSignalMapper.signalToDeadSignal(signal); | ||
deadSignal.setErrorReason(DUPLICATE_SIGNAL_ERROR.toString()); | ||
return Mono.just(deadSignal); | ||
} | ||
|
||
private Mono<Signal> saveToDeadSignal(Signal signal) { | ||
return getDeadSignal(signal) | ||
.flatMap(this::createDeadSignal) | ||
.then(Mono.just(signal)); | ||
} | ||
} |
23 changes: 0 additions & 23 deletions
23
src/test/java/it/pagopa/interop/signalhub/persister/ApplicationTest.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters