Skip to content

Instantly share code, notes, and snippets.

@unclebay143
Last active November 28, 2021 18:34
Show Gist options
  • Save unclebay143/e692f7862fa72c632c7b75bb6142a15e to your computer and use it in GitHub Desktop.
Save unclebay143/e692f7862fa72c632c7b75bb6142a15e to your computer and use it in GitHub Desktop.
FULL TODO LIST BACKEND CODE
// import express
const express = require('express');
const app = express();
//Parse URL-encoded bodies - Allows
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// import cors
var cors = require('cors');
app.use(cors());
// import the harperdb instance
const db = require('./dbconfig');
// set your preferred server port
const port = 3000;
// 1. create your post route that handles creating new todo item
app.post('/add', async (req, res) => {
// 2. retrieve the todo from req.body
// 3. Validate the todo to nsure the user does not submit an empty form
if (!req.body.todo || req.body.todo === '') {
res.status(400).send('Todo is required');
} else {
// 4. prepare the todo in an object
const option = {
todo: req.body.todo,
status: 'pending',
};
// 5. ensure to catch the error using try/catch
try {
// 6. if the todo is not empty
const response = await db.insert({
table: 'items',
records: [option],
});
// 7. notify the frontend or sender with the success response
res.status(200).send(response);
} catch (error) {
// 7. notify the frontend or sender with the error response
res.status(500).send(error);
}
}
});
// 1. route to retrieve all todos in the database
app.get('/todos', async (req, res) => {
// 2. use try/catch to control errors
try {
// 3. use query method to get all todo from the database table
const response = await db.query('SELECT * FROM todos.items');
// 4. send success message to the frontend
res.status(200).send(response.data);
} catch (error) {
// 4. send error message to the frontend
res.status(500).send('something went wrong');
}
});
// 1. route to update a todo
app.post('/edit', async (req, res) => {
// 2. set the updated todo and specify the todo identifier - hash attribute
const option = {
id: req.body.id,
todo: req.body.todo,
status: req.body.status,
};
// 3. use try/catch to control errors
try {
// 4. send the updated todo
const response = await db.update({
table: 'items',
records: [option],
});
// 5. send success message to the frontend
res.status(200).send(response);
} catch (error) {
// 5. send error message to the frontend
res.status(500).send(error);
}
});
// 1. route to delete a todo using its id
app.post('/delete/:todo_id', async (req, res) => {
// 2. get the id from the url parameter
const { todo_id } = req.params;
// 3. use try/catch to control errors
try {
// 4. Send a delete request to the database
const response = await db.delete({
table: 'items',
hashValues: [todo_id],
});
// 5. send success message to the frontend
res.status(200).send(response);
} catch (error) {
// 5. send error message to the frontend
res.status(500).send(error);
}
});
// just a notification in the console
app.listen(port, () => {
console.log(`Your server ⚡ is running 🏃‍♂️ todo on http://localhost:${port}`);
});
require("dotenv").config();
const harperive = require("harperive");
const DB_CONFIG = {
harperHost: process.env.INSTANCE_URL,
username: process.env.INSTANCE_USERNAME,
password: process.env.INSTANCE_PASSWORD,
schema: process.env.INSTANCE_SCHEMA,
};
const Client = harperive.Client;
const db = new Client(DB_CONFIG);
module.exports = db;
@suenodesign2015
Copy link

Thank you.
At the first attemp, it didn't work.

I solved the problem by adjusting line number 7 in [complete_todo_list_api_code.js]
from // app.use(express.json); to app.use(express.json());

@unclebay143
Copy link
Author

Oh mine, thanks a lot for catching that, I pasted the wrong version here.

I have updated it.

How was building with the tutorial like?

@suenodesign2015
Copy link

Thank you Ayodele,

I followed the tutorial step by step and it was clear.

Now, i'm able to send requests to HarperDB like a Pro.

Thank you again.

@unclebay143
Copy link
Author

unclebay143 commented Nov 22, 2021

Thank you Ayodele,

I followed the tutorial step by step and it was clear.

Now, i'm able to send requests to HarperDB like a Pro.

Thank you again.

Glad you completed the tutorial and able to create your own APIs,

You're awesome @suenodesign2015 🤗

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment