*/ chdir(dirname(__FILE__)); require '../config.php'; $number_of_matches = 2; /* Get list of existing teams ******************************************************************************/ $teams = $db->fetch("SELECT id FROM Team", null, 'Team'); for ($i = 0; $i < $number_of_matches; $i++) { /* Assign two random opposing teams ******************************************************************************/ $team_1 = $teams[mt_rand(0, count($teams) - 1)]->id; // Prevent second team from being the same as the first one do { $team_2 = $teams[mt_rand(0, count($teams) - 1)]->id; } while ($team_2 == $team_1); /* Generate match start dates in the near future and duration ******************************************************************************/ $format = 'Y-m-d H:i:s'; $duration = 5; // Duration of the match (in minutes) $start_time = date($format, strtotime("now +" . mt_rand(5, 25) . " minutes")); $end_time = date($format, strtotime("{$start_time} +{$duration} minutes")); /* Create the match ******************************************************************************/ $match = new Match(); $match->team_1 = $team_1; $match->team_2 = $team_2; $match->start_time = $start_time; $match->end_time = $end_time; $match->Create(); }
/* Create match **********************************************************************************************************************/ $app->post('/matches', function () use($app) { $data = json_decode($app->request->getBody(), true); $team_1 = $data['team_1']; $team_2 = $data['team_2']; $start_time = $data['start_time']; $end_time = $data['end_time']; // Setup the match $match = new Match(); $match->team_1 = $team_1; $match->team_2 = $team_2; $match->start_time = $start_time; $match->end_time = $end_time; // Create the match if ($match_id = $match->Create()) { $app->render_json(["id" => $match_id]); } else { throw new Exception("Error creating the match", 400); } }); /* Insert comment **********************************************************************************************************************/ $app->post('/matches/:id/comments', function ($id) use($app) { $data = json_decode($app->request->getBody(), true); $text = $data['text']; $user = User::GetByToken($app->request->headers->get('token')); if (empty($text)) { throw new Exception("No text in comment", 400); } // Insert the comment