function create_topic($request) { Authenticator::assert_manager_or_professor($request->cookies['authToken']); $msg = new Messages($GLOBALS['locale']); try { $raw_input = $request->getBody(); $content_type = explode(';', $request->type)[0]; if ($content_type !== 'application/json') { Util::output_errors_and_die('', 415); } $input_data = json_decode($raw_input, true); if (empty($input_data)) { Util::output_errors_and_die('', 400); } $model = new Model(); if (!isset($input_data['name'])) { $input_data['name'] = ''; } $topic_id = $model->create_topic($input_data['name']); if ($topic_id) { http_response_code(201); header('Content-Type: text/plain'); echo '/topics/' . $topic_id; die; } else { Util::output_errors_and_die('', 400); } } catch (ConflictException $e) { Util::output_errors_and_die($e->getMessage(), 409); } catch (DatabaseException $e) { Util::output_errors_and_die($e->getMessage(), 503); } catch (Exception $e) { Util::output_errors_and_die($e->getMessage(), 400); } }