Desenvolupat prototip inicial
L'estructura del codi del backend està completa, caldrà anar
desenvolupant més codi però sota la mateixa estructura.
Aquest commit introdueix la implementació de l'inici de sessió amb
comptes de Google de la UPC i un mètode de l'API que retorna totes les
assignatures disponibles.
diff --git a/inc/API.php b/inc/API.php
new file mode 100644
index 0000000..0d5530e
--- /dev/null
+++ b/inc/API.php
@@ -0,0 +1,104 @@
+<?php
+namespace DAFME\Covid;
+
+class API {
+ private static function returnJSON($array) {
+ echo json_encode($array);
+ }
+
+ public static function returnError($errorMessage = 'Unexpected error') {
+ http_response_code(400);
+ self::returnJson([
+ 'status' => 'error',
+ 'errorMessage' => $errorMessage
+ ]);
+ }
+
+ public static function returnPayload($payload) {
+ self::returnJson([
+ 'status' => 'ok',
+ 'payload' => $payload
+ ]);
+ }
+
+ public static function returnOk() {
+ self::returnJson([
+ 'status' => 'ok'
+ ]);
+ }
+
+ private static function checkSignInStatus() {
+ if (!Users::isSignedIn()) {
+ self::returnError('The user hasn\'t signed in.');
+ exit();
+ }
+ }
+
+ public static function process($path) {
+ global $conf;
+
+ header('Content-Type: application/json');
+
+ if (isset($conf['frontendUrl']) && !empty($conf['frontendUrl']))
+ header('Access-Control-Allow-Origin: '.$conf['frontendUrl']);
+
+ $parts = explode('/', $path);
+ $method = $parts[0] ?? '';
+
+ switch ($method) {
+ case 'getAuthUrl':
+ $auth = new Auth();
+ self::returnPayload([
+ 'url' => $auth->getAuthUrl()
+ ]);
+ break;
+
+ case 'isSignedIn':
+ $isSignedIn = \DAFME\Covid\Users::isSignedIn();
+ self::returnPayload([
+ 'signedIn' => $isSignedIn
+ ]);
+ break;
+
+ case 'signOut':
+ \DAFME\Covid\Users::signOut();
+ self::returnOk();
+ break;
+
+ case 'getAllSubjects':
+ $subjects = Subjects::getAll();
+
+ if ($subjects === false)
+ self::returnError();
+
+ self::returnPayload([
+ 'subjects' => $subjects
+ ]);
+ break;
+
+ case 'getUserSubjects':
+ self::checkSignInStatus();
+ // @TODO: Implement this method
+ break;
+
+ case 'setUserSubjects':
+ self::checkSignInStatus();
+ // @TODO: Implement this method
+ break;
+
+ case 'getClasses':
+ self::checkSignInStatus();
+ // @TODO: Implement this method
+ break;
+
+ case 'setClassState':
+ self::checkSignInStatus();
+ // @TODO: Handle this method
+ break;
+
+ default:
+ self::returnError('The method requested doesn\'t exist.');
+ break;
+ }
+ }
+}