From e800682a774495e24e0858c272bd77d1f862218b Mon Sep 17 00:00:00 2001 From: Vova Stelmashchuk Date: Sun, 2 Jun 2024 11:31:39 +0300 Subject: [PATCH] code formating --- database/db.js | 12 +++--- database/schemas.js | 22 +++++----- features/post-list/post-details-rest.js | 55 ++++++++++++------------- features/post-list/post-list-rest.js | 20 ++++----- index.js | 4 +- package.json | 2 +- 6 files changed, 57 insertions(+), 58 deletions(-) diff --git a/database/db.js b/database/db.js index c9d4016..3ea9bce 100644 --- a/database/db.js +++ b/database/db.js @@ -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; \ No newline at end of file +module.exports = mongoose; diff --git a/database/schemas.js b/database/schemas.js index 9646919..daee31e 100644 --- a/database/schemas.js +++ b/database/schemas.js @@ -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') -}; \ No newline at end of file + Blog: mongoose.model('Blog', blogSchema, 'blog') +}; diff --git a/features/post-list/post-details-rest.js b/features/post-list/post-details-rest.js index 964044d..5b3b904 100644 --- a/features/post-list/post-details-rest.js +++ b/features/post-list/post-details-rest.js @@ -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; \ No newline at end of file +module.exports = router; diff --git a/features/post-list/post-list-rest.js b/features/post-list/post-list-rest.js index 626e6a0..9e3e013 100644 --- a/features/post-list/post-list-rest.js +++ b/features/post-list/post-list-rest.js @@ -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; \ No newline at end of file +module.exports = router; diff --git a/index.js b/index.js index 775062c..334d09a 100644 --- a/index.js +++ b/index.js @@ -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}/`); -}); \ No newline at end of file + console.log(`Server running at http://localhost:${port}/`); +}); diff --git a/package.json b/package.json index 3cfac01..ebe6b2a 100644 --- a/package.json +++ b/package.json @@ -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": "",