-
Notifications
You must be signed in to change notification settings - Fork 0
Part two answer #2
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
base: master
Are you sure you want to change the base?
Changes from all commits
e7e2f3f
c01f9b7
6063543
ed460ce
860fc4c
d9e11ec
8da6bb6
088fc2c
332c07c
42bcd27
6077317
79e88a3
a98ad6d
ab12b18
39087fe
9da34e7
a4ae41b
249f674
58c27a2
ea24f5f
857089f
e581194
4a8469d
db68dad
788fc28
2834e6c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| DB_USERNAME='DB_USERNAME' | ||
| DB_PASSWORD='DB_PASSWORD' | ||
| DB_DATABASE='DB_DATABASE' | ||
| DB_HOST='DB_HOST' | ||
| DB_DIALECT='DB_DIALECT' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| module.exports = { | ||
| "env": { | ||
| "es6": true, | ||
| "node": true, | ||
| }, | ||
| "extends": "eslint:recommended", | ||
| "parserOptions": { | ||
| "ecmaVersion": 2018, | ||
| "sourceType": "module" | ||
| }, | ||
| "rules": { | ||
| "array-bracket-spacing": ["error", "never"], | ||
| "array-element-newline": ["error", "consistent"], | ||
| "arrow-spacing": ["error", { "before": true, "after": true }], | ||
| "block-spacing": "error", | ||
| "camelcase": 0, | ||
| "comma-spacing": ["error", { "before": false, "after": true }], | ||
| "computed-property-spacing": ["error", "never"], | ||
| "dot-location": ["error", "property"], | ||
| "eol-last": ["error", "always"], | ||
| "func-call-spacing": ["error", "never"], | ||
| "function-paren-newline": ["error", "never"], | ||
| "implicit-arrow-linebreak": ["error", "beside"], | ||
| "indent": ["error", 2, { "SwitchCase": 1 }], | ||
| "key-spacing": ["error", { "afterColon": true }], | ||
| "keyword-spacing": ["error", { "before": true, "after": true }], | ||
| "linebreak-style": ["error", "unix"], | ||
| "max-len": ["error", { "code": 120, "ignoreComments": true, "ignoreTrailingComments": true }], | ||
| "no-console": "error", | ||
| "no-case-declarations": 0, | ||
| "no-mixed-spaces-and-tabs": "error", | ||
| "no-multi-spaces": "error", | ||
| "no-spaced-func": "error", | ||
| "no-trailing-spaces": ["error", { "ignoreComments": true }], | ||
| "no-whitespace-before-property": "error", | ||
| "nonblock-statement-body-position": ["error", "beside"], | ||
| "object-curly-newline": ["error", { "multiline": true, "minProperties": 5, "consistent": true }], | ||
| "object-curly-spacing": ["error", "always", { "arraysInObjects": true, "objectsInObjects": true }], | ||
| "object-property-newline": ["error", { "allowAllPropertiesOnSameLine": true }], | ||
| "one-var": ["error", "never"], | ||
| "operator-linebreak": ["error", "after", { "overrides": { "?": "before", ":": "before" } }], | ||
| "padded-blocks": ["error", "never"], | ||
| "padding-line-between-statements": [ | ||
| "error", | ||
| { blankLine: "always", prev: "*", next: "return" }, | ||
| { blankLine: "always", prev: ["const", "let", "var"], next: "*" }, | ||
| { blankLine: "any", prev: ["const", "let", "var"], next: ["const", "let", "var"] }, | ||
| ], | ||
| "quotes": ["error", "single"], | ||
| "rest-spread-spacing": ["error", "never"], | ||
| "space-before-blocks": "error", | ||
| "space-before-function-paren": ["error", { "anonymous": "always", "named": "never", "asyncArrow": "always" }], | ||
| "space-in-parens": ["error", "never"], | ||
| "space-infix-ops": "error", | ||
| "space-unary-ops": "error", | ||
| "spaced-comment": ["error", "always"], | ||
| "semi": ["error", "never"], | ||
| "semi-spacing": ["error", { "before": false, "after": true }], | ||
| "switch-colon-spacing": "error", | ||
| "template-tag-spacing": ["error", "always"], | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,3 @@ | ||
| node_modules | ||
| setup.sql | ||
| .env |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| module.exports = { | ||
| disallowAttributeConcatenation: true, | ||
| disallowBlockExpansion: true, | ||
| disallowClassLiterals: true, | ||
| disallowDuplicateAttributes: true, | ||
| disallowHtmlText: true, | ||
| disallowIdLiterals: true, | ||
| disallowLegacyMixinCall: true, | ||
| disallowMultipleLineBreaks: true, | ||
| disallowSpacesInsideAttributeBrackets: true, | ||
| disallowStringConcatenation: 'aggressive', | ||
| disallowTagInterpolation: true, | ||
| disallowTrailingSpaces: true, | ||
| requireLineFeedAtFileEnd: true, | ||
| requireLowerCaseAttributes: true, | ||
| requireLowerCaseTags: true, | ||
| requireSpaceAfterCodeOperator: true, | ||
| requireStrictEqualityOperators: true, | ||
| validateAttributeQuoteMarks: '"', | ||
| validateExtensions: true, | ||
| validateIndentation: 2, | ||
| validateLineBreaks: 'LF', | ||
| validateTemplateString: true | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| module.exports = { | ||
| development: { | ||
| username: process.env.DB_USERNAME, | ||
| password: process.env.DB_PASSWORD, | ||
| database: process.env.DB_DATABASE, | ||
| host: process.env.DB_HOST, | ||
| dialect: process.env.DB_DIALECT, | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| const models = require('../models') | ||
|
|
||
| const getAllForms = async (request, response) => { | ||
| try { | ||
| const allForms = await models.Forms.findAll() | ||
|
|
||
| return response.send(allForms) | ||
| } catch (error) { | ||
| return response.status(500).send('Unable to retrieve all the forms, please try again.') | ||
| } | ||
| } | ||
|
|
||
| const getFormById = async (request, response) => { | ||
| try { | ||
| const { id } = request.params | ||
| const Form = await models.Forms.findOne({ | ||
| where: { name: id }, | ||
| include: [{ model: models.Pokemons }] | ||
| }) | ||
|
|
||
| return Form | ||
| ? response.status(200).send(Form) | ||
| : response.status(404).send(`It appears there is no form called '${id}' does not exist.`) | ||
| } catch (error) { | ||
| return response.status(500).send('Unable to retrieve the form, please try again.') | ||
| } | ||
| } | ||
|
|
||
| module.exports = { getAllForms, getFormById } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| const models = require('../models') | ||
|
|
||
| const getAllPokemon = async (request, response) => { | ||
| try { | ||
| const allPokemon = await models.Pokemons.findAll() | ||
|
|
||
| return response.send(allPokemon) | ||
| } catch (error) { | ||
| return response.status(500).send('Could not retrieve all pokemon, please try again.') | ||
| } | ||
| } | ||
|
|
||
| const getPokemonById = async (request, response) => { | ||
| try { | ||
| const { id } = request.params | ||
| const pokemon = await models.Pokemons.findOne({ | ||
| where: { name: id }, | ||
| include: [ | ||
| { model: models.Forms }, | ||
| { model: models.Types } | ||
| ] | ||
| }) | ||
|
|
||
| return pokemon | ||
| ? response.status(200).send(pokemon) | ||
| : response.status(404).send(`Could not find the pokemon ${id}.`) | ||
| } catch (error) { | ||
| return response.status(500).send('Could not retrieve the pokemon, please try again.') | ||
| } | ||
| } | ||
|
|
||
| const getPokemonByGenerationId = async (request, response) => { | ||
| try { | ||
| const { id } = request.params | ||
| const allPokemonInGeneration = await models.Pokemons.findAll({ where: { generationNumber: id } }) | ||
|
|
||
| return allPokemonInGeneration.length | ||
| ? response.send(allPokemonInGeneration) | ||
| : response.status(404).send(`No generation ${id} pokemon currently exists.`) | ||
| } catch (error) { | ||
| return response.status(500).send('Could not retrieve the pokemon by the sepcified generation, please try again.') | ||
| } | ||
| } | ||
|
|
||
| const saveNewPokemon = async (request, response) => { | ||
| try { | ||
| const { | ||
| name, | ||
| generationNumber, | ||
| types | ||
| } = request.body | ||
| const isProtected = request.body.isProtected || 0 | ||
| const fromId = request.body.fromId.toLowerCase() === 'null' | ||
| ? null | ||
| : request.body.fromId | ||
|
|
||
|
|
||
| if (!name || !generationNumber) { | ||
| return response | ||
| .status(400) | ||
| .send('At least one of the following attributes is missing: pokedexNumber, name, or generationNumber') | ||
| } | ||
|
|
||
| const [savedPokemon, created] = await models.Pokemons.findOrCreate({ | ||
| where: { name }, | ||
| defaults: { generationNumber, fromId, isProtected } | ||
| }) | ||
|
|
||
| Promise.resolve(savedPokemon) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is unnecessary, you awaited the database call, savedPokemon is already resolved. |
||
|
|
||
| if (created) { | ||
| const promisedTypesId = types.map(async typeName => { | ||
| const [type] = await models.Types.findOrCreate({ where: { name: typeName } }) | ||
|
|
||
| return type.id | ||
| }) | ||
|
|
||
|
|
||
| const typesId = await Promise.all(promisedTypesId) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need a separate variable here. Promises resolve in place. const typesId = types.map(async typeName => {
const [type] = await models.Types.findOrCreate({ where: { name: typeName } })
return type.id
})
await Promise.all(typesId) |
||
| const { pokedexNumber } = savedPokemon | ||
|
|
||
| typesId.map(async (typeId) => { | ||
| const promisedPokemonType = await models.PokemonTypes.findOrCreate({ | ||
| where: { PokemonPokedexNumber: pokedexNumber }, | ||
| defaults: { TypeId: typeId } | ||
| }) | ||
|
|
||
| const pokemonType = Promise.resolve(promisedPokemonType) | ||
|
|
||
| return pokemonType | ||
| }) | ||
| } | ||
|
|
||
|
|
||
| return response.status(201).send(savedPokemon) | ||
| } catch (error) { | ||
| return response.status(500).send('Could not reach the database, please try again.') | ||
| } | ||
| } | ||
|
|
||
| const deletePokemon = async (request, response) => { | ||
| try { | ||
| const { name } = request.params | ||
|
|
||
| const pokemon = await models.Pokemons.findOne({ where: { name } }) | ||
|
|
||
| if (!pokemon) return response.status(404).send(`No pokemon matching the name: ${name}`) | ||
|
|
||
| if (pokemon.isProtected) return response.status(409).send('Cannot delete protected pokemon') | ||
|
|
||
| await models.PokemonTypes.destroy({ where: { PokemonPokedexNumber: pokemon.pokedexNumber } }) | ||
| await models.Pokemons.destroy({ where: { name } }) | ||
|
|
||
| return response.send(`Successfully deleted the pokemon: ${name}.`) | ||
| } catch (error) { | ||
| return response.status(500).send('Unknown error while deleting pokemon, please try again.') | ||
| } | ||
| } | ||
|
|
||
| module.exports = { | ||
| getAllPokemon, | ||
| getPokemonById, | ||
| getPokemonByGenerationId, | ||
| saveNewPokemon, | ||
| deletePokemon | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| const models = require('../models') | ||
|
|
||
| const getAllTypes = async (request, response) => { | ||
| try { | ||
| const allTypes = await models.Types.findAll() | ||
|
|
||
| return response.send(allTypes) | ||
| } catch (error) { | ||
| return response.status(500).send('Could not retrieve all the types, please try again.') | ||
| } | ||
| } | ||
|
|
||
| const getTypeById = async (request, response) => { | ||
| try { | ||
| const { id } = request.params | ||
| const type = await models.Types.findOne({ where: { name: id }, include: [{ model: models.Pokemons }] }) | ||
|
|
||
| return type | ||
| ? response.status(200).send(type) | ||
| : response.status(404).send(`Could not find the type: ${id}.`) | ||
| } catch (error) { | ||
| return response.status(500).send('Could not retrieve the specified type, please try again.') | ||
| } | ||
| } | ||
|
|
||
| module.exports = { getAllTypes, getTypeById } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| const express = require('express') | ||
| const bodyParser = require('body-parser') | ||
| const { | ||
| getAllPokemon, | ||
| getPokemonById, | ||
| getPokemonByGenerationId, | ||
| saveNewPokemon, | ||
| deletePokemon | ||
| } = require('./controllers/Pokemons') | ||
| const { getAllTypes, getTypeById } = require('./controllers/Types') | ||
| const { getAllForms, getFormById } = require('./controllers/Forms') | ||
|
|
||
| const app = express() | ||
|
|
||
| app.set('view engine', 'pug') | ||
| app.use(express.static('public')) | ||
|
|
||
| app.get('/', (request, response) => { | ||
| return response.status(200).render('index') | ||
| }) | ||
|
|
||
| app.get('/pokemon', getAllPokemon) | ||
| app.post('/pokemon', bodyParser.json(), saveNewPokemon) | ||
| app.get('/pokemon/:id', getPokemonById) | ||
| app.delete('/pokemon/:name', deletePokemon) | ||
|
|
||
| app.get('/generation/:id', getPokemonByGenerationId) | ||
|
|
||
| app.get('/types', getAllTypes) | ||
| app.get('/types/:id', getTypeById) | ||
|
|
||
| app.get('/forms', getAllForms) | ||
| app.get('/forms/:id', getFormById) | ||
|
|
||
| app.all('*', (request, response) => { | ||
| return response.status(404).send('Looks like you found nothing in the pokedex.') | ||
| }) | ||
|
|
||
| app.listen(1337, () => { | ||
| console.log('Listening on 1337...')// eslint-disable-line no-console | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to clean up the fromId, you can't insert the string
'null'because it is a string not null.