Skip to content

Commit

Permalink
Merge pull request #5 from amiparadis250/ft-mybrand-ci
Browse files Browse the repository at this point in the history
Ft mybrand ci
  • Loading branch information
amiparadis250 authored Mar 8, 2024
2 parents 6833e7e + ed9bb69 commit 9383466
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 43 deletions.
6 changes: 3 additions & 3 deletions --Test--/sample.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ describe('blogs controllers',()=>{
const response=await supertest(app).delete(`/api/blogs/'+${4455}`)
.set('Authorization', `Bearer ${ Authtoken }`);
expect(response.statusCode).toBe(500);
expect(response.body.status).toBe('error')

});

it("POST api/blogs/:id :Updating blog", async()=>{
Expand Down Expand Up @@ -278,8 +278,8 @@ describe("Creation Comments", () => {
})
.set('Authorization', `Bearer ${Authtoken}`);

expect(response.statusCode).toBe(500);
expect(response.body.status).toBe('error');
expect(response.statusCode).toBe(403);

});

it("POST api/blogs/:id/comments: Creating Comments with valid blogID", async () => {
Expand Down
8 changes: 8 additions & 0 deletions .hintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": [
"development"
],
"hints": {
"typescript-config/strict": "off"
}
}
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
3 changes: 2 additions & 1 deletion src/routes/commentRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import {
} from '../controllers/commentsCtl';
import { isLogin } from '../middlewares/isLogin';
import isAdmin from '../middlewares/isAdmin';
import validateComments from '../validations/commentsvalidation';

const commentRoutes = express.Router();

commentRoutes.post('/:id/comments', isLogin, addComment);
commentRoutes.post('/:id/comments', isLogin,validateComments ,addComment);
commentRoutes.delete('/:id/comments/:commentId', isLogin, isAdmin, deleteComment);
commentRoutes.get('/:id/comments/:commentId', getOneComment);
commentRoutes.get('/:id/comments', getAllCommentsForBlog);
Expand Down
12 changes: 6 additions & 6 deletions src/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
"password": { "type": "string", "example": "12345790003Us*@" },
"fullName": { "type": "string", "example": "[email protected]" }
},
"required": ["email", "password", "FullName"]
"required": ["email", "password", "fullName"]
}
}
}
Expand All @@ -110,13 +110,13 @@
"email": { "type": "string" },
"password": { "type": "string" }
},
"required": ["FullName", "email", "password"]
"required": ["fullName", "email", "password"]
}
}
}
},
"409": {
"description": "User already exists"
"40o": {
"description": "Bad Request"
}
}
}
Expand Down Expand Up @@ -157,8 +157,8 @@
}
}
},
"500": {
"description": "Wrong login credentials"
"400": {
"description": "Bad Request"
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/validations/QuerriesValidation.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import joi from 'joi';
export const createQuerryValidationSchema =joi.object({
email: joi.string().email().required(),
guestName: joi.string().pattern(/^[a-zA-Z\s]{5,}$/).required(),
guestQuery: joi.string().min(10).required(),
import joi from 'joi'

export const createQuerryValidationSchema = joi.object({
email: joi.string().email().required(),
guestName: joi.string().pattern(/^[a-zA-Z\s]{5,}$/).required(),
guestQuery: joi.string().min(5).required(),
});


Expand Down
10 changes: 0 additions & 10 deletions src/validations/blogsValidatio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,6 @@ export const updateBlogValidation = Joi.object({
content: Joi.string().max(5000),
});

// export const addCommentValidation = Joi.object({
// text: Joi.string().required().max(1000),
// commenterName: Joi.string().required().max(255),
// commenterEmail: Joi.string().email().required().max(255),
// });

// export const updateCommentValidation = Joi.object({
// text: Joi.string().max(1000),
// commenterName: Joi.string().max(255),
// commenterEmail: Joi.string().email().max(255),
// });


17 changes: 17 additions & 0 deletions src/validations/commentsvalidation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import joi from 'joi'

export const commentsValidationSchema = joi.object({
text: joi.string().required().max(1000).trim().message('Please enter a valid comment'),
commenterName: joi.string().required().max(255).regex(/^[A-Za-z\s]+$/).trim().message('Please enter a valid name without special characters'),
commenterEmail: joi.string().email().required().max(255).trim().message('Please enter a valid email'),
});

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});
} else {
next();
}
};
export default validateComments
4 changes: 2 additions & 2 deletions src/validations/userValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ export const validateUserSchema= joi.object({
password:joi.string().min(12).regex(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]+$/)
.message('Password must be at least 12 characters long and include at least one lowercase letter, one uppercase letter, one digit, and one special character.'),
fullName:joi.string().min(3).max(70).regex(/^[a-zA-Z]+(?: [a-zA-Z]+)*$/)
.message('Full name must be between 3 and 70 characters and should not include special characters.'),
.message('Your names must be between 3 and 70 characters and should not include special characters.'),
})
const usersValidation = async (req:any, res:any, next) => {
const value = validateUserSchema.validate(req.body, { abortEarly: false });
if (value.error) {
return res.status(400).json({error: value.error.details});
return res.status(403).send({ error: value.error});
} else {
next();
}
Expand Down

0 comments on commit 9383466

Please sign in to comment.