Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
VovaStelmashchuk committed Jun 2, 2024
2 parents eb80282 + e800682 commit 753724e
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 58 deletions.
12 changes: 6 additions & 6 deletions database/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ const dbName = process.env.DB_NAME;
const uri = `mongodb://${dbUser}:${dbPassword}@${dbHost}:${dbPort}`;

mongoose.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
dbName: dbName,
useNewUrlParser: true,
useUnifiedTopology: true,
dbName: dbName,
});

const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
console.log('We are connected to MongoDB!');
db.once('open', function() {
console.log('We are connected to MongoDB!');
});

module.exports = mongoose;
module.exports = mongoose;
22 changes: 11 additions & 11 deletions database/schemas.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
const mongoose = require('./db');

const bodySchema = new mongoose.Schema({
type: String,
values: mongoose.Schema.Types.Mixed
}, {_id: false});
type: String,
values: mongoose.Schema.Types.Mixed
}, { _id: false });

const blogSchema = new mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
title: String,
body: [bodySchema],
image: String,
slug: String
}, {collection: 'blog'});
_id: mongoose.Schema.Types.ObjectId,
title: String,
body: [bodySchema],
image: String,
slug: String
}, { collection: 'blog' });

module.exports = {
Blog: mongoose.model('Blog', blogSchema, 'blog')
};
Blog: mongoose.model('Blog', blogSchema, 'blog')
};
55 changes: 27 additions & 28 deletions features/post-list/post-details-rest.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,40 @@
require('dotenv').config();
const express = require("express");
const axios = require('axios');
const {Blog} = require('../../database/schemas');
const { Blog } = require('../../database/schemas');
const router = express.Router();

const oldApiHost = process.env.OLD_API_HOST;

router.get('/api/blog/post-details/:slug', (req, res) => {
console.log("GET /api/blog/post-details/:slug")
const slug = req.params.slug;
Blog.findOne({slug: slug}, async (err, blog) => {
if (err) {
console.log(err)
res.status(500).send(err);
} else if (!blog) {
res.status(404).send('Blog post not found');
} else {
const response = blog.toObject();
const slug = req.params.slug;
Blog.findOne({ slug: slug }, async (err, blog) => {
if (err) {
console.log(err)
res.status(500).send(err);
} else if (!blog) {
res.status(404).send('Blog post not found');
} else {
const response = blog.toObject();

for (let i = 0; i < response.body.length; i++) {
// If the type is 'cocktail_id', make a request to your backend to get the cocktail details
if (response.body[i].type === 'cocktail') {
const cocktailSlug = response.body[i].values.slug;
try {
const cocktailDetails = await axios.get(`${oldApiHost}/v2/cocktail/${cocktailSlug}`);
// Include the cocktail details in the response
response.body[i].values = cocktailDetails.data;
} catch (error) {
console.error(`Failed to get cocktail details for slug ${cocktailId}: ${error}`);
}
}
}

response.responseTime = new Date(); // Add the responseTime field
res.status(200).json(response);
for (let i = 0; i < response.body.length; i++) {
// If the type is 'cocktail_id', make a request to your backend to get the cocktail details
if (response.body[i].type === 'cocktail') {
const cocktailSlug = response.body[i].values.slug;
try {
const cocktailDetails = await axios.get(`${oldApiHost}/v2/cocktail/${cocktailSlug}`);
// Include the cocktail details in the response
response.body[i].values = cocktailDetails.data;
} catch (error) {
console.error(`Failed to get cocktail details for slug ${cocktailId}: ${error}`);
}
}
}

response.responseTime = new Date(); // Add the responseTime field
res.status(200).json(response);
}
});
});

module.exports = router;
module.exports = router;
20 changes: 10 additions & 10 deletions features/post-list/post-list-rest.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
const express = require('express');
const {Blog} = require('../../database/schemas');
const { Blog } = require('../../database/schemas');
const router = express.Router();

router.get('/api/blog/post-list', (req, res) => {
Blog.find({}, 'title slug image', (err, blogs) => {
if (err) {
console.log(err);
res.status(500).send(err);
} else {
res.status(200).json(blogs);
}
});
Blog.find({}, 'title slug image', (err, blogs) => {
if (err) {
console.log(err);
res.status(500).send(err);
} else {
res.status(200).json(blogs);
}
});
});

module.exports = router;
module.exports = router;
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ const port = process.env.APP_PORT;
app.use(postDetailsRoutes, postListRoutes);

app.listen(port, () => {
console.log(`Server running at http://localhost:${port}/`);
});
console.log(`Server running at http://localhost:${port}/`);
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
"dev": "nodemon index.js"
},
"keywords": [],
"author": "",
Expand Down

0 comments on commit 753724e

Please sign in to comment.