Skip to content

Commit

Permalink
todo routes added
Browse files Browse the repository at this point in the history
  • Loading branch information
aritro66 committed Dec 18, 2023
1 parent 4781079 commit 16deb50
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 11 deletions.
16 changes: 5 additions & 11 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ services:
container_name: mainapp
ports:
- "3000:3000"
networks:
- nodeapp-network
environment:
REDISURL: redis
DATABASE_HOST: postgres
DATABASE_SSL: false
depends_on:
- postgres
- redis
Expand All @@ -20,20 +20,14 @@ services:
ports:
- "5432:5432"
environment:
POSTGRES_DB: data
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
networks:
- nodeapp-network
volumes:
- ./init.sql:/docker-entrypoint-initdb.d/init.sql

redis:
image: redis:alpine
container_name: redis
ports:
- "6379:6379"
networks:
- nodeapp-network

networks:
nodeapp-network:
driver: bridge
4 changes: 4 additions & 0 deletions init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE SCHEMA IF NOT EXISTS emma_test;

-- Switch to the "emma_test" schema
SET search_path TO emma_test;
79 changes: 79 additions & 0 deletions src/routes/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,26 @@ import config from '../config'
const logger = customLogger(path.basename(__filename))
module.exports = app => {
//:TODO need to secure these apis too.
const cache = app.get('redisCache')
const db = app.get('db')
const { sequelize, Sequelize } = db

// Todo table
const Todo = sequelize.define('Todo', {
title: {
type: Sequelize.STRING,
},
description: {
type: Sequelize.TEXT,
},
});

Todo.sync().then(() => {
logger.info("Todo Model synced");
}).catch((err) => {
logger.error(`Error in Todo: ${err}`)
});

app.use(function (req, res, next) {
req.realmName = req.headers.realmname
req.redisKey = config.redis.serviceName
Expand All @@ -21,6 +41,7 @@ module.exports = app => {
// }
})
app.use(checkCache)

app.get('/', (req, res, next) => {
try {
req.dbData = `Hello world!`
Expand All @@ -30,4 +51,62 @@ module.exports = app => {
logger.error(`Error in get ${e}`)
}
})

app.post('/todo', async (req, res) => {
try {
const { title, description } = req.body;
const todo = await Todo.create({
title,
description,
});

res.json(todo);
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Internal Server Error' });
}
});

// Route to get all TODO items
app.get('/todo', async (req, res) => {
try {
const key = 'todos_all';
const cachedData = await cache.get(key)
if (cachedData !== null) {
logger.info(`Cache Hit for ${key}`);
res.json(JSON.parse(cachedData));
} else {
logger.info(`Cache Miss for ${key}`)
const todos = await Todo.findAll();
cache.set(key, JSON.stringify(todos), 60);
res.json(todos);
}
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Internal Server Error' });
}
});

// Route to get Todo by Id
app.get('/todo/:id', async (req, res) => {
try {
const { id } = req.params;
const key = `todos_${id}`
const cachedData = await cache.get(key)
if (cachedData !== null) {
logger.info(`Cache Hit for ${key}`);
res.json(JSON.parse(cachedData));
} else {
logger.info(`Cache Miss for ${key}`)
const todo = await Todo.findByPk(id);
cache.set(key, JSON.stringify(todo), 60);
res.json(todo);
}

} catch (error) {
console.error(error);
res.status(500).json({ error: 'Internal Server Error' });
}
});

}

0 comments on commit 16deb50

Please sign in to comment.