Skip to content

Commit 4408d67

Browse files
committed
duplicated endpoints calls deleted
1 parent 804533a commit 4408d67

File tree

5 files changed

+23
-39
lines changed

5 files changed

+23
-39
lines changed

server/Service/commentService.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,7 @@ const commentService = {
77
createComment,
88
};
99

10-
// get comment by id
11-
async function getCommentById(id) {
12-
const comment = await Comment.findById(id);
13-
if (comment) {
14-
return comment;
15-
} else {
16-
return new NotFoundError(`Comment Not Found`);
17-
}
18-
}
10+
1911

2012
// create comment
2113
async function createComment(userId, calification, description) {
@@ -40,7 +32,6 @@ async function getAllComment(page, size) {
4032

4133
// delete comment by id
4234
async function deleteComment(commentId) {
43-
await getCommentById(commentId);
4435
const res = await Comment.findByIdAndDelete(commentId);
4536
return res;
4637
}

server/Service/productService.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function isValidObjectId(id) {
2323
return false;
2424
}
2525
}
26-
26+
// get product by id
2727
async function getProductById(productId) {
2828
if (productId && isValidObjectId(productId)) {
2929
const product = await Product.findById(productId);
@@ -32,6 +32,7 @@ async function getProductById(productId) {
3232
throw new NotFoundError(`Product with id ${productId} Not found`);
3333
}
3434
}
35+
// pagination products
3536

3637
async function getProduct(page, size) {
3738
// Pagination for products
@@ -51,6 +52,7 @@ async function getProduct(page, size) {
5152
return { products, totalPages };
5253
}
5354

55+
// recomendations && categories
5456
async function getProductByTag(category, page, size) {
5557
const actualPage = parseInt(page) || 1;
5658
const pageSize = parseInt(size) || 8;
@@ -67,6 +69,7 @@ async function getProductByTag(category, page, size) {
6769
return { products, totalPages };
6870
}
6971

72+
// create products
7073
async function createProduct(
7174
name,
7275
img,
@@ -89,13 +92,14 @@ async function createProduct(
8992
});
9093
return await newProduct.save();
9194
}
92-
95+
// products by query
9396
async function searchProduct(query) {
9497
const product = await Product.find({
9598
name: { $regex: query, $options: 'i' },
9699
}).limit(40);
97100
return product;
98101
}
102+
// change products properties
99103

100104
async function updateProduct(productId, newPrice, hot, newDescription) {
101105
const updatedProperties = {};
@@ -117,9 +121,8 @@ async function updateProduct(productId, newPrice, hot, newDescription) {
117121
);
118122
return result;
119123
}
120-
124+
// delete products
121125
async function deleteProduct(productId) {
122-
await getProductById(productId);
123126
return await Product.findOneAndDelete(productId);
124127
}
125128

server/Service/purchasesService.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@ const purchaseService = {
1313
processPayment,
1414
};
1515

16+
// find purchase by id
1617
async function findPurchaseById(id) {
1718
const purchase = await Purchase.findById(id);
18-
if (purchase) {
1919
return purchase;
20-
} else {
21-
throw new NotFoundError(`Purchase with id ${id} Not found`);
22-
}
23-
}
2420

21+
}
22+
// create purchases
2523
async function createPurchase(userId, cartId, amount, shippingAddress) {
2624
const purchase = new Purchase({
2725
userId,
@@ -32,7 +30,7 @@ async function createPurchase(userId, cartId, amount, shippingAddress) {
3230

3331
return await purchase.save();
3432
}
35-
33+
// get all purchases
3634
async function getAllPurchase(page, size) {
3735
// get a pagination with purchases instead all purchases
3836
const actualPage = parseInt(page) || 1;
@@ -50,11 +48,11 @@ async function getAllPurchase(page, size) {
5048

5149
return { products, totalPages };
5250
}
53-
51+
// get user purchases
5452
async function getUserPurchase(id) {
5553
return await Purchase.find({ userId: id });
5654
}
57-
55+
// get monthly purchases
5856
async function getMonthly() {
5957
//show the last 2 months sales
6058
const date = new Date();
@@ -78,12 +76,11 @@ async function getMonthly() {
7876
]);
7977
return income;
8078
}
81-
79+
// delete purchases
8280
async function cleanPurchase(id) {
83-
await findPurchaseById(id);
8481
return await Purchase.findByIdAndDelete(id);
8582
}
86-
83+
// change status purchases
8784
async function updatePurchaseState(id, status) {
8885
await findPurchaseById(id);
8986
const newStatus = await Purchase.findOneAndUpdate(

server/Service/shopingCartService.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,13 @@ const cartService = {
99
getAll,
1010
};
1111

12-
// get cart by id
12+
// get cart by id
1313
async function findCartById(id) {
1414
const cart = await ShoppingCart.findById(id);
15-
if (cart) {
16-
return cart;
17-
} else {
18-
return new NotFoundError(`Cart Not Found`);
19-
}
15+
return cart;
2016
}
2117

22-
// create cart
18+
// create cart
2319
async function createCart(products, userId) {
2420
const newCart = new ShoppingCart({ products, userId });
2521
return await newCart.save();
@@ -44,15 +40,14 @@ async function getAll(page, size) {
4440
return { carts, totalPages };
4541
}
4642

47-
// get user carts by user id
43+
// get user carts by user id
4844
async function getUserCart(userId) {
4945
await userService.getUser(userId);
5046
return await ShoppingCart.findOne({ userId });
5147
}
5248

5349
// edit user cart by id
5450
async function editCart(body, cartId) {
55-
await findCartById(cartId);
5651
return await ShoppingCart.findByIdAndUpdate(
5752
cartId,
5853
{
@@ -64,7 +59,6 @@ async function editCart(body, cartId) {
6459

6560
//delete user cart by cart id
6661
async function deleteCart(cartId) {
67-
await findCartById(cartId);
6862
return await ShoppingCart.findByIdAndDelete(cartId);
6963
}
7064

server/Service/usersService.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ async function signIn(user) {
5252
}
5353

5454
async function checkLength(item, num) {
55-
if (item.length >= num) {
55+
if (item.length >= num) {
5656
return true;
5757
} else {
5858
throw new BadRequestError(
@@ -71,15 +71,14 @@ async function signUp(user) {
7171
return obj;
7272
}, {});
7373

74-
const registeredUser = await findByEmail(filteredUser.email);
7574
const validatePass = await checkLength(filteredUser.password, 8);
76-
if (!registeredUser && validatePass) {
75+
if (validatePass) {
7776
const encryptPassword = await encrypt.hashPassword(filteredUser.password);
7877
const createUser = new User({ ...filteredUser, password: encryptPassword });
7978
const newUser = await createUser.save();
8079
return newUser;
8180
} else {
82-
throw new BadRequestError('email already in use');
81+
throw new BadRequestError('password does not match');
8382
}
8483
}
8584

0 commit comments

Comments
 (0)