56 lines
1.4 KiB
Java
56 lines
1.4 KiB
Java
package de.ph87.data.plot.axis;
|
|
|
|
import de.ph87.data.plot.PlotDto;
|
|
import de.ph87.data.plot.PlotService;
|
|
import de.ph87.data.plot.axis.graph.Graph;
|
|
import de.ph87.data.plot.axis.graph.GraphRepository;
|
|
import lombok.NonNull;
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
import java.util.function.Consumer;
|
|
|
|
@Slf4j
|
|
@Service
|
|
@RequiredArgsConstructor
|
|
public class AxisService {
|
|
|
|
private final AxisRepository axisRepository;
|
|
|
|
private final GraphRepository graphRepository;
|
|
|
|
private final PlotService plotService;
|
|
|
|
@NonNull
|
|
@Transactional
|
|
public PlotDto delete(final long id) {
|
|
final Axis axis = getById(id);
|
|
axisRepository.delete(axis);
|
|
axis.getPlot().getAxes().remove(axis);
|
|
log.info("Deleted: axis={}", axis);
|
|
return plotService.publish(axis.getPlot(), false);
|
|
}
|
|
|
|
@NonNull
|
|
@Transactional
|
|
public PlotDto addGraph(final long axisId) {
|
|
return set(axisId, axis -> axis.addGraph(graphRepository.save(new Graph(axis))));
|
|
}
|
|
|
|
@NonNull
|
|
@Transactional
|
|
public PlotDto set(final long id, @NonNull final Consumer<Axis> modifier) {
|
|
final Axis axis = getById(id);
|
|
modifier.accept(axis);
|
|
return plotService.publish(axis.getPlot(), false);
|
|
}
|
|
|
|
@NonNull
|
|
private Axis getById(final long id) {
|
|
return axisRepository.findById(id).orElseThrow();
|
|
}
|
|
|
|
}
|