diff options
author | Aaron Parecki <aaron@parecki.com> | 2024-08-31 16:09:48 +0000 |
---|---|---|
committer | Aaron Parecki <aaron@parecki.com> | 2024-08-31 16:09:48 +0000 |
commit | 11237831316aba7c36bbbc86cf1578a408086046 (patch) | |
tree | 0636c45d4169563a9ddaa37cb05af693389a483f /controllers | |
parent | 01f99ab9407ad1e4235b90344181ae4ba96bd9dc (diff) |
add exercise posts
Diffstat (limited to 'controllers')
-rw-r--r-- | controllers/controllers.php | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/controllers/controllers.php b/controllers/controllers.php index b1d96c4..4feba3f 100644 --- a/controllers/controllers.php +++ b/controllers/controllers.php @@ -1085,3 +1085,63 @@ $app->post('/weight', function() use($app) { ))); } }); + + +function create_exercise(&$user, $activity, $minutes, $heartrate, $published) { + $micropub_request = array( + 'type' => ['h-entry'], + 'properties' => [ + 'workout' => [[ + 'type' => ['h-workout'], + 'properties' => [ + 'activity' => [$activity], + 'duration' => [[ + 'type' => ['h-measure'], + 'properties' => [ + 'num' => [($minutes*60)], + 'unit' => ['second'] + ], + ]], + 'heartrate' => [[ + 'type' => 'h-measure', + 'properties' => [ + 'num' => [$heartrate], + 'unit' => ['bpm'], + ] + ]] + ] + ]] + ] + ); + try { + $date = new DateTime($published); + $micropub_request['properties']['published'] = [$date->format('c')]; + } catch(Exception $e) { + } + $r = micropub_post_for_user($user, $micropub_request, null, true); + + return $r; +} + +$app->get('/exercise', function() use($app){ + if($user=require_login($app)) { + render('new-exercise', array( + 'title' => 'New Exercise', + )); + } +}); + +$app->post('/exercise', function() use($app) { + if($user=require_login($app)) { + $params = $app->request()->params(); + + $r = create_exercise($user, $params['activity'], $params['minutes'], $params['heartrate'], $params['published']); + $location = $r['location']; + + $app->response()['Content-type'] = 'application/json'; + $app->response()->body(json_encode(array( + 'location' => $location, + 'error' => $r['error'] + ))); + } +}); |