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

Filters #115

Closed
wants to merge 11 commits into from
Closed
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
192 changes: 189 additions & 3 deletions backend/database/schema.sql

Large diffs are not rendered by default.

104 changes: 93 additions & 11 deletions backend/package-lock.json

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

2 changes: 2 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"test": "jest"
},
"dependencies": {
"axios": "^1.6.2",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"mysql2": "^3.5.2"
Expand Down
6 changes: 1 addition & 5 deletions backend/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,15 @@ const app = express();
// 4. Be sure to only have URLs in the array with domains from which you want to allow requests.
// For example: ["http://mysite.com", "http://another-domain.com"]

/*
const cors = require("cors");

app.use(
cors({
origin: [
process.env.FRONTEND_URL, // keep this one, after checking the value in `backend/.env`
"http://mysite.com",
"http://another-domain.com",
]
],
})
);
*/

/* ************************************************************************* */

Expand Down
67 changes: 67 additions & 0 deletions backend/src/controllers/pkmnControllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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 pokemons from the database
const pokemons = await tables.pokemon.readAll();

// Respond with the pokemons in JSON format
res.json(pokemons);
} 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 pokemon from the database based on the provided ID
const pokemon = await tables.pokemon.read(req.params.id);

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

// The E of BREAD - Edit (Update) operation
// This operation is not yet implemented

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

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

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

// The D of BREAD - Destroy (Delete) operation
// This operation is not yet implemented

// Ready to export the controller functions
module.exports = {
browse,
read,
// edit,
add,
// destroy,
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ 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 items = await tables.item.readAll();
// Fetch all types from the database
const types = await tables.pktype.readAll();

// Respond with the items in JSON format
res.json(items);
// Respond with the types in JSON format
res.json(types);
} catch (err) {
// Pass any errors to the error-handling middleware
next(err);
Expand All @@ -18,15 +18,15 @@ const browse = async (req, res, next) => {
// 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 item = await tables.item.read(req.params.id);
// Fetch a specific type from the database based on the provided ID
const pktype = await tables.pktype.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 (item == null) {
// If the type is not found, respond with HTTP 404 (Not Found)
// Otherwise, respond with the type in JSON format
if (pktype == null) {
res.sendStatus(404);
} else {
res.json(item);
res.json(pktype);
}
} catch (err) {
// Pass any errors to the error-handling middleware
Expand All @@ -39,14 +39,14 @@ const read = async (req, res, next) => {

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

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

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