Skip to content

Commit

Permalink
Merge pull request #7 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 11, 2024
2 parents 8795632 + d91cc8a commit 4194da2
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 40 deletions.
10 changes: 5 additions & 5 deletions --Test--/sample.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ describe('Aunthetications', () => {
const response = await supertest(app)
.post('/api/users/login')
.send({
"password": "12345790003Us*@",
"email": "pishimweaime5000@gmail.com"
"password": "0791966291Is*",
"email": "amiparadis250@gmail.com"
});

// Check if the response status is 200
Expand Down Expand Up @@ -213,14 +213,14 @@ describe('blogs controllers',()=>{
});

it("POST api/blogs/:id :Updating blog", async()=>{
const response=await supertest(app).put('/api/blogs/'+blogID).
const response=await supertest(app).patch('/api/blogs/'+blogID).
set('Authorization', `Bearer ${ Authtoken }`);
expect(response.statusCode).toBe(200);
expect(response.body.status).toBe('success')

});
it("POST api/blogs/:id :Updating blog without Authorization", async()=>{
const response=await supertest(app).put('/api/blogs/'+blogID);
const response=await supertest(app).patch('/api/blogs/'+blogID);
expect(response.statusCode).toBe(401);
expect(response.body.status).toBe('error');
expect(response.body.message).toBe('Unauthorized. Token not provided.')
Expand Down Expand Up @@ -347,4 +347,4 @@ describe("Creation Comments", () => {
expect(response.statusCode).toBe(500);
});
});
});
});
Binary file added --Test--/uploads/tesing.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"liveServer.settings.port": 5501
"liveServer.settings.port": 5502
}
32 changes: 32 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"debug": "^4.3.4",
"dotenv": "^16.4.4",
"express": "^4.18.2",
"form-data-extended": "^0.0.10",
"install": "^0.13.0",
"joi": "^17.12.1",
"join": "^3.0.0",
Expand Down
4 changes: 0 additions & 4 deletions src/controllers/blogsCtrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@ export const uploadImageToCloudinary = async (buffer: Buffer): Promise<string> =
}
};

// ... (rest of your code)


// ... (rest of your code)



Expand Down
25 changes: 10 additions & 15 deletions src/controllers/commentsCtl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,29 +173,24 @@ export const deleteComment = async (req: Request, res: Response) => {
const commentId = req.params.commentId;

try {
// Find the blog by ID
const blog = await Blog.findById(blogId);

if (!blog) {
// Use findByIdAndUpdate to remove the comment directly
const result = await Blog.findByIdAndUpdate(
blogId,
{ $pull: { comments: { _id: commentId } } },
{ new: true }
).populate('comments');

if (!result) {
return res.status(404).json({
status: 'error',
message: 'Blog not found',
});
}

// Remove the comment by ID from the comments array
blog.comments = blog.comments.filter(comment => comment._id.toString() !== commentId);

// Save the updated blog
await blog.save();

// Populate comments before responding
await blog.populate('comments');

// Respond with success and the updated comments array
res.json({
status: 'success',
data: blog.comments,
data: result.comments,
});
} catch (err) {
console.error('Error in deleteComment:', err);
Expand All @@ -204,4 +199,4 @@ export const deleteComment = async (req: Request, res: Response) => {
message: err.message,
});
}
};
};
2 changes: 1 addition & 1 deletion src/model/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const userSchema = new mongoose.Schema({
},
isAdmin: {
type: Boolean,
default: true,
default: false,
},
blogs: [
{
Expand Down
15 changes: 3 additions & 12 deletions src/routes/blogRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// routes/Blogs/allRoutes.js
import express from 'express';
import multer from 'multer';
import { createBlogValidation, updateBlogValidation, } from '../validations/blogsValidatio';
import { createBlogValidation } from '../validations/blogsValidatio';
import {
createBlog,
updateBlogById,
Expand Down Expand Up @@ -45,18 +45,9 @@ router.post('/', upload.single('file'),isLogin,isAdmin, async (req:any, res) =>
});

// Update a blog by ID
router.put('/:id', upload.single('file'),isLogin,isAdmin, async (req:any, res) => {
router.patch('/:id', upload.single('file'), isLogin, isAdmin, async (req: any, res) => {
try {
// Validate request body
const { error } = updateBlogValidation.validate(req.body);
if (error) {
return res.status(400).json({
status: 'error',
message: error.details[0].message,
});
}

// Continue with updating the blog
// Continue with updating the blog without validation
await updateBlogById(req, res);
} catch (err) {
res.status(500).json({
Expand Down
13 changes: 12 additions & 1 deletion src/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"post": {
"summary": "Create an account",
"tags": ["Authentication"],
"security": [],
"requestBody": {
"content": {
"application/json": {
Expand Down Expand Up @@ -125,6 +126,7 @@
"post": {
"summary": "User Login",
"tags": ["Authentication"],
"security": [],
"requestBody": {
"content": {
"application/json": {
Expand Down Expand Up @@ -160,6 +162,7 @@
"400": {
"description": "Bad Request"
}

}
}
},
Expand Down Expand Up @@ -285,6 +288,7 @@
"get": {
"summary": "Read blogs",
"tags": ["Blogs"],
"security": [],
"responses": {
"200": {
"description": "OK",
Expand Down Expand Up @@ -333,6 +337,7 @@
"get": {
"summary": "Read a single blog",
"tags": ["Blogs"],
"security": [],
"parameters": [
{
"name": "id",
Expand Down Expand Up @@ -396,7 +401,7 @@
}
}
},
"put": {
"patch": {
"summary": "Update a blog",
"tags": ["Blogs"],
"parameters": [
Expand Down Expand Up @@ -554,6 +559,7 @@
"post": {
"summary": "Add a comment to a blog",
"tags": ["Comments"],
"security": [],
"parameters": [
{
"name": "id",
Expand Down Expand Up @@ -622,6 +628,7 @@
"get": {
"summary": "Read comments for a blog",
"tags": ["Comments"],
"security": [],
"parameters": [
{
"name": "id",
Expand All @@ -644,6 +651,7 @@
"post": {
"summary": "Create a user query",
"tags": ["Queries"],
"security": [],
"consumes": ["application/json"],
"produces": ["application/json"],
"requestBody": {
Expand Down Expand Up @@ -737,6 +745,7 @@
"post": {
"summary": "Add a like to a blog",
"tags": ["Likes"],
"security": [],
"parameters": [
{
"name": "id",
Expand All @@ -759,6 +768,7 @@
"post": {
"summary": "Remove like from a blog",
"tags": ["Likes"],
"security": [],
"parameters": [
{
"name": "id",
Expand All @@ -781,6 +791,7 @@
"post": {
"summary": "Add views to a blog",
"tags": ["Likes"],
"security": [],
"parameters": [
{
"name": "id",
Expand Down
2 changes: 1 addition & 1 deletion src/validations/blogsValidatio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Joi from 'joi';
export const createBlogValidation = Joi.object({
title: Joi.string().required().max(255),
desc: Joi.string().required().max(500),
content: Joi.string().required().max(5000),
content: Joi.string().required().max(1000000),

});

Expand Down

0 comments on commit 4194da2

Please sign in to comment.