Skip to content

Commit 1da1ef5

Browse files
author
VillePihlava
committed
Osa 3 valmis
1 parent 5418737 commit 1da1ef5

File tree

7 files changed

+935
-104
lines changed

7 files changed

+935
-104
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build

.eslintrc.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module.exports = {
2+
'env': {
3+
'node': true,
4+
'commonjs': true,
5+
'es2021': true
6+
},
7+
'extends': 'eslint:recommended',
8+
'parserOptions': {
9+
'ecmaVersion': 12
10+
},
11+
'rules': {
12+
'indent': [
13+
'error',
14+
2
15+
],
16+
'linebreak-style': [
17+
'error',
18+
'unix'
19+
],
20+
'quotes': [
21+
'error',
22+
'single'
23+
],
24+
'semi': [
25+
'error',
26+
'never'
27+
],
28+
'eqeqeq': 'error',
29+
'no-trailing-spaces': 'error',
30+
'object-curly-spacing': [
31+
'error', 'always'
32+
],
33+
'arrow-spacing': [
34+
'error', { 'before': true, 'after': true }
35+
],
36+
'no-console': 0,
37+
}
38+
}

index.js

Lines changed: 64 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ const morgan = require('morgan')
55
const app = express()
66
const Person = require('./models/person')
77

8-
morgan.token('data', function (req, res) {
9-
const result = JSON.stringify(req.body)
10-
if (result !== '{}') {
11-
return result
12-
} else {
13-
return ''
14-
}
8+
morgan.token('data', function (request) {
9+
const result = JSON.stringify(request.body)
10+
if (result !== '{}') {
11+
return result
12+
} else {
13+
return ''
14+
}
1515
})
1616

1717
app.use(express.static('build'))
@@ -42,96 +42,96 @@ app.use(morgan(':method :url :status :res[content-length] - :response-time ms :d
4242
] */
4343

4444
app.get('/api/persons', (request, response) => {
45-
Person
46-
.find({})
47-
.then(persons => {
48-
response.json(persons.map(person => person.toJSON()))
49-
})
45+
Person
46+
.find({})
47+
.then(persons => {
48+
response.json(persons.map(person => person.toJSON()))
49+
})
5050
})
5151

5252
app.get('/info', (request, response) => {
53-
Person
54-
.find({})
55-
.then(persons => {
56-
const date = new Date()
57-
response.send(`<p>Phonebook has info for ${persons.length} people</p><p>${date}</p>`)
58-
})
53+
Person
54+
.find({})
55+
.then(persons => {
56+
const date = new Date()
57+
response.send(`<p>Phonebook has info for ${persons.length} people</p><p>${date}</p>`)
58+
})
5959
})
6060

6161
app.get('/api/persons/:id', (request, response, next) => {
62-
Person
63-
.findById(request.params.id)
64-
.then(person => {
65-
if (person) {
66-
response.json(person.toJSON())
67-
} else {
68-
response.status(404).end()
69-
}
70-
})
71-
.catch(error => next(error))
62+
Person
63+
.findById(request.params.id)
64+
.then(person => {
65+
if (person) {
66+
response.json(person.toJSON())
67+
} else {
68+
response.status(404).end()
69+
}
70+
})
71+
.catch(error => next(error))
7272
})
7373

7474
app.delete('/api/persons/:id', (request, response, next) => {
75-
Person
76-
.findByIdAndRemove(request.params.id)
77-
.then(result => {
78-
response.status(204).end()
79-
})
80-
.catch(error => next(error))
75+
Person
76+
.findByIdAndRemove(request.params.id)
77+
.then(() => {
78+
response.status(204).end()
79+
})
80+
.catch(error => next(error))
8181
})
8282

8383
app.post('/api/persons', (request, response, next) => {
84-
const body = request.body
84+
const body = request.body
8585

86-
const person = new Person({
87-
name: body.name,
88-
number: body.number,
89-
})
86+
const person = new Person({
87+
name: body.name,
88+
number: body.number,
89+
})
9090

91-
person
92-
.save()
93-
.then(savedPerson => {
94-
response.json(savedPerson.toJSON())
95-
})
96-
.catch(error => next(error))
91+
person
92+
.save()
93+
.then(savedPerson => {
94+
response.json(savedPerson.toJSON())
95+
})
96+
.catch(error => next(error))
9797
})
9898

9999
app.put('/api/persons/:id', (request, response, next) => {
100-
const body = request.body
100+
const body = request.body
101101

102-
const person = {
103-
name: body.name,
104-
number: body.number
105-
}
102+
const person = {
103+
name: body.name,
104+
number: body.number
105+
}
106106

107-
Person.findByIdAndUpdate(request.params.id, person, { new: true })
108-
.then(updatedPerson => {
109-
response.json(updatedPerson.toJSON())
110-
})
111-
.catch(error => next(error))
107+
Person.findByIdAndUpdate(request.params.id, person, { new: true })
108+
.then(updatedPerson => {
109+
response.json(updatedPerson.toJSON())
110+
})
111+
.catch(error => next(error))
112112
})
113113

114114
const unknownEndpoint = (request, response) => {
115-
response.status(404).send({ error: 'unknown endpoint' })
115+
response.status(404).send({ error: 'unknown endpoint' })
116116
}
117117

118118
app.use(unknownEndpoint)
119119

120120
const errorHandler = (error, request, response, next) => {
121-
console.error(error.message)
121+
console.error(error.message)
122122

123-
if (error.name === 'CastError') {
124-
return response.status(400).send({ error: 'malformatted id' })
125-
} else if (error.name === 'ValidationError') {
126-
return response.status(400).json({ error: error.message })
127-
}
123+
if (error.name === 'CastError') {
124+
return response.status(400).send({ error: 'malformatted id' })
125+
} else if (error.name === 'ValidationError') {
126+
return response.status(400).json({ error: error.message })
127+
}
128128

129-
next(error)
129+
next(error)
130130
}
131131

132132
app.use(errorHandler)
133133

134134
const PORT = process.env.PORT
135135
app.listen(PORT, () => {
136-
console.log(`Server running on port ${PORT}`)
136+
console.log(`Server running on port ${PORT}`)
137137
})

models/person.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,26 @@ const url = process.env.MONGODB_URI
55

66
console.log('connecting to', url)
77
mongoose.connect(url, { useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false, useCreateIndex: true })
8-
.then(result => {
9-
console.log('connected to MongoDB')
10-
})
11-
.catch((error) => {
12-
console.log('error connecting to MongoDB:', error.message)
13-
})
8+
.then(() => {
9+
console.log('connected to MongoDB')
10+
})
11+
.catch((error) => {
12+
console.log('error connecting to MongoDB:', error.message)
13+
})
1414

1515
const personSchema = new mongoose.Schema({
16-
name: { type: String, required: true, unique: true, minlength: 3 },
17-
number: { type: String, required: true, minlength: 8 }
16+
name: { type: String, required: true, unique: true, minlength: 3 },
17+
number: { type: String, required: true, minlength: 8 }
1818
})
1919

2020
personSchema.plugin(uniqueValidator)
2121

2222
personSchema.set('toJSON', {
23-
transform: (document, returnedObject) => {
24-
returnedObject.id = returnedObject._id.toString()
25-
delete returnedObject._id
26-
delete returnedObject.__v
27-
}
23+
transform: (document, returnedObject) => {
24+
returnedObject.id = returnedObject._id.toString()
25+
delete returnedObject._id
26+
delete returnedObject.__v
27+
}
2828
})
2929

3030
module.exports = mongoose.model('Person', personSchema)

mongo.js

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
const { response } = require('express')
21
const mongoose = require('mongoose')
32

43
if (process.argv.length < 3) {
5-
console.log('give password as argument')
6-
process.exit(1)
4+
console.log('give password as argument')
5+
process.exit(1)
76
}
87

98
const password = process.argv[2]
@@ -13,33 +12,33 @@ const url = `mongodb+srv://fullstackopen:${password}@cluster0.cj64b.mongodb.net/
1312
mongoose.connect(url, { useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false, useCreateIndex: true })
1413

1514
const personSchema = new mongoose.Schema({
16-
name: String,
17-
number: String
15+
name: String,
16+
number: String
1817
})
1918

2019
const Person = mongoose.model('Person', personSchema)
2120

22-
if (process.argv.length == 5) {
23-
const person = new Person({
24-
name: process.argv[3],
25-
number: process.argv[4]
21+
if (process.argv.length === 5) {
22+
const person = new Person({
23+
name: process.argv[3],
24+
number: process.argv[4]
25+
})
26+
person
27+
.save()
28+
.then(() => {
29+
console.log(`added ${person.name} number ${person.number} to phonebook`)
30+
mongoose.connection.close()
31+
})
32+
} else if (process.argv.length === 3) {
33+
Person
34+
.find({})
35+
.then(persons => {
36+
console.log('phonebook:')
37+
persons.forEach(person => {
38+
console.log(`${person.name} ${person.number}`)
39+
})
40+
mongoose.connection.close()
2641
})
27-
person
28-
.save()
29-
.then(response => {
30-
console.log(`added ${person.name} number ${person.number} to phonebook`)
31-
mongoose.connection.close()
32-
})
33-
} else if (process.argv.length == 3) {
34-
Person
35-
.find({})
36-
.then(persons => {
37-
console.log('phonebook:')
38-
persons.forEach(person => {
39-
console.log(`${person.name} ${person.number}`)
40-
})
41-
mongoose.connection.close()
42-
})
4342
} else {
44-
process.exit(1)
43+
process.exit(1)
4544
}

0 commit comments

Comments
 (0)