Robert Marshall 8 gadi atpakaļ
vecāks
revīzija
a1b4c5f0cc

+ 15 - 5
Controller/Weight.php

@@ -1,8 +1,8 @@
 <?php
 ApplicationSettings::RegisterDefaultSetting("weight", "graph_history_duration", "1M");
 
-class Weight extends Controller {	
-	public function Index(IWeightReadingRepository $weightReadingRepo, IUserSettingsRepository $userSettingRepo) {
+class Weight extends Controller {
+	private function GetUserReadings(IWeightReadingRepository $weightReadingRepo, IUserSettingsRepository $userSettingRepo){
 		$user=Session::GetLoggedInUser();
 		$graphDuration=ApplicationSettings::GetSetting("weight", "graph_history_duration");
 		$userDurationSetting=$userSettingRepo->GetSetting($user, "weight_graph_duration")->Value;
@@ -12,7 +12,15 @@ class Weight extends Controller {
 		$dateTo=new DateTime();
 		$dateFrom=clone $dateTo;
 		$dateFrom->sub(new DateInterval("P".$graphDuration));
-		return new View("Weight/index.view",array("readings"=>$weightReadingRepo->GetReadingsInDateRange($user->UserId,$dateFrom,$dateTo)));
+		return $weightReadingRepo->GetReadingsInDateRange($user->UserId,$dateFrom,$dateTo);
+	}
+	
+	public function Index(IWeightReadingRepository $weightReadingRepo, IUserSettingsRepository $userSettingRepo) {
+		return new View("Weight/index.view",array("readings"=>$this->GetUserReadings($weightReadingRepo, $userSettingRepo)));
+	}
+	
+	public function GetReadings(IWeightReadingRepository $weightReadingRepo, IUserSettingsRepository $userSettingRepo) {
+		return json_encode($this->GetUserReadings($weightReadingRepo, $userSettingRepo));
 	}
 	
 	public function Add($weight, $fat, IUserSettingsRepository $userSettingsRepo){
@@ -24,10 +32,12 @@ class Weight extends Controller {
 
 		$reading->Weight=$weight;
 		$reading->Fat=$fat;
-		
 		$reading->Save(Session::GetLoggedInUser());
 		
-		//return '{"Weight":'.$reading->Weight.',"BMI":'.$reading->Bmi.',"Fat":'.$reading->Fat.'}';
 		return json_encode($reading);
 	}
+	
+	public function Delete($id,IWeightReadingRepository $weightReadingRepo){
+		$weightReadingRepo->Delete($id);
+	}
 }

+ 3 - 0
Database/IWeightReadingRepository.php

@@ -1,5 +1,8 @@
 <?php
 interface IWeightReadingRepository {
+
+	public function Delete($id);
+
 	public function GetAll($userId);
 	public function GetReadingsInDateRange($userId, DateTime $from, DateTime $to);
 }

+ 5 - 0
Database/WeightReadingRepository.php

@@ -28,4 +28,9 @@ class WeightReadingRepository extends BaseRepository implements IWeightReadingRe
 			$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]);
+	}
 }

+ 1 - 0
Model/WeightReading.php

@@ -33,6 +33,7 @@ class WeightReading extends DBObject implements JsonSerializable  {
 	
 	public function jsonSerialize() {
 		$output=new stdClass();
+		$output->id=(int)$this->ReadingId;
 		$output->date=date("c", strtotime($this->Timestamp));
 		$output->weight=(float)$this->Weight;
 		$output->bmi=(float)$this->Bmi;

+ 3 - 1
View/Weight/index.view

@@ -32,7 +32,7 @@
 		</div>
 	</div>
 	<div class="row col-lg-1">
-		<google-chart data="readings" headings="headings" legend-position="top" height="400" title="Historical Weight Data"></google-chart>
+		<google-chart data="readings" headings="headings" legend-position="top" height="400" title="Historical Weight Data" ignore="['id']"></google-chart>
 	</div>
 	<div class="row col-lg-1">
 		<div class="col">
@@ -43,12 +43,14 @@
 					<th>Weight</th>
 					<th>BMI</th>
 					<th>Fat %</th>
+					<th></th>
 				</tr>
 				<tr ng-repeat="reading in tenLatestReadings track by $index">
 					<td>{{reading.date | date:'d/M/yyyy'}}</td>
 					<td>{{reading.weight}}</td>
 					<td>{{reading.bmi}}</td>
 					<td>{{reading.fat}}</td>
+					<td><a href="#" ng-click="deleteReading(reading.id)"><img src="/images/delete.svg" /></a></td>
 				</tr>
 			</table>
 		</div>

+ 16 - 2
scripts/controllers/weight.js

@@ -6,6 +6,12 @@ app.controller("weight", ["$scope", "weightService", function($scope, weightServ
 		$scope.readings = [];
 		$scope.headings = {weight: "Weight (KG)", fat: "Fat %", bmi: "BMI"};
 
+		function refreshReadings() {
+			weightService.getReadings().then(function(data) {
+				$scope.readings = data;
+			});
+		}
+
 		$scope.$watch("readings", function() {
 			for (var i = 0; i < $scope.readings.length; i++) {
 				$scope.readings[i].date = new Date($scope.readings[i].date);
@@ -18,10 +24,18 @@ app.controller("weight", ["$scope", "weightService", function($scope, weightServ
 
 			$scope.submitting = true;
 			weightService.addWeight($scope.weight, $scope.fat).then(function(data) {
-				$scope.readings = $scope.readings.concat([data]);
+				refreshReadings();
 				$scope.submitting = false;
+				$scope.weight = null;
+				$scope.fat = null;
 			});
-		}
+		};
+
+		$scope.deleteReading = function(id) {
+			weightService.deleteWeight(id).then(function() {
+				refreshReadings();
+			});
+		};
 
 		Object.defineProperty($scope, "tenLatestReadings", {
 			get: function() {

+ 14 - 0
scripts/directives/googleChart.js

@@ -7,6 +7,7 @@ app.directive('googleChart', function() {
 		scope: {
 			data: '=',
 			headings: '=?',
+			ignore: '=?',
 			height: '='
 		},
 		link: function(scope, element, attributes) {
@@ -15,6 +16,9 @@ app.directive('googleChart', function() {
 			if (!scope.headings)
 				scope.headings = [];
 
+			if (!scope.ignore)
+				scope.ignore = [];
+
 			var chartData, chart;
 			var chartOptions = {
 				title: attributes.title,
@@ -40,6 +44,10 @@ app.directive('googleChart', function() {
 					return args[index].toString(format);
 				});
 			}
+			
+			function shouldIgnore(value){
+				return scope.ignore.indexOf(value)>-1;
+			}
 
 			function setupChart() {
 				if (!chartLoaded)
@@ -48,6 +56,9 @@ app.directive('googleChart', function() {
 				var firstRow = scope.data[0];
 				var fields = {};
 				for (var propertyName in firstRow) {
+					if (shouldIgnore(propertyName))
+						continue;
+					
 					var datum = firstRow[propertyName];
 					var type = typeof (datum);
 					if (type === "object")
@@ -68,6 +79,9 @@ app.directive('googleChart', function() {
 				angular.forEach(scope.data, function(value) {
 					var row = [];
 					angular.forEach(value, function(value2, index) {
+						if (shouldIgnore(index))
+							return;
+					
 						row.push(value2);
 						var attrIndex = 'toolTip' + index.upperCaseFirst();
 						if (attributes[attrIndex])

+ 17 - 0
scripts/services/weightService.js

@@ -8,4 +8,21 @@ app.service('weightService', function($http) {
 			return response.data;
 		});
 	};
+	this.deleteWeight=function(id){
+		return $http({
+			method:'post',
+			url:'/weight/delete',
+			data:{id:id},
+		}).then(function(response){
+			return response.data;
+		});
+	};
+	this.getReadings=function(){
+		return $http({
+			method:'post',
+			url:'/weight/getreadings'
+		}).then(function(response){
+			return response.data;
+		});
+	};
 });