├── README.md ├── loadEnv.js ├── .gitignore ├── .env ├── db ├── conn.js └── models │ ├── poke-man.js │ ├── pokemon.js │ └── users.js ├── package.json ├── routes ├── pokemon.js └── users.js └── index.js /README.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /loadEnv.js: -------------------------------------------------------------------------------- 1 | import dotenv from "dotenv"; 2 | dotenv.config(); -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .env 3 | package-lock.json 4 | package.json -------------------------------------------------------------------------------- /.env: -------------------------------------------------------------------------------- 1 | ATLAS_URI="mongodb+srv://iannislopez720:Winnie18@mongopratice.rgunsig.mongodb.net/?retryWrites=true&w=majority" 2 | PORT= -------------------------------------------------------------------------------- /db/conn.js: -------------------------------------------------------------------------------- 1 | import {MongoClient} from 'mongodb'; 2 | 3 | console.log(process.env.ATLAS_URI); 4 | const connectionString = process.env.ATLAS_URI || ""; 5 | 6 | const client = new MongoClient(connectionString); 7 | 8 | let conn; 9 | 10 | try { 11 | conn = await client.connect(); 12 | console.log(`Connected to mongodb`); 13 | } catch (e) { 14 | console.log(e); 15 | } 16 | 17 | const db = conn.db("pokedex-app"); 18 | 19 | export default db; -------------------------------------------------------------------------------- /db/models/poke-man.js: -------------------------------------------------------------------------------- 1 | import mongoose from 'mongoose'; 2 | 3 | const PokemaneSchema = new mongoose.Schema({ 4 | name: { type: String, required: true }, 5 | team: { type: String, required: true, min: 6 }, 6 | pokeImage: { type: String , default: true }, 7 | boxed: { type: Boolean, default: true}, 8 | createdAt: { type: Date, default: Date.now }, 9 | location: { type: String, enum: ["battle area", "Route 5", "Unknown", "Area1", "Vern Forest"]}, 10 | }); 11 | 12 | const Pokemane = mongoose.model('pokemane', PokemaneSchema); 13 | 14 | export default Pokemane; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "bcrypt": "^5.1.1", 4 | "cors": "^2.8.5", 5 | "dotenv": "^16.4.1", 6 | "express": "^4.18.2", 7 | "mongoose": "^8.1.1", 8 | "morgan": "^1.10.0" 9 | }, 10 | "name": "backend", 11 | "version": "1.0.0", 12 | "main": "index.js", 13 | "type": "module", 14 | "devDependencies": { 15 | "nodemon": "^3.0.3" 16 | }, 17 | "scripts": { 18 | "test": "echo \"Error: no test specified\" && exit 1", 19 | "start": "nodemon index.js" 20 | }, 21 | "keywords": [], 22 | "author": "Iannis Lopez", 23 | "license": "ISC", 24 | "description": "Pokemon?" 25 | } 26 | -------------------------------------------------------------------------------- /routes/pokemon.js: -------------------------------------------------------------------------------- 1 | import {Router} from 'express'; 2 | import {objectId} from 'mongoose'; 3 | import pokemon from '../db/models/pokemon'; 4 | 5 | const router = new Router; 6 | 7 | // Get 8 | router.get('/', async (req, res) => { 9 | try { 10 | const collection = await db.collection("pokemon"); 11 | 12 | const pokemon = await collection.find().toArray(); 13 | 14 | const topThreeHundred = pokemon.slice(0, 300); 15 | console.log(topThreeHundred) 16 | res.status(200).json(topThreeHundred); 17 | } catch (error) { 18 | console.log(error); 19 | } 20 | }); 21 | 22 | router.get("/:id", async (req, res) => { 23 | const collection = await db.collection('pokemon'); 24 | 25 | const query = { _id: new ObjectId(req.params.id) }; 26 | 27 | const result = await collection.findOne(query); 28 | 29 | if (!result) res.send("Not found").status(404); 30 | else res.send(result).status(200); 31 | }); 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | export default router; -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import './loadEnv.js'; 2 | import express from 'express'; 3 | import cors from 'cors'; 4 | import morgan from 'morgan'; 5 | import usersRouter from './routes/users.js' 6 | 7 | const app = express() 8 | const PORT = 3000; 9 | 10 | 11 | // middlewares 12 | app.use(cors()); // allows frontend to connect to backend 13 | app.use(morgan('dev')); // logger 14 | app.use(express.json());// for data in req.body 15 | app.use(express.urlencoded({extended: true})); // allow data in url string 16 | 17 | app.use('/api/users', usersRouter); 18 | // app.use('/api/users', usersRouter); 19 | 20 | app.get('/', (req, res) => { 21 | res.send('backend...') 22 | }); 23 | 24 | // global error handling 25 | app.use((err, _req, res, next) => { 26 | res.status(500).send("Seems like we messed up somewhere..."); 27 | }); 28 | 29 | app.listen(PORT, () => { 30 | console.log(`Server running on port: ${PORT}`); 31 | }); 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /db/models/pokemon.js: -------------------------------------------------------------------------------- 1 | // import mongoose from 'mongoose'; 2 | // import bcrypt from 'bcrypt'; 3 | 4 | // const SALT_ROUNDS = 8; 5 | 6 | // const PokemonSchema = new mongoose.Schema({ 7 | // name: { 8 | // type: String, 9 | // required: true, 10 | // minLength: 3, 11 | // maxLenght: 18, 12 | // }, 13 | // type: { 14 | // type: String, 15 | // required: true, 16 | // unique: true, 17 | // }, 18 | // gender: { 19 | // type: String, 20 | // minLength: 1, 21 | // maxLength: 8, 22 | // required: true, 23 | // }, 24 | // quanity: { 25 | // type: Number, 26 | // min: 1, 27 | // max: 20000, 28 | // }, 29 | // cry: { 30 | // type: Boolean, 31 | // defult: false 32 | // }, 33 | // location: { 34 | // type: String, 35 | // enum: ["battle area", "Route 5", "Unknown", "Area1", "Vern Forest"] 36 | // }, 37 | // }, { 38 | // timestamps: true, 39 | // toJSON: { 40 | // transform: function (doc, retDoc) { 41 | // delete retDoc.password; // removes password from the json doc 42 | // return retDoc; 43 | 44 | // } 45 | 46 | // } 47 | // }); 48 | // PokemonSchema.index({ name: 1 }); 49 | // PokemonSchema.index({ quanity: 1 }); 50 | 51 | 52 | 53 | // PokemonSchemaSchema.pre('save', async function (next) { 54 | // // if the password has not changed continue 55 | // if (!this.isModified("password")) return next(); 56 | 57 | // this.password = await bcrypt.hash(this.password, SALT_ROUNDS); 58 | // return next(); 59 | // }); 60 | 61 | 62 | // export default mongoose.model('pokemon', PokemonSchema); -------------------------------------------------------------------------------- /db/models/users.js: -------------------------------------------------------------------------------- 1 | import mongoose from 'mongoose'; 2 | import bcrypt from 'bcrypt'; 3 | 4 | const SALT_ROUNDS = 8; 5 | 6 | const usersSchema = new mongoose.Schema({ 7 | username: { 8 | type: String, 9 | required: true, 10 | minLength: 1, 11 | maxLenght: 12, 12 | }, 13 | email: { 14 | type: String, 15 | required: true, 16 | unique: true, 17 | }, 18 | password: { 19 | type: String, 20 | minLength: 8, 21 | maxLength: 14, 22 | required: true, 23 | }, 24 | age: { 25 | type: Number, 26 | min: 18, 27 | max: 200, 28 | }, 29 | admin: { 30 | type: Boolean, 31 | defult: false 32 | }, 33 | title: { 34 | type: String, 35 | enum: ["PokeRanger", "Lass", "Youngster", "PKMN Trainer", "Researcher","Biker","Artist","PokeBreeder","Couple","Explorer","Swimmer","Leader"] 36 | }, 37 | team: { type: String, required: true, min: 6 }, 38 | boxed: { type: Boolean, default: true}, 39 | createdAt: { type: Date, default: Date.now }, 40 | location: { type: String, enum: ["battle area", "Route 5", "Unknown", "Area1", "Vern Forest"]}, 41 | }, { 42 | timestamps: true, 43 | toJSON: { 44 | transform: function (doc, retDoc) { 45 | delete retDoc.password; // removes password from the json doc 46 | return retDoc; 47 | 48 | } 49 | 50 | } 51 | }); 52 | usersSchema.index({ email: 1 }); 53 | usersSchema.index({ username: 1 }); 54 | 55 | 56 | //Authentication 57 | usersSchema.pre('save', async function (next) { 58 | // if the password has not changed continue 59 | if (!this.isModified("password")) return next(); 60 | 61 | this.password = await bcrypt.hash(this.password, SALT_ROUNDS); 62 | return next(); 63 | }); 64 | 65 | 66 | export default mongoose.model('User', usersSchema); -------------------------------------------------------------------------------- /routes/users.js: -------------------------------------------------------------------------------- 1 | import { Router } from 'express'; 2 | import db from "../db/conn.js"; 3 | import { ObjectId } from "mongodb"; 4 | 5 | 6 | const router = new Router(); 7 | 8 | router.get('/', async (req, res) => { 9 | try { 10 | const collection = await db.collection("users"); 11 | 12 | const pokedex = await collection.find().toArray(); 13 | 14 | const topThreeHundred = pokedex.slice(0, 300); 15 | console.log(topThreeHundred) 16 | res.status(200).json(topThreeHundred); 17 | } catch (error) { 18 | console.log(error); 19 | } 20 | }); 21 | 22 | 23 | router.get("/:id", async (req, res) => { 24 | const collection = await db.collection('users'); 25 | 26 | const query = { _id: new ObjectId(req.params.id) }; 27 | 28 | const result = await collection.findOne(query); 29 | 30 | if (!result) res.send("Not found").status(404); 31 | else res.send(result).status(200); 32 | }); 33 | 34 | 35 | router.post('/', async (req, res) => { 36 | const collection = await db.collection('users'); 37 | const newUser = { 38 | email: req.body.email, 39 | password: req.body.password, 40 | username: req.body.username, 41 | team: [],// make an array in front end...... 42 | boxed: []// make an array in front end...... 43 | } 44 | console.log(newUser) 45 | const result = await collection.insertOne(newUser); 46 | res.send(result).status(204); 47 | }); 48 | router.put('/:id/add-team', async (req, res) => { 49 | let collection = await db.collection("users"); 50 | let query = { _id: new ObjectId(req.params.id) }; 51 | const result = collection.updateOne({ _id: query._id }, { $push: { team: req.body.pokemon } }) 52 | 53 | if (!result) res.send("Not found").status(404); 54 | else res.send(result).status(200); 55 | }); 56 | 57 | router.put('/:id/add-boxed', async (req, res) => { 58 | let collection = await db.collection("users"); 59 | let query = { _id: new ObjectId(req.params.id) }; 60 | collection.updateOne({ _id: query._id }, { $push: { boxed: req.body.pokemon } }) 61 | 62 | if (!result) res.send("Not found").status(404); 63 | else res.send(result).status(200); 64 | }); 65 | 66 | 67 | router.post('/signin', async (req, res) => { 68 | // check if user exist 69 | // check if password is a match 70 | // send the db user 71 | const user = { _id: '1', email: 'alex@gmail.com', userName: 'alex123' }; 72 | res.json(user); 73 | }); 74 | 75 | 76 | 77 | 78 | export default router; --------------------------------------------------------------------------------