-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
292 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/** | ||
* All the API calls | ||
*/ | ||
import Course from './models/Course'; | ||
import Exam from './models/Exam'; | ||
|
||
const BASEURL = '/api'; | ||
|
||
async function getAllCourses() { | ||
// call: GET /api/courses | ||
const response = await fetch(BASEURL + '/courses'); | ||
const coursesJson = await response.json(); | ||
if (response.ok) { | ||
return coursesJson.map((co) => Course.from(co)); | ||
} else { | ||
throw coursesJson; // an object with the error coming from the server | ||
} | ||
} | ||
|
||
async function getAllExams() { | ||
// call: GET /api/exams | ||
const response = await fetch(BASEURL + '/exams'); | ||
const examsJson = await response.json(); | ||
if (response.ok) { | ||
return examsJson.map((ex) => Exam.from(ex)); | ||
} else { | ||
throw examsJson; // an object with the error coming from the server | ||
} | ||
} | ||
|
||
function deleteExam(coursecode) { | ||
// call: DELETE /api/exams/:coursecode | ||
return new Promise((resolve, reject) => { | ||
fetch(BASEURL + '/exams/' + coursecode, { | ||
method: 'DELETE', | ||
}).then((response) => { | ||
if (response.ok) { | ||
resolve(null); | ||
} else { | ||
// analyze the cause of error | ||
response.json() | ||
.then((message) => { reject(message); }) // error message in the response body | ||
.catch(() => { reject({ error: "Cannot parse server response." }) }); // something else | ||
} | ||
}).catch(() => { reject({ error: "Cannot communicate with the server." }) }); // connection errors | ||
}); | ||
} | ||
|
||
function addExam(exam) { | ||
// call: POST /api/exams | ||
return new Promise((resolve, reject) => { | ||
fetch(BASEURL + '/exams', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({code: exam.coursecode, score: exam.score, date: exam.date}), | ||
}).then((response) => { | ||
if (response.ok) { | ||
resolve(null); | ||
} else { | ||
// analyze the cause of error | ||
response.json() | ||
.then((message) => { reject(message); }) // error message in the response body | ||
.catch(() => { reject({ error: "Cannot parse server response." }) }); // something else | ||
} | ||
}).catch(() => { reject({ error: "Cannot communicate with the server." }) }); // connection errors | ||
}); | ||
} | ||
|
||
function updateExam(exam) { | ||
// call: PUT /api/exams/:coursecode | ||
return new Promise((resolve, reject) => { | ||
fetch(BASEURL + '/exams/' + exam.coursecode, { | ||
method: 'PUT', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({code: exam.coursecode, score: exam.score, date: exam.date}), | ||
}).then((response) => { | ||
if (response.ok) { | ||
resolve(null); | ||
} else { | ||
// analyze the cause of error | ||
response.json() | ||
.then((obj) => { reject(obj); }) // error message in the response body | ||
.catch(() => { reject({ error: "Cannot parse server response." }) }); // something else | ||
} | ||
}).catch(() => { reject({ error: "Cannot communicate with the server." }) }); // connection errors | ||
}); | ||
} | ||
|
||
const API = {getAllCourses, getAllExams, deleteExam, addExam, updateExam}; | ||
export default API; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Object describing a course | ||
*/ | ||
class Course { | ||
/** | ||
* Create a new Course | ||
* @param {*} coursecode unique code for the course | ||
* @param {*} name full name of the course | ||
* @param {*} CFU number of CFU credits | ||
*/ | ||
constructor(coursecode, name, CFU) { | ||
this.coursecode = coursecode; | ||
this.name = name; | ||
this.CFU = CFU; | ||
} | ||
|
||
/** | ||
* Creates a new Course from plain (JSON) objects | ||
* @param {*} json a plain object (coming form JSON deserialization) | ||
* with the right properties | ||
* @return {Course} the newly created object | ||
*/ | ||
static from(json) { | ||
const course = new Course(); | ||
delete Object.assign(course, json, {coursecode: json.code}).code; | ||
return course; | ||
} | ||
|
||
} | ||
|
||
export default Course; |
Oops, something went wrong.