Skip to content

Commit

Permalink
Merge branch 'dev' into user_connexion_backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexis-NM authored Jan 4, 2024
2 parents d57e909 + 1356e1e commit 159f440
Show file tree
Hide file tree
Showing 27 changed files with 624 additions and 156 deletions.
70 changes: 70 additions & 0 deletions backend/src/controllers/locationControllers.js
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,
};
83 changes: 83 additions & 0 deletions backend/src/models/LocationManager.js
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;
18 changes: 18 additions & 0 deletions backend/src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ const { validateArtwork } = require("./services/validateArtwork");
const userControllers = require("./controllers/userControllers");
const { validateUser } = require("./services/validateUser");

const locationControllers = require("./controllers/locationControllers");
const { validateLocation } = require("./services/validateLocation");

// Route to get a list of users
router.get("/users", userControllers.browse);

Expand Down Expand Up @@ -38,4 +41,19 @@ router.put("/artworks/:id", validateArtwork, artworksControllers.edit);
// Route to delete an artwork by ID
router.delete("/artworks/:id", artworksControllers.destroy);

// Route to get a list of locations
router.get("/locations", locationControllers.browse);

// Route to get a specific location by ID
router.get("/locations/:id", locationControllers.read);

// Route to add a new location
router.post("/locations", validateLocation, locationControllers.add);

// Route to update an existing location by ID
router.put("/locations/:id", validateLocation, locationControllers.edit);

// Route to delete a location by ID
router.delete("/locations/:id", locationControllers.destroy);

module.exports = router;
30 changes: 30 additions & 0 deletions backend/src/services/validateLocation.js
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 };
4 changes: 3 additions & 1 deletion backend/src/tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

const UserManager = require("./models/UserManager");
const ArtworkManager = require("./models/ArtworksManager");
const LocationManager = require("./models/LocationManager");

const managers = [
UserManager,
ArtworkManager, // Add other managers here
ArtworkManager,
LocationManager, // Add other managers here
];

// Create an empty object to hold data managers for different tables
Expand Down
10 changes: 8 additions & 2 deletions frontend/index.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
<!doctype html>
<html lang="en">
<html lang="fr">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/src/assets/favicon.svg" />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Knewave&display=swap"
rel="stylesheet"
/>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite App</title>
<title>Street Art Hunter</title>
</head>
<body>
<div id="root"></div>
Expand Down
Loading

0 comments on commit 159f440

Please sign in to comment.