package de.tbressler.waterrower.workout.db.selects;

import de.tbressler.waterrower.workout.model.Weight;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

import static de.tbressler.waterrower.workout.db.dao.WeightDAO.*;
import static de.tbressler.waterrower.workout.db.utils.DerbyUtils.convertDateToLocalDate;

/**
 * SQL-SELECT for weights.
 *
 * @author Tobias Bressler
 * @version 1.0
 */
public class WeightSelect extends AbstractDBSelect<List<Weight>> {

    /**
     * SQL-SELECT for weights.
     *
     * @param query The SQL query, must not be null.
     * @param limit The limit or 0 if no limit.
     */
    public WeightSelect(String query, int limit) {
        super(query, limit);
    }


    @Override
    public List<Weight> getResult(ResultSet resultSet) throws SQLException {
        List<Weight> result = new ArrayList<>();

        while (resultSet.next()) {

            int id = resultSet.getInt(WEIGHT_ID);
            double value = resultSet.getDouble(WEIGHT_VALUE);
            LocalDate created = convertDateToLocalDate(resultSet.getDate(WEIGHT_CREATED));
            int userId = resultSet.getInt(WEIGHT_FK_USER_ID);

            result.add(new Weight(id, value, created, userId));
        }

        return result;
    }

}