diff --git a/README.md b/README.md index df53d8a..10a7e8c 100644 --- a/README.md +++ b/README.md @@ -20,9 +20,9 @@ Make sure that you have already created your own **organisation** with your team Edit this file by filling your team composition here: -- [ ] _Student full name_ | _Github username_ | **Scrum Master** -- [ ] _Student full name_ | _Gihub username_ -- [ ] _Student full name_ | _Gihub username_ +- [ ] Eya Chafter | Eya26 | +- [ ] Chaima Rajah | chaimarajah +- [ ] Marouene Addhoum | MaroueneA ## Technologies diff --git a/poke-mongo/client/.gitignore b/poke-mongo/client/.gitignore new file mode 100644 index 0000000..4d29575 --- /dev/null +++ b/poke-mongo/client/.gitignore @@ -0,0 +1,23 @@ +# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. + +# dependencies +/node_modules +/.pnp +.pnp.js + +# testing +/coverage + +# production +/build + +# misc +.DS_Store +.env.local +.env.development.local +.env.test.local +.env.production.local + +npm-debug.log* +yarn-debug.log* +yarn-error.log* diff --git a/poke-mongo/client/README.md b/poke-mongo/client/README.md new file mode 100644 index 0000000..58beeac --- /dev/null +++ b/poke-mongo/client/README.md @@ -0,0 +1,70 @@ +# Getting Started with Create React App + +This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). + +## Available Scripts + +In the project directory, you can run: + +### `npm start` + +Runs the app in the development mode.\ +Open [http://localhost:3000](http://localhost:3000) to view it in your browser. + +The page will reload when you make changes.\ +You may also see any lint errors in the console. + +### `npm test` + +Launches the test runner in the interactive watch mode.\ +See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. + +### `npm run build` + +Builds the app for production to the `build` folder.\ +It correctly bundles React in production mode and optimizes the build for the best performance. + +The build is minified and the filenames include the hashes.\ +Your app is ready to be deployed! + +See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. + +### `npm run eject` + +**Note: this is a one-way operation. Once you `eject`, you can't go back!** + +If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. + +Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own. + +You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it. + +## Learn More + +You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). + +To learn React, check out the [React documentation](https://reactjs.org/). + +### Code Splitting + +This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting) + +### Analyzing the Bundle Size + +This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size) + +### Making a Progressive Web App + +This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app) + +### Advanced Configuration + +This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration) + +### Deployment + +This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment) + +### `npm run build` fails to minify + +This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify) diff --git a/poke-mongo/client/package.json b/poke-mongo/client/package.json new file mode 100644 index 0000000..49d457f --- /dev/null +++ b/poke-mongo/client/package.json @@ -0,0 +1,42 @@ +{ + "name": "client", + "version": "0.1.0", + "private": true, + "dependencies": { + "@testing-library/jest-dom": "^5.16.1", + "@testing-library/react": "^12.1.2", + "@testing-library/user-event": "^13.5.0", + "axios": "^0.24.0", + "react": "^17.0.2", + "react-dom": "^17.0.2", + "react-router-dom": "^6.2.1", + "react-scripts": "5.0.0", + "web-vitals": "^2.1.2", + "webpack": "^5.65.0" + }, + "scripts": { + "start": "react-scripts start", + "build": "react-scripts build", + "test": "react-scripts test", + "eject": "react-scripts eject" + }, + "proxy": "http://localhost:8000", + "eslintConfig": { + "extends": [ + "react-app", + "react-app/jest" + ] + }, + "browserslist": { + "production": [ + ">0.2%", + "not dead", + "not op_mini all" + ], + "development": [ + "last 1 chrome version", + "last 1 firefox version", + "last 1 safari version" + ] + } +} diff --git a/poke-mongo/client/public/favicon.ico b/poke-mongo/client/public/favicon.ico new file mode 100644 index 0000000..a11777c Binary files /dev/null and b/poke-mongo/client/public/favicon.ico differ diff --git a/poke-mongo/client/public/index.html b/poke-mongo/client/public/index.html new file mode 100644 index 0000000..aa069f2 --- /dev/null +++ b/poke-mongo/client/public/index.html @@ -0,0 +1,43 @@ + + + + + + + + + + + + + React App + + + +
+ + + diff --git a/poke-mongo/client/public/logo192.png b/poke-mongo/client/public/logo192.png new file mode 100644 index 0000000..fc44b0a Binary files /dev/null and b/poke-mongo/client/public/logo192.png differ diff --git a/poke-mongo/client/public/logo512.png b/poke-mongo/client/public/logo512.png new file mode 100644 index 0000000..a4e47a6 Binary files /dev/null and b/poke-mongo/client/public/logo512.png differ diff --git a/poke-mongo/client/public/manifest.json b/poke-mongo/client/public/manifest.json new file mode 100644 index 0000000..080d6c7 --- /dev/null +++ b/poke-mongo/client/public/manifest.json @@ -0,0 +1,25 @@ +{ + "short_name": "React App", + "name": "Create React App Sample", + "icons": [ + { + "src": "favicon.ico", + "sizes": "64x64 32x32 24x24 16x16", + "type": "image/x-icon" + }, + { + "src": "logo192.png", + "type": "image/png", + "sizes": "192x192" + }, + { + "src": "logo512.png", + "type": "image/png", + "sizes": "512x512" + } + ], + "start_url": ".", + "display": "standalone", + "theme_color": "#000000", + "background_color": "#ffffff" +} diff --git a/poke-mongo/client/public/robots.txt b/poke-mongo/client/public/robots.txt new file mode 100644 index 0000000..e9e57dc --- /dev/null +++ b/poke-mongo/client/public/robots.txt @@ -0,0 +1,3 @@ +# https://www.robotstxt.org/robotstxt.html +User-agent: * +Disallow: diff --git a/poke-mongo/client/src/App.css b/poke-mongo/client/src/App.css new file mode 100644 index 0000000..90c5ac9 --- /dev/null +++ b/poke-mongo/client/src/App.css @@ -0,0 +1,104 @@ +.App { + text-align: center; +} + +.App-logo { + height: 40vmin; + pointer-events: none; +} + +@media (prefers-reduced-motion: no-preference) { + .App-logo { + animation: App-logo-spin infinite 20s linear; + } +} + +.App-header { + background-color: #282c34; + min-height: 100vh; + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; + font-size: calc(10px + 2vmin); + color: white; +} + +.App-link { + color: #61dafb; +} + +@keyframes App-logo-spin { + from { + transform: rotate(0deg); + } + to { + transform: rotate(360deg); + } +} + +.title { + color: red; + text-align: center; + font-family: serif; + size: 600; +} + +.header{ + margin-left: 18%; + display: inline-block; +} +.myLogo { + align-content: center; + display: inline; + float: left; + margin-top: 10px; + margin-left: 30px; + margin-right: autos; + width: 50%; + content: url('https://o.remove.bg/downloads/164a26ba-41cd-4d7d-b500-8075dc058961/2-24425_pinoy-pokemon-go-pokemon-go-logo-transparent-removebg-preview.png'); +} +.user { + text-align: center; + white-space: nowrap; + display: inline; + float: left; + margin-top: 100px; + margin-right: 2px; + font-size: large; + color: #004478; + text-align: center; + font-family: serif; + font-size: 70px; +} + +.button { + background-color: #00559A; + font-weight: 200; + margin-top: 40px; + border: solid 2px; + padding: 10px; + font-size: 20px; + color: white; + width: 20%; + margin-left: 10%; + align-self: center; +} + +.button:hover { + background-color: green; + color: white; +} + +.Welcome { + background-image: url('https://png.pngtree.com/thumb_back/fw800/back_our/20190628/ourmid/pngtree-elegant-light-blue-line-background-image_272095.jpg'); + width:100%; + height:100%; + + box-sizing:border-box; + background-size:cover; + background-repeat:no-repeat; + background-position:center center; + +} + diff --git a/poke-mongo/client/src/App.js b/poke-mongo/client/src/App.js new file mode 100644 index 0000000..e919992 --- /dev/null +++ b/poke-mongo/client/src/App.js @@ -0,0 +1,39 @@ +import React, {useState} from 'react'; +import PokemonList from './components/PokemonList'; +import AddPokemon from './components/AddPokemon'; +import DeletePokemon from './components/DeletePokemon'; +import NavBar from './components/NavBar'; + +import './App.css'; + + + + +export default function App (){ + const [active,setActive] = useState("NavBar") + return ( +
+
+
+ +
+ {active === "NavBar" && } + {active === "List" && } + {active === "Add" && } + {active === "Delete" && } + +
+
+ + ) + +}; + + diff --git a/poke-mongo/client/src/App.test.js b/poke-mongo/client/src/App.test.js new file mode 100644 index 0000000..1f03afe --- /dev/null +++ b/poke-mongo/client/src/App.test.js @@ -0,0 +1,8 @@ +import { render, screen } from '@testing-library/react'; +import App from './App'; + +test('renders learn react link', () => { + render(); + const linkElement = screen.getByText(/learn react/i); + expect(linkElement).toBeInTheDocument(); +}); diff --git a/poke-mongo/client/src/components/AddPokemon.css b/poke-mongo/client/src/components/AddPokemon.css new file mode 100644 index 0000000..de4cbbd --- /dev/null +++ b/poke-mongo/client/src/components/AddPokemon.css @@ -0,0 +1,53 @@ +* { + font-family: 'Open Sans', sans-serif; +} +#submit-button { + background-color: #00559A; + font-weight: 200; + margin-top: 40px; + border: solid 1px; + padding: 10px; + font-size: 20px; + color: white; + width: 20%; + margin-left: 25%; + +} +#submit-button:hover{ + background-color: green; + color: white; + +} +#image { + margin-left: 25%; +} +.input { + display: flex; + font: bold; + margin-top: 10px; + margin-left:30px; + margin-right: 10px; + font-family: Zen Antique; + color: white; + font-size: 18px; + font-weight: bold; + +} + +label{ + width:5%; +} +#title { + font-family: Zen Antique; + color: navy; + margin-left: 25%; + size: 70px; + font: bold; + +} + +.Form { + border-color: blue; + box-sizing:border-box; + position: static; +} \ No newline at end of file diff --git a/poke-mongo/client/src/components/AddPokemon.js b/poke-mongo/client/src/components/AddPokemon.js new file mode 100644 index 0000000..06b1efa --- /dev/null +++ b/poke-mongo/client/src/components/AddPokemon.js @@ -0,0 +1,53 @@ +import React, { Component } from 'react'; +import react, {useState} from "react"; +import './AddPokemon.css'; +import axios from 'axios' + + +const AddPokemon = () => { + const [number, setNumber] = useState(""); + const [name, setName] = useState(""); + const [types, setTypes] = useState(""); + const [imageUrl, setImg] = useState(""); + + const submit = () => { + axios.post('./pokemon', { + number, + name, + types, + imageUrl + }).then(res => console.log(res)) + .catch(error =>console.log(error.message)) + } + + return ( +
+
+
+ + setNumber(e.target.value)} /> +
+
+ + setName(e.target.value)} /> +
+
+ + setTypes(e.target.value)} /> +
+
+ + setImg(e.target.value)} /> +
+ + + + +
+
+ ) + +} + + +export default AddPokemon \ No newline at end of file diff --git a/poke-mongo/client/src/components/DeletePokemon.js b/poke-mongo/client/src/components/DeletePokemon.js new file mode 100644 index 0000000..d0eaf52 --- /dev/null +++ b/poke-mongo/client/src/components/DeletePokemon.js @@ -0,0 +1,52 @@ +import React, { PureComponent } from 'react'; +import {useState, useEffect} from 'react' ; +import axios from 'axios' +class DeletePokemon extends React.Component { + constructor(){ + super(); + this.state={ + pokemons:[{number:0,name:"",types:[],imageUrl:""}] + } + } + componentDidMount(){ + fetch('./pokemon') + .then(res => res.json()) + .then(pokemons => this.setState({pokemons}, () => console.log('Pokemons fetched', pokemons))) + } + + Delete = () => { + axios.delete('./pokemon/:number', { + number: this.state.number, + name: this.state.name, + types: this.state.types, + imageUrl: this.state.imageUrl + + }) + .then(res => { + const pok = this.state.pokemons.filter(c => c.number !== res); + this.setState({ pok })}) + .catch(error =>console.log(error.message)) + } + + handleDelete = item => { + const Newpokemons = this.state.pokemons.filter(c => c.number !== item.number); + this.setState({ pokemons: Newpokemons }); + }; + render() { + return( +
+

List Of Pokemons

+ +
+ ); + } + } + + + + +export default DeletePokemon \ No newline at end of file diff --git a/poke-mongo/client/src/components/NavBar.css b/poke-mongo/client/src/components/NavBar.css new file mode 100644 index 0000000..c1870f5 --- /dev/null +++ b/poke-mongo/client/src/components/NavBar.css @@ -0,0 +1,10 @@ +.HomePage { + margin-top: 100px; + margin-right: 2px; + font-size: large; + color: #174E78; + text-align: center; + font-family: serif; + font-size: 65px; + font-weight: bold; +} \ No newline at end of file diff --git a/poke-mongo/client/src/components/NavBar.js b/poke-mongo/client/src/components/NavBar.js new file mode 100644 index 0000000..b0d6ab0 --- /dev/null +++ b/poke-mongo/client/src/components/NavBar.js @@ -0,0 +1,14 @@ +import React, { Component } from 'react'; +import './NavBar.css' +class NavBar extends React.Component { + render() { + return ( +
+ Welcome to PokemonGo game +
+ + ) + } +} + +export default NavBar ; \ No newline at end of file diff --git a/poke-mongo/client/src/components/PokemonList.css b/poke-mongo/client/src/components/PokemonList.css new file mode 100644 index 0000000..2257e64 --- /dev/null +++ b/poke-mongo/client/src/components/PokemonList.css @@ -0,0 +1,19 @@ +ul{ + min-width: 696px; + list-style: none; + padding-top: 20px; + } + li{ + display: inline; + } + +.titlePok{ + margin-top: 100px; + margin-right: 2px; + font-size: large; + color: #174E78; + text-align: center; + font-family: serif; + font-size: 50px; + font-weight: bold; +} \ No newline at end of file diff --git a/poke-mongo/client/src/components/PokemonList.js b/poke-mongo/client/src/components/PokemonList.js new file mode 100644 index 0000000..4787c12 --- /dev/null +++ b/poke-mongo/client/src/components/PokemonList.js @@ -0,0 +1,31 @@ +import React, { Component } from 'react'; +import './PokemonList.css' +class PokemonList extends React.Component { + constructor(){ + super(); + this.state={ + pokemons:[{number:0,name:"",types:[],imageUrl:""}] + } + } + + + componentDidMount(){ + fetch('./pokemon') + .then(res => res.json()) + .then(pokemons => this.setState({pokemons}, () => console.log('Pokemons fetched', pokemons))) + } + render() { + return( +
+

List Of Pokemons

+ +
+ ); + } + } + + +export default PokemonList; diff --git a/poke-mongo/client/src/index.css b/poke-mongo/client/src/index.css new file mode 100644 index 0000000..08d2b60 --- /dev/null +++ b/poke-mongo/client/src/index.css @@ -0,0 +1,16 @@ +body { + margin: 0; + font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', + 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', + sans-serif; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; +} + +code { + font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', + monospace; +} + + + diff --git a/poke-mongo/client/src/index.js b/poke-mongo/client/src/index.js new file mode 100644 index 0000000..ef2edf8 --- /dev/null +++ b/poke-mongo/client/src/index.js @@ -0,0 +1,17 @@ +import React from 'react'; +import ReactDOM from 'react-dom'; +import './index.css'; +import App from './App'; +import reportWebVitals from './reportWebVitals'; + +ReactDOM.render( + + + , + document.getElementById('root') +); + +// If you want to start measuring performance in your app, pass a function +// to log results (for example: reportWebVitals(console.log)) +// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals +reportWebVitals(); diff --git a/poke-mongo/client/src/logo.svg b/poke-mongo/client/src/logo.svg new file mode 100644 index 0000000..9dfc1c0 --- /dev/null +++ b/poke-mongo/client/src/logo.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/poke-mongo/client/src/reportWebVitals.js b/poke-mongo/client/src/reportWebVitals.js new file mode 100644 index 0000000..5253d3a --- /dev/null +++ b/poke-mongo/client/src/reportWebVitals.js @@ -0,0 +1,13 @@ +const reportWebVitals = onPerfEntry => { + if (onPerfEntry && onPerfEntry instanceof Function) { + import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => { + getCLS(onPerfEntry); + getFID(onPerfEntry); + getFCP(onPerfEntry); + getLCP(onPerfEntry); + getTTFB(onPerfEntry); + }); + } +}; + +export default reportWebVitals; diff --git a/poke-mongo/client/src/setupTests.js b/poke-mongo/client/src/setupTests.js new file mode 100644 index 0000000..8f2609b --- /dev/null +++ b/poke-mongo/client/src/setupTests.js @@ -0,0 +1,5 @@ +// jest-dom adds custom jest matchers for asserting on DOM nodes. +// allows you to do things like: +// expect(element).toHaveTextContent(/react/i) +// learn more: https://github.com/testing-library/jest-dom +import '@testing-library/jest-dom'; diff --git a/poke-mongo/data/2-24425_pinoy-pokemon-go-pokemon-go-logo-transparent-removebg-preview.png b/poke-mongo/data/2-24425_pinoy-pokemon-go-pokemon-go-logo-transparent-removebg-preview.png new file mode 100644 index 0000000..dda6170 Binary files /dev/null and b/poke-mongo/data/2-24425_pinoy-pokemon-go-pokemon-go-logo-transparent-removebg-preview.png differ diff --git a/poke-mongo/data/logo.png b/poke-mongo/data/logo.png new file mode 100644 index 0000000..2d4a904 Binary files /dev/null and b/poke-mongo/data/logo.png differ diff --git a/poke-mongo/data/pokemon.json b/poke-mongo/data/pokemon.json index 863c9ad..d3c3aec 100644 --- a/poke-mongo/data/pokemon.json +++ b/poke-mongo/data/pokemon.json @@ -905,4 +905,4 @@ "types": ["Psychic"], "imageUrl": "https://github.com/HybridShivam/Pokemon/blob/master/assets/images/151.png?raw=true" } - ] \ No newline at end of file + ] diff --git a/poke-mongo/package.json b/poke-mongo/package.json new file mode 100644 index 0000000..e0e8473 --- /dev/null +++ b/poke-mongo/package.json @@ -0,0 +1,28 @@ +{ + "name": "poke-mongo", + "version": "1.0.0", + "description": "You are going to build a Pokémon API with Node, Express, and MongoDB. Your API will allow users to perform CRUD operations on each of the 151 original Pokémon, and power a front-end.", + "main": "index.js", + "scripts": { + "start": "node index.js", + "start-dev": "nodemon index.js", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "body-parser": "^1.19.1", + "browserify-zlib": "^0.2.0", + "concurrently": "^6.5.1", + "express": "^4.17.2", + "jquery": "^3.6.0", + "mongoose": "^6.1.4", + "node-polyfill-webpack-plugin": "^1.1.4", + "path-browserify": "^1.0.1", + "util": "^0.12.4" + }, + "devDependencies": { + "nodemon": "^2.0.15", + "webpack": "^5.65.0" + } +} diff --git a/poke-mongo/server/PopulatingDatabse.js b/poke-mongo/server/PopulatingDatabse.js new file mode 100644 index 0000000..89bb8b5 --- /dev/null +++ b/poke-mongo/server/PopulatingDatabse.js @@ -0,0 +1,10 @@ +var request = require('request'); + +var PopulatingDatabse = function() { + request('../data/pokemon.json', function (error, response, body) { + console.log('error:', error); + console.log('statusCode:', response && response.statusCode); + console.log('body:', body); +}); +}; +PopulatingDatabse(); \ No newline at end of file diff --git a/poke-mongo/server/db/index.js b/poke-mongo/server/db/index.js index 433babf..16b1cb5 100644 --- a/poke-mongo/server/db/index.js +++ b/poke-mongo/server/db/index.js @@ -1,3 +1,6 @@ -/** - * Your Database Connection comes here - */ \ No newline at end of file +const mongoose = require("mongoose"); +const { db } = require("../resources/pokemon/Pokemon"); +mongoose.connect('mongodb://localhost:27017/Pokemon', +() =>{console.log("successfully connected to the DB")}) + +module.exports = db; diff --git a/poke-mongo/server/index.js b/poke-mongo/server/index.js index 66cd61e..30460ea 100644 --- a/poke-mongo/server/index.js +++ b/poke-mongo/server/index.js @@ -1,3 +1,23 @@ -/** - * Your server comes here - */ \ No newline at end of file + +const express = require("express"); +const app = express(); +const bodyParser= require("body-parser"); +const pokemonRouter = require("./resources/pokemon/pokemonRouter") + +const db = require("./db") +const mongoose = require("mongoose"); +const Pokemon = require("./resources/pokemon/Pokemon"); +const res = require("express/lib/response"); + +app.use('/pokemon', pokemonRouter) +//app.use('/api/pokemon/:number',pokemonRouter) +app.use(bodyParser()) + + + + + + +app.listen(8000, () => { + console.log("started listening for resquests on port 8000"); +}) diff --git a/poke-mongo/server/package.json b/poke-mongo/server/package.json new file mode 100644 index 0000000..144fdab --- /dev/null +++ b/poke-mongo/server/package.json @@ -0,0 +1,19 @@ +{ + "name": "server", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "start": "node index.js", + "start-dev": "nodemon index.js", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "express": "^4.17.2", + "n": "^8.0.1", + "nodemon": "^2.0.15", + "save": "^2.4.0" + } +} diff --git a/poke-mongo/server/resources/pokemon/Pokemon.js b/poke-mongo/server/resources/pokemon/Pokemon.js new file mode 100644 index 0000000..18b0535 --- /dev/null +++ b/poke-mongo/server/resources/pokemon/Pokemon.js @@ -0,0 +1,15 @@ +var mongoose= require("mongoose") +// connecting to db +/*const Pokemondata = require('./pokemon.json')*/ + +var Pokemon = mongoose.model("Pokemon", +{ + number: { type: Number, unique:true }, + name : { type: String, unique:true }, + types : [String], + imgUrl: String +}) +module.exports = Pokemon; + + + diff --git a/poke-mongo/server/resources/pokemon/pokemonController.js b/poke-mongo/server/resources/pokemon/pokemonController.js new file mode 100644 index 0000000..5f2b3cb --- /dev/null +++ b/poke-mongo/server/resources/pokemon/pokemonController.js @@ -0,0 +1,69 @@ +const { json } = require("body-parser"); +const { render } = require("express/lib/response"); +const Pokemon = require("./Pokemon"); + + + + + +exports.pokemon_get_all = (req,res) => { + Pokemon.find({}) + .then((pokemons) => { + res.status(200).json(pokemons) + }) +} + + +exports.pokemon_get_one = (req,res) => { + Pokemon.findOne({ 'number':req.params.number }) + .then((pokemons) => { + res.status(200).json(pokemons) + }) + }; + +exports.pokemon_post = (req,res) => { + console.log("the new pokemon is :",req.body); + const newPokemon = new Pokemon(req.body); + newPokemon.save().then((pokemon) =>{ + res.status(201).send(pokemon) + }) + .catch((error)=> { + res.status(500).send(error); + }) +}; + +exports.pokemon_put = (req,res) => { + + Pokemon.findOneAndUpdate({ number:req.params.number}, + { name:req.body.name, + types:req.body.types, + imageUrl:req.body.imageUrl + }) + .then(() => { + console.log("Saved!") + res.status(201).json(Pokemon); +}) + .catch((error) => { + console.log("error"); + res.send(error) +}) +} + +exports.pokemon_delete_one = (req,res) => { + Pokemon.where().findOneAndRemove({number:req.params.number},function (err, pokemon) { + if (err) return handleError(err); + console.log( pokemon); + }); + + res.send() +}; + +exports.pokemon_delete = (req,res) => { + Pokemon.deleteMany({},function (err, pokemon) { + if (err) return handleError(err); + + }); + + res.send() +}; + diff --git a/poke-mongo/server/resources/pokemon/pokemonRouter.js b/poke-mongo/server/resources/pokemon/pokemonRouter.js new file mode 100644 index 0000000..0ae1317 --- /dev/null +++ b/poke-mongo/server/resources/pokemon/pokemonRouter.js @@ -0,0 +1,22 @@ +const express=require("express"); +const pokemonRouter = express.Router() +const Pokemon = require("./Pokemon"); +pokemonRouter.use(express.json()) + +const pokemonController =require("./pokemonController") +const bodyParser= require("body-parser"); + +pokemonRouter.use(bodyParser()) +pokemonRouter.get("/",pokemonController.pokemon_get_all); + +pokemonRouter.get("/:number",pokemonController.pokemon_get_one); + +pokemonRouter.post("/", pokemonController.pokemon_post) + +pokemonRouter.put("/:number", pokemonController.pokemon_put); + + +pokemonRouter.delete("/:number",pokemonController.pokemon_delete_one); +pokemonRouter.delete("/",pokemonController.pokemon_delete); + +module.exports = pokemonRouter;