getMessage()) { case "NotFound": throw ApiError::notFound("user"); default: throw $err; } } } /** * Users PATCH handler * * Update a user. * * Returns updated user. * * @param mixed $id User ID * * @throws 404 User not found * @throws 500 Failed to update user */ public function patch($id): void { // Fetch all inputs. $username = Input::patch("username"); $password = Input::patch("password"); $email = Input::patch("email"); // Try and update user. // Throw errors according to situation. try { Response::json(User::getByID($id)->update($username, $password, $email)); } catch (Exception $err) { switch ($err->getMessage()) { case "NotFound": throw ApiError::notFound("user"); default: // Due to how the failed field is handled, it's ApiError is inside the models update throw $err; } } } /** * Users PATCH handler * * Update a user. User is retrieved using the authentication `token`. * * Returns updated user. * * @throws 404 User not found * @throws 500 Failed to update user */ public function patchSelf(): void { // Fetch all inputs. $token = Request::token(); $username = Input::patch("username"); $password = Input::patch("password"); $email = Input::patch("email"); // Try and update user. // Throw errors according to situation. try { Response::json(User::getByToken($token)->update($username, $password, $email)); } catch (Exception $err) { switch ($err->getMessage()) { case "NotFound": throw ApiError::notFound("user"); default: // Due to how the failed field is handled, it's ApiError is inside the models update throw $err; } } } /** * Users DELETE handler * * Deletes a user. Optional parameter is `l` (limit of list for which the returned pages is calculated). * * Returns deleted user and resulting amount of pages for a given limit. * * @param mixed $id User ID * * @throws 404 User not found */ public function delete($id): void { // Fetch and constrain all parameters. $limit = constrain(0, 30, intval(Input::get("l", 10))); // Try to delete user, 404 if not found. try { Response::json(User::getByID($id)->delete($limit)); } catch (Exception $err) { switch ($err->getMessage()) { case "NotFound": throw ApiError::notFound("user"); default: throw $err; } } } }