package meerkat.bulletinboard.sqlserver.mappers;

import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import meerkat.protobuf.BulletinBoardApi.BulletinBoardMessage;
import meerkat.protobuf.BulletinBoardApi.UnsignedBulletinBoardMessage;
import meerkat.util.BulletinBoardUtils;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by Arbel Deutsch Peled on 11-Dec-15.
 */
public class MessageStubMapper implements RowMapper<BulletinBoardMessage> {

    @Override
    public BulletinBoardMessage mapRow(ResultSet rs, int rowNum) throws SQLException {

        return BulletinBoardMessage.newBuilder()
                .setEntryNum(rs.getLong(1))
                .setMsg(UnsignedBulletinBoardMessage.newBuilder()
                        .setMsgId(ByteString.copyFrom(rs.getBytes(2)))
                        .setTimestamp(BulletinBoardUtils.toTimestampProto(rs.getTimestamp(3)))
                        .build())
                .build();

    }

}
