initial commit.
This commit is contained in:
42
src/app.js
Normal file
42
src/app.js
Normal file
@ -0,0 +1,42 @@
|
||||
|
||||
import express from 'express'
|
||||
import { db } from './db';
|
||||
|
||||
/*
|
||||
* utility function to either get the value in the post data
|
||||
* or send an error message to the response.
|
||||
*/
|
||||
function valOrMissing(data, key, res) {
|
||||
if (!data[key]) {
|
||||
res.status(403).json({"error": `missing "${key}"`})
|
||||
return null;
|
||||
}
|
||||
return data[key];
|
||||
}
|
||||
|
||||
|
||||
export function createApp() {
|
||||
|
||||
// Creating an instance of Express
|
||||
const app = express();
|
||||
app.use(express.json()) // for parsing application/json
|
||||
|
||||
app.put("/note", async (req, res) => {
|
||||
const title = valOrMissing(req.body, "title", res);
|
||||
if (!title) return;
|
||||
const text = valOrMissing(req.body, "text", res);
|
||||
if (!text) return;
|
||||
const created = req.body["created"] ? Date.parse(req.body["created"]) : new Date();
|
||||
const id = await db("note").insert({
|
||||
title, text, created
|
||||
}, ["id"])
|
||||
return res.status(202).send({ "note": id[0].id });
|
||||
});
|
||||
|
||||
app.get("/note/:id", async (req, res, next) => {
|
||||
const note = await db.from("note").where({ id: req.params.id });
|
||||
return res.status(200).send({ "note": note[0] });
|
||||
});
|
||||
|
||||
return app;
|
||||
}
|
26
src/app.spec.js
Normal file
26
src/app.spec.js
Normal file
@ -0,0 +1,26 @@
|
||||
import { db } from "./db";
|
||||
import { createApp } from "./app";
|
||||
import request from "supertest";
|
||||
|
||||
const app = createApp();
|
||||
|
||||
const note = {
|
||||
title: 'Test Note',
|
||||
text: 'This is a test note.'
|
||||
};
|
||||
|
||||
describe('PUT and GET methods of "/note" endpoint', () => {
|
||||
it('should create and return a note with PUT method', async () => {
|
||||
const res = await request(app).put('/note').send(note);
|
||||
expect(res.status).toBe(202);
|
||||
expect(res.body).toHaveProperty("note");
|
||||
});
|
||||
|
||||
it('should return the created note with GET method', async () => {
|
||||
const res = await request(app).put('/note').send(note);
|
||||
const id = res.body["note"];
|
||||
const res2 = await request(app).get(`/note/${id}`).send(note);
|
||||
expect(res2.status).toBe(200);
|
||||
expect(res2.body).toMatchObject({note: { ...note, id }});
|
||||
});
|
||||
});
|
13
src/db.js
Normal file
13
src/db.js
Normal file
@ -0,0 +1,13 @@
|
||||
// Importing the Knex library
|
||||
import knex from 'knex';
|
||||
import 'dotenv/config'
|
||||
|
||||
if (!process.env.DB_URL) {
|
||||
fail("Ensure you set DB_URL");
|
||||
};
|
||||
|
||||
// Creating an instance of Knex
|
||||
export const db = knex({
|
||||
client: 'postgres',
|
||||
connection: process.env.DB_URL
|
||||
});
|
13
src/index.js
Normal file
13
src/index.js
Normal file
@ -0,0 +1,13 @@
|
||||
// Importing express
|
||||
import { createApp } from './app';
|
||||
|
||||
(() => {
|
||||
const app = createApp();
|
||||
// Start the Express server
|
||||
app.listen(3000, () => {
|
||||
console.log('Server started on port 3000');
|
||||
});
|
||||
})();
|
||||
|
||||
// Exporting the app
|
||||
export default app;
|
Reference in New Issue
Block a user