generated from WildCodeSchool/create-js-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into user_connexion_backend
- Loading branch information
Showing
27 changed files
with
624 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
const tables = require("../tables"); | ||
|
||
const browse = async (req, res, next) => { | ||
try { | ||
const location = await tables.locations.readAll(); | ||
res.json(location); | ||
} catch (err) { | ||
next(err); | ||
} | ||
}; | ||
|
||
const read = async (req, res, next) => { | ||
try { | ||
const location = await tables.locations.read(req.params.id); | ||
if (location == null) { | ||
res.sendStatus(404); | ||
} else { | ||
res.json(location); | ||
} | ||
} catch (err) { | ||
next(err); | ||
} | ||
}; | ||
|
||
const edit = async (req, res, next) => { | ||
const location = req.body; | ||
try { | ||
const insertId = await tables.locations.update(req.params.id, location); | ||
if (insertId == null) { | ||
res.sendStatus(404); | ||
} else { | ||
res.sendStatus(204); | ||
} | ||
} catch (err) { | ||
next(err); | ||
} | ||
}; | ||
|
||
const add = async (req, res, next) => { | ||
const location = req.body; | ||
|
||
try { | ||
const insertId = await tables.locations.create(location); | ||
res.status(201).json({ insertId }); | ||
} catch (err) { | ||
next(err); | ||
} | ||
}; | ||
|
||
const destroy = async (req, res, next) => { | ||
try { | ||
const location = await tables.locations.delete(req.params.id); | ||
|
||
if (location == null) { | ||
res.sendStatus(404); | ||
} else { | ||
res.sendStatus(204); | ||
} | ||
} catch (err) { | ||
next(err); | ||
} | ||
}; | ||
|
||
module.exports = { | ||
browse, | ||
read, | ||
edit, | ||
add, | ||
destroy, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
const AbstractManager = require("./AbstractManager"); | ||
|
||
class LocationManager extends AbstractManager { | ||
constructor() { | ||
// Call the constructor of the parent class (AbstractManager) | ||
// and pass the table name "item" as configuration | ||
super({ table: "locations" }); | ||
} | ||
|
||
async create(location) { | ||
// Execute the SQL SELECT query to retrieve all items from the "item" table | ||
const [result] = await this.database.query( | ||
`insert into ${this.table} (city, country, post_code, street, street_number, latitude, longitude) values (?,?,?,?,?,?,?)`, | ||
[ | ||
location.city, | ||
location.country, | ||
location.post_code, | ||
location.street, | ||
location.street_number, | ||
location.latitude, | ||
location.longitude, | ||
] | ||
); | ||
|
||
// Return the ID of the newly inserted item | ||
return result.insertId; | ||
} | ||
|
||
async readAll() { | ||
// Execute the SQL SELECT query to retrieve all items from the "item" table | ||
const [rows] = await this.database.query(`select * from ${this.table}`); | ||
|
||
// Return the array of items | ||
return rows; | ||
} | ||
|
||
async read(id) { | ||
const [rows] = await this.database.query( | ||
`select * from ${this.table} where id = ?`, | ||
[id] | ||
); | ||
|
||
// Return the first row of the result, which represents the item | ||
return rows[0]; | ||
} | ||
|
||
// The U of CRUD - Update operation | ||
// TODO: Implement the update operation to modify an existing item | ||
|
||
async update(id, location) { | ||
const [result] = await this.database.query( | ||
`update ${this.table} set city = ?, country = ?, post_code = ?, street = ?, street_number = ?, latitude = ?, longitude = ? where id = ?`, | ||
[ | ||
location.city, | ||
location.country, | ||
location.post_code, | ||
location.street, | ||
location.street_number, | ||
location.latitude, | ||
location.longitude, | ||
id, | ||
] | ||
); | ||
|
||
// Return the ID of the newly inserted item | ||
return result.insertId; | ||
} | ||
|
||
// The D of CRUD - Delete operation | ||
// TODO: Implement the delete operation to remove an item by its ID | ||
|
||
async delete(id) { | ||
const [rows] = await this.database.query( | ||
`delete from ${this.table} where id = ?`, | ||
[id] | ||
); | ||
|
||
// Return the first row of the result, which represents the item | ||
return rows[0]; | ||
} | ||
} | ||
|
||
module.exports = LocationManager; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const Joi = require("joi"); | ||
|
||
// Joi schema for validating artwork data | ||
const locationSchema = Joi.object({ | ||
city: Joi.string().max(255).required(), | ||
country: Joi.string().max(255).required(), | ||
post_code: Joi.number().integer().required(), | ||
street: Joi.string().max(255).required(), | ||
street_number: Joi.number().integer().required(), | ||
latitude: Joi.number().required(), | ||
longitude: Joi.number().required(), | ||
}); | ||
|
||
// Middleware for validating artwork data before creating or updating | ||
const validateLocation = (req, res, next) => { | ||
const locationData = req.body; | ||
|
||
// Validate the artwork data against the Joi schema | ||
const { error } = locationSchema.validate(locationData); | ||
|
||
if (error) { | ||
// If validation fails, respond with a 400 Bad Request and the validation error details | ||
res.status(400).json({ error: error.details.map((err) => err.message) }); | ||
} else { | ||
// If validation passes, proceed to the next middleware or route handler | ||
next(); | ||
} | ||
}; | ||
|
||
module.exports = { validateLocation }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.