Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
90edbcd
change comments to increase maintainability index in LicensePlateServ…
gpittis Dec 20, 2024
e06117a
Change comments in LicensePlateService.js
gpittis Dec 20, 2024
471353c
split complex checks into multiple ones in LicensePlateService.js
gpittis Dec 20, 2024
71c2df7
restore the code
gpittis Dec 20, 2024
009f880
change comments
gpittis Dec 20, 2024
663389a
add more comments
gpittis Dec 20, 2024
676aefe
change comments in SpotOwnerService.js
gpittis Dec 20, 2024
1df6ef7
add comments
gpittis Dec 20, 2024
b537ffd
change comments position
gpittis Dec 20, 2024
e0c6c76
remove unused dummy data
gpittis Dec 20, 2024
ccc9ab6
remove the unused checks
gpittis Dec 20, 2024
f9466c6
change comments
gpittis Dec 20, 2024
e7a2378
add spaces
gpittis Dec 20, 2024
9297c01
change comments
gpittis Dec 20, 2024
c95dc54
change comments positions
gpittis Dec 20, 2024
2b6bc25
add spaces
gpittis Dec 20, 2024
0c1b988
change comments
gpittis Dec 20, 2024
b1576c7
add comments
gpittis Dec 20, 2024
7da9ff9
change and add comments
gpittis Dec 20, 2024
c13abde
remove unnecessary semicolons
gpittis Dec 20, 2024
e68a6bc
remove unused dummy data
gpittis Dec 20, 2024
369b923
make more simple the code of SpotOwnerService.js
gpittis Dec 20, 2024
d740178
make more simple the code of LicensePlateService.js
gpittis Dec 20, 2024
f13626d
Simplify the code and reduce the number of reject(error) commands in …
gpittis Dec 20, 2024
e5a9401
Make the final documentation and add more precise checks
gpittis Dec 21, 2024
ae95e5b
Make the final documentation
gpittis Dec 21, 2024
8f30205
Make the final documentation
gpittis Dec 21, 2024
5b1cce0
Update POST_payment.test.js from main
gpittis Dec 21, 2024
7165388
Update POST_reservation.test.js from main
gpittis Dec 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 42 additions & 68 deletions service/LicensePlateService.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,37 @@
* body LicensePlate License plate object to update
* no response value expected for this operation
**/


exports.modifyPlate = function(body) {
return new Promise(function(resolve, reject) {



// Μια ήδη καταχωρημένη πινακίδα μέσα στο σύστημα
var existingPlates = {
"licensePlate": "AKH1314", // όνομα πινακίδας
"id": 15, // το id της πινακίδας

};

if (!body.licensePlate || body.licensePlate === "" ) {
//αν το licensePlate δεν υπάρχει στο request body ή ισούται με το κενό string τότε έχω σφάλμα με κωδικό σφάλματος 400
const error = new Error("license Plate does not exist");
error.response = { statusCode: 400 };
reject(error);
return;
}

// Ο έλεγχος αυτός έγινε comment διότι εκτελείται από το swagger. Συνεπώς, τον κάνω comment ώστε να μην μου χαλάει το
//coverage του modifyPlate
// if (typeof body.licensePlate !== "string"){
// //αν το licensePlate έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400
// const error = new Error("Invalid licenseplate");
// error.response = { statusCode: 400 };
// reject(error);
// return;
// }

//Ορίζω κανόνες εγκυρότητας για τα fields της πινακίδας.
//Αν το licensePlate δεν υπάρχει στο request body ή ισούται με το κενό string τότε έχω σφάλμα με κωδικό σφάλματος 400.
//Επίσης, αν το id έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400.
// Έτσι, μειώνω το πλήθος των reject(error).
const validations = [
{ key: 'licensePlate', rule: (value) => value && value !== "", errorMessage: "license Plate does not exist" },
{ key: 'id', rule: (value) => Number.isInteger(value) && value > 0,
errorMessage: "Invalid id: must be a positive integer." }
];

if (!Number.isInteger(body.id) || body.id < 0) {
//αν το id έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400
const error = new Error("Invalid id: must be a positive integer.");
error.response = { statusCode: 400 };
reject(error);
return;
// Έλεγχος για σφάλματα στα fields της πινακίδας
for (const { key, rule, errorMessage } of validations) {
const value = body[key];
if (!rule(value)) { // Αν η τιμή δεν ικανοποιεί τον κανόνα εγκυρότητας (rule), τότε υπάρχει σφάλμα
const error = new Error(errorMessage);
error.response = { statusCode: 400 }; // Επιστρέφει κωδικό 400
reject(error);
return;
}
}

//Αν η dummy πινακίδα που είναι ήδη καταχωρημένη μέσα στο σύστημα έχει ίδιο id με εκείνο του request body και διαφορετικό
//Αν η dummy πινακίδα που είναι ήδη καταχωρημένη μέσα στο σύστημα έχει ίδιο id με εκείνο του request body και διαφορετικό
// όνομα πινακίδας με εκείνο του request body, τότε συμβαίνει με επιτυχία η τροποποίηση της πινακίδας. Αν τα γνωρίσματα της
// dummy καταχωρημένης πινακίδας και του request body ταυτίζονται , τότε δεν τροποποιώ την πινακίδα αλλά αντίθετα διατηρώ
// τα "παλιά" της στοιχεία.Τέλος, αν όλα τα γνωρίσματα της dummy πινακίδας διαφέρουν με τα αντίστοιχα γνωρίσματα του request
Expand All @@ -72,14 +61,13 @@ exports.modifyPlate = function(body) {
// Handle non-existent ID (η πινακίδα δεν υπάρχει μέσα στο σύστημα)
const isNonExistent = existingPlates.id !== body.id;
if (isNonExistent) {
const error = new Error("License plate doesn't exist.");
const error = new Error("License plate doesn't exist."); // Η πινακίδα που επιθυμώ να τροποποιήσω ΔΕΝ βρέθηκε.
error.response = { statusCode: 404 }; // Επιστρέφει κωδικό σφάλματος 404.
//Δηλαδή, η πινακίδα που επιθυμώ να τροποποιήσω ΔΕΝ βρέθηκε.
reject(error);
return;
}

resolve(); // Return the updated plate object
resolve();
});
};

Expand All @@ -92,42 +80,28 @@ exports.modifyPlate = function(body) {
* no response value expected for this operation
**/
exports.registerPlate = function(body) {
return new Promise((resolve, reject) => {
return new Promise((resolve, reject) => {
//Ορίζω κανόνες εγκυρότητας για τα fields της πινακίδας.
//Αν το id έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400.
//Αν το licensePlate δεν υπάρχει στο request body ή ισούται με το κενό string τότε έχω σφάλμα με κωδικό σφάλματος 400.
// Έτσι, μειώνω το πλήθος των reject(error).
const validations = [
{ key: 'id', rule: (value) => Number.isInteger(value) && value > 0, errorMessage: 'Invalid id: must be a positive integer.' },
{ key: 'licensePlate', rule: (value) => value && value !== "", errorMessage: 'licenseplate does not exist' }
];

// Μια ήδη καταχωρημένη πινακίδα μέσα στο σύστημα
var existingPlates = {
"licensePlate": "AKH1314", // το όνομα της πινακίδας
"id": 15, // το id της πινακίδας

};


if (!Number.isInteger(body.id) || body.id < 0) {
//αν το id έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400
const error = new Error("Invalid id: must be a positive integer.");
error.response = { statusCode: 400 };
reject(error);
return;
}


if (!body.licensePlate || body.licensePlate === "") {
//αν το licensePlate δεν υπάρχει στο request body ή ισούται με το κενό string τότε έχω σφάλμα με κωδικό σφάλματος 400
const error = new Error("licenseplate does not exist");
error.response = { statusCode: 400 };
reject(error);
return;
// Έλεγχος για σφάλματα στα fields της πινακίδας
for (const { key, rule, errorMessage } of validations) {
const value = body[key];
if (!rule(value)) { // Αν η τιμή δεν ικανοποιεί τον κανόνα εγκυρότητας (rule), τότε υπάρχει σφάλμα
const error = new Error(errorMessage);
error.response = { statusCode: 400 }; // Επιστρέφει κωδικό 400
reject(error);
return;
}
}

// Ο έλεγχος αυτός έγινε comment διότι εκτελείται από το swagger. Συνεπώς, τον κάνω comment ώστε να μην μου χαλάει το
//coverage του registerPlate
// if (typeof body.licensePlate !== "string"){
// //αν το licensePlate έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400
// const error = new Error("Invalid licenseplate");
// error.response = { statusCode: 400 };
// reject(error);
// return;
// }

resolve();
});
});
};

97 changes: 30 additions & 67 deletions service/SpotOwnerService.js
Original file line number Diff line number Diff line change
@@ -1,80 +1,43 @@
'use strict';


/**
* Add a new spot owner
* FR15: The system administrator must be able to add a spot owner to the system
*
* body SpotOwner Spot owner object to add
* no response value expected for this operation
**/


exports.addSpotOwner = function(body) {
return new Promise((resolve, reject) =>{
//SpotOwner = Ιδιοκτήτης θέσεων πάρκινγκ
var existingSpotOwners = {
"id":1, // το id του ιδιοκτήτη θέσεων
"idNumber": "AK1234", // ο αριθμός ταυτότητας του ιδιοκτήτη θέσεων
"name":"John Doe", // ο ονοματεπώνυμο του ιδιοκτήτη
"email":"johhdoe@gmail.com", // το email του ιδιοκτήτη
"phone":"1234567890",// το τηλέφωνο του ιδιοκτήτη
"spots":[] // οι θέσεις του ιδιοκτήτη
};

return new Promise((resolve, reject) => {

if (!body.id || body.id < 0) {
//αν το id έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400
const error = new Error("Invalid id: must be a positive integer.");
error.response = { statusCode: 400 };
reject(error);
return;
}

if (!body.idNumber || typeof body.idNumber !== "string") {
//αν το idNumber έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400
const error = new Error("Invalid idNumber: must be a string.");
error.response = { statusCode: 400 };
reject(error);
return;
}

if (!body.name || typeof body.name !== "string") {
//αν το name έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400
const error = new Error("Invalid name: must be a string.");
error.response = { statusCode: 400 };
reject(error);
return;
}

if (!body.email || typeof body.email !== "string") {

//αν το email έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400
const error = new Error("Invalid email: must be a string.");
error.response = { statusCode: 400 };
reject(error);
return;
}
//idNumber = αριθμός ταυτότητας
//spots = μια λίστα που περιέχει τις θέσεις που ανήκουν στον ιδιοκτήτη θέσεων.

if (!body.phone) {

//αν το phone δεν υπάρχει στο request body τότε έχω σφάλμα με κωδικό σφάλματος 400
const error = new Error("No phone.");
error.response = { statusCode: 400 };
reject(error);
return;
}

if (!body.spots) {

//αν το spots δεν υπάρχει στο request body τότε έχω σφάλμα με κωδικό σφάλματος 400
const error = new Error("No spots.");
error.response = { statusCode: 400 };
reject(error);
return;
// Ορίζω κανόνες εγκυρότητας για κάθε field του Ιδιοκτήτη θέσεων.
//Αν το id δεν είναι θετικός ακέραιος τότε έχω σφάλμα με κωδικό 400.
//Αν το idNumber δεν είναι string τότε έχω σφάλμα με κωδικό 400. Το ίδιο ισχύει και για τα name και email.
//Αν το phone δεν υπάρχει στο request body τότε έχω σφάλμα με κωδικό σφάλματος 400. Το ίδιο ισχύει και για το spots.
const validations = [
{ key: 'id', rule: (value) => value > 0 && Number.isInteger(value), errorMessage: 'Invalid id: must be a positive integer.' },
{ key: 'idNumber', rule: (value) => typeof value === 'string' && value !== undefined
&& value !== null && value.trim() !== '', errorMessage: 'Invalid idNumber: must be a string.' },
{ key: 'name', rule: (value) => typeof value === 'string' && value !== undefined
&& value !== null && value.trim() !== '', errorMessage: 'Invalid name: must be a string.' },
{ key: 'email', rule: (value) => typeof value === 'string' && value !== undefined
&& value !== null && value.trim() !== '', errorMessage: 'Invalid email: must be a string.' },
{ key: 'phone', rule: (value) => value !== undefined, errorMessage: 'No phone.' },
{ key: 'spots', rule: (value) => value !== undefined, errorMessage: 'No spots.' },
];

// Έλεγχος για σφάλματα στα fields του Ιδιοκτήτη θέσεων
for (const { key, rule, errorMessage } of validations) {
const value = body[key];
if (!rule(value)) { // Αν η τιμή δεν ικανοποιεί τον κανόνα εγκυρότητας (rule), τότε υπάρχει σφάλμα
const error = new Error(errorMessage);
error.response = { statusCode: 400 }; // Επιστρέφει κωδικό 400
reject(error);
return;
}
}

resolve();
});

}
});
};
Loading
Loading