Skip to content

Commit

Permalink
Added functions to save workouts, added new methods to DBHelper for d…
Browse files Browse the repository at this point in the history
…ifferent queries, renamed RegistrationService to UserService
  • Loading branch information
shimizust committed Oct 23, 2015
1 parent 9045284 commit bfba569
Show file tree
Hide file tree
Showing 10 changed files with 528 additions and 61 deletions.
15 changes: 0 additions & 15 deletions core/autoloader.php

This file was deleted.

7 changes: 0 additions & 7 deletions core/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
<?php
/*
require_once('autoloader.php');
spl_autoload_register('Autoloader::model');
spl_autoload_register('Autoloader::service');
//register others here
*/

$includePaths = array();
$includePaths[] = __DIR__ . '/../model';
Expand Down
97 changes: 85 additions & 12 deletions model/DAOTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
//TODO: Replace with phpunit
include "../core/bootstrap.php";

$root = realpath("../core/bootstrap.php");
echo $root;
$dbHelper = new DBHelper();
$propKeys = array("prop1","prop2","prop3");
echo $dbHelper::genPlaceholderList($propKeys)."\n\n";

$regService = new RegistrationService();
var_dump($regService->registerUser("tester8","test","tester8@gmail.com"));

$userService = new UserService();
var_dump($userService->registerUser("tester8","test","tester8@gmail.com"));

$CDAO = new ClimbingAreaDAO();
$areaid = 7;
Expand All @@ -26,12 +28,83 @@
var_dump($UserDAO->getUserRecords(954));


$WorkoutDAO = new WorkoutDAO();
$workout_segments = array(
array("climbType"=>"boulder","ascentType"=>"flash","absGradeIndex"=>1,"reps"=>1),
array("climbType"=>"toprope","ascentType"=>"project","absGradeIndex"=>9,"reps"=>5),
array("climbType"=>"lead","ascentType"=>"redpoint","absGradeIndex"=>4,"reps"=>6),
array("climbType"=>"boulder","ascentType"=>"onsight","absGradeIndex"=>13,"reps"=>2)
$WorkoutService = new WorkoutLoggingService();
$workout_segments_abs = array(
array("climb_type"=>"boulder","ascent_type"=>"flash","grade_index"=>1,"reps"=>1),
array("climb_type"=>"toprope","ascent_type"=>"project","grade_index"=>9,"reps"=>5),
array("climb_type"=>"lead","ascent_type"=>"redpoint","grade_index"=>11,"reps"=>6),
array("climb_type"=>"boulder","ascent_type"=>"onsight","grade_index"=>13,"reps"=>2)
);
var_dump($WorkoutDAO->calcWorkoutPoints($workout_segments));
?>
var_dump($WorkoutService->calcWorkoutPoints($workout_segments_abs));


// Save a workout
$DBManager = new DBConnectionManager();
$db = $DBManager->connect();



// userid, date_workout, gymid, boulder_notes,
// tr_notes, lead_notes, other_notes
$userid = 4;

$workout_info = array("userid"=>$userid, "date_workout"=>"2015-10-23", "gymid"=>1,
"boulder_notes"=>"bouldering!", "tr_notes"=>"tr rocks", "lead_notes"=>"",
"other_notes"=>"other notes!!!");

$workout_info["boulder_points"] = 200;
$workout_info["TR_points"] = 300;
$workout_info["Lead_points"] = 400;

$userrecordDAO = new UserRecordsDAO();
var_dump($userrecordDAO->getUserRecords($userid));

$results = $WorkoutService->saveWorkoutAbsGrades($workout_info, $workout_segments_abs);
echo $results;

//$grading_systems = array("boulder"=>2,"route"=>4);

//$results = $WorkoutService->saveWorkoutRelGrades($workout_info, $workout_segments_abs, $grading_systems);

//$gradingConversionService = new GradingConversionService();
//$result = $gradingConversionService->convertRelToAbsGradeIndex("boulder", 2, 3);
//var_dump($result);

$userrecordDAO = new UserRecordsDAO();
var_dump($userrecordDAO->getUserRecords($userid));
?>



































104 changes: 103 additions & 1 deletion model/DBHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
include '../core/bootstrap.php';

class DBHelper {


public static function genPrepareString($propValArray) {
//The property name has already been verified, so no need to use
//placeholders for these, just the property values
Expand All @@ -29,4 +31,104 @@ public static function genExecuteArray($propValArray) {
}
return $executeArray;
}
}

public static function genFieldList($propKeys) {
/*
* Return a comma-delimited string of property values in $propValArray
* Input: $propKeys = array("prop1","prop2",...)
* Output: "prop1,prop2,..."
*/
return implode(",", $propKeys);
}

public static function genPlaceholderList($propKeys) {
/*
* Input: $propKeys = array("prop1","prop2",...)
* Output: ":prop1,:prop2,..."
*/
$placeholderList = $propKeys;
//Use '&' to actually modify the array
array_walk($placeholderList, function(&$value, $key) {
$value = ":".$value;
});
return implode(",", $placeholderList);
}

public static function performUpdateQuery($db, $table, $propValArray, $selectKeyValue) {
/*
* Input:
* $db = a database connection
* $table = the name of the table to update row
* $propValArray = array("prop1"=>val1,"prop2"=>val2,...)
* Update an existing row into the specified table with the
* corresponding values
* $selectKeyValue = [0=>key, 1=>value]
*/

$selectKey = $selectKeyValue[0];
$selectValue = $selectKeyValue[1];
$prepareString = self::genPrepareString($propValArray);
$stmt = $db->prepare("UPDATE ".$table." SET ".$prepareString.
" WHERE ".$selectKey."=:".$selectKey);

$executeArray = self::genExecuteArray($propValArray);
$executeArray[":".$selectKey] = $selectValue;

return $stmt->execute($executeArray);

}

public static function performInsertQuery($db, $table, $propValArray) {
/*
* Input:
* $db = a database connection
* $table = the name of the table to insert row into
* $propValArray = array("prop1"=>val1,"prop2"=>val2,...)
* Insert a new row into the specified table with the corresponding values
*/
$propKeys = array_keys($propValArray);
$fieldList = self::genFieldList($propKeys);
$placeholderList = self::genPlaceholderList($propKeys);

$stmt = $db->prepare("INSERT INTO ".$table." (".
$fieldList.") VALUES (".$placeholderList.")");

$result = $stmt->execute(DBHelper::genExecuteArray($propValArray));
return ["result"=>$result, "insertID"=>$db->lastInsertId()];
}

public static function performSimpleSelectQuery($db, $table, $selectKeyValue) {
/*
* Input:
* $db = a database connection
* $table = the name of the table to update row
* $selectKeyValue = [0=>key, 1=>value]
*/
$selectKey = $selectKeyValue[0];
$selectValue = $selectKeyValue[1];
$stmt = $db->prepare("SELECT * FROM ".$table." WHERE ".
$selectKey."=:".$selectKey);
$stmt->execute(array(":".$selectKey=>$selectValue));
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
}



















79 changes: 79 additions & 0 deletions model/UserRecordsDAO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

include "../core/bootstrap.php";

class UserRecordsDAO {

protected $db;
private static $fields = array(
"highestBoulderProject",
"highestBoulderRedpoint",
"highestBoulderFlash",
"highestBoulderOnsight",
"highestTRProject",
"highestTRRedpoint",
"highestTRFlash",
"highestTROnsight",
"highestLeadProject",
"highestLeadRedpoint",
"highestLeadFlash",
"highestLeadOnsight"
);
private static $USERRECORDS_TABLENAME="userrecords";

public function __construct() {
$DBManager = new DBConnectionManager();
$this->db = $DBManager->connect();

}

public function getUserRecords($userid) {
$selectKeyValue = ["userid", $userid];
return DBHelper::performSimpleSelectQuery($this->db,
self::$USERRECORDS_TABLENAME, $selectKeyValue);
}

public function createUserRecords($userid, $user_records) {
return DBHelper::performInsertQuery($this->db,
self::$USERRECORDS_TABLENAME, $user_records);
}

public function updateUserRecords($userid, $user_records) {
$selectValueKey = ["userid", $userid];
return DBHelper::performUpdateQuery($this->db,
self::$USERRECORDS_TABLENAME, $user_records, $selectValueKey);
}
}

































27 changes: 14 additions & 13 deletions model/WorkoutDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,32 @@ public function getWorkoutInfo($workout_id) {

public function saveWorkoutInfo($workout_info) {
/*
* Input: $workout_data = array(userid, date_workout, gymid,
* Input: $workout_info = array(userid, date_workout, gymid,
* boulder_points, TR_points, Lead_points, boulder_notes,
* tr_notes, lead_notes, other_notes)
*
* Output: array(result, insertID)
*/

//get propvals
$propVals = array_keys($workout_info);
$fieldList = implode(",", $propVals);
$placeholderList = array_walk($propVals, function($value, $key) {
$value = ":".$value;
});
$stmt = $this->db->prepare("INSERT INTO workouts (".
$fieldList.") VALUES (".$placeholderList.")");
$result = $stmt->execute(DBHelper::genExecuteArray($workout_info));
return ["result"=>$result];
return DBHelper::performInsertQuery($this->db, "workouts", $workout_info);

}

public function updateWorkoutInfo($workout_id, $workout_info) {

}

public function saveWorkoutSegments($workout_segments) {
public function saveWorkoutSegment($workout_id, $workout_segment) {
/*
* Input: $workout_segments =
* Input: $workout_segment = array(climb_type, ascent_type, grade_index, reps)
*
* Output: array(result, insertID)
*/

// Add workout_id to workout_segment array
$workout_segment["workout_id"] = $workout_id;
return DBHelper::performInsertQuery($this->db, "workout_segments", $workout_segment);

}

public function updateWorkoutSegments($workout_id, $workout_segments) {
Expand Down
4 changes: 2 additions & 2 deletions register-user.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
$email = $_POST['email'];
$password = $_POST['pass'];

$regService = new RegistrationService();
$regUserResult = $regService->registerUser($username, $password, $email);
$userService = new UserService();
$regUserResult = $userService->registerUser($username, $password, $email);

if (!$regUserResult["result"]) {
die($regUserResult["error"]);
Expand Down
Loading

0 comments on commit bfba569

Please sign in to comment.