From 90edbcd328231ae8b072eceb2871e938089f18ff Mon Sep 17 00:00:00 2001 From: gpittis Date: Fri, 20 Dec 2024 09:00:22 +0200 Subject: [PATCH 01/29] change comments to increase maintainability index in LicensePlateService.js --- service/LicensePlateService.js | 43 +++++++--------------------------- 1 file changed, 8 insertions(+), 35 deletions(-) diff --git a/service/LicensePlateService.js b/service/LicensePlateService.js index 5db813a..3ee79c1 100644 --- a/service/LicensePlateService.js +++ b/service/LicensePlateService.js @@ -8,41 +8,25 @@ * 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 της πινακίδας - }; + //αν το licensePlate δεν υπάρχει στο request body ή ισούται με το κενό string τότε έχω σφάλμα με κωδικό σφάλματος 400 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; - // } - - + //αν το id έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 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); @@ -63,18 +47,19 @@ exports.modifyPlate = function(body) { } // Allow updating the license plate for an existing ID (Επιτυχής τροποποίηση πινακίδας) + // Valid update returns response code 200 const isValidUpdate = existingPlates.id === body.id && existingPlates.licensePlate !== body.licensePlate; if (isValidUpdate) { - resolve(); // This means the update is valid, return 200 + resolve(); return; } // Handle non-existent ID (η πινακίδα δεν υπάρχει μέσα στο σύστημα) + // Αν η πινακίδα που επιθυμώ να τροποποιήσω ΔΕΝ βρεθεί, τότε επιστρέφει κωδικό σφάλματος 404. const isNonExistent = existingPlates.id !== body.id; if (isNonExistent) { const error = new Error("License plate doesn't exist."); - error.response = { statusCode: 404 }; // Επιστρέφει κωδικό σφάλματος 404. - //Δηλαδή, η πινακίδα που επιθυμώ να τροποποιήσω ΔΕΝ βρέθηκε. + error.response = { statusCode: 404 }; reject(error); return; } @@ -98,36 +83,24 @@ exports.registerPlate = function(body) { var existingPlates = { "licensePlate": "AKH1314", // το όνομα της πινακίδας "id": 15, // το id της πινακίδας - }; - + //αν το id έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 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; } - + //αν το licensePlate δεν υπάρχει στο request body ή ισούται με το κενό string τότε έχω σφάλμα με κωδικό σφάλματος 400 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; } - // Ο έλεγχος αυτός έγινε 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(); }); }; From e06117a3c3f8812d5018d9beca550be92cc9d22d Mon Sep 17 00:00:00 2001 From: gpittis Date: Fri, 20 Dec 2024 09:13:47 +0200 Subject: [PATCH 02/29] Change comments in LicensePlateService.js --- service/LicensePlateService.js | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/service/LicensePlateService.js b/service/LicensePlateService.js index 3ee79c1..f4d51fa 100644 --- a/service/LicensePlateService.js +++ b/service/LicensePlateService.js @@ -11,13 +11,13 @@ exports.modifyPlate = function(body) { return new Promise(function(resolve, reject) { - // Μια ήδη καταχωρημένη πινακίδα μέσα στο σύστημα + // A license plate already registered in the system var existingPlates = { - "licensePlate": "AKH1314", // όνομα πινακίδας - "id": 15, // το id της πινακίδας + "licensePlate": "AKH1314", + "id": 15, }; - //αν το licensePlate δεν υπάρχει στο request body ή ισούται με το κενό string τότε έχω σφάλμα με κωδικό σφάλματος 400 + // If the licensePlate is not present in the request body or is an empty string, return an error with status code 400 if (!body.licensePlate || body.licensePlate === "" ) { const error = new Error("license Plate does not exist"); error.response = { statusCode: 400 }; @@ -25,7 +25,7 @@ exports.modifyPlate = function(body) { return; } - //αν το id έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 + // If the id has an invalid value, return an error with status code 400 if (!Number.isInteger(body.id) || body.id < 0) { const error = new Error("Invalid id: must be a positive integer."); error.response = { statusCode: 400 }; @@ -33,11 +33,7 @@ exports.modifyPlate = function(body) { return; } - //Αν η dummy πινακίδα που είναι ήδη καταχωρημένη μέσα στο σύστημα έχει ίδιο id με εκείνο του request body και διαφορετικό - // όνομα πινακίδας με εκείνο του request body, τότε συμβαίνει με επιτυχία η τροποποίηση της πινακίδας. Αν τα γνωρίσματα της - // dummy καταχωρημένης πινακίδας και του request body ταυτίζονται , τότε δεν τροποποιώ την πινακίδα αλλά αντίθετα διατηρώ - // τα "παλιά" της στοιχεία.Τέλος, αν όλα τα γνωρίσματα της dummy πινακίδας διαφέρουν με τα αντίστοιχα γνωρίσματα του request - // body , τότε δημιουργώ μια νέα πινακίδα και ΔΕΝ τροποποιώ μια ήδη υπάρχουσα πινακίδα. + // Only if the registered plate has the same id but a different name, update it. const isExactMatch = existingPlates.id === body.id && existingPlates.licensePlate === body.licensePlate; if (isExactMatch) { const error = new Error("License plate already exists."); @@ -46,16 +42,14 @@ exports.modifyPlate = function(body) { return; } - // Allow updating the license plate for an existing ID (Επιτυχής τροποποίηση πινακίδας) - // Valid update returns response code 200 + // Allow updating the license plate for an existing ID(Successful license plate modification). Returns code 200. const isValidUpdate = existingPlates.id === body.id && existingPlates.licensePlate !== body.licensePlate; if (isValidUpdate) { resolve(); return; } - // Handle non-existent ID (η πινακίδα δεν υπάρχει μέσα στο σύστημα) - // Αν η πινακίδα που επιθυμώ να τροποποιήσω ΔΕΝ βρεθεί, τότε επιστρέφει κωδικό σφάλματος 404. + // Handle non-existent ID. If the plate does not exist in the system, then returns code 404. const isNonExistent = existingPlates.id !== body.id; if (isNonExistent) { const error = new Error("License plate doesn't exist."); @@ -64,7 +58,7 @@ exports.modifyPlate = function(body) { return; } - resolve(); // Return the updated plate object + resolve(); }); }; @@ -79,13 +73,13 @@ exports.modifyPlate = function(body) { exports.registerPlate = function(body) { return new Promise((resolve, reject) => { - // Μια ήδη καταχωρημένη πινακίδα μέσα στο σύστημα + // A license plate already registered in the system var existingPlates = { - "licensePlate": "AKH1314", // το όνομα της πινακίδας - "id": 15, // το id της πινακίδας + "licensePlate": "AKH1314", + "id": 15, }; - //αν το id έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 + // If the id has an invalid value, return an error with status code 400. if (!Number.isInteger(body.id) || body.id < 0) { const error = new Error("Invalid id: must be a positive integer."); error.response = { statusCode: 400 }; @@ -93,7 +87,7 @@ exports.registerPlate = function(body) { return; } - //αν το licensePlate δεν υπάρχει στο request body ή ισούται με το κενό string τότε έχω σφάλμα με κωδικό σφάλματος 400 + // If the licensePlate is not present in the request body or is an empty string, return an error with status code 400 if (!body.licensePlate || body.licensePlate === "") { const error = new Error("licenseplate does not exist"); error.response = { statusCode: 400 }; From 471353cd851f1664fe3130e622d3a7df0b0a090d Mon Sep 17 00:00:00 2001 From: gpittis Date: Fri, 20 Dec 2024 09:26:45 +0200 Subject: [PATCH 03/29] split complex checks into multiple ones in LicensePlateService.js --- service/LicensePlateService.js | 44 +++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/service/LicensePlateService.js b/service/LicensePlateService.js index f4d51fa..a4c37a8 100644 --- a/service/LicensePlateService.js +++ b/service/LicensePlateService.js @@ -17,16 +17,32 @@ exports.modifyPlate = function(body) { "id": 15, }; - // If the licensePlate is not present in the request body or is an empty string, return an error with status code 400 - if (!body.licensePlate || body.licensePlate === "" ) { + // If the licensePlate is not present in the request body, return an error with status code 400 + if (!body.licensePlate) { const error = new Error("license Plate does not exist"); error.response = { statusCode: 400 }; reject(error); return; } + // If the licensePlate is empty string in the request body, return an error with status code 400 + if (body.licensePlate === "" ) { + const error = new Error("license Plate does not exist"); + error.response = { statusCode: 400 }; + reject(error); + return; + } + + // If the id has an invalid value, return an error with status code 400 + if (!Number.isInteger(body.id)) { + const error = new Error("Invalid id: must be a positive integer."); + error.response = { statusCode: 400 }; + reject(error); + return; + } + // If the id has an invalid value, return an error with status code 400 - if (!Number.isInteger(body.id) || body.id < 0) { + if (body.id < 0) { const error = new Error("Invalid id: must be a positive integer."); error.response = { statusCode: 400 }; reject(error); @@ -80,15 +96,31 @@ exports.registerPlate = function(body) { }; // If the id has an invalid value, return an error with status code 400. - if (!Number.isInteger(body.id) || body.id < 0) { + if (!Number.isInteger(body.id)) { + const error = new Error("Invalid id: must be a positive integer."); + error.response = { statusCode: 400 }; + reject(error); + return; + } + + // If the id has an invalid value, return an error with status code 400. + if (body.id < 0) { const error = new Error("Invalid id: must be a positive integer."); error.response = { statusCode: 400 }; reject(error); return; } - // If the licensePlate is not present in the request body or is an empty string, return an error with status code 400 - if (!body.licensePlate || body.licensePlate === "") { + // If the licensePlate is not present in the request body, return an error with status code 400 + if (!body.licensePlate) { + const error = new Error("licenseplate does not exist"); + error.response = { statusCode: 400 }; + reject(error); + return; + } + + // If the licensePlate is an empty string, return an error with status code 400 + if (body.licensePlate === "") { const error = new Error("licenseplate does not exist"); error.response = { statusCode: 400 }; reject(error); From 71c2df726a543256ec84f3929e4a503b62212a0e Mon Sep 17 00:00:00 2001 From: gpittis Date: Fri, 20 Dec 2024 09:28:55 +0200 Subject: [PATCH 04/29] restore the code --- service/LicensePlateService.js | 60 ++++++++++------------------------ 1 file changed, 17 insertions(+), 43 deletions(-) diff --git a/service/LicensePlateService.js b/service/LicensePlateService.js index a4c37a8..b3571e0 100644 --- a/service/LicensePlateService.js +++ b/service/LicensePlateService.js @@ -17,39 +17,27 @@ exports.modifyPlate = function(body) { "id": 15, }; - // If the licensePlate is not present in the request body, return an error with status code 400 - if (!body.licensePlate) { + // If the licensePlate is not present in the request body or is an empty string, return an error with status code 400 + if (!body.licensePlate || body.licensePlate === "" ) { const error = new Error("license Plate does not exist"); error.response = { statusCode: 400 }; reject(error); return; } - // If the licensePlate is empty string in the request body, return an error with status code 400 - if (body.licensePlate === "" ) { - const error = new Error("license Plate does not exist"); - error.response = { statusCode: 400 }; - reject(error); - return; - } - - // If the id has an invalid value, return an error with status code 400 - if (!Number.isInteger(body.id)) { - const error = new Error("Invalid id: must be a positive integer."); - error.response = { statusCode: 400 }; - reject(error); - return; - } - // If the id has an invalid value, return an error with status code 400 - if (body.id < 0) { + if (!Number.isInteger(body.id) || body.id < 0) { const error = new Error("Invalid id: must be a positive integer."); error.response = { statusCode: 400 }; reject(error); return; } - // Only if the registered plate has the same id but a different name, update it. + // If the dummy plate already registered in the system has the same id as the request body but a different + // license plate name from the request body, the modification of the license plate is successful. + // If the attributes of the dummy registered plate and the request body match, then the plate is not modified, + // and its "old" attributes are retained. Finally, if all attributes of the dummy plate differ from those + // of the request body, a new plate is created and an existing plate is NOT modified. const isExactMatch = existingPlates.id === body.id && existingPlates.licensePlate === body.licensePlate; if (isExactMatch) { const error = new Error("License plate already exists."); @@ -58,14 +46,16 @@ exports.modifyPlate = function(body) { return; } - // Allow updating the license plate for an existing ID(Successful license plate modification). Returns code 200. + // Allow updating the license plate for an existing ID (Successful license plate modification) + // Valid update returns response code 200 const isValidUpdate = existingPlates.id === body.id && existingPlates.licensePlate !== body.licensePlate; if (isValidUpdate) { resolve(); return; } - // Handle non-existent ID. If the plate does not exist in the system, then returns code 404. + // Handle non-existent ID (the plate does not exist in the system) + // If the plate to be modified is NOT found, return error code 404. const isNonExistent = existingPlates.id !== body.id; if (isNonExistent) { const error = new Error("License plate doesn't exist."); @@ -74,7 +64,7 @@ exports.modifyPlate = function(body) { return; } - resolve(); + resolve(); // Return the updated plate object }); }; @@ -95,32 +85,16 @@ exports.registerPlate = function(body) { "id": 15, }; - // If the id has an invalid value, return an error with status code 400. - if (!Number.isInteger(body.id)) { - const error = new Error("Invalid id: must be a positive integer."); - error.response = { statusCode: 400 }; - reject(error); - return; - } - - // If the id has an invalid value, return an error with status code 400. - if (body.id < 0) { + // If the id has an invalid value, return an error with status code 400 + if (!Number.isInteger(body.id) || body.id < 0) { const error = new Error("Invalid id: must be a positive integer."); error.response = { statusCode: 400 }; reject(error); return; } - // If the licensePlate is not present in the request body, return an error with status code 400 - if (!body.licensePlate) { - const error = new Error("licenseplate does not exist"); - error.response = { statusCode: 400 }; - reject(error); - return; - } - - // If the licensePlate is an empty string, return an error with status code 400 - if (body.licensePlate === "") { + // If the licensePlate is not present in the request body or is an empty string, return an error with status code 400 + if (!body.licensePlate || body.licensePlate === "") { const error = new Error("licenseplate does not exist"); error.response = { statusCode: 400 }; reject(error); From 009f880e3968604857b7ea4ad44687aec4dddd2f Mon Sep 17 00:00:00 2001 From: gpittis Date: Fri, 20 Dec 2024 09:35:16 +0200 Subject: [PATCH 05/29] change comments --- service/LicensePlateService.js | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/service/LicensePlateService.js b/service/LicensePlateService.js index b3571e0..1395e5e 100644 --- a/service/LicensePlateService.js +++ b/service/LicensePlateService.js @@ -11,13 +11,13 @@ exports.modifyPlate = function(body) { return new Promise(function(resolve, reject) { - // A license plate already registered in the system + /**A license plate already registered in the system **/ var existingPlates = { "licensePlate": "AKH1314", "id": 15, }; - // If the licensePlate is not present in the request body or is an empty string, return an error with status code 400 + /**If the licensePlate is not present in the request body or is an empty string, return an error with status code 400 **/ if (!body.licensePlate || body.licensePlate === "" ) { const error = new Error("license Plate does not exist"); error.response = { statusCode: 400 }; @@ -25,7 +25,7 @@ exports.modifyPlate = function(body) { return; } - // If the id has an invalid value, return an error with status code 400 + /**If the id has an invalid value, return an error with status code 400 **/ if (!Number.isInteger(body.id) || body.id < 0) { const error = new Error("Invalid id: must be a positive integer."); error.response = { statusCode: 400 }; @@ -33,11 +33,7 @@ exports.modifyPlate = function(body) { return; } - // If the dummy plate already registered in the system has the same id as the request body but a different - // license plate name from the request body, the modification of the license plate is successful. - // If the attributes of the dummy registered plate and the request body match, then the plate is not modified, - // and its "old" attributes are retained. Finally, if all attributes of the dummy plate differ from those - // of the request body, a new plate is created and an existing plate is NOT modified. + /** Check if the provided id and licensePlate match exactly with any existing registration. **/ const isExactMatch = existingPlates.id === body.id && existingPlates.licensePlate === body.licensePlate; if (isExactMatch) { const error = new Error("License plate already exists."); @@ -46,16 +42,14 @@ exports.modifyPlate = function(body) { return; } - // Allow updating the license plate for an existing ID (Successful license plate modification) - // Valid update returns response code 200 + /** Allow updating the license plate for an existing ID. Returns response code 200 **/ const isValidUpdate = existingPlates.id === body.id && existingPlates.licensePlate !== body.licensePlate; if (isValidUpdate) { resolve(); return; } - // Handle non-existent ID (the plate does not exist in the system) - // If the plate to be modified is NOT found, return error code 404. + /** Handle non-existent ID (the plate does not exist in the system).Return error code 404. **/ const isNonExistent = existingPlates.id !== body.id; if (isNonExistent) { const error = new Error("License plate doesn't exist."); @@ -64,7 +58,7 @@ exports.modifyPlate = function(body) { return; } - resolve(); // Return the updated plate object + resolve(); }); }; @@ -79,13 +73,13 @@ exports.modifyPlate = function(body) { exports.registerPlate = function(body) { return new Promise((resolve, reject) => { - // A license plate already registered in the system + /** A license plate already registered in the system **/ var existingPlates = { "licensePlate": "AKH1314", "id": 15, }; - // If the id has an invalid value, return an error with status code 400 + /** If the id has an invalid value, return an error with status code 400 **/ if (!Number.isInteger(body.id) || body.id < 0) { const error = new Error("Invalid id: must be a positive integer."); error.response = { statusCode: 400 }; @@ -93,7 +87,7 @@ exports.registerPlate = function(body) { return; } - // If the licensePlate is not present in the request body or is an empty string, return an error with status code 400 + /** If the licensePlate is not present in the request body or is an empty string, return an error with status code 400 **/ if (!body.licensePlate || body.licensePlate === "") { const error = new Error("licenseplate does not exist"); error.response = { statusCode: 400 }; From 663389ab7b7cc0c96208bba98a0e8d6819410fbf Mon Sep 17 00:00:00 2001 From: gpittis Date: Fri, 20 Dec 2024 09:39:48 +0200 Subject: [PATCH 06/29] add more comments --- service/LicensePlateService.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/service/LicensePlateService.js b/service/LicensePlateService.js index 1395e5e..67d8502 100644 --- a/service/LicensePlateService.js +++ b/service/LicensePlateService.js @@ -35,6 +35,8 @@ exports.modifyPlate = function(body) { /** Check if the provided id and licensePlate match exactly with any existing registration. **/ const isExactMatch = existingPlates.id === body.id && existingPlates.licensePlate === body.licensePlate; + + /** Implement the check */ if (isExactMatch) { const error = new Error("License plate already exists."); error.response = { statusCode: 400 }; @@ -44,6 +46,8 @@ exports.modifyPlate = function(body) { /** Allow updating the license plate for an existing ID. Returns response code 200 **/ const isValidUpdate = existingPlates.id === body.id && existingPlates.licensePlate !== body.licensePlate; + + /** Implement the check */ if (isValidUpdate) { resolve(); return; @@ -51,6 +55,8 @@ exports.modifyPlate = function(body) { /** Handle non-existent ID (the plate does not exist in the system).Return error code 404. **/ const isNonExistent = existingPlates.id !== body.id; + + /** Implement the check */ if (isNonExistent) { const error = new Error("License plate doesn't exist."); error.response = { statusCode: 404 }; From 676aefeb5a05f698b8c70ed1bcbea386b602e748 Mon Sep 17 00:00:00 2001 From: gpittis Date: Fri, 20 Dec 2024 09:47:07 +0200 Subject: [PATCH 07/29] change comments in SpotOwnerService.js --- service/SpotOwnerService.js | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/service/SpotOwnerService.js b/service/SpotOwnerService.js index fce6d6f..01adca2 100644 --- a/service/SpotOwnerService.js +++ b/service/SpotOwnerService.js @@ -8,66 +8,61 @@ * body SpotOwner Spot owner object to add * no response value expected for this operation **/ - - exports.addSpotOwner = function(body) { return new Promise((resolve, reject) =>{ - //SpotOwner = Ιδιοκτήτης θέσεων πάρκινγκ + + //Ένας ήδη καταχωρημένος ιδιοκτήτης θέσεων έχει id, ταυτότητα, ονοματεπώνυμο, email, τηλέφωνο, λίστα με δικές του θέσεις. var existingSpotOwners = { - "id":1, // το id του ιδιοκτήτη θέσεων - "idNumber": "AK1234", // ο αριθμός ταυτότητας του ιδιοκτήτη θέσεων - "name":"John Doe", // ο ονοματεπώνυμο του ιδιοκτήτη - "email":"johhdoe@gmail.com", // το email του ιδιοκτήτη - "phone":"1234567890",// το τηλέφωνο του ιδιοκτήτη - "spots":[] // οι θέσεις του ιδιοκτήτη + "id":1, + "idNumber": "AK1234", + "name":"John Doe", + "email":"johhdoe@gmail.com", + "phone":"1234567890", + "spots":[] }; - + //αν το id έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 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; } + //αν το idNumber έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 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; } + //αν το name έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 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; } + //αν το email έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 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; } + //αν το phone δεν υπάρχει στο request body τότε έχω σφάλμα με κωδικό σφάλματος 400 if (!body.phone) { - - //αν το phone δεν υπάρχει στο request body τότε έχω σφάλμα με κωδικό σφάλματος 400 const error = new Error("No phone."); error.response = { statusCode: 400 }; reject(error); return; } + //αν το spots δεν υπάρχει στο request body τότε έχω σφάλμα με κωδικό σφάλματος 400 if (!body.spots) { - - //αν το spots δεν υπάρχει στο request body τότε έχω σφάλμα με κωδικό σφάλματος 400 const error = new Error("No spots."); error.response = { statusCode: 400 }; reject(error); From 1df6ef7231c96aadfbad4ac81d8bcb7b693e13cf Mon Sep 17 00:00:00 2001 From: gpittis Date: Fri, 20 Dec 2024 09:55:17 +0200 Subject: [PATCH 08/29] add comments --- service/SpotOwnerService.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/service/SpotOwnerService.js b/service/SpotOwnerService.js index 01adca2..dd64d4b 100644 --- a/service/SpotOwnerService.js +++ b/service/SpotOwnerService.js @@ -4,10 +4,19 @@ /** * 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 - **/ + * @param {Object} body - Spot owner object to add. + * @property {number} body.id - Unique identifier for the spot owner. Must be a positive integer. + * @property {string} body.idNumber - Identification number for the spot owner. Must be a non-empty string. + * @property {string} body.name - Full name of the spot owner. Must be a non-empty string. + * @property {string} body.email - Email address of the spot owner. Must be a valid non-empty string. + * @property {string} body.phone - Contact number of the spot owner. Must be present. + * @property {Array} body.spots - List of spots owned by the user. Must be present. + * + * @returns {Promise} Resolves if addition is successful, rejects with an error otherwise. + */ exports.addSpotOwner = function(body) { return new Promise((resolve, reject) =>{ From b537ffd91621f27e48f401c5fcb11f5964450ff1 Mon Sep 17 00:00:00 2001 From: gpittis Date: Fri, 20 Dec 2024 10:06:20 +0200 Subject: [PATCH 09/29] change comments position --- service/SpotOwnerService.js | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/service/SpotOwnerService.js b/service/SpotOwnerService.js index dd64d4b..29fe48e 100644 --- a/service/SpotOwnerService.js +++ b/service/SpotOwnerService.js @@ -4,23 +4,14 @@ /** * 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 - * @param {Object} body - Spot owner object to add. - * @property {number} body.id - Unique identifier for the spot owner. Must be a positive integer. - * @property {string} body.idNumber - Identification number for the spot owner. Must be a non-empty string. - * @property {string} body.name - Full name of the spot owner. Must be a non-empty string. - * @property {string} body.email - Email address of the spot owner. Must be a valid non-empty string. - * @property {string} body.phone - Contact number of the spot owner. Must be present. - * @property {Array} body.spots - List of spots owned by the user. Must be present. - * - * @returns {Promise} Resolves if addition is successful, rejects with an error otherwise. - */ + **/ exports.addSpotOwner = function(body) { return new Promise((resolve, reject) =>{ - //Ένας ήδη καταχωρημένος ιδιοκτήτης θέσεων έχει id, ταυτότητα, ονοματεπώνυμο, email, τηλέφωνο, λίστα με δικές του θέσεις. + //Ένας ήδη καταχωρημένος ιδιοκτήτης θέσεων με id, ταυτότητα, ονοματεπώνυμο, email, τηλέφωνο και λίστα με δικές του θέσεις. var existingSpotOwners = { "id":1, "idNumber": "AK1234", @@ -30,48 +21,48 @@ exports.addSpotOwner = function(body) { "spots":[] }; - //αν το id έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 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; } - //αν το idNumber έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 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; } - //αν το name έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 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; } - //αν το email έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 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; } - //αν το phone δεν υπάρχει στο request body τότε έχω σφάλμα με κωδικό σφάλματος 400 if (!body.phone) { + //αν το phone δεν υπάρχει στο request body τότε έχω σφάλμα με κωδικό σφάλματος 400 const error = new Error("No phone."); error.response = { statusCode: 400 }; reject(error); return; } - //αν το spots δεν υπάρχει στο request body τότε έχω σφάλμα με κωδικό σφάλματος 400 if (!body.spots) { + //αν το spots δεν υπάρχει στο request body τότε έχω σφάλμα με κωδικό σφάλματος 400 const error = new Error("No spots."); error.response = { statusCode: 400 }; reject(error); From e0c6c7652e9704e69f097e22abf2c00da57ff33a Mon Sep 17 00:00:00 2001 From: gpittis Date: Fri, 20 Dec 2024 10:09:24 +0200 Subject: [PATCH 10/29] remove unused dummy data --- service/SpotOwnerService.js | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/service/SpotOwnerService.js b/service/SpotOwnerService.js index 29fe48e..2ce9cbb 100644 --- a/service/SpotOwnerService.js +++ b/service/SpotOwnerService.js @@ -10,16 +10,6 @@ **/ exports.addSpotOwner = function(body) { return new Promise((resolve, reject) =>{ - - //Ένας ήδη καταχωρημένος ιδιοκτήτης θέσεων με id, ταυτότητα, ονοματεπώνυμο, email, τηλέφωνο και λίστα με δικές του θέσεις. - var existingSpotOwners = { - "id":1, - "idNumber": "AK1234", - "name":"John Doe", - "email":"johhdoe@gmail.com", - "phone":"1234567890", - "spots":[] - }; if (!body.id || body.id < 0) { //αν το id έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 From ccc9ab6b3e8bf18e74dbc55fe4ad13ab45565669 Mon Sep 17 00:00:00 2001 From: gpittis Date: Fri, 20 Dec 2024 10:13:02 +0200 Subject: [PATCH 11/29] remove the unused checks --- service/SpotService.js | 35 +---------------------------------- 1 file changed, 1 insertion(+), 34 deletions(-) diff --git a/service/SpotService.js b/service/SpotService.js index e7f24ac..f8c85cd 100644 --- a/service/SpotService.js +++ b/service/SpotService.js @@ -119,11 +119,6 @@ exports.getSpots = function () { * id Integer * no response value expected for this operation **/ -// exports.modifySpot = function (body, address, type, charger, id) { -// return new Promise(function (resolve, reject) { -// resolve(); -// }); -// } exports.modifySpot = function (body, address, type, charger, id) { return new Promise((resolve, reject) => { // Οι έλεγχοι που έγιναν comment εκτελούνται από το swagger, // επομένως είναι περιττό να τους γράψω μέσα στο modifySpot διότι μου χαλάνε το coverage του SpotService.js @@ -136,17 +131,6 @@ exports.modifySpot = function (body, address, type, charger, id) { charger: false, }; - // Έλεγχος query παραμέτρων - - // // Το address πρέπει να είναι string - // if (!address) { - // //αν το address έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 - // const error = new Error("Invalid address in query: must be a string."); - // error.response = { statusCode: 400 }; - // reject(error); - // return; - // } - //Το type πρέπει να ισούται με "Garage" ή "Open" ή "Underground" if (!["Garage", "Open", "Underground"].includes(type)) { //αν το type έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 @@ -156,15 +140,6 @@ exports.modifySpot = function (body, address, type, charger, id) { return; } - // //Το charger πρέπει να είναι boolean - // if (typeof charger !== 'boolean') { - // //αν το charger έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 - // const error = new Error("Invalid charger in query: must be a boolean."); - // error.response = { statusCode: 400 }; - // reject(error); - // return; - // } - //Το id πρέπει να είναι μη αρνητικός ακέραιος αριθμός if (!id || id < 0) { //αν το id έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 @@ -173,15 +148,7 @@ exports.modifySpot = function (body, address, type, charger, id) { reject(error); return; } - - // // Έλεγχος request body - // if (!body || typeof body !== 'object') { - // const error = new Error("Invalid request body."); - // error.response = { statusCode: 400 }; - // reject(error); - // return; - // } - + // Σύγκριση query παραμέτρων με request body attributes. // Τα αντίστοιχα query attributes με τα αντίστοιχα attributes του request body πρέπει να ταυτίζονται. if (body.address !== address) { From f9466c6680732e7b2020b91fb871f77653ad824a Mon Sep 17 00:00:00 2001 From: gpittis Date: Fri, 20 Dec 2024 10:20:52 +0200 Subject: [PATCH 12/29] change comments --- service/SpotService.js | 41 +++++++++++++---------------------------- 1 file changed, 13 insertions(+), 28 deletions(-) diff --git a/service/SpotService.js b/service/SpotService.js index f8c85cd..f638370 100644 --- a/service/SpotService.js +++ b/service/SpotService.js @@ -19,36 +19,32 @@ exports.createSpot = function (body) { "chargerAvailability": false }; - // Το address πρέπει να είναι string + // Το address πρέπει να είναι string. Αν το address έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 if (!body.address) { - //αν το address έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 const error = new Error("Invalid address: must be a string."); error.response = { statusCode: 400 }; reject(error); return; } - //Το id πρέπει να είναι μη αρνητικός ακέραιος αριθμός + //Το id πρέπει να είναι μη αρνητικός ακέραιος αριθμός. Αν έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 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; } - //Το type πρέπει να ισούται με "Garage" ή "Open" ή "Underground" + //Το type πρέπει να ισούται με "Garage" ή "Open" ή "Underground". Διαφορετικά, έχω σφάλμα με κωδικό σφάλματος 400 if (!["Garage", "Open", "Underground"].includes(body.type)) { - //αν το type έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 const error = new Error("Invalid type: must be one of 'Garage', 'Open', or 'Underground'."); error.response = { statusCode: 400 }; reject(error); return; } - //Το chargerAvailability πρέπει να είναι boolean + //Το chargerAvailability πρέπει να είναι boolean. Διαφορετικά, έχω σφάλμα με κωδικό σφάλματος 400 if (typeof body.chargerAvailability !== 'boolean') { - //αν το chargerAvailability έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 const error = new Error("Invalid chargerAvailability: must be a boolean."); error.response = { statusCode: 400 }; reject(error); @@ -62,18 +58,15 @@ exports.createSpot = function (body) { existingSpots.address === body.address && existingSpots.type === body.type && existingSpots.chargerAvailability === body.chargerAvailability; - + + //Αν βρεθεί διπλότυπη θέση τότε έχω σφάλμα με κωδικό σφάλματος 400 if (spotExists) { - //Αν βρεθεί διπλότυπη θέση τότε έχω σφάλμα με κωδικό σφάλματος 400 const error = new Error("Spot already exists: a spot with the same attributes already exists."); error.response = { statusCode: 400 }; reject(error); return; } - // Η θέση έχει περάσει όλους τους ελέγχους και μπορώ να την προσθέσω - // Δεν το κάνω, καθώς η συνάρτηση είναι dummy - resolve(); }); }; @@ -120,9 +113,7 @@ exports.getSpots = function () { * no response value expected for this operation **/ exports.modifySpot = function (body, address, type, charger, id) { - return new Promise((resolve, reject) => { // Οι έλεγχοι που έγιναν comment εκτελούνται από το swagger, - // επομένως είναι περιττό να τους γράψω μέσα στο modifySpot διότι μου χαλάνε το coverage του SpotService.js - + return new Promise((resolve, reject) => { // Τα δεδομένα των ήδη καταχωρημένων θέσεων μέσα στο σύστημα var existingSpots = { address: "Navarinou 18", @@ -131,24 +122,22 @@ exports.modifySpot = function (body, address, type, charger, id) { charger: false, }; - //Το type πρέπει να ισούται με "Garage" ή "Open" ή "Underground" + //Το type πρέπει να ισούται με "Garage" ή "Open" ή "Underground". Διαφορετικά, έχω σφάλμα με κωδικό σφάλματος 400 if (!["Garage", "Open", "Underground"].includes(type)) { - //αν το type έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 const error = new Error("Invalid type in query: must be one of 'Garage', 'Open', or 'Underground'."); error.response = { statusCode: 400 }; reject(error); return; } - //Το id πρέπει να είναι μη αρνητικός ακέραιος αριθμός + //Το id πρέπει να είναι μη αρνητικός ακέραιος αριθμός. Διαφορετικά, έχω σφάλμα με κωδικό σφάλματος 400 if (!id || id < 0) { - //αν το id έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 const error = new Error("Invalid id in query: must be a positive integer."); error.response = { statusCode: 400 }; reject(error); return; } - + // Σύγκριση query παραμέτρων με request body attributes. // Τα αντίστοιχα query attributes με τα αντίστοιχα attributes του request body πρέπει να ταυτίζονται. if (body.address !== address) { @@ -181,15 +170,14 @@ exports.modifySpot = function (body, address, type, charger, id) { // Έλεγχος αν τα τροποποιημένα στοιχεία της θέσης ταυτίζονται με τα "παλιά" στοιχεία της θέσης. //Ουσιαστικά, δεν γίνεται τροποποίηση. - const spotExists = //Δεν ελέγχω τα id του existingSpots και του body διότι θεωρώ εκ των πραγμάτων ότι ισούται μεταξύ τους. // Αυτό συμβαίνει διότι κάνω modify μια ήδη υπάρχουσα θέση , άρα το id δεν αλλάζει κατά το modification. // Αν άλλαζε το id τότε θα έκανα create μια νέα θέση. // Αντίστοιχος έλεγχος έγινε και στο PUT /licensePlate. //Επίσης αυτός ο έλεγχος αφορά και το PUT /reservation , αλλά δεν γράφτηκε για λόγους συντομίας. - existingSpots.address === body.address && - existingSpots.type === body.type && - existingSpots.charger === body.charger; + const spotExists = existingSpots.address === body.address && + existingSpots.type === body.type && + existingSpots.charger === body.charger; if (spotExists) { const error = new Error("Spot already exists: a spot with the same attributes already exists."); @@ -198,9 +186,6 @@ exports.modifySpot = function (body, address, type, charger, id) { return; } - // Η θέση έχει περάσει όλους τους ελέγχους και μπορώ να την τροποποιήσω - // Δεν το κάνω, καθώς η συνάρτηση είναι dummy - resolve(); }); }; From e7a2378954982442d7bc57f3d313cea7b637a6c9 Mon Sep 17 00:00:00 2001 From: gpittis Date: Fri, 20 Dec 2024 10:23:28 +0200 Subject: [PATCH 13/29] add spaces --- service/SpotService.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/service/SpotService.js b/service/SpotService.js index f638370..8ae8298 100644 --- a/service/SpotService.js +++ b/service/SpotService.js @@ -21,6 +21,7 @@ exports.createSpot = function (body) { // Το address πρέπει να είναι string. Αν το address έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 if (!body.address) { + const error = new Error("Invalid address: must be a string."); error.response = { statusCode: 400 }; reject(error); @@ -29,6 +30,7 @@ exports.createSpot = function (body) { //Το id πρέπει να είναι μη αρνητικός ακέραιος αριθμός. Αν έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 if (!body.id || body.id < 0) { + const error = new Error("Invalid id: must be a positive integer."); error.response = { statusCode: 400 }; reject(error); @@ -37,6 +39,7 @@ exports.createSpot = function (body) { //Το type πρέπει να ισούται με "Garage" ή "Open" ή "Underground". Διαφορετικά, έχω σφάλμα με κωδικό σφάλματος 400 if (!["Garage", "Open", "Underground"].includes(body.type)) { + const error = new Error("Invalid type: must be one of 'Garage', 'Open', or 'Underground'."); error.response = { statusCode: 400 }; reject(error); @@ -45,6 +48,7 @@ exports.createSpot = function (body) { //Το chargerAvailability πρέπει να είναι boolean. Διαφορετικά, έχω σφάλμα με κωδικό σφάλματος 400 if (typeof body.chargerAvailability !== 'boolean') { + const error = new Error("Invalid chargerAvailability: must be a boolean."); error.response = { statusCode: 400 }; reject(error); @@ -61,6 +65,7 @@ exports.createSpot = function (body) { //Αν βρεθεί διπλότυπη θέση τότε έχω σφάλμα με κωδικό σφάλματος 400 if (spotExists) { + const error = new Error("Spot already exists: a spot with the same attributes already exists."); error.response = { statusCode: 400 }; reject(error); @@ -124,6 +129,7 @@ exports.modifySpot = function (body, address, type, charger, id) { //Το type πρέπει να ισούται με "Garage" ή "Open" ή "Underground". Διαφορετικά, έχω σφάλμα με κωδικό σφάλματος 400 if (!["Garage", "Open", "Underground"].includes(type)) { + const error = new Error("Invalid type in query: must be one of 'Garage', 'Open', or 'Underground'."); error.response = { statusCode: 400 }; reject(error); @@ -132,6 +138,7 @@ exports.modifySpot = function (body, address, type, charger, id) { //Το id πρέπει να είναι μη αρνητικός ακέραιος αριθμός. Διαφορετικά, έχω σφάλμα με κωδικό σφάλματος 400 if (!id || id < 0) { + const error = new Error("Invalid id in query: must be a positive integer."); error.response = { statusCode: 400 }; reject(error); @@ -141,6 +148,7 @@ exports.modifySpot = function (body, address, type, charger, id) { // Σύγκριση query παραμέτρων με request body attributes. // Τα αντίστοιχα query attributes με τα αντίστοιχα attributes του request body πρέπει να ταυτίζονται. if (body.address !== address) { + const error = new Error("Address mismatch between query and body."); error.response = { statusCode: 400 }; reject(error); @@ -148,6 +156,7 @@ exports.modifySpot = function (body, address, type, charger, id) { } if (body.type !== type) { + const error = new Error("Type mismatch between query and body."); error.response = { statusCode: 400 }; reject(error); @@ -155,6 +164,7 @@ exports.modifySpot = function (body, address, type, charger, id) { } if (body.charger !== charger) { + const error = new Error("Charger mismatch between query and body."); error.response = { statusCode: 400 }; reject(error); @@ -162,6 +172,7 @@ exports.modifySpot = function (body, address, type, charger, id) { } if (body.id !== id) { + const error = new Error("ID mismatch between query and body."); error.response = { statusCode: 400 }; reject(error); @@ -180,6 +191,7 @@ exports.modifySpot = function (body, address, type, charger, id) { existingSpots.charger === body.charger; if (spotExists) { + const error = new Error("Spot already exists: a spot with the same attributes already exists."); error.response = { statusCode: 400 }; reject(error); From 9297c01fc75f09da31f255a613c766de97060951 Mon Sep 17 00:00:00 2001 From: gpittis Date: Fri, 20 Dec 2024 10:27:37 +0200 Subject: [PATCH 14/29] change comments --- service/SpotService.js | 23 +---------------------- 1 file changed, 1 insertion(+), 22 deletions(-) diff --git a/service/SpotService.js b/service/SpotService.js index 8ae8298..e49cd41 100644 --- a/service/SpotService.js +++ b/service/SpotService.js @@ -21,7 +21,6 @@ exports.createSpot = function (body) { // Το address πρέπει να είναι string. Αν το address έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 if (!body.address) { - const error = new Error("Invalid address: must be a string."); error.response = { statusCode: 400 }; reject(error); @@ -30,7 +29,6 @@ exports.createSpot = function (body) { //Το id πρέπει να είναι μη αρνητικός ακέραιος αριθμός. Αν έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 if (!body.id || body.id < 0) { - const error = new Error("Invalid id: must be a positive integer."); error.response = { statusCode: 400 }; reject(error); @@ -39,7 +37,6 @@ exports.createSpot = function (body) { //Το type πρέπει να ισούται με "Garage" ή "Open" ή "Underground". Διαφορετικά, έχω σφάλμα με κωδικό σφάλματος 400 if (!["Garage", "Open", "Underground"].includes(body.type)) { - const error = new Error("Invalid type: must be one of 'Garage', 'Open', or 'Underground'."); error.response = { statusCode: 400 }; reject(error); @@ -48,7 +45,6 @@ exports.createSpot = function (body) { //Το chargerAvailability πρέπει να είναι boolean. Διαφορετικά, έχω σφάλμα με κωδικό σφάλματος 400 if (typeof body.chargerAvailability !== 'boolean') { - const error = new Error("Invalid chargerAvailability: must be a boolean."); error.response = { statusCode: 400 }; reject(error); @@ -56,8 +52,6 @@ exports.createSpot = function (body) { } // Έλεγχος αν υπάρχει ήδη το spot , δηλαδή γίνεται έλεγχος για διπλότυπη θέση - // Αν όλα τα attributes του existingSpots ισούται ένα προς ένα με όλα τα attributes του spot που θέλω να φτιάξω, - // τότε θέλω να δημιουργήσω μια διπλότυπη θέση. Άρα, έχω σφάλμα const spotExists = existingSpots.id === body.id && existingSpots.address === body.address && existingSpots.type === body.type && @@ -65,7 +59,6 @@ exports.createSpot = function (body) { //Αν βρεθεί διπλότυπη θέση τότε έχω σφάλμα με κωδικό σφάλματος 400 if (spotExists) { - const error = new Error("Spot already exists: a spot with the same attributes already exists."); error.response = { statusCode: 400 }; reject(error); @@ -129,7 +122,6 @@ exports.modifySpot = function (body, address, type, charger, id) { //Το type πρέπει να ισούται με "Garage" ή "Open" ή "Underground". Διαφορετικά, έχω σφάλμα με κωδικό σφάλματος 400 if (!["Garage", "Open", "Underground"].includes(type)) { - const error = new Error("Invalid type in query: must be one of 'Garage', 'Open', or 'Underground'."); error.response = { statusCode: 400 }; reject(error); @@ -138,17 +130,14 @@ exports.modifySpot = function (body, address, type, charger, id) { //Το id πρέπει να είναι μη αρνητικός ακέραιος αριθμός. Διαφορετικά, έχω σφάλμα με κωδικό σφάλματος 400 if (!id || id < 0) { - const error = new Error("Invalid id in query: must be a positive integer."); error.response = { statusCode: 400 }; reject(error); return; } - // Σύγκριση query παραμέτρων με request body attributes. - // Τα αντίστοιχα query attributes με τα αντίστοιχα attributes του request body πρέπει να ταυτίζονται. + // Σύγκριση query παραμέτρων με request body attributes. if (body.address !== address) { - const error = new Error("Address mismatch between query and body."); error.response = { statusCode: 400 }; reject(error); @@ -156,7 +145,6 @@ exports.modifySpot = function (body, address, type, charger, id) { } if (body.type !== type) { - const error = new Error("Type mismatch between query and body."); error.response = { statusCode: 400 }; reject(error); @@ -164,7 +152,6 @@ exports.modifySpot = function (body, address, type, charger, id) { } if (body.charger !== charger) { - const error = new Error("Charger mismatch between query and body."); error.response = { statusCode: 400 }; reject(error); @@ -172,7 +159,6 @@ exports.modifySpot = function (body, address, type, charger, id) { } if (body.id !== id) { - const error = new Error("ID mismatch between query and body."); error.response = { statusCode: 400 }; reject(error); @@ -180,18 +166,11 @@ exports.modifySpot = function (body, address, type, charger, id) { } // Έλεγχος αν τα τροποποιημένα στοιχεία της θέσης ταυτίζονται με τα "παλιά" στοιχεία της θέσης. - //Ουσιαστικά, δεν γίνεται τροποποίηση. - //Δεν ελέγχω τα id του existingSpots και του body διότι θεωρώ εκ των πραγμάτων ότι ισούται μεταξύ τους. - // Αυτό συμβαίνει διότι κάνω modify μια ήδη υπάρχουσα θέση , άρα το id δεν αλλάζει κατά το modification. - // Αν άλλαζε το id τότε θα έκανα create μια νέα θέση. - // Αντίστοιχος έλεγχος έγινε και στο PUT /licensePlate. - //Επίσης αυτός ο έλεγχος αφορά και το PUT /reservation , αλλά δεν γράφτηκε για λόγους συντομίας. const spotExists = existingSpots.address === body.address && existingSpots.type === body.type && existingSpots.charger === body.charger; if (spotExists) { - const error = new Error("Spot already exists: a spot with the same attributes already exists."); error.response = { statusCode: 400 }; reject(error); From c95dc5430fbe7f365701d2a77ef98cd0a689137e Mon Sep 17 00:00:00 2001 From: gpittis Date: Fri, 20 Dec 2024 10:40:03 +0200 Subject: [PATCH 15/29] change comments positions --- service/SpotService.js | 49 ++++++++++++++++-------------------------- 1 file changed, 19 insertions(+), 30 deletions(-) diff --git a/service/SpotService.js b/service/SpotService.js index e49cd41..498463f 100644 --- a/service/SpotService.js +++ b/service/SpotService.js @@ -11,54 +11,47 @@ exports.createSpot = function (body) { return new Promise((resolve, reject) => { - // Τα δεδομένα των ήδη καταχωρημένων θέσεων μέσα στο σύστημα - var existingSpots = { + var existingSpots = { // Τα δεδομένα των ήδη καταχωρημένων θέσεων μέσα στο σύστημα "address": "Navarinou 18", "id": 15, "type": "Garage", "chargerAvailability": false }; - // Το address πρέπει να είναι string. Αν το address έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 - if (!body.address) { + if (!body.address) { // Το address πρέπει να είναι string. Διαφορετικά έχω σφάλμα με κωδικό σφάλματος 400 const error = new Error("Invalid address: must be a string."); error.response = { statusCode: 400 }; reject(error); return; } - //Το id πρέπει να είναι μη αρνητικός ακέραιος αριθμός. Αν έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 - if (!body.id || body.id < 0) { + 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; } - //Το type πρέπει να ισούται με "Garage" ή "Open" ή "Underground". Διαφορετικά, έχω σφάλμα με κωδικό σφάλματος 400 - if (!["Garage", "Open", "Underground"].includes(body.type)) { + if (!["Garage", "Open", "Underground"].includes(body.type)) {//Το type πρέπει να ισούται με "Garage" ή "Open" ή "Underground" const error = new Error("Invalid type: must be one of 'Garage', 'Open', or 'Underground'."); error.response = { statusCode: 400 }; reject(error); return; } - //Το chargerAvailability πρέπει να είναι boolean. Διαφορετικά, έχω σφάλμα με κωδικό σφάλματος 400 - if (typeof body.chargerAvailability !== 'boolean') { + if (typeof body.chargerAvailability !== 'boolean') { //Το chargerAvailability πρέπει να είναι boolean. const error = new Error("Invalid chargerAvailability: must be a boolean."); error.response = { statusCode: 400 }; reject(error); return; } - // Έλεγχος αν υπάρχει ήδη το spot , δηλαδή γίνεται έλεγχος για διπλότυπη θέση - const spotExists = existingSpots.id === body.id && + const spotExists = existingSpots.id === body.id && // Έλεγχος αν υπάρχει ήδη το spot. (Έλεγχος για διπλότυπη θέση) existingSpots.address === body.address && existingSpots.type === body.type && existingSpots.chargerAvailability === body.chargerAvailability; - - //Αν βρεθεί διπλότυπη θέση τότε έχω σφάλμα με κωδικό σφάλματος 400 - if (spotExists) { + + if (spotExists) { //Αν βρεθεί διπλότυπη θέση τότε έχω σφάλμα με κωδικό σφάλματος 400 const error = new Error("Spot already exists: a spot with the same attributes already exists."); error.response = { statusCode: 400 }; reject(error); @@ -112,65 +105,61 @@ exports.getSpots = function () { **/ exports.modifySpot = function (body, address, type, charger, id) { return new Promise((resolve, reject) => { - // Τα δεδομένα των ήδη καταχωρημένων θέσεων μέσα στο σύστημα - var existingSpots = { + + var existingSpots = { // Τα δεδομένα των ήδη καταχωρημένων θέσεων μέσα στο σύστημα address: "Navarinou 18", id: 15, type: "Garage", charger: false, }; - //Το type πρέπει να ισούται με "Garage" ή "Open" ή "Underground". Διαφορετικά, έχω σφάλμα με κωδικό σφάλματος 400 - if (!["Garage", "Open", "Underground"].includes(type)) { + if (!["Garage", "Open", "Underground"].includes(type)) {//Το type πρέπει να ισούται με "Garage" ή "Open" ή "Underground" const error = new Error("Invalid type in query: must be one of 'Garage', 'Open', or 'Underground'."); error.response = { statusCode: 400 }; reject(error); return; } - //Το id πρέπει να είναι μη αρνητικός ακέραιος αριθμός. Διαφορετικά, έχω σφάλμα με κωδικό σφάλματος 400 - if (!id || id < 0) { + if (!id || id < 0) { //Το id πρέπει να είναι μη αρνητικός ακέραιος αριθμός const error = new Error("Invalid id in query: must be a positive integer."); error.response = { statusCode: 400 }; reject(error); return; } - // Σύγκριση query παραμέτρων με request body attributes. - if (body.address !== address) { + if (body.address !== address) { // Σύγκριση query παραμέτρου με το αντίστοιχο request body attribute. const error = new Error("Address mismatch between query and body."); error.response = { statusCode: 400 }; reject(error); return; } - if (body.type !== type) { + if (body.type !== type) { // Σύγκριση query παραμέτρου με το αντίστοιχο request body attribute. const error = new Error("Type mismatch between query and body."); error.response = { statusCode: 400 }; reject(error); return; } - if (body.charger !== charger) { + if (body.charger !== charger) { // Σύγκριση query παραμέτρου με το αντίστοιχο request body attribute. const error = new Error("Charger mismatch between query and body."); error.response = { statusCode: 400 }; reject(error); return; } - if (body.id !== id) { + if (body.id !== id) { // Σύγκριση query παραμέτρου με το αντίστοιχο request body attribute. const error = new Error("ID mismatch between query and body."); error.response = { statusCode: 400 }; reject(error); return; } - // Έλεγχος αν τα τροποποιημένα στοιχεία της θέσης ταυτίζονται με τα "παλιά" στοιχεία της θέσης. - const spotExists = existingSpots.address === body.address && - existingSpots.type === body.type && + const spotExists = existingSpots.address === body.address && + existingSpots.type === body.type && existingSpots.charger === body.charger; - if (spotExists) { + if (spotExists) { //Έλεγχος αν τα τροποποιημένα στοιχεία της θέσης ταυτίζονται με τα "παλιά" της στοιχεία const error = new Error("Spot already exists: a spot with the same attributes already exists."); error.response = { statusCode: 400 }; reject(error); From 2b6bc25018f6ceff85648e392b129933fa4ceae3 Mon Sep 17 00:00:00 2001 From: gpittis Date: Fri, 20 Dec 2024 10:43:05 +0200 Subject: [PATCH 16/29] add spaces --- service/SpotService.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/service/SpotService.js b/service/SpotService.js index 498463f..cedbd8c 100644 --- a/service/SpotService.js +++ b/service/SpotService.js @@ -71,7 +71,9 @@ exports.createSpot = function (body) { **/ exports.getSpots = function () { return new Promise(function (resolve, reject) { + var examples = {}; + examples['application/json'] = [{ "address": "address", "id": 0, @@ -83,9 +85,11 @@ exports.getSpots = function () { "type": "type", "chargerAvailability": true }]; + if (Object.keys(examples).length > 0) { resolve(examples[Object.keys(examples)[0]]); } else { + resolve(); } }); @@ -105,7 +109,7 @@ exports.getSpots = function () { **/ exports.modifySpot = function (body, address, type, charger, id) { return new Promise((resolve, reject) => { - + var existingSpots = { // Τα δεδομένα των ήδη καταχωρημένων θέσεων μέσα στο σύστημα address: "Navarinou 18", id: 15, @@ -180,6 +184,7 @@ exports.modifySpot = function (body, address, type, charger, id) { **/ exports.removeSpot = function (id) { return new Promise(function (resolve, reject) { + resolve(); }); } @@ -196,7 +201,9 @@ exports.removeSpot = function (id) { **/ exports.searchSpot = function(address,type,charger) { return new Promise(function(resolve, /*reject*/) { // remove unused reject + var examples = {}; + examples['application/json'] = [ { "address" : "address", "id" : 0, @@ -208,14 +215,18 @@ exports.searchSpot = function(address,type,charger) { "type" : "type", "chargerAvailability" : true } ]; + if (address === "address" || type === "type" || charger === true) { resolve(examples[Object.keys(examples)[0]]); } + else { + resolve({ status: 404, message: "No matching parking spots found.", }) + } }); } \ No newline at end of file From 0c1b9884cd85df6606752d26d6af5dda7d5eee10 Mon Sep 17 00:00:00 2001 From: gpittis Date: Fri, 20 Dec 2024 10:56:37 +0200 Subject: [PATCH 17/29] change comments --- service/SpotOwnerService.js | 48 ++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/service/SpotOwnerService.js b/service/SpotOwnerService.js index 2ce9cbb..a588e17 100644 --- a/service/SpotOwnerService.js +++ b/service/SpotOwnerService.js @@ -9,54 +9,48 @@ * no response value expected for this operation **/ exports.addSpotOwner = function(body) { - return new Promise((resolve, reject) =>{ + 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; + const error = new Error("Invalid id: must be a positive integer."); // id must be non negative integer + error.response = { statusCode: 400 }; // if id is invalid then returns 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 }; + const error = new Error("Invalid idNumber: must be a string."); // id number must be string + error.response = { statusCode: 400 }; // if idNumber is invalid then returns 400 reject(error); - return; + return; } - if (!body.name || typeof body.name !== "string") { - //αν το name έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 - const error = new Error("Invalid name: must be a string."); - error.response = { statusCode: 400 }; + if (!body.name || typeof body.name !== "string") { + const error = new Error("Invalid name: must be a string."); // name must be string + error.response = { statusCode: 400 }; // if name is invalid then returns 400 reject(error); - return; + return; } if (!body.email || typeof body.email !== "string") { - //αν το email έχει μη έγκυρη τιμή τότε έχω σφάλμα με κωδικό σφάλματος 400 - const error = new Error("Invalid email: must be a string."); - error.response = { statusCode: 400 }; + const error = new Error("Invalid email: must be a string."); // email must be string + error.response = { statusCode: 400 }; // if email is invalid then returns 400 reject(error); - return; + return; } if (!body.phone) { - //αν το phone δεν υπάρχει στο request body τότε έχω σφάλμα με κωδικό σφάλματος 400 - const error = new Error("No phone."); - error.response = { statusCode: 400 }; + const error = new Error("No phone."); // phone must exists in SpotOwner's data + error.response = { statusCode: 400 }; // if phone is non - existent then returns 400 reject(error); - return; + return; } if (!body.spots) { - //αν το spots δεν υπάρχει στο request body τότε έχω σφάλμα με κωδικό σφάλματος 400 - const error = new Error("No spots."); - error.response = { statusCode: 400 }; + const error = new Error("No spots."); // Spots must exists in SpotOwner's data + error.response = { statusCode: 400 }; // if the spot's list is non - existent then returns 400 reject(error); - return; + return; } resolve(); From b1576c7b79cf0d0fd7a6113de2df90f3138a181e Mon Sep 17 00:00:00 2001 From: gpittis Date: Fri, 20 Dec 2024 11:05:01 +0200 Subject: [PATCH 18/29] add comments --- service/SpotOwnerService.js | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/service/SpotOwnerService.js b/service/SpotOwnerService.js index a588e17..2464382 100644 --- a/service/SpotOwnerService.js +++ b/service/SpotOwnerService.js @@ -14,46 +14,45 @@ exports.addSpotOwner = function(body) { if (!body.id || body.id < 0) { const error = new Error("Invalid id: must be a positive integer."); // id must be non negative integer error.response = { statusCode: 400 }; // if id is invalid then returns 400 - reject(error); + reject(error); // reject the error return; - } + }; if (!body.idNumber || typeof body.idNumber !== "string") { const error = new Error("Invalid idNumber: must be a string."); // id number must be string error.response = { statusCode: 400 }; // if idNumber is invalid then returns 400 - reject(error); + reject(error); // reject the error return; - } + }; if (!body.name || typeof body.name !== "string") { const error = new Error("Invalid name: must be a string."); // name must be string error.response = { statusCode: 400 }; // if name is invalid then returns 400 - reject(error); + reject(error); // reject the error return; - } + }; if (!body.email || typeof body.email !== "string") { const error = new Error("Invalid email: must be a string."); // email must be string error.response = { statusCode: 400 }; // if email is invalid then returns 400 - reject(error); + reject(error); // reject the error return; - } + }; if (!body.phone) { const error = new Error("No phone."); // phone must exists in SpotOwner's data error.response = { statusCode: 400 }; // if phone is non - existent then returns 400 - reject(error); + reject(error); // reject the error return; - } + }; if (!body.spots) { const error = new Error("No spots."); // Spots must exists in SpotOwner's data error.response = { statusCode: 400 }; // if the spot's list is non - existent then returns 400 - reject(error); + reject(error); // reject the error return; - } + }; resolve(); }); - } \ No newline at end of file From 7da9ff9fe4191596d5db27f97362922d5d722c39 Mon Sep 17 00:00:00 2001 From: gpittis Date: Fri, 20 Dec 2024 11:34:48 +0200 Subject: [PATCH 19/29] change and add comments --- service/SpotService.js | 144 ++++++++++++++++++++--------------------- 1 file changed, 72 insertions(+), 72 deletions(-) diff --git a/service/SpotService.js b/service/SpotService.js index cedbd8c..88430be 100644 --- a/service/SpotService.js +++ b/service/SpotService.js @@ -12,53 +12,53 @@ exports.createSpot = function (body) { return new Promise((resolve, reject) => { var existingSpots = { // Τα δεδομένα των ήδη καταχωρημένων θέσεων μέσα στο σύστημα - "address": "Navarinou 18", - "id": 15, - "type": "Garage", - "chargerAvailability": false + "address": "Navarinou 18", // η διεύθυνση της θέσης + "id": 15, // το id της θέσης + "type": "Garage", // ο τύπος της θέσης + "chargerAvailability": false // η διαθεσιμότητα φορτιστή της θέσης }; - if (!body.address) { // Το address πρέπει να είναι string. Διαφορετικά έχω σφάλμα με κωδικό σφάλματος 400 - const error = new Error("Invalid address: must be a string."); - error.response = { statusCode: 400 }; + if (!body.address) { // Έλεγχος αν υπάρχει το address.Το address πρέπει να είναι string. + const error = new Error("Invalid address: must be a string."); // Το address πρέπει να υπάρχει και να είναι string. + error.response = { statusCode: 400 }; // Διαφορετικά έχω σφάλμα με κωδικό σφάλματος 400 reject(error); - return; + return; // επιστρέφει 400 } - if (!body.id || body.id < 0) { //Το id πρέπει να είναι μη αρνητικός ακέραιος αριθμός. Διαφορετικά, έχω κωδικό σφάλματος 400 - const error = new Error("Invalid id: must be a positive integer."); - error.response = { statusCode: 400 }; + if (!body.id || body.id < 0) { //Το id πρέπει να υπάρχει και να είναι θετικό + const error = new Error("Invalid id: must be a positive integer."); //Διαφορετικά, έχω σφάλμα + error.response = { statusCode: 400 }; // με κωδικό σφάλματος 400 reject(error); - return; + return; // Επιστρέφει 400 } if (!["Garage", "Open", "Underground"].includes(body.type)) {//Το type πρέπει να ισούται με "Garage" ή "Open" ή "Underground" - const error = new Error("Invalid type: must be one of 'Garage', 'Open', or 'Underground'."); - error.response = { statusCode: 400 }; + const error = new Error("Invalid type: must be one of 'Garage', 'Open', or 'Underground'."); // Αλλιώς έχω σφάλμα + error.response = { statusCode: 400 }; // με κωδικό σφάλματος 400 reject(error); - return; - } + return; // Επιστρέφει 400 + } if (typeof body.chargerAvailability !== 'boolean') { //Το chargerAvailability πρέπει να είναι boolean. - const error = new Error("Invalid chargerAvailability: must be a boolean."); - error.response = { statusCode: 400 }; + const error = new Error("Invalid chargerAvailability: must be a boolean."); // Αλλιώς έχω σφάλμα + error.response = { statusCode: 400 }; // Με κωδικό σφάλματος 400 reject(error); - return; + return; // Επιστρέφει 400 } - const spotExists = existingSpots.id === body.id && // Έλεγχος αν υπάρχει ήδη το spot. (Έλεγχος για διπλότυπη θέση) - existingSpots.address === body.address && - existingSpots.type === body.type && - existingSpots.chargerAvailability === body.chargerAvailability; + const spotExists = existingSpots.id === body.id && // Έλεγχος αν υπάρχει ήδη το spot. Ειδικότερα, ελέγχω την ισότητα των id + existingSpots.address === body.address && // τσεκάρω την ισότητα των διευθύνσεων + existingSpots.type === body.type && // τσεκάρω την ισότητα των τύπων + existingSpots.chargerAvailability === body.chargerAvailability; // και τέλος τσεκάρω τη διαθεσιμότητα φορτιστή - if (spotExists) { //Αν βρεθεί διπλότυπη θέση τότε έχω σφάλμα με κωδικό σφάλματος 400 - const error = new Error("Spot already exists: a spot with the same attributes already exists."); - error.response = { statusCode: 400 }; + if (spotExists) { //Αν βρεθεί διπλότυπη θέση + const error = new Error("Spot already exists: a spot with the same attributes already exists."); // έχω σφάλμα + error.response = { statusCode: 400 }; // με κωδικό 400 reject(error); - return; + return; // Επιστρέφει 400 } - resolve(); + resolve(); // Δημιουργεί τη θέση πάρκινγκ. }); }; @@ -70,11 +70,11 @@ exports.createSpot = function (body) { * returns List **/ exports.getSpots = function () { - return new Promise(function (resolve, reject) { + return new Promise(function (resolve, /**reject*/) { // reject is unused var examples = {}; - examples['application/json'] = [{ + examples['application/json'] = [{ // dummy δεδομένα θέσεων "address": "address", "id": 0, "type": "type", @@ -90,7 +90,7 @@ exports.getSpots = function () { resolve(examples[Object.keys(examples)[0]]); } else { - resolve(); + resolve(); // Eπιστρέφει όλες τις διαθέσιμες θέσεις μέσα στο σύστημα. } }); } @@ -111,66 +111,66 @@ exports.modifySpot = function (body, address, type, charger, id) { return new Promise((resolve, reject) => { var existingSpots = { // Τα δεδομένα των ήδη καταχωρημένων θέσεων μέσα στο σύστημα - address: "Navarinou 18", - id: 15, - type: "Garage", - charger: false, + address: "Navarinou 18", // η διεύθυνση της θέσης + id: 15, // το id της θέσης + type: "Garage", // ο τύπος της θέσης + charger: false, // η διαθεσιμότητα φορτιστή της θέσης }; if (!["Garage", "Open", "Underground"].includes(type)) {//Το type πρέπει να ισούται με "Garage" ή "Open" ή "Underground" - const error = new Error("Invalid type in query: must be one of 'Garage', 'Open', or 'Underground'."); - error.response = { statusCode: 400 }; + const error = new Error("Invalid type in query: must be one of 'Garage', 'Open', or 'Underground'."); // Αλλιώς σφάλμα + error.response = { statusCode: 400 }; // με κωδικό σφάλματος 400 reject(error); - return; + return; // Επιστρέφει 400 } - if (!id || id < 0) { //Το id πρέπει να είναι μη αρνητικός ακέραιος αριθμός - const error = new Error("Invalid id in query: must be a positive integer."); - error.response = { statusCode: 400 }; + if (!id || id < 0) { //Το id πρέπει να υπάρχει και να είναι μη αρνητικός ακέραιος αριθμός + const error = new Error("Invalid id in query: must be a positive integer."); // Αλλιώς έχω σφάλμα + error.response = { statusCode: 400 }; // με κωδικό 400 reject(error); - return; + return; // Επιστρέφει 400 } - if (body.address !== address) { // Σύγκριση query παραμέτρου με το αντίστοιχο request body attribute. - const error = new Error("Address mismatch between query and body."); - error.response = { statusCode: 400 }; - reject(error); - return; + if (body.address !== address) { // Σύγκριση του query address με το address του body. + const error = new Error("Address mismatch between query and body."); // Αν είναι διαφορετικά, τότε έχω σφάλμα + error.response = { statusCode: 400 }; // Με κωδικό 400 + reject(error); + return; // Επιστρέφει 400 } - if (body.type !== type) { // Σύγκριση query παραμέτρου με το αντίστοιχο request body attribute. - const error = new Error("Type mismatch between query and body."); - error.response = { statusCode: 400 }; + if (body.type !== type) { // Σύγκριση του query type με το type του request body. + const error = new Error("Type mismatch between query and body."); // Αν είναι διαφορετικά, τότε έχω σφάλμα + error.response = { statusCode: 400 }; // Με κωδικό 400 reject(error); - return; + return; // Επιστρέφει 400 } - if (body.charger !== charger) { // Σύγκριση query παραμέτρου με το αντίστοιχο request body attribute. - const error = new Error("Charger mismatch between query and body."); - error.response = { statusCode: 400 }; + if (body.charger !== charger) { // Σύγκριση του query charger με το charger του request body. + const error = new Error("Charger mismatch between query and body."); // Αν είναι διαφορετικά, τότε έχω σφάλμα + error.response = { statusCode: 400 }; // Με κωδικό 400 reject(error); - return; + return; // Επιστρέφει 400 } - if (body.id !== id) { // Σύγκριση query παραμέτρου με το αντίστοιχο request body attribute. - const error = new Error("ID mismatch between query and body."); - error.response = { statusCode: 400 }; + if (body.id !== id) { // Σύγκριση του query id με το id του request body. + const error = new Error("ID mismatch between query and body."); // Αν είναι διαφορετικά, τότε έχω σφάλμα + error.response = { statusCode: 400 }; // Με κωδικό 400 reject(error); - return; + return; // Επιστρέφει 400 } - const spotExists = existingSpots.address === body.address && - existingSpots.type === body.type && - existingSpots.charger === body.charger; + const spotExists = existingSpots.address === body.address && // Ελέγχω αν όντως τροποποίησα την θέση. Τσεκάρω διεύθυνση + existingSpots.type === body.type && // Τσεκάρω τύπο + existingSpots.charger === body.charger; // Τσεκάρω διαθεσιμότητα φορτιστή if (spotExists) { //Έλεγχος αν τα τροποποιημένα στοιχεία της θέσης ταυτίζονται με τα "παλιά" της στοιχεία - const error = new Error("Spot already exists: a spot with the same attributes already exists."); - error.response = { statusCode: 400 }; + const error = new Error("Spot already exists: a spot with the same attributes already exists."); // Αν ναι , τότε σφάλμα + error.response = { statusCode: 400 }; // με κωδικό 400 reject(error); - return; + return; // Επιστρέφει 400 } - resolve(); + resolve(); // Επιστρέφει τη τροποποιημένη θέση }); }; @@ -185,7 +185,7 @@ exports.modifySpot = function (body, address, type, charger, id) { exports.removeSpot = function (id) { return new Promise(function (resolve, reject) { - resolve(); + resolve(); // Διαγράφει τη θέση από το σύστημα }); } @@ -204,7 +204,7 @@ exports.searchSpot = function(address,type,charger) { var examples = {}; - examples['application/json'] = [ { + examples['application/json'] = [ { // dummy δεδομένα θέσεων "address" : "address", "id" : 0, "type" : "type", @@ -216,14 +216,14 @@ exports.searchSpot = function(address,type,charger) { "chargerAvailability" : true } ]; - if (address === "address" || type === "type" || charger === true) { - resolve(examples[Object.keys(examples)[0]]); + if (address === "address" || type === "type" || charger === true) { // Φίλτρα αναζήτησης θέσης + resolve(examples[Object.keys(examples)[0]]); // Αν βρεθούν θέσεις τις επιστρέφει } - else { + else { // Αλλιώς, αν δεν βρεθούν θέσεις resolve({ - status: 404, + status: 404, // Επιστρέφει κωδικό message: "No matching parking spots found.", }) From c13abdee936d7d5cb6d6e14ace42e52c30b37878 Mon Sep 17 00:00:00 2001 From: gpittis Date: Fri, 20 Dec 2024 11:37:31 +0200 Subject: [PATCH 20/29] remove unnecessary semicolons --- service/SpotOwnerService.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/service/SpotOwnerService.js b/service/SpotOwnerService.js index 2464382..ba13c49 100644 --- a/service/SpotOwnerService.js +++ b/service/SpotOwnerService.js @@ -16,42 +16,42 @@ exports.addSpotOwner = function(body) { error.response = { statusCode: 400 }; // if id is invalid then returns 400 reject(error); // reject the error return; - }; + } if (!body.idNumber || typeof body.idNumber !== "string") { const error = new Error("Invalid idNumber: must be a string."); // id number must be string error.response = { statusCode: 400 }; // if idNumber is invalid then returns 400 reject(error); // reject the error return; - }; + } if (!body.name || typeof body.name !== "string") { const error = new Error("Invalid name: must be a string."); // name must be string error.response = { statusCode: 400 }; // if name is invalid then returns 400 reject(error); // reject the error return; - }; + } if (!body.email || typeof body.email !== "string") { const error = new Error("Invalid email: must be a string."); // email must be string error.response = { statusCode: 400 }; // if email is invalid then returns 400 reject(error); // reject the error return; - }; + } if (!body.phone) { const error = new Error("No phone."); // phone must exists in SpotOwner's data error.response = { statusCode: 400 }; // if phone is non - existent then returns 400 reject(error); // reject the error return; - }; + } if (!body.spots) { const error = new Error("No spots."); // Spots must exists in SpotOwner's data error.response = { statusCode: 400 }; // if the spot's list is non - existent then returns 400 reject(error); // reject the error return; - }; + } resolve(); }); From e68a6bcb31b1505ffbd58aed7f7c64a581d8ff4d Mon Sep 17 00:00:00 2001 From: gpittis Date: Fri, 20 Dec 2024 11:40:46 +0200 Subject: [PATCH 21/29] remove unused dummy data --- service/LicensePlateService.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/service/LicensePlateService.js b/service/LicensePlateService.js index 67d8502..6ac7643 100644 --- a/service/LicensePlateService.js +++ b/service/LicensePlateService.js @@ -79,12 +79,6 @@ exports.modifyPlate = function(body) { exports.registerPlate = function(body) { return new Promise((resolve, reject) => { - /** A license plate already registered in the system **/ - var existingPlates = { - "licensePlate": "AKH1314", - "id": 15, - }; - /** If the id has an invalid value, return an error with status code 400 **/ if (!Number.isInteger(body.id) || body.id < 0) { const error = new Error("Invalid id: must be a positive integer."); From 369b9232e5e9e36701c5bbff6e42dc79bd192dfd Mon Sep 17 00:00:00 2001 From: gpittis Date: Fri, 20 Dec 2024 12:22:08 +0200 Subject: [PATCH 22/29] make more simple the code of SpotOwnerService.js --- service/SpotOwnerService.js | 67 ++++++++++++------------------------- 1 file changed, 21 insertions(+), 46 deletions(-) diff --git a/service/SpotOwnerService.js b/service/SpotOwnerService.js index ba13c49..b6ec43e 100644 --- a/service/SpotOwnerService.js +++ b/service/SpotOwnerService.js @@ -1,6 +1,3 @@ -'use strict'; - - /** * Add a new spot owner * FR15: The system administrator must be able to add a spot owner to the system @@ -10,49 +7,27 @@ **/ exports.addSpotOwner = function(body) { return new Promise((resolve, reject) => { - - if (!body.id || body.id < 0) { - const error = new Error("Invalid id: must be a positive integer."); // id must be non negative integer - error.response = { statusCode: 400 }; // if id is invalid then returns 400 - reject(error); // reject the error - return; - } - - if (!body.idNumber || typeof body.idNumber !== "string") { - const error = new Error("Invalid idNumber: must be a string."); // id number must be string - error.response = { statusCode: 400 }; // if idNumber is invalid then returns 400 - reject(error); // reject the error - return; - } - - if (!body.name || typeof body.name !== "string") { - const error = new Error("Invalid name: must be a string."); // name must be string - error.response = { statusCode: 400 }; // if name is invalid then returns 400 - reject(error); // reject the error - return; - } - - if (!body.email || typeof body.email !== "string") { - const error = new Error("Invalid email: must be a string."); // email must be string - error.response = { statusCode: 400 }; // if email is invalid then returns 400 - reject(error); // reject the error - return; - } - - if (!body.phone) { - const error = new Error("No phone."); // phone must exists in SpotOwner's data - error.response = { statusCode: 400 }; // if phone is non - existent then returns 400 - reject(error); // reject the error - return; - } + // Define validation rules for each field + 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', errorMessage: 'Invalid idNumber: must be a string.' }, + { key: 'name', rule: (value) => typeof value === 'string', errorMessage: 'Invalid name: must be a string.' }, + { key: 'email', rule: (value) => typeof value === 'string', 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.' }, + ]; - if (!body.spots) { - const error = new Error("No spots."); // Spots must exists in SpotOwner's data - error.response = { statusCode: 400 }; // if the spot's list is non - existent then returns 400 - reject(error); // reject the error - return; + // Loop through validations + for (const { key, rule, errorMessage } of validations) { + const value = body[key]; + if (!rule(value)) { + const error = new Error(errorMessage); + error.response = { statusCode: 400 }; + reject(error); // Reject with the appropriate error message and status code + return; + } } - resolve(); - }); -} \ No newline at end of file + resolve(); // All validations passed + }); +}; \ No newline at end of file From d740178201383c1034c0427a41853cc0768a5546 Mon Sep 17 00:00:00 2001 From: gpittis Date: Fri, 20 Dec 2024 12:48:32 +0200 Subject: [PATCH 23/29] make more simple the code of LicensePlateService.js --- service/LicensePlateService.js | 65 +++++++++++++++++----------------- 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/service/LicensePlateService.js b/service/LicensePlateService.js index 6ac7643..8c6827a 100644 --- a/service/LicensePlateService.js +++ b/service/LicensePlateService.js @@ -17,20 +17,20 @@ exports.modifyPlate = function(body) { "id": 15, }; - /**If the licensePlate is not present in the request body or is an empty string, return an error with status code 400 **/ - if (!body.licensePlate || body.licensePlate === "" ) { - const error = new Error("license Plate does not exist"); - error.response = { statusCode: 400 }; - reject(error); - return; - } + 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 the id has an invalid value, return an error with status code 400 **/ - if (!Number.isInteger(body.id) || body.id < 0) { - const error = new Error("Invalid id: must be a positive integer."); - error.response = { statusCode: 400 }; - reject(error); - return; + // Loop through validations + for (const { key, rule, errorMessage } of validations) { + const value = body[key]; + if (!rule(value)) { + const error = new Error(errorMessage); + error.response = { statusCode: 400 }; + reject(error); // Reject with the appropriate error message and status code + return; + } } /** Check if the provided id and licensePlate match exactly with any existing registration. **/ @@ -56,7 +56,7 @@ exports.modifyPlate = function(body) { /** Handle non-existent ID (the plate does not exist in the system).Return error code 404. **/ const isNonExistent = existingPlates.id !== body.id; - /** Implement the check */ + /*Implement check*/ if (isNonExistent) { const error = new Error("License plate doesn't exist."); error.response = { statusCode: 404 }; @@ -77,24 +77,25 @@ 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) => { + // Define validation rules for each field + 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' } + ]; - /** If the id has an invalid value, return an error with status code 400 **/ - if (!Number.isInteger(body.id) || body.id < 0) { - const error = new Error("Invalid id: must be a positive integer."); - error.response = { statusCode: 400 }; - reject(error); - return; - } - - /** If the licensePlate is not present in the request body or is an empty string, return an error with status code 400 **/ - if (!body.licensePlate || body.licensePlate === "") { - const error = new Error("licenseplate does not exist"); - error.response = { statusCode: 400 }; - reject(error); - return; + // Loop through validations + for (const { key, rule, errorMessage } of validations) { + const value = body[key]; + if (!rule(value)) { + const error = new Error(errorMessage); + error.response = { statusCode: 400 }; + reject(error); // Reject with the appropriate error message and status code + return; + } } - - resolve(); - }); + + resolve(); // All validations passed + }); }; + From f13626d8be567b775cf575125c431cbe670df438 Mon Sep 17 00:00:00 2001 From: gpittis Date: Sat, 21 Dec 2024 01:14:36 +0200 Subject: [PATCH 24/29] Simplify the code and reduce the number of reject(error) commands in SpotService.js --- service/SpotService.js | 111 +++++++++++++++++------------------------ 1 file changed, 45 insertions(+), 66 deletions(-) diff --git a/service/SpotService.js b/service/SpotService.js index 88430be..f54a548 100644 --- a/service/SpotService.js +++ b/service/SpotService.js @@ -18,34 +18,29 @@ exports.createSpot = function (body) { "chargerAvailability": false // η διαθεσιμότητα φορτιστή της θέσης }; - if (!body.address) { // Έλεγχος αν υπάρχει το address.Το address πρέπει να είναι string. - const error = new Error("Invalid address: must be a string."); // Το address πρέπει να υπάρχει και να είναι string. - error.response = { statusCode: 400 }; // Διαφορετικά έχω σφάλμα με κωδικό σφάλματος 400 - reject(error); - return; // επιστρέφει 400 + // Define validation rules for each field + const validations = [ + { key: 'address', rule: (value) => typeof value === 'string', + errorMessage: "Invalid address: must be a string." }, + { key: 'id', rule: (value) => value >= 0 && Number.isInteger(value), + errorMessage: "Invalid id: must be a positive integer." }, + { key: 'type', rule: (value) => ["Garage", "Open", "Underground"].includes(value), + errorMessage: "Invalid type: must be one of 'Garage', 'Open', or 'Underground'." }, + { key: 'chargerAvailability', rule: (value) => typeof value === 'boolean', + errorMessage: "Invalid chargerAvailability: must be a boolean." } + ]; + + // Loop through validations and check for errors + for (const { key, rule, errorMessage } of validations) { + const value = body[key]; + if (!rule(value)) { + const error = new Error(errorMessage); + error.response = { statusCode: 400 }; + reject(error); // Reject with the appropriate error message and status code + return; + } } - if (!body.id || body.id < 0) { //Το id πρέπει να υπάρχει και να είναι θετικό - const error = new Error("Invalid id: must be a positive integer."); //Διαφορετικά, έχω σφάλμα - error.response = { statusCode: 400 }; // με κωδικό σφάλματος 400 - reject(error); - return; // Επιστρέφει 400 - } - - if (!["Garage", "Open", "Underground"].includes(body.type)) {//Το type πρέπει να ισούται με "Garage" ή "Open" ή "Underground" - const error = new Error("Invalid type: must be one of 'Garage', 'Open', or 'Underground'."); // Αλλιώς έχω σφάλμα - error.response = { statusCode: 400 }; // με κωδικό σφάλματος 400 - reject(error); - return; // Επιστρέφει 400 - } - - if (typeof body.chargerAvailability !== 'boolean') { //Το chargerAvailability πρέπει να είναι boolean. - const error = new Error("Invalid chargerAvailability: must be a boolean."); // Αλλιώς έχω σφάλμα - error.response = { statusCode: 400 }; // Με κωδικό σφάλματος 400 - reject(error); - return; // Επιστρέφει 400 - } - const spotExists = existingSpots.id === body.id && // Έλεγχος αν υπάρχει ήδη το spot. Ειδικότερα, ελέγχω την ισότητα των id existingSpots.address === body.address && // τσεκάρω την ισότητα των διευθύνσεων existingSpots.type === body.type && // τσεκάρω την ισότητα των τύπων @@ -117,46 +112,30 @@ exports.modifySpot = function (body, address, type, charger, id) { charger: false, // η διαθεσιμότητα φορτιστή της θέσης }; - if (!["Garage", "Open", "Underground"].includes(type)) {//Το type πρέπει να ισούται με "Garage" ή "Open" ή "Underground" - const error = new Error("Invalid type in query: must be one of 'Garage', 'Open', or 'Underground'."); // Αλλιώς σφάλμα - error.response = { statusCode: 400 }; // με κωδικό σφάλματος 400 - reject(error); - return; // Επιστρέφει 400 - } - - if (!id || id < 0) { //Το id πρέπει να υπάρχει και να είναι μη αρνητικός ακέραιος αριθμός - const error = new Error("Invalid id in query: must be a positive integer."); // Αλλιώς έχω σφάλμα - error.response = { statusCode: 400 }; // με κωδικό 400 - reject(error); - return; // Επιστρέφει 400 - } - - if (body.address !== address) { // Σύγκριση του query address με το address του body. - const error = new Error("Address mismatch between query and body."); // Αν είναι διαφορετικά, τότε έχω σφάλμα - error.response = { statusCode: 400 }; // Με κωδικό 400 - reject(error); - return; // Επιστρέφει 400 - } - - if (body.type !== type) { // Σύγκριση του query type με το type του request body. - const error = new Error("Type mismatch between query and body."); // Αν είναι διαφορετικά, τότε έχω σφάλμα - error.response = { statusCode: 400 }; // Με κωδικό 400 - reject(error); - return; // Επιστρέφει 400 - } - - if (body.charger !== charger) { // Σύγκριση του query charger με το charger του request body. - const error = new Error("Charger mismatch between query and body."); // Αν είναι διαφορετικά, τότε έχω σφάλμα - error.response = { statusCode: 400 }; // Με κωδικό 400 - reject(error); - return; // Επιστρέφει 400 - } - - if (body.id !== id) { // Σύγκριση του query id με το id του request body. - const error = new Error("ID mismatch between query and body."); // Αν είναι διαφορετικά, τότε έχω σφάλμα - error.response = { statusCode: 400 }; // Με κωδικό 400 - reject(error); - return; // Επιστρέφει 400 + // Define validation rules for the query and body comparison + const validations = [ + { condition: () => ["Garage", "Open", "Underground"].includes(type), + errorMessage: "Invalid type in query: must be one of 'Garage', 'Open', or 'Underground'." }, + { condition: () => id && id >= 0, + errorMessage: "Invalid id in query: must be a positive integer." }, + { condition: () => body.address === address, + errorMessage: "Address mismatch between query and body." }, + { condition: () => body.type === type, + errorMessage: "Type mismatch between query and body." }, + { condition: () => body.charger === charger, + errorMessage: "Charger mismatch between query and body." }, + { condition: () => body.id === id, + errorMessage: "ID mismatch between query and body." }, + ]; + + // Loop through validations and check for errors + for (const { condition, errorMessage } of validations) { + if (!condition()) { + const error = new Error(errorMessage); + error.response = { statusCode: 400 }; + reject(error); // Reject with the appropriate error message and status code + return; + } } const spotExists = existingSpots.address === body.address && // Ελέγχω αν όντως τροποποίησα την θέση. Τσεκάρω διεύθυνση From e5a94017825ed343a3703f43b8dca4cfd98e7d8f Mon Sep 17 00:00:00 2001 From: gpittis Date: Sat, 21 Dec 2024 02:22:47 +0200 Subject: [PATCH 25/29] Make the final documentation and add more precise checks --- service/SpotService.js | 101 +++++++++++++++++++++-------------------- 1 file changed, 51 insertions(+), 50 deletions(-) diff --git a/service/SpotService.js b/service/SpotService.js index f54a548..70446f3 100644 --- a/service/SpotService.js +++ b/service/SpotService.js @@ -18,42 +18,45 @@ exports.createSpot = function (body) { "chargerAvailability": false // η διαθεσιμότητα φορτιστή της θέσης }; - // Define validation rules for each field + // Ορίζω τους κανόνες εγκυρότητας για κάθε field της θέσης πάρκινγκ. Το κάνω αυτό για να μειώσω το πλήθος των reject(error) const validations = [ - { key: 'address', rule: (value) => typeof value === 'string', - errorMessage: "Invalid address: must be a string." }, - { key: 'id', rule: (value) => value >= 0 && Number.isInteger(value), - errorMessage: "Invalid id: must be a positive integer." }, - { key: 'type', rule: (value) => ["Garage", "Open", "Underground"].includes(value), + { key: 'address', rule: (value) => value !== undefined && value !== null && typeof value === 'string' + && value.trim() !== '', errorMessage: "Invalid address: must be a string." }, // Η διεύθυνση είναι string + { key: 'id', rule: (value) => value >= 0 && Number.isInteger(value), // το id είναι μη αρνητικός ακέραιος αριθμός + errorMessage: "Invalid id: must be a non-negative integer." }, + { key: 'type', rule: (value) => ["Garage", "Open", "Underground"].includes(value), // ο τύπος μπορεί να πάρει 3 διαφορετικές τιμές errorMessage: "Invalid type: must be one of 'Garage', 'Open', or 'Underground'." }, - { key: 'chargerAvailability', rule: (value) => typeof value === 'boolean', + { key: 'chargerAvailability', rule: (value) => typeof value === 'boolean', // Η διαθεσιμότητα φορτιστή είναι boolean errorMessage: "Invalid chargerAvailability: must be a boolean." } ]; - // Loop through validations and check for errors + // Έλεγχος για σφάλματα στα fields της θέσης πάρκινγκ for (const { key, rule, errorMessage } of validations) { const value = body[key]; - if (!rule(value)) { + if (!rule(value)) { // Αν η τιμή δεν ικανοποιεί τον κανόνα εγκυρότητας (rule), τότε υπάρχει σφάλμα const error = new Error(errorMessage); - error.response = { statusCode: 400 }; - reject(error); // Reject with the appropriate error message and status code + error.response = { statusCode: 400 }; // Επιστρέφει κωδικό 400 + reject(error); return; } } - const spotExists = existingSpots.id === body.id && // Έλεγχος αν υπάρχει ήδη το spot. Ειδικότερα, ελέγχω την ισότητα των id - existingSpots.address === body.address && // τσεκάρω την ισότητα των διευθύνσεων - existingSpots.type === body.type && // τσεκάρω την ισότητα των τύπων - existingSpots.chargerAvailability === body.chargerAvailability; // και τέλος τσεκάρω τη διαθεσιμότητα φορτιστή + // Έλεγχος αν υπάρχει ήδη το spot , δηλαδή γίνεται έλεγχος για διπλότυπη θέση. + // Αν όλα τα attributes του existingSpots ισούται ένα προς ένα με όλα τα attributes του spot που θέλω να φτιάξω, + // τότε θέλω να δημιουργήσω μια διπλότυπη θέση. Άρα, έχω σφάλμα + const spotExists = existingSpots.id === body.id && + existingSpots.address === body.address && + existingSpots.type === body.type && + existingSpots.chargerAvailability === body.chargerAvailability; if (spotExists) { //Αν βρεθεί διπλότυπη θέση const error = new Error("Spot already exists: a spot with the same attributes already exists."); // έχω σφάλμα error.response = { statusCode: 400 }; // με κωδικό 400 reject(error); - return; // Επιστρέφει 400 + return; } - resolve(); // Δημιουργεί τη θέση πάρκινγκ. + resolve();// Η θέση έχει περάσει όλους τους ελέγχους και μπορώ να την προσθέσω. Δεν το κάνω, καθώς η συνάρτηση είναι dummy. }); }; @@ -66,10 +69,8 @@ exports.createSpot = function (body) { **/ exports.getSpots = function () { return new Promise(function (resolve, /**reject*/) { // reject is unused - var examples = {}; - - examples['application/json'] = [{ // dummy δεδομένα θέσεων + examples['application/json'] = [{ "address": "address", "id": 0, "type": "type", @@ -80,12 +81,10 @@ exports.getSpots = function () { "type": "type", "chargerAvailability": true }]; - if (Object.keys(examples).length > 0) { resolve(examples[Object.keys(examples)[0]]); } else { - - resolve(); // Eπιστρέφει όλες τις διαθέσιμες θέσεις μέσα στο σύστημα. + resolve(); } }); } @@ -112,44 +111,52 @@ exports.modifySpot = function (body, address, type, charger, id) { charger: false, // η διαθεσιμότητα φορτιστή της θέσης }; - // Define validation rules for the query and body comparison + //Ορίζω κανόνες εγκυρότητας για το type και το id της θέσης πάρκινγκ. Τα address και charger ελέγχονται από το swagger. + //Επίσης, ορίζω κανόνες εγκυρότητας για τη σύγκριση της ισότητας των query και body παραμέτρων. + //Το κάνω αυτό για να μειώσω το πλήθος των reject(error). const validations = [ - { condition: () => ["Garage", "Open", "Underground"].includes(type), + { condition: () => ["Garage", "Open", "Underground"].includes(type), // ο τύπος μπορεί να πάρει 3 διαφορετικές τιμές errorMessage: "Invalid type in query: must be one of 'Garage', 'Open', or 'Underground'." }, - { condition: () => id && id >= 0, + { condition: () => id && id >= 0, // το id πρέπει να είναι μη αρνητικός ακέραιος αριθμός errorMessage: "Invalid id in query: must be a positive integer." }, - { condition: () => body.address === address, + { condition: () => body.address === address, // Τα address των query και body πρέπει να ταυτίζονται errorMessage: "Address mismatch between query and body." }, - { condition: () => body.type === type, + { condition: () => body.type === type, // Τα type των query και body πρέπει να ταυτίζονται errorMessage: "Type mismatch between query and body." }, - { condition: () => body.charger === charger, + { condition: () => body.charger === charger, // Τα charger των query και body πρέπει να ταυτίζονται errorMessage: "Charger mismatch between query and body." }, - { condition: () => body.id === id, + { condition: () => body.id === id, // Τα id των query και body πρέπει να ταυτίζονται errorMessage: "ID mismatch between query and body." }, ]; - // Loop through validations and check for errors + // Έλεγχος για σφάλματα με βάση τους παραπάνω κανόνες εγκυρότητας for (const { condition, errorMessage } of validations) { - if (!condition()) { + if (!condition()) { // Αν δεν ικανοποιείται το condition, τότε υπάρχει σφάλμα const error = new Error(errorMessage); - error.response = { statusCode: 400 }; - reject(error); // Reject with the appropriate error message and status code + error.response = { statusCode: 400 }; // Επιστρέφει 400 + reject(error); return; } } - const spotExists = existingSpots.address === body.address && // Ελέγχω αν όντως τροποποίησα την θέση. Τσεκάρω διεύθυνση - existingSpots.type === body.type && // Τσεκάρω τύπο - existingSpots.charger === body.charger; // Τσεκάρω διαθεσιμότητα φορτιστή + // Έλεγχος αν τα τροποποιημένα στοιχεία της θέσης ταυτίζονται με τα "παλιά" στοιχεία της θέσης. + //Ουσιαστικά, δεν γίνεται τροποποίηση. + //Δεν ελέγχω τα id του existingSpots και του body διότι θεωρώ εκ των πραγμάτων ότι ισούται μεταξύ τους. + // Αυτό συμβαίνει διότι κάνω modify μια ήδη υπάρχουσα θέση , άρα το id δεν αλλάζει κατά το modification. + // Αν άλλαζε το id τότε θα έκανα create μια νέα θέση. + // Αντίστοιχος έλεγχος έγινε και στο PUT /licensePlate. + const spotExists = existingSpots.address === body.address && + existingSpots.type === body.type && + existingSpots.charger === body.charger; if (spotExists) { //Έλεγχος αν τα τροποποιημένα στοιχεία της θέσης ταυτίζονται με τα "παλιά" της στοιχεία const error = new Error("Spot already exists: a spot with the same attributes already exists."); // Αν ναι , τότε σφάλμα error.response = { statusCode: 400 }; // με κωδικό 400 reject(error); - return; // Επιστρέφει 400 + return; } - resolve(); // Επιστρέφει τη τροποποιημένη θέση + resolve();// Η θέση έχει περάσει όλους τους ελέγχους και μπορώ να την τροποποιήσω. Δεν το κάνω, καθώς η συνάρτηση είναι dummy. }); }; @@ -163,7 +170,6 @@ exports.modifySpot = function (body, address, type, charger, id) { **/ exports.removeSpot = function (id) { return new Promise(function (resolve, reject) { - resolve(); // Διαγράφει τη θέση από το σύστημα }); } @@ -180,10 +186,8 @@ exports.removeSpot = function (id) { **/ exports.searchSpot = function(address,type,charger) { return new Promise(function(resolve, /*reject*/) { // remove unused reject - var examples = {}; - - examples['application/json'] = [ { // dummy δεδομένα θέσεων + examples['application/json'] = [ { "address" : "address", "id" : 0, "type" : "type", @@ -194,15 +198,12 @@ exports.searchSpot = function(address,type,charger) { "type" : "type", "chargerAvailability" : true } ]; - - if (address === "address" || type === "type" || charger === true) { // Φίλτρα αναζήτησης θέσης - resolve(examples[Object.keys(examples)[0]]); // Αν βρεθούν θέσεις τις επιστρέφει + if (address === "address" || type === "type" || charger === true) { + resolve(examples[Object.keys(examples)[0]]); } - - else { // Αλλιώς, αν δεν βρεθούν θέσεις - + else { resolve({ - status: 404, // Επιστρέφει κωδικό + status: 404, message: "No matching parking spots found.", }) From ae95e5be6df620ce69e31cbbe43b313438f5600f Mon Sep 17 00:00:00 2001 From: gpittis Date: Sat, 21 Dec 2024 03:41:11 +0200 Subject: [PATCH 26/29] Make the final documentation --- service/LicensePlateService.js | 62 +++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 28 deletions(-) diff --git a/service/LicensePlateService.js b/service/LicensePlateService.js index 8c6827a..abcd991 100644 --- a/service/LicensePlateService.js +++ b/service/LicensePlateService.js @@ -11,32 +11,39 @@ exports.modifyPlate = function(body) { return new Promise(function(resolve, reject) { - /**A license plate already registered in the system **/ + // Μια ήδη καταχωρημένη πινακίδα μέσα στο σύστημα var existingPlates = { - "licensePlate": "AKH1314", - "id": 15, + "licensePlate": "AKH1314", // όνομα πινακίδας + "id": 15, // το id της πινακίδας }; - + + //Ορίζω κανόνες εγκυρότητας για τα 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." } + { key: 'id', rule: (value) => Number.isInteger(value) && value > 0, + errorMessage: "Invalid id: must be a positive integer." } ]; - // Loop through validations + // Έλεγχος για σφάλματα στα fields της πινακίδας for (const { key, rule, errorMessage } of validations) { const value = body[key]; - if (!rule(value)) { + if (!rule(value)) { // Αν η τιμή δεν ικανοποιεί τον κανόνα εγκυρότητας (rule), τότε υπάρχει σφάλμα const error = new Error(errorMessage); - error.response = { statusCode: 400 }; - reject(error); // Reject with the appropriate error message and status code + error.response = { statusCode: 400 }; // Επιστρέφει κωδικό 400 + reject(error); return; } } - /** Check if the provided id and licensePlate match exactly with any existing registration. **/ + //Αν η dummy πινακίδα που είναι ήδη καταχωρημένη μέσα στο σύστημα έχει ίδιο id με εκείνο του request body και διαφορετικό + // όνομα πινακίδας με εκείνο του request body, τότε συμβαίνει με επιτυχία η τροποποίηση της πινακίδας. Αν τα γνωρίσματα της + // dummy καταχωρημένης πινακίδας και του request body ταυτίζονται , τότε δεν τροποποιώ την πινακίδα αλλά αντίθετα διατηρώ + // τα "παλιά" της στοιχεία.Τέλος, αν όλα τα γνωρίσματα της dummy πινακίδας διαφέρουν με τα αντίστοιχα γνωρίσματα του request + // body , τότε δημιουργώ μια νέα πινακίδα και ΔΕΝ τροποποιώ μια ήδη υπάρχουσα πινακίδα. const isExactMatch = existingPlates.id === body.id && existingPlates.licensePlate === body.licensePlate; - - /** Implement the check */ if (isExactMatch) { const error = new Error("License plate already exists."); error.response = { statusCode: 400 }; @@ -44,22 +51,18 @@ exports.modifyPlate = function(body) { return; } - /** Allow updating the license plate for an existing ID. Returns response code 200 **/ + // Allow updating the license plate for an existing ID (Επιτυχής τροποποίηση πινακίδας) const isValidUpdate = existingPlates.id === body.id && existingPlates.licensePlate !== body.licensePlate; - - /** Implement the check */ if (isValidUpdate) { - resolve(); + resolve(); // This means the update is valid, return 200 return; } - /** Handle non-existent ID (the plate does not exist in the system).Return error code 404. **/ + // Handle non-existent ID (η πινακίδα δεν υπάρχει μέσα στο σύστημα) const isNonExistent = existingPlates.id !== body.id; - - /*Implement check*/ if (isNonExistent) { - const error = new Error("License plate doesn't exist."); - error.response = { statusCode: 404 }; + const error = new Error("License plate doesn't exist."); // Η πινακίδα που επιθυμώ να τροποποιήσω ΔΕΝ βρέθηκε. + error.response = { statusCode: 404 }; // Επιστρέφει κωδικό σφάλματος 404. reject(error); return; } @@ -78,24 +81,27 @@ exports.modifyPlate = function(body) { **/ exports.registerPlate = function(body) { return new Promise((resolve, reject) => { - // Define validation rules for each field + //Ορίζω κανόνες εγκυρότητας για τα 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: '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' } ]; - // Loop through validations + // Έλεγχος για σφάλματα στα fields της πινακίδας for (const { key, rule, errorMessage } of validations) { const value = body[key]; - if (!rule(value)) { + if (!rule(value)) { // Αν η τιμή δεν ικανοποιεί τον κανόνα εγκυρότητας (rule), τότε υπάρχει σφάλμα const error = new Error(errorMessage); - error.response = { statusCode: 400 }; - reject(error); // Reject with the appropriate error message and status code + error.response = { statusCode: 400 }; // Επιστρέφει κωδικό 400 + reject(error); return; } } - resolve(); // All validations passed + resolve(); }); }; From 8f302054cb29a09424f13197c3f41db2ce81dbe1 Mon Sep 17 00:00:00 2001 From: gpittis Date: Sat, 21 Dec 2024 04:19:54 +0200 Subject: [PATCH 27/29] Make the final documentation --- service/SpotOwnerService.js | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/service/SpotOwnerService.js b/service/SpotOwnerService.js index b6ec43e..a0ab805 100644 --- a/service/SpotOwnerService.js +++ b/service/SpotOwnerService.js @@ -7,27 +7,37 @@ **/ exports.addSpotOwner = function(body) { return new Promise((resolve, reject) => { - // Define validation rules for each field + + //idNumber = αριθμός ταυτότητας + //spots = μια λίστα που περιέχει τις θέσεις που ανήκουν στον ιδιοκτήτη θέσεων. + + // Ορίζω κανόνες εγκυρότητας για κάθε 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', errorMessage: 'Invalid idNumber: must be a string.' }, - { key: 'name', rule: (value) => typeof value === 'string', errorMessage: 'Invalid name: must be a string.' }, - { key: 'email', rule: (value) => typeof value === 'string', errorMessage: 'Invalid email: must be a string.' }, + { 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.' }, ]; - // Loop through validations + // Έλεγχος για σφάλματα στα fields του Ιδιοκτήτη θέσεων for (const { key, rule, errorMessage } of validations) { const value = body[key]; - if (!rule(value)) { + if (!rule(value)) { // Αν η τιμή δεν ικανοποιεί τον κανόνα εγκυρότητας (rule), τότε υπάρχει σφάλμα const error = new Error(errorMessage); - error.response = { statusCode: 400 }; - reject(error); // Reject with the appropriate error message and status code + error.response = { statusCode: 400 }; // Επιστρέφει κωδικό 400 + reject(error); return; } } - resolve(); // All validations passed + resolve(); }); }; \ No newline at end of file From 5b1cce0b96ccc25658ba246e542bcec416f1cd5f Mon Sep 17 00:00:00 2001 From: gpittis Date: Sat, 21 Dec 2024 04:56:40 +0200 Subject: [PATCH 28/29] Update POST_payment.test.js from main --- test/POST_payment.test.js | 57 ++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/test/POST_payment.test.js b/test/POST_payment.test.js index 3f4f8a5..51bd0df 100644 --- a/test/POST_payment.test.js +++ b/test/POST_payment.test.js @@ -20,35 +20,36 @@ test.after.always((t) => { //amount = το χρηματικό ποσό της πληρωμής //Από το yaml αρχείο παρατηρώ ότι το amount μπορεί να είναι μόνο ακέραιος , αλλιώς έχω σφάλμα. - -//Τεστάρω την συνάρτηση makeReservation για την επιτυχή δημιουργία μιας καινούριας πληρωμής -test("makePayment function succeeds with valid data", async (t) => { - const body = await makePayment({ - id: 123, - user: { - id: 456, - name: "Jane Doe", - licensePlate: { +//Happy path για το payment +test("POST /payment succesfull", async (t) => { + const {body, statusCode} = await t.context.got.post('payment', { + json : { + id: 123, + user: { + id: 456, + name: "Jane Doe", + licensePlate: { + userId: 456, + licensePlate: "XYZ-7890", + }, + reservation: [ + { + id: 789, + spotId: 321, userId: 456, - licensePlate: "XYZ-7890", + startTime: "2024-11-23T10:00:00Z", + duration: "2024-11-23T12:00:00Z", + date: "2024-11-23", }, - reservation: [ - { - id: 789, - spotId: 321, - userId: 456, - startTime: "2024-11-23T10:00:00Z", - duration: "2024-11-23T12:00:00Z", - date: "2024-11-23", - }, - ], - }, - amount: 150, - }); - - t.falsy(body); // το body είναι κενό. - }); - + ], + }, + amount: 150, + } +}); + + t.is(statusCode, 200); //checking that the status code is 200 + t.falsy(body); //confirming that the response body is empty +}); //Τεστ για μη έγκυρα δεδομένα στο payment test("POST /payment with invalid data returns 400", async (t) => { @@ -129,4 +130,4 @@ test("POST /payment with missing id returns 400", async (t) => { }, }); t.is(body.statusCode, 400); // To τεστ αποτυχαίνει και επιστρέφει κωδικό αποτυχίας 400. -}); +}); \ No newline at end of file From 71653882757a9e96b4b98f72e4d5e73a32123f0e Mon Sep 17 00:00:00 2001 From: gpittis Date: Sat, 21 Dec 2024 05:02:55 +0200 Subject: [PATCH 29/29] Update POST_reservation.test.js from main --- test/POST_reservation.test.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/test/POST_reservation.test.js b/test/POST_reservation.test.js index dc6369a..68d25d3 100644 --- a/test/POST_reservation.test.js +++ b/test/POST_reservation.test.js @@ -30,6 +30,20 @@ test("makeReservation function checks for a duplicate reservation", async (t) => t.is(body.response.statusCode, 400); }); +test("POST /reservation with correct data returns code 200", async (t) => { + const { body, statusCode } = await t.context.got.post('reservation', { + json: { + date: "2024-11-19", + startTime: "2024-11-19T08:00:00Z", + duration: "2024-11-19T11:00:00Z", + spotId: 1, + id: 1, + userId: 1 + } + }); + t.is(statusCode, 200); +}); + //Τεστάρω αν δεδομένα της κράτησης είναι έγκυρα test("POST /reservation with invalid data returns 400", async (t) => { // Σε αυτό το τεστ , τα δεδομένα της κράτησης είναι μη έγκυρα. @@ -88,5 +102,4 @@ test("POST /reservation with missing duration returns 400", async (t) => { json: { date:"2024-11-19", startTime: "2024-11-19T08:00:00Z", userId:1, spotId: 1, id:1 } }); t.is(body.response.statusCode, 400); -}); - +}); \ No newline at end of file