Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Artworks backend #7

Merged
merged 2 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/database/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ create table users (
points INT,
is_administrator BOOLEAN NOT NULL
);

create table artists (
id INT PRIMARY KEY AUTO_INCREMENT NOT NULL,
name VARCHAR(255) NOT NULL,
Expand Down
87 changes: 87 additions & 0 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"dependencies": {
"dotenv": "^16.3.1",
"express": "^4.18.2",
"joi": "^17.11.0",
"mysql2": "^3.5.2"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion backend/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ app.use(

// Uncomment one or more of these options depending on the format of the data sent by your client:

// app.use(express.json());
app.use(express.json());
// app.use(express.urlencoded());
// app.use(express.text());
// app.use(express.raw());
Expand Down
102 changes: 102 additions & 0 deletions backend/src/controllers/artworksControllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Import access to database tables
const tables = require("../tables");

// The B of BREAD - Browse (Read All) operation
const browse = async (req, res, next) => {
try {
// Fetch all items from the database
const artworks = await tables.artworks.readAll();

// Respond with the items in JSON format
res.json(artworks);
} catch (err) {
// Pass any errors to the error-handling middleware
next(err);
}
};

// The R of BREAD - Read operation
const read = async (req, res, next) => {
try {
// Fetch a specific item from the database based on the provided ID
const artwork = await tables.artworks.read(req.params.id);

// If the item is not found, respond with HTTP 404 (Not Found)
// Otherwise, respond with the item in JSON format
if (artwork == null) {
res.sendStatus(404);
} else {
res.json(artwork);
}
} catch (err) {
// Pass any errors to the error-handling middleware
next(err);
}
};

// The E of BREAD - Edit (Update) operation
const edit = async (req, res, next) => {
// Extract the updated item data from the request body
const updatedArtwork = req.body;

try {
// Update the item in the database based on the provided ID
const success = await tables.artworks.update(req.params.id, updatedArtwork);

// If the item is not found, respond with HTTP 404 (Not Found)
// If the update is successful, respond with HTTP 204 (No Content)
if (!success) {
res.sendStatus(404);
} else {
res.sendStatus(204);
}
} catch (err) {
// Pass any errors to the error-handling middleware
next(err);
}
};

// The A of BREAD - Add (Create) operation
const add = async (req, res, next) => {
// Extract the item data from the request body
const artwork = req.body;

try {
// Insert the item into the database
const insertId = await tables.artworks.create(artwork);

// Respond with HTTP 201 (Created) and the ID of the newly inserted item
res.status(201).json({ insertId });
} catch (err) {
// Pass any errors to the error-handling middleware
next(err);
}
};

// The D of BREAD - Destroy (Delete) operation
const destroy = async (req, res, next) => {
try {
// Delete the item from the database based on the provided ID
const success = await tables.artworks.delete(req.params.id);

// If the item is not found, respond with HTTP 404 (Not Found)
// If the deletion is successful, respond with HTTP 204 (No Content)
if (!success) {
res.sendStatus(404);
} else {
res.sendStatus(204);
}
} catch (err) {
// Pass any errors to the error-handling middleware
next(err);
}
};

// Ready to export the controller functions
module.exports = {
browse,
read,
edit,
add,
destroy,
};
67 changes: 0 additions & 67 deletions backend/src/controllers/itemControllers.js

This file was deleted.

Loading