diff --git a/repositories/data.json b/repositories/data.json index a69f2c7..68e5d5b 100644 --- a/repositories/data.json +++ b/repositories/data.json @@ -89,15 +89,6 @@ "created_at": "2021-09-15T10:43:02.014Z", "updated_at": "2021-08-07T07:06:51.050Z" }, - { - "id": 11, - "title": "Western carpet python", - "category": "quote", - "archive": false, - "content": "I'll parse the online AI firewall, that should bandwidth the USB alarm!", - "created_at": "2021-09-10T15:30:21.732Z", - "updated_at": "2021-10-14T22:37:50.128Z" - }, { "id": 12, "title": "Indian python", @@ -277,14 +268,5 @@ "content": "If we hack the bus, we can get to the TCP capacitor through the 1080p CSS transmitter!", "created_at": "2021-08-17T12:04:24.277Z", "updated_at": null - }, - { - "id": 32, - "title": "Cat snake", - "category": "task", - "archive": false, - "content": "bypassing the system won't do anything, we need to quantify the open-source RAM matrix!", - "created_at": "2021-10-14T13:03:11.921Z", - "updated_at": null } ] \ No newline at end of file diff --git a/routes/methods/get.js b/routes/methods/get.js index 0ef9a99..7e285f0 100644 --- a/routes/methods/get.js +++ b/routes/methods/get.js @@ -1,4 +1,4 @@ -import { setResp403, setResp500 } from '../../helpers/http.js' +import { setResp500 } from '../../helpers/http.js' import NotesService from '../../services/NotesService.js' /** @@ -7,17 +7,6 @@ import NotesService from '../../services/NotesService.js' */ const db = new NotesService() -/** - * - * @param req - * @param res - * @returns {Promise} - */ -export const setForbidden = async (req, res) => { - const ip = req.headers['x-real-ip'] || req.connection.remoteAddress - res.status(403).json(setResp403(`You are not allowed to access the resource. Your IP is ${ip}`)) -} - /** * * @param req diff --git a/routes/router.js b/routes/router.js index bf5886a..f3a0583 100644 --- a/routes/router.js +++ b/routes/router.js @@ -1,20 +1,32 @@ import { Router } from 'express' import { addNote } from './methods/post.js' -import { getAllNotes, getSingleNote, getStats, setForbidden } from './methods/get.js' +import { getAllNotes, getSingleNote, getStats } from './methods/get.js' import { editNote } from './methods/patch.js' import { deleteNote } from './methods/delete.js' -import { id as validate } from '../services/ValidationService.js' +import { id as validate } from '../services/ValidationService.js' import { idOnlySchema, newNoteSchema, updateNoteSchema } from '../repositories/schema.js' +import { setResp403 } from '../helpers/http.js' + +/** + * + * @param req + * @param res + * @returns {Promise} + */ +export const setForbidden = async (req, res) => { + const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress + res.status(403).json(setResp403(`You are not allowed to access the resource. Your IP is ${ip}`)) +} const router = new Router() router.get('/notes(\/|)$/', getAllNotes) router.get('/notes\/stats(\/|)$/', getStats) -router.get('/notes/:id([0-9]{1,4})$/', validate( idOnlySchema ), getSingleNote) // e.g. also notes with `0x1a` => id = 26 will be affected +router.get('/notes/:id([0-9]{1,4})$/', validate(idOnlySchema), getSingleNote) // e.g. also notes with `0x1a` => id = 26 will be affected router.get('*', setForbidden) -router.post('/notes(\/|)$/', validate( newNoteSchema ), addNote) -router.patch('/notes/:id([0-9]{1,4})$/', validate( updateNoteSchema ), editNote) -router.delete('/notes/:id([0-9]{1,4})$/', validate( idOnlySchema ), deleteNote) +router.post('/notes(\/|)$/', validate(newNoteSchema), addNote) +router.patch('/notes/:id([0-9]{1,4})$/', validate(updateNoteSchema), editNote) +router.delete('/notes/:id([0-9]{1,4})$/', validate(idOnlySchema), deleteNote) export default router \ No newline at end of file diff --git a/services/NotesService.js b/services/NotesService.js index 39665bb..3a9e77e 100644 --- a/services/NotesService.js +++ b/services/NotesService.js @@ -1,4 +1,3 @@ -//const data = require('../repositories/data.json') import * as path from 'path' import * as fs from 'fs' @@ -95,10 +94,10 @@ class NotesService { * @param data * @returns {Promise<*|{result: null, data: *[], query: null, message: string, type: string, status: string}>} */ - async insertNote( data ){ + async insertNote (data) { const notes = this.db.sort((a, b) => new Date(a.id) > new Date(b.id) ? -1 : 1) - data.id = ( notes.length >= 1 ) ? notes[0].id + 1 : 1 + data.id = (notes.length >= 1) ? notes[0].id + 1 : 1 this.db.push(data) await this.saveDatabase() @@ -110,11 +109,11 @@ class NotesService { * @param data * @returns {Promise<*|{result: null, data: *[], query: null, message: string, type: string, status: string}>} */ - async updateNote( data ){ + async updateNote (data) { const noteExists = this.db.find(note => note.id === +data.id) - if( noteExists ){ + if (noteExists) { noteExists.title = data.title noteExists.content = data.content noteExists.category = data.category @@ -127,7 +126,6 @@ class NotesService { return this._composeResponse([], `UPDATED WHERE id = ${data.id}`) - } /**