|
| 1 | +package life.qbic.projectmanagement.application.batch; |
| 2 | + |
| 3 | +import life.qbic.application.commons.Result; |
| 4 | +import life.qbic.projectmanagement.domain.project.repository.BatchRepository; |
| 5 | +import life.qbic.projectmanagement.domain.project.sample.Batch; |
| 6 | +import life.qbic.projectmanagement.domain.project.sample.BatchId; |
| 7 | +import life.qbic.projectmanagement.domain.project.sample.SampleId; |
| 8 | +import org.springframework.beans.factory.annotation.Autowired; |
| 9 | +import org.springframework.stereotype.Service; |
| 10 | + |
| 11 | +/** |
| 12 | + * <b><class short description - 1 Line!></b> |
| 13 | + * |
| 14 | + * <p><More detailed description - When to use, what it solves, etc.></p> |
| 15 | + * |
| 16 | + * @since <version tag> |
| 17 | + */ |
| 18 | +@Service |
| 19 | +public class BatchRegistrationService { |
| 20 | + |
| 21 | + private final BatchRepository batchRepository; |
| 22 | + |
| 23 | + public BatchRegistrationService(@Autowired BatchRepository batchRepository) { |
| 24 | + this.batchRepository = batchRepository; |
| 25 | + } |
| 26 | + |
| 27 | + public Result<BatchId, ResponseCode> registerBatch(String label, boolean isPilot) { |
| 28 | + Batch batch = Batch.create(label, isPilot); |
| 29 | + var result = batchRepository.add(batch); |
| 30 | + if (result.isError()) { |
| 31 | + return Result.fromError(ResponseCode.BATCH_CREATION_FAILED); |
| 32 | + } |
| 33 | + return Result.fromValue(batch.batchId()); |
| 34 | + } |
| 35 | + |
| 36 | + public Result<BatchId, ResponseCode> addSampleToBatch(SampleId sampleId, BatchId batchId) { |
| 37 | + var searchResult = batchRepository.find(batchId); |
| 38 | + if (searchResult.isEmpty()) { |
| 39 | + return Result.fromError(ResponseCode.BATCH_NOT_FOUND); |
| 40 | + } |
| 41 | + searchResult.ifPresent(batch -> { |
| 42 | + batch.addSample(sampleId); |
| 43 | + batchRepository.update(batch); |
| 44 | + }); |
| 45 | + return Result.fromValue(batchId); |
| 46 | + } |
| 47 | + |
| 48 | + public enum ResponseCode { |
| 49 | + BATCH_UPDATE_FAILED, BATCH_NOT_FOUND, BATCH_CREATION_FAILED |
| 50 | + } |
| 51 | + |
| 52 | +} |
0 commit comments