Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MONGO_URI = mongodb://127.0.0.1:27017/BlogApp
4 changes: 3 additions & 1 deletion client/src/componets/Blogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import config from "../config";

const Blogs = () => {
const [blogs, setBlogs] = useState();
console.log("hi")
const sendRequest = async () => {
const res = await axios
.get(`${config.BASE_URL}/api/blogs`)
.catch((err) => console.log(err));
const data = await res.data;
console.log(data.data.blogs)
return data;
};
useEffect(() => {
sendRequest().then((data) => setBlogs(data.blogs));
sendRequest().then((data) => setBlogs(data.data.blogs));
}, []);
console.log(blogs);
return (
Expand Down
6 changes: 3 additions & 3 deletions client/src/componets/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ const Login = () => {
password: inputs.password,
})
.catch((err) => console.log(err));

console.log(res)
const data = await res.data;
console.log("return");
console.log(data);
console.log(data.user._id)
console.log(data.data);
return data;
};

Expand Down
7 changes: 4 additions & 3 deletions server/config/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ require('dotenv').config();
mongoose.set('strictQuery', false);


mongoose.connect(process.env.MONGO_URI || "mongodb://mongo:27017/Blog").then(()=>{
console.log("connected!");
mongoose.connect(process.env.MONGO_URI || "mongodb://localhost:27017/Blog").then(()=>{
console.log("Database connected!");
}).catch((err)=>{
console.log(err);
console.log("Database not connected", err);

})
20 changes: 11 additions & 9 deletions server/controller/blog-controller.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
const mongoose = require("mongoose");
const Blog = require("../model/Blog");
const User = require("../model/User");
const { ApiResponse } = require("../utils/ApiResponse");
const { ApiError } = require("../utils/ApiError");
const ApiResponse = require("../utils/ApiResponse");
const ApiError = require("../utils/ApiError");

const getAllBlogs = async (req, res, next) => {
try {
console.log("Hello")
const blogs = await Blog.find();
console.log("Blogs", blogs);
if (!blogs || blogs.length === 0) {
return res.status(404).json(new ApiError(404, "No blogs found"));
}
Expand All @@ -17,7 +19,9 @@ const getAllBlogs = async (req, res, next) => {
};

const addBlog = async (req, res, next) => {
console.log("Inside add blog");
const { title, desc, img, user } = req.body;
console.log(req.body)
const currentDate = new Date();

try {
Expand All @@ -26,17 +30,15 @@ const addBlog = async (req, res, next) => {
return res.status(400).json(new ApiError(400, "Unauthorized"));
}

const blog = new Blog({ title, desc, img, user, date: currentDate });
const blog = await Blog.create({ title, desc, img, user, date: currentDate });

existingUser.blogs.push(blog._id);
await existingUser.save();

const session = await mongoose.startSession();
session.startTransaction();
await blog.save({ session });
existingUser.blogs.push(blog);
await existingUser.save({ session });
await session.commitTransaction();

return res.status(201).json(new ApiResponse(201, { blog }, "Blog created successfully"));
} catch (e) {
console.log("error", e)
return res.status(500).json(new ApiError(500, e.message));
}
};
Expand Down
10 changes: 7 additions & 3 deletions server/controller/user-contoller.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const User = require("../model/User");
const bcrypt = require("bcryptjs");
const { ApiResponse } = require("../utils/ApiResponse");
const { ApiError } = require("../utils/ApiError");
const ApiResponse = require("../utils/ApiResponse");
const ApiError = require("../utils/ApiError");

const getAllUser = async (req, res, next) => {
try {
Expand All @@ -21,6 +21,7 @@ const signUp = async (req, res, next) => {

try {
const existingUser = await User.findOne({ email });
console.log(existingUser)
if (existingUser) {
return res.status(400).json(new ApiError(400, "User already exists"));
}
Expand All @@ -32,8 +33,10 @@ const signUp = async (req, res, next) => {
password: hashedPassword,
blogs: []
});
console.log(user)

await user.save();
console.log("User saved")
return res.status(201).json(new ApiResponse(201, { user }, "User registered successfully"));
} catch (e) {
console.error(e);
Expand All @@ -43,9 +46,10 @@ const signUp = async (req, res, next) => {

const logIn = async (req, res, next) => {
const { email, password } = req.body;

console.log(email)
try {
const existingUser = await User.findOne({ email });
console.log("Exisiting")
if (!existingUser) {
return res.status(404).json(new ApiError(404, "User not found"));
}
Expand Down
180 changes: 120 additions & 60 deletions server/package.json
Original file line number Diff line number Diff line change
@@ -1,83 +1,143 @@
{






"name": "blogapp",






"version": "1.0.0",






"description": "",






"main": "server.js",






"scripts": {












"start": "nodemon server.js",












"test": "echo \"Error: no test specified\" && exit 1"






},






"author": "khushi patel",






"license": "ISC",






"devDependencies": {












"nodemon": "^2.0.16"






},






"dependencies": {












"bcryptjs": "^2.4.3",












"cors": "^2.8.5",












"dotenv": "^16.5.0",












"express": "^4.18.1",












"helmet": "^8.1.0",












"mongoose": "^6.3.4"






}
}
2 changes: 1 addition & 1 deletion server/utils/ApiError.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ class ApiError extends Error {
}
}

export {ApiError}
module.exports = ApiError;
2 changes: 1 addition & 1 deletion server/utils/ApiResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ class ApiResponse {
}
}

export { ApiResponse }
module.exports = ApiResponse;
10 changes: 10 additions & 0 deletions server/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,11 @@ fresh@0.5.2:
resolved "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz"
integrity sha512-zJ2mQYM18rEFOudeV4GShTGIQ7RbzA7ozbU9I/XBpm7kqgMywgmylMwXHxZJmkVoYkna9d2pVXVXPdYTP9ej8Q==

fsevents@~2.3.2:
version "2.3.3"
resolved "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz"
integrity sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==

function-bind@^1.1.2:
version "1.1.2"
resolved "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz"
Expand Down Expand Up @@ -1272,6 +1277,11 @@ hasown@^2.0.0:
dependencies:
function-bind "^1.1.2"

helmet@^8.1.0:
version "8.1.0"
resolved "https://registry.npmjs.org/helmet/-/helmet-8.1.0.tgz"
integrity sha512-jOiHyAZsmnr8LqoPGmCjYAaiuWwjAPLgY8ZX2XrmHawt99/u1y6RgrZMTeoPfpUbV96HOalYgz1qzkRbw54Pmg==

http-errors@2.0.0:
version "2.0.0"
resolved "https://registry.npmjs.org/http-errors/-/http-errors-2.0.0.tgz"
Expand Down