Skip to content

Commit

Permalink
fixing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
amiparadis250 committed Mar 1, 2024
1 parent e1cc7b5 commit 34dca6c
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 20 deletions.
10 changes: 4 additions & 6 deletions src/controllers/commentsCtl.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Import necessary modules and functions
import { Request, Response } from 'express';
import Blog from "../model/Blogs";
import Comment from "../model/Comments";
import Comment,{CommentModel} from "../model/Comments";

export const addComment = async (req: Request, res: Response) => {
const blogId = req.params.id;
Expand Down Expand Up @@ -121,18 +121,16 @@ export const getAllComments = async (req: Request, res: Response) => {
}
};

export const getAllCommentsGlobal = async (req: Request, res: Response) => {
export const getAllCommentsGlobal = async (req: Request, res: Response): Promise<void> => {
try {
// Populate comments before responding
const allComments = await Comment.find().populate('blog');
const allComments: CommentModel[] = await Comment.find();

// Respond with success and all comments
res.json({
status: 'success',
data: allComments,
});
} catch (err) {
console.error('Error in getAllCommentsGlobal:', err);
console.error('Error in geting AllComments:', err);
res.status(500).json({
status: 'error',
message: err.message,
Expand Down
17 changes: 10 additions & 7 deletions src/controllers/userCtl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const registerUser = async (req:any, res:any) => {
// Check if user already exists
const userFound = await User.findOne({ email });
if (userFound) {
return res.json.status(409)({
return res.status(409).json({
status: 'error',
msg: 'User already exists',
});
Expand Down Expand Up @@ -39,14 +39,15 @@ export const registerUser = async (req:any, res:any) => {
}
};

// Login
// Login
export const loginUser = async (req, res) => {
const { email, password } = req.body;

try {
const userFound = await User.findOne({ email });
if (!userFound) {
return res.json({
return res.status(409).json({
status: 'error',
msg: 'Wrong login credentials',
});
Expand All @@ -55,21 +56,22 @@ export const loginUser = async (req, res) => {
// Check password validity
const isPasswordMatched = await bcrypt.compare(password, userFound.password);
if (!isPasswordMatched) {
return res.json.status(409)({
// Missing closing bracket for this condition
return res.status(409).json({
status: 'error',
msg: 'Wrong login credentials',
});
}

res.json({
status: 'success',
data:{
data: {
id: userFound.id,
fullName: userFound.fullName,
fullName: userFound.fullName,
email: userFound.email,
isAdmin: userFound.isAdmin,
token: generateToken(userFound.id)
}
token: generateToken(userFound.id),
},
});
} catch (err) {
console.error(err);
Expand All @@ -79,6 +81,7 @@ export const loginUser = async (req, res) => {
});
}
};

//getting user information by ID
export const getUserProfileById = async (req, res) => {
try {
Expand Down
4 changes: 2 additions & 2 deletions src/model/Blogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ const blogSchema = new Schema<BlogModel>({
},
user: {
type: Schema.Types.ObjectId,
ref: 'User', // Assuming your User model is named 'User'
ref: 'User',

},
comments: [{
type: Schema.Types.ObjectId,
ref: 'Comment', // Assuming your Comment model is named 'Comment'
ref: 'Comment',
}],
});

Expand Down
2 changes: 1 addition & 1 deletion src/model/Comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const commentSchema = new Schema<CommentModel>({
},
blog: {
type: Schema.Types.ObjectId,
ref: 'Blog', // Assuming your Blog model is named 'Blog'
ref: 'Blog',
},
});

Expand Down
2 changes: 1 addition & 1 deletion src/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
"email": { "type": "string" },
"password": { "type": "string" }
},
"required": ["FullName", "email", "password"]
"required": ["fullName", "email", "password"]
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/validations/QuerriesValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import joi from 'joi'
const querryValidation = async (req, res, next) => {
const value = createQuerryValidationSchema.validate(req.body, { abortEarly: false });
if (value.error) {
return res.status(403).send({message:"Invalid message details",error:value.error.details[0].message});
return res.status(403).send({message:"Invalid message details",error:value.error});
} else {
next();
}
Expand Down
2 changes: 1 addition & 1 deletion src/validations/commentsvalidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const commentsValidationSchema = joi.object({
const validateComments = async (req, res, next) => {
const value = commentsValidationSchema.validate(req.body, { abortEarly: false });
if (value.error) {
return res.status(403).send({message:"Invalid comments details",error:value.error.details[0].message});
return res.status(403).send({message:"Invalid comments details",error:value.error});
} else {
next();
}
Expand Down
2 changes: 1 addition & 1 deletion src/validations/userValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const validateUserSchema= joi.object({
const usersValidation = async (req:any, res:any, next) => {
const value = validateUserSchema.validate(req.body, { abortEarly: false });
if (value.error) {
return res.status(400).res.json({ error: value.error.details[0].message});
return res.status(403).res.json({ error: value.error});
} else {
next();
}
Expand Down

0 comments on commit 34dca6c

Please sign in to comment.