Skip to content

Commit

Permalink
Merge branch 'main' into snyk-upgrade-5495070ffeefe0c498486db1fffcbe9d
Browse files Browse the repository at this point in the history
  • Loading branch information
navodveduth authored Sep 26, 2024
2 parents 530b936 + b41cbe4 commit 41daa56
Show file tree
Hide file tree
Showing 63 changed files with 2,864 additions and 2,334 deletions.
6 changes: 4 additions & 2 deletions BackEnd/config/db.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import mongoose from 'mongoose';
import colors from 'colors';
import logger from '../controllers/logger.js';

const connectDB = async () => {
try {
const conn = await mongoose.connect(process.env.MONGO_URI, {});
console.log(
logger.info(
`MongoDB database connection established successfully: ${conn.connection.host}`
.cyan.underline
);
} catch (error) {
console.log(`Error: ${error.message}`.red.underline.bold);
logger.error(`Error: ${error.message}`.red.underline.bold);
process.exit(1);
}
};
Expand Down
8 changes: 4 additions & 4 deletions BackEnd/controllers/blogController.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ const searchBlogs = asyncHandler(async (req, res) => {
res.status(404).json({ message: 'Blog not found' });
}
} catch (error) {
console.error(error);
res.status(400).json({ message: error.message });
res.status(404).json({ message: 'Blog not found' });
throw new Error(error.message);
}
});

Expand Down Expand Up @@ -277,8 +277,8 @@ const getBlogsByAuthor = asyncHandler(async (req, res) => {
res.status(404).json({ message: 'No blogs found' });
}
} catch (error) {
console.log(error);
res.status(404).json({ message: 'Blog not found' });
throw new Error(error.message);
}
});

Expand Down Expand Up @@ -324,8 +324,8 @@ const getLatestBlogs = asyncHandler(async (req, res) => {
throw new Error('No blogs found');
}
} catch (error) {
console.log(error);
res.status(404).json({ message: 'No blogs found' });
throw new Error(error.message);
}
});

Expand Down
41 changes: 23 additions & 18 deletions BackEnd/controllers/cropController.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const getCrops = asyncHandler(async (req, res) => {
const crops = await Crop.find({}).populate('author', 'firstName lastName');
res.json(crops);
} catch (error) {
console.log(error);
res.status(400).json({ message: error.message });
throw new Error(error.message);
}
});

Expand All @@ -28,8 +28,8 @@ const getCropById = asyncHandler(async (req, res) => {
throw new Error('Crop not found');
}
} catch (error) {
console.log(error);
res.status(400).json({ message: error.message });
throw new Error(error.message);
}
});

Expand All @@ -41,14 +41,13 @@ const getAllAcceptedCrops = asyncHandler(async (req, res) => {
const crops = await Crop.find({ isAccepted: true });
if (crops) {
res.json(crops);
console.log('crops', crops);
} else {
res.status(404);
throw new Error('Crops not found');
}
} catch (error) {
console.log(error);
res.status(400).json({ message: error.message });
throw new Error(error.message);
}
});

Expand All @@ -65,8 +64,8 @@ const deleteCrop = asyncHandler(async (req, res) => {
throw new Error('Crop not found');
}
} catch (error) {
console.log(error);
res.status(400).json({ message: error.message });
throw new Error(error.message);
}
});

Expand Down Expand Up @@ -116,8 +115,8 @@ const createCrop = asyncHandler(async (req, res) => {

res.status(201).json(createdCrop);
} catch (error) {
console.log(error);
res.status(400).json({ message: error.message });
throw new Error(error.message);
}
});

Expand Down Expand Up @@ -156,8 +155,8 @@ const updateCrop = asyncHandler(async (req, res) => {
throw new Error('Crop not found');
}
} catch (error) {
console.log(error);
res.status(400).json({ message: error.message });
throw new Error(error.message);
}
});

Expand All @@ -177,8 +176,8 @@ const searchCrops = asyncHandler(async (req, res) => {
res.status(404).json({ message: 'Crops not found' });
}
} catch (error) {
console.error(error);
res.status(400).json({ message: error.message });
throw new Error(error.message);
}
});

Expand All @@ -197,6 +196,7 @@ const getShortCrops = asyncHandler(async (req, res) => {
} catch (error) {
console.log(error);
res.status(400).json({ message: error.message });
throw new Error(error.message);
}
});

Expand Down Expand Up @@ -231,8 +231,8 @@ const getCropsByAuthor = asyncHandler(async (req, res) => {
throw new Error('Crops not found');
}
} catch (error) {
console.log(error);
res.status(400).json({ message: error.message });
throw new Error(error.message);
}
});

Expand All @@ -248,22 +248,27 @@ const addRemoveCropBookmark = asyncHandler(async (req, res) => {
crop.bookmarkedBy.filter(
(bookmark) => bookmark.toString() === req.body.userId.toString()
).length > 0
) {
crop.bookmarkedBy = crop.bookmarkedBy.filter(
(bookmark) => bookmark.toString() !== req.body.userId.toString()
);
} else {
crop.bookmarkedBy.push(req.body.userId);
}
)
if (
crop.bookmarkedBy.filter(
(bookmark) => bookmark.toString() === req.body.userId.toString()
).length > 0
) {
crop.bookmarkedBy = crop.bookmarkedBy.filter(
(bookmark) => bookmark.toString() !== req.body.userId.toString()
);
} else {
crop.bookmarkedBy.push(req.body.userId);
}
const updatedCrop = await crop.save();
res.json(updatedCrop);
} else {
res.status(404);
throw new Error('Crop not found');
}
} catch (error) {
console.log(error);
res.status(400).json({ message: error.message });
throw new Error(error.message);
}
});

Expand All @@ -277,8 +282,8 @@ const getCropBookmarksByUser = asyncHandler(async (req, res) => {
throw new Error('Crops not found');
}
} catch (error) {
console.log(error);
res.status(400).json({ message: error.message });
throw new Error(error.message);
}
});

Expand Down
23 changes: 11 additions & 12 deletions BackEnd/controllers/diseaseController.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ const getCropDiseases = asyncHandler(async (req, res) => {
);
res.json(cropDiseases);
} catch (error) {
console.log(error);
res.status(400).json({ message: error.message });
throw new Error(error.message);
}
});

Expand All @@ -31,8 +31,8 @@ const getCropDiseaseById = asyncHandler(async (req, res) => {
throw new Error('Crop disease not found');
}
} catch (error) {
console.log(error);
res.status(400).json({ message: error.message });
throw new Error(error.message);
}
});

Expand All @@ -44,14 +44,13 @@ const getAllAcceptedCropDiseases = asyncHandler(async (req, res) => {
const cropDiseases = await CropDisease.find({ isAccepted: true });
if (cropDiseases) {
res.json(cropDiseases);
console.log('cropDiseases', cropDiseases);
} else {
res.status(404);
throw new Error('Crop diseases not found');
}
} catch (error) {
console.log(error);
res.status(400).json({ message: error.message });
throw new Error(error.message);
}
});

Expand All @@ -68,8 +67,8 @@ const deleteCropDisease = asyncHandler(async (req, res) => {
throw new Error('Crop disease not found');
}
} catch (error) {
console.log(error);
res.status(400).json({ message: error.message });
throw new Error(error.message);
}
});

Expand Down Expand Up @@ -118,8 +117,8 @@ const createCropDisease = asyncHandler(async (req, res) => {
const createdCropDisease = await cropDisease.save();
res.status(201).json(createdCropDisease);
} catch (error) {
console.log(error);
res.status(400).json({ message: error.message });
throw new Error(error.message);
}
});

Expand Down Expand Up @@ -160,8 +159,8 @@ const updateCropDisease = asyncHandler(async (req, res) => {
throw new Error('Crop disease not found');
}
} catch (error) {
console.log(error);
res.status(400).json({ message: error.message });
throw new Error(error.message);
}
});

Expand All @@ -180,8 +179,8 @@ const searchCropDisease = asyncHandler(async (req, res) => {
res.status(404).json({ message: 'Crop disease not found' });
}
} catch (error) {
console.error(error);
res.status(400).json({ message: error.message });
throw new Error(error.message);
}
});

Expand All @@ -198,8 +197,8 @@ const getRandomCropDiseases = asyncHandler(async (req, res) => {
throw new Error('Crop disease not found');
}
} catch (error) {
console.log(error);
res.status(400).json({ message: error.message });
throw new Error(error.message);
}
});

Expand Down Expand Up @@ -235,8 +234,8 @@ const getDiseasesByAuthor = asyncHandler(async (req, res) => {
throw new Error('Diseases not found');
}
} catch (error) {
console.log(error);
res.status(400).json({ message: error.message });
throw new Error(error.message);
}
});

Expand Down Expand Up @@ -266,8 +265,8 @@ const addRemoveDiseaseBookmark = asyncHandler(async (req, res) => {
throw new Error('Disease not found');
}
} catch (error) {
console.log(error);
res.status(400).json({ message: error.message });
throw new Error(error.message);
}
});

Expand All @@ -281,8 +280,8 @@ const getDiseaseBookmarksByUser = asyncHandler(async (req, res) => {
throw new Error('Diseases not found');
}
} catch (error) {
console.log(error);
res.status(400).json({ message: error.message });
throw new Error(error.message);
}
});

Expand Down
41 changes: 41 additions & 0 deletions BackEnd/controllers/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* This file is used to log info to a file
* Three log levels exist: info, warning, error
*/

import fs from 'fs';
import path from 'path';

export const LOG = {
INFO: 'info',
WARNING: 'warning',
ERROR: 'error',
};
const __dirname = path.resolve();

const log = (level, message) => {
const logMessage = `${new Date().toISOString()} [${level.toUpperCase()}]: ${message}\n`;

const logPath = path.join(__dirname, '../logs');
if (!fs.existsSync(logPath)) {
fs.mkdirSync(logPath);
}

const logFile = path.join(logPath, `${level}.log`);
fs.appendFileSync(logFile, logMessage);
console.log(logMessage);
};

const info = (message) => {
log(LOG.INFO, message);
};

const warning = (message) => {
log(LOG.WARNING, message);
};

const error = (message) => {
log(LOG.ERROR, message);
};

export default { info, warning, error };
Loading

0 comments on commit 41daa56

Please sign in to comment.