-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/MixDrinks/backend-node
- Loading branch information
Showing
6 changed files
with
57 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters