Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
e7e2f3f
Added the eslint and pug-lint rules to master
orbitalbitmap Apr 19, 2020
c01f9b7
Actually added the pug-lint rules.
orbitalbitmap Apr 19, 2020
6063543
Initial commit for branch part-one-answer.
orbitalbitmap Apr 19, 2020
ed460ce
Set up some of the documentatin site and some of the styling.
orbitalbitmap Apr 23, 2020
860fc4c
Added some more styling and added the functionality to show and hide …
orbitalbitmap Apr 28, 2020
d9e11ec
Split the index.pug into partials, added a javascript folder in publi…
orbitalbitmap Apr 29, 2020
8da6bb6
Adjusted styles.css to be sorted and adjusted some styling.
orbitalbitmap Apr 30, 2020
088fc2c
Took out some code that wasn't doing anything in styles.css.
orbitalbitmap Apr 30, 2020
332c07c
Minor styling changes.
orbitalbitmap Apr 30, 2020
42bcd27
Fixed some typos in /views/partials/rightSideocumentation.pug and fix…
orbitalbitmap May 2, 2020
6077317
Set up the test and the esfix scripts. Installed tevDependencies: moc…
orbitalbitmap May 13, 2020
79e88a3
Fixed some folder structure and added the draft ERD.
orbitalbitmap May 17, 2020
a98ad6d
The setup.sql file contains code to create the database, the tables, …
orbitalbitmap May 18, 2020
ab12b18
Added tests and then wrote code for accesssing the paths. Attempted t…
orbitalbitmap May 23, 2020
39087fe
Renamed AlternateForms to Forms to be simpler but also accurate. (Pos…
orbitalbitmap May 23, 2020
9da34e7
Added test for generation search and the request path for generation …
orbitalbitmap May 23, 2020
a4ae41b
More tests and paths coded.
orbitalbitmap May 24, 2020
249f674
Created migrations for creating the tables and for loading the tables…
orbitalbitmap May 24, 2020
58c27a2
Fixed coopy pastas in the describe and it desriptions.
orbitalbitmap May 25, 2020
ea24f5f
Changed the connection parameters to use environment variables instea…
orbitalbitmap May 25, 2020
857089f
Worked on the documentation page: restyled, corrected routes to be be…
orbitalbitmap May 25, 2020
e581194
Got a a little OCD over the higlightinf flag now lining up correctly.
orbitalbitmap May 25, 2020
4a8469d
Fixed a test to be ore specific, added data to migrations, fixed Poke…
orbitalbitmap May 28, 2020
db68dad
Updated the ERD image file.
orbitalbitmap May 28, 2020
788fc28
Fixed documentation discrepencies and fixed the fromId = 'null' issue.
orbitalbitmap Jun 11, 2020
2834e6c
Fixed two broken tests from the update on the previous commit.
orbitalbitmap Jun 11, 2020
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
5 changes: 5 additions & 0 deletions .env-sample
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'
62 changes: 62 additions & 0 deletions .eslintrc.js
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"],
}
};
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules
setup.sql
.env
24 changes: 24 additions & 0 deletions .pug-lintrc.js
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
}
Binary file added Pokemon-API-ERD.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions configs/sequelize.js
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,
}
}
29 changes: 29 additions & 0 deletions controllers/Forms.js
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 }
126 changes: 126 additions & 0 deletions controllers/Pokemons.js
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 }
Copy link
Collaborator

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.

image

})

Promise.resolve(savedPokemon)
Copy link
Collaborator

Choose a reason for hiding this comment

The 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)
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
}
26 changes: 26 additions & 0 deletions controllers/Types.js
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 }
41 changes: 41 additions & 0 deletions index.js
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
})
Loading