Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.vscode
part3/practice-app/node_modules
part3/phonebook-backend/node_modules
123 changes: 123 additions & 0 deletions part3/phonebook-backend/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
const express = require('express')
const morgan = require('morgan')
const app = express()

morgan.token('content', function (req, res) {
if(req.method == 'POST')
{
return JSON.stringify(req.body)
}
else
{
return ""
}
})

app.use(express.json())
app.use(morgan(':method :url :status :res[content-length] - :response-time ms conent- :content'))

const ids = new Set([1, 2, 3, 4])

let persons = [
{
"id": 1,
"name": "Arto Hellas",
"number": "040-123456"
},
{
"id": 2,
"name": "Ada Lovelace",
"number": "39-44-5323523"
},
{
"id": 3,
"name": "Dan Abramov",
"number": "12-43-234345"
},
{
"id": 4,
"name": "Mary Poppendieck",
"number": "39-23-6423122"
}
]

app.get('/', (request, response) => {
response.send('<h1>Hello World</h1>')
})

app.get('/api/persons', (request, response) => {
response.json(persons)
})

app.get('/info', (request, response) => {
const time = new Date()
response.send(`<p>Phonebook has info for ${persons.length} people </br> ${time}<p>`)
})

app.get('/api/persons/:id', (request, response) => {
const id = Number(request.params.id)
const person = persons.find(person => person.id === id)
if (person){
response.json(person)
} else {
response.status(404).end()
}
})

app.delete('/api/persons/:id', (request, response) => {
const id = Number(request.params.id)
persons = persons.filter(person => person.id !== id)

response.status(204).end()
})

const generateId = () => {
let id = Math.round( Math.random() * 1000000 )
while (ids.has(id))
{
id = Math.round( Math.random() * 1000000 )
}
ids.add(id)
return id
}

const checkDuplicate = (name) => {
if(persons.find(person => person.name.trim().toLowerCase() === name.trim().toLowerCase()))
{
return true
}
return false
}

app.post('/api/persons', (request, response) => {
const body = request.body

if(!body.name){
return response.status(400).json({
error: "missing name"
})
} else if(!body.number) {
return response.status(400).json({
error: "missing number"
})
} else if (checkDuplicate(body.name)){
return response.status(400).json({
error: "Duplicate name"
})
}

const person = {
id : generateId(),
name : body.name,
number : body.number
}

persons = persons.concat(person)

response.json(person)
})

const PORT = 3001
app.listen(PORT , ()=>{
console.log('Server Running on port: ', PORT);
})
20 changes: 20 additions & 0 deletions part3/phonebook-backend/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "phonebook-backend",
"version": "1.0.0",
"description": "FSO part 3 project",
"main": "index.js",
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Aditya Singh",
"license": "MIT",
"dependencies": {
"express": "^4.19.2",
"morgan": "^1.10.0"
},
"devDependencies": {
"nodemon": "^3.1.2"
}
}
1 change: 1 addition & 0 deletions part3/phonebook-backend/requests/delete_contact.rest
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DELETE http://localhost:3001/api/persons/2 HTTP/1.1
1 change: 1 addition & 0 deletions part3/phonebook-backend/requests/get_all_contacts.rest
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GET http://localhost:3001/api/persons HTTP/1.1
16 changes: 16 additions & 0 deletions part3/phonebook-backend/requests/post_contact.rest
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
POST http://localhost:3001/api/persons
Content-Type: application/json

{
"name": "you",
"number": "12345"
}

###
POST http://localhost:3001/api/persons
Content-Type: application/json

{
"name": "name hai na bc",
"number": "qwefwfwfw"
}
9 changes: 9 additions & 0 deletions part3/phonebook-backend/whale
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
docker run \
-u "$(id -u):$(id -g)" \
-v $(pwd):/work \
-w /work \
-it \
-p 5173:5173 \
-p 3001:3001 \
node:20.11.0 $@

Loading