<?php
class WeightReadingRepository extends BaseRepository implements IWeightReadingRepository {
	private $_userSettingsRepo;

	public function __construct(IUserSettingsRepository $userSettingsRepo) {
		parent::__construct();
		$this->_userSettingsRepo=$userSettingsRepo;
	}
	
	public function GetAll($userId){
		$readings=array();
		$ids=self::$PDO->query("SELECT reading_id FROM weight_readings WHERE user_id=".(int)$userId)->fetchAll(PDO::FETCH_COLUMN);
		foreach ($ids as $id)
			$readings[]=new WeightReading($this->_userSettingsRepo ,$id);
		return $readings;
	}
	
	public function GetReadingsInDateRange($userId, DateTime $from, DateTime $to){
		$readings=array();
		$prep=self::$PDO->prepare("SELECT reading_id FROM weight_readings WHERE user_id=:user_id AND (`timestamp`>=:from AND `timestamp`<=:to)");
		$prep->execute(array(
			":user_id"=>$userId,
			":from"=>$from->format('Y-m-d 00:00:00'),
			":to"=>$to->format('Y-m-d 23:59:59')
		));
		$results=$prep->fetchAll(PDO::FETCH_COLUMN);
		foreach ($results as $id)
			$readings[]=new WeightReading($this->_userSettingsRepo ,$id);
		return $readings;
	}

	public function Delete($id) {
		$prep=self::$PDO->prepare("DELETE FROM weight_readings WHERE reading_id=?");
		$prep->execute([$id]);
	}
}
