Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: edysegura/nodejs-restful
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.1.0
Choose a base ref
...
head repository: edysegura/nodejs-restful
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Loading
Showing with 3,158 additions and 434 deletions.
  1. +23 −0 .github/workflows/docker-github.yml
  2. +33 −0 .github/workflows/docker.yml
  3. +21 −0 .github/workflows/nodejs.yml
  4. +1 −1 Dockerfile
  5. +57 −0 app/TaskService.js
  6. +26 −39 app/taskApi.js
  7. +2,985 −385 package-lock.json
  8. +5 −4 package.json
  9. +6 −4 server.js
  10. +1 −1 test/Tasks.postman_collection.json
23 changes: 23 additions & 0 deletions .github/workflows/docker-github.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Publish Docker image
on:
push:
branches:
- 'master'
jobs:
push_to_registry:
name: Push Docker image to GitHub Packages
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
steps:
- name: Check out the repo
uses: actions/checkout@v2
- name: Push to GitHub Packages
uses: docker/build-push-action@v1
with:
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
registry: docker.pkg.github.com
repository: edysegura/nodejs-restful/node-restful
tag_with_ref: true
33 changes: 33 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: DockerHub

on:
push:
branches:
- 'master'

jobs:
docker:
runs-on: ubuntu-latest
steps:
-
name: Set up QEMU
uses: docker/setup-qemu-action@v1
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
-
name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Build and push
id: docker_build
uses: docker/build-push-action@v2
with:
push: true
tags: edysegura/node-restful:latest
-
name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }}
21 changes: 21 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Node CI

on: [push]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x, 14.x]
steps:
- uses: actions/checkout@v1
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: npm install, build, and test
run: |
npm ci --only=production
env:
CI: true
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:12
FROM node:lts-alpine

# Create app directory
WORKDIR /usr/src/app
57 changes: 57 additions & 0 deletions app/TaskService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const db = {}
let sequence = 0

class TaskService {

static add(newTask) {
return new Promise((resolve) => {
const task = {
id: ++sequence,
done: newTask.done || false,
description: newTask.description
}
db[task.id] = task
resolve(task)
})
}

static getAll() {
const toArray = key => db[key]
return new Promise((resolve) => {
const tasks = Object.keys(db).map(toArray)
resolve(tasks)
})
}

static getById(id) {
return new Promise((resolve) => {
resolve(db[id])
})
}

static update(taskId, updatedTask) {
return new Promise(async (resolve) => {
const task = await TaskService.getById(taskId)
if (task) {
const hasValue = updatedTask.done != null
task.done = hasValue ? updatedTask.done : task.done
task.description = updatedTask.description || task.description
resolve(task)
}
resolve(null)
})
}

static delete(id) {
return new Promise((resolve) => {
const task = db[id]
if (task) {
delete db[id]
resolve(true)
}
resolve(false)
})
}
}

module.exports = TaskService
65 changes: 26 additions & 39 deletions app/taskApi.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,49 @@
const express = require('express')
const router = express.Router()

let db = {}
let sequence = 0
const TaskService = require('./TaskService')

const notFound = response => {
response.status(404).send('Not found!')
}


router.get('/', (request, response) => {
const toArray = key => {
return db[key]
}
const tasks = Object.keys(db).map(toArray)
tasks.length
router.get('/', async (request, response) => {
const tasks = await TaskService.getAll()
tasks && tasks.length
? response.json(tasks)
: response.status(204).send('')
: response.status(204).end('')
})

router.get('/:id', (request, response) => {
const task = db[request.params.id]
router.get('/:id', async (request, response) => {
const taskId = request.params.id
const task = await TaskService.getById(taskId)
task
? response.json(task)
: notFound(response)
})

router.post('/', (request, response) => {
const newTask = {
'id': ++sequence,
'done': request.body.done || false,
'description': request.body.description
}

db[newTask.id] = newTask

response.status(201).json(newTask)
router.post('/', async (request, response) => {
const task = await TaskService.add(request.body)
response
.status(201)
.json(task)
})

router.put('/:id', (request, response) => {
const task = db[request.params.id]
if (task) {
task.done = request.body.done != null ? request.body.done : false
task.description = request.body.description || task.description
response.json(task)
} else {
notFound(response)
}
router.patch('/:id', async (request, response) => {
const updatedTask = await TaskService.update(
request.params.id,
request.body
);
updatedTask
? response.json(updatedTask)
: notFound(response);
})

router.delete('/:id', (request, response) => {
var task = db[request.params.id]
if (task) {
delete db[request.params.id]
response.send('Task deleted')
} else {
notFound(response)
}
router.delete('/:id', async (request, response) => {
const isDeleted = await TaskService.delete(request.params.id);
isDeleted
? response.end('Task deleted!')
: notFound(response)
})

module.exports = router
Loading