Skip to content

Instantly share code, notes, and snippets.

View LanciWeb's full-sized avatar

Marco Lancellotti LanciWeb

View GitHub Profile
@LanciWeb
LanciWeb / index.js
Last active October 15, 2020 10:23
Initial setup of fastify app and connection with MongoDB
//import fastify & mongoose
const fastify = require('fastify');
const mongoose = require('mongoose');
//initialized Fastify App
const app = fastify();
//connected fastify to mongoose
try {
mongoose.connect('mongodb://localhost:27017/notes_db');
@LanciWeb
LanciWeb / preHandlerHook.js
Created October 15, 2020 17:39
preHandler hook for Fastfy + React admin project on medium
const Note = require('../models/note');
module.exports = (request, reply, done) => {
Note.count({}, (err, count) => {
if (err) {
console.error(err);
reply.code(500).send('Error!');
}
reply.header('Content-Range', `notes 0-10}/${count}`);
done();
@LanciWeb
LanciWeb / notesConroller.js
Created October 15, 2020 17:43
Complete, controller for the fastify + React Admin project on Medium
const Note = require('../models/note');
module.exports = {
//# create a note
create: async (request, reply) => {
try {
const note = request.body;
const newNote = await Note.create(note);
reply.code(201).send(newNote);
} catch (e) {
@LanciWeb
LanciWeb / notesConroller.js
Last active October 15, 2020 17:47
Controller create function for the Fastify + React Admin project on Medium
//# create a note
create: async (request, reply) => {
try {
const note = request.body;
const newNote = await Note.create(note);
reply.code(201).send(newNote);
} catch (e) {
reply.code(500).send(e);
}
},
@LanciWeb
LanciWeb / notesConroller.js
Created October 15, 2020 17:48
Controller fetch function for the Fastify + React Admin project on Medium
//#get the list of notes
fetch: async (request, reply) => {
try {
const notes = await Note.find({});
reply.code(200).send(notes);
} catch (e) {
reply.code(500).send(e);
}
},
@LanciWeb
LanciWeb / notesConroller.js
Created October 15, 2020 17:49
Controller get function for the Fastify + React Admin project on Medium
//#get a single note
get: async (request, reply) => {
try {
const noteId = request.params.id;
const note = await Note.findById(noteId);
reply.code(200).send(note);
} catch (e) {
reply.code(500).send(e);
}
},
@LanciWeb
LanciWeb / notesConroller.js
Created October 15, 2020 17:50
Controller update function for the Fastify + React Admin project on Medium
//#update a note
update: async (request, reply) => {
try {
const noteId = request.params.id;
const updates = request.body;
await Note.findByIdAndUpdate(noteId, updates);
const noteToUpdate = await Note.findById(noteId);
reply.code(200).send({ data: noteToUpdate });
} catch (e) {
reply.code(500).send(e);
@LanciWeb
LanciWeb / notesConroller.js
Created October 15, 2020 17:51
Controller delete function for the Fastify + React Admin project on Medium
//#delete a note
delete: async (request, reply) => {
try {
const noteId = request.params.id;
const noteToDelete = await Note.findById(noteId);
await Note.findByIdAndDelete(noteId);
reply.code(200).send({ data: noteToDelete });
} catch (e) {
reply.code(500).send(e);
}
@LanciWeb
LanciWeb / notesConroller.js
Last active October 15, 2020 17:57
empty controller for the Fastify + React Admin project on Medium
const Note = require('../models/note');
module.exports = {
//# create a note
create: async (request, reply) => {},
//#get the list of notes
fetch: async (request, reply) => {},
//#get a single note
@LanciWeb
LanciWeb / noteRoutes.js
Created October 15, 2020 17:58
V2 routes for the Fastify + React Admin project on Medium
const notesController = require('../controllers/notesController');
module.exports = (app) => {
//# create a note
app.post('/api/notes', notesController.create);
//#get the list of notes
app.get('/api/notes', notesController.fetch);
//#get a single note