DONE AGAIN

This commit is contained in:
2021-10-15 02:15:38 +03:00
parent ade92e0493
commit 32d7e45670
4 changed files with 23 additions and 42 deletions

View File

@@ -89,15 +89,6 @@
"created_at": "2021-09-15T10:43:02.014Z", "created_at": "2021-09-15T10:43:02.014Z",
"updated_at": "2021-08-07T07:06:51.050Z" "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, "id": 12,
"title": "Indian python", "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!", "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", "created_at": "2021-08-17T12:04:24.277Z",
"updated_at": null "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
} }
] ]

View File

@@ -1,4 +1,4 @@
import { setResp403, setResp500 } from '../../helpers/http.js' import { setResp500 } from '../../helpers/http.js'
import NotesService from '../../services/NotesService.js' import NotesService from '../../services/NotesService.js'
/** /**
@@ -7,17 +7,6 @@ import NotesService from '../../services/NotesService.js'
*/ */
const db = new NotesService() const db = new NotesService()
/**
*
* @param req
* @param res
* @returns {Promise<void>}
*/
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 * @param req

View File

@@ -1,20 +1,32 @@
import { Router } from 'express' import { Router } from 'express'
import { addNote } from './methods/post.js' 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 { editNote } from './methods/patch.js'
import { deleteNote } from './methods/delete.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 { idOnlySchema, newNoteSchema, updateNoteSchema } from '../repositories/schema.js'
import { setResp403 } from '../helpers/http.js'
/**
*
* @param req
* @param res
* @returns {Promise<void>}
*/
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() const router = new Router()
router.get('/notes(\/|)$/', getAllNotes) router.get('/notes(\/|)$/', getAllNotes)
router.get('/notes\/stats(\/|)$/', getStats) 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.get('*', setForbidden)
router.post('/notes(\/|)$/', validate( newNoteSchema ), addNote) router.post('/notes(\/|)$/', validate(newNoteSchema), addNote)
router.patch('/notes/:id([0-9]{1,4})$/', validate( updateNoteSchema ), editNote) router.patch('/notes/:id([0-9]{1,4})$/', validate(updateNoteSchema), editNote)
router.delete('/notes/:id([0-9]{1,4})$/', validate( idOnlySchema ), deleteNote) router.delete('/notes/:id([0-9]{1,4})$/', validate(idOnlySchema), deleteNote)
export default router export default router

View File

@@ -1,4 +1,3 @@
//const data = require('../repositories/data.json')
import * as path from 'path' import * as path from 'path'
import * as fs from 'fs' import * as fs from 'fs'
@@ -95,10 +94,10 @@ class NotesService {
* @param data * @param data
* @returns {Promise<*|{result: null, data: *[], query: null, message: string, type: string, status: string}>} * @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) 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) this.db.push(data)
await this.saveDatabase() await this.saveDatabase()
@@ -110,11 +109,11 @@ class NotesService {
* @param data * @param data
* @returns {Promise<*|{result: null, data: *[], query: null, message: string, type: string, status: string}>} * @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) const noteExists = this.db.find(note => note.id === +data.id)
if( noteExists ){ if (noteExists) {
noteExists.title = data.title noteExists.title = data.title
noteExists.content = data.content noteExists.content = data.content
noteExists.category = data.category noteExists.category = data.category
@@ -127,7 +126,6 @@ class NotesService {
return this._composeResponse([], `UPDATED WHERE id = ${data.id}`) return this._composeResponse([], `UPDATED WHERE id = ${data.id}`)
} }
/** /**