package io.senslog.database.repository;

import io.senslog.database.DBConnection;
import io.senslog.database.DBException;
import cz.hsrs.db.model.Observation;

public class ObservationRepository implements AbstractRepository {

    private final DBConnection connection;

    public ObservationRepository(DBConnection connection) {
        this.connection = connection;
    }

    public long insert(Observation observation) throws DBException {
        try {
            connection.get().<Exception>useHandle(h -> h.inTransaction(t -> t.execute(
                    "INSERT INTO observations(time_stamp, observed_value, sensor_id, unit_id) VALUES (?, ?, ?, ?)",
                    observation.getTimeStamp(), observation.getObservedValue(), observation.getSensorId(), observation.getUnitId()
                    )
            ));
        } catch (Exception e) {
            throw new DBException(e.getMessage());
        }
        return 0L; // observation_id
    }
}
