Skip to content

Commit 29513dd

Browse files
committed
1.0.2 && getting start testing
1 parent c6ea2e8 commit 29513dd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1280
-1128
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
node_modules/
1+
node_modules/

FearAllah.PNG

17.7 KB
Loading

Models.PNG

82.3 KB
Loading
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
{
2-
"jwtPrivatekey": "virtual_jwtPrivatekey"
3-
}
1+
{
2+
"jwtPrivatekey": "virtual_jwtPrivatekey"
3+
}

config/default.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
{
2-
"jwtPrivatekey": ""
3-
}
1+
{
2+
"jwtPrivatekey": ""
3+
}

controllers/admin.js

Lines changed: 108 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,108 @@
1-
const Admin = require('../models/Admin');
2-
const joi = require('joi');
3-
const bcrypt = require('bcrypt');
4-
5-
const registerValidator = (data) => {
6-
const schema = {
7-
name: joi.string().max(55).required(),
8-
userName: joi.string().max(100).required(),
9-
password: joi.string().required(),
10-
confirmPassword: joi.string().required()
11-
}
12-
13-
return joi.validate(data,schema);
14-
}
15-
16-
const updateValidator = data => {
17-
const schema = {
18-
name: joi.string().max(55).required(),
19-
userName: joi.string().max(100).required(),
20-
oldPassword: joi.string().required(),
21-
newPassword: joi.string().required()
22-
}
23-
return joi.validate(data,schema);
24-
}
25-
26-
const loginValidator = data => {
27-
const schema = {
28-
userName: joi.string().max(256).required(),
29-
password: joi.string().required()
30-
}
31-
return joi.validate(data,schema)
32-
}
33-
34-
const login = async (req,res) => {
35-
const {error} = loginValidator(req.body);
36-
if(error) return res.status(400).send(error.message);
37-
const {userName,password} = req.body;
38-
const admin = await Admin.findOne({userName});
39-
if(!admin) return res.status(400).send("usernaem or password is invalid1");
40-
const isValidPass = await bcrypt.compare(password,admin.password);
41-
if(!isValidPass) return res.status(400).send("username or password is invalid2");
42-
const token = admin.getAdminToken();
43-
return res.header('admin_token',token).status(200).send(token);
44-
}
45-
46-
47-
const register = async (req,res) => {
48-
const {error} = registerValidator(req.body);
49-
if(error) return res.status(400).send(error.message);
50-
51-
const {name,userName,password,confirmPassword} = req.body;
52-
if(password !== confirmPassword) return res.status(400).send("confirm password doesn't match with password !")
53-
const admin = await new Admin({name,userName,password});
54-
const salt = await bcrypt.genSalt(10);
55-
admin.password = await bcrypt.hash(admin.password,salt);
56-
await admin.save();
57-
if(!admin) return res.status(500).send("something wrong");
58-
return res.status(201).send(true);
59-
}
60-
61-
const getUpdateAdmin = async (req,res) => {
62-
const {error} = updateValidator(req.body);
63-
if(error) return res.status(400).send(error.message);
64-
65-
const {name,userName,oldPassword,newPassword} = req.body;
66-
const admin = await Admin.findById(req.admin._id);
67-
const oldPassIsValid = await bcrypt.compare(oldPassword,admin.password);
68-
if(!oldPassIsValid) return res.status(400).send("old password doesn't match!");
69-
const salt = await bcrypt.genSalt(10);
70-
const password = await bcrypt.hash(newPassword,salt);
71-
const updated = await Admin.findByIdAndUpdate({_id: req.admin._id},{$set: {name,userName,password}},{new: true});
72-
if(!updated) return res.status(500).send("something wrong");
73-
return res.status(200).send(updated);
74-
}
75-
76-
const getAllAdmin = async (req,res) => {
77-
const admins = await Admin.find();
78-
if(!admins) return res.status(500).send("something wrong");
79-
return res.status(200).send(admins);
80-
}
81-
82-
const getSingleAdmin = async (req,res) => {
83-
const admin = await Admin.findById(req.params.adminId);
84-
if(!admin) return res.status(500).send("something wrong");
85-
return res.status(200).send(admin);
86-
}
87-
88-
const getMe = async (req,res) => {
89-
const admin = await Admin.findById(req.admin._id);
90-
if(!admin) return res.status(500).send("something wrong");
91-
return res.status(200).send(admin);
92-
}
93-
94-
const deleteAdmin = async (req,res) => {
95-
const admin = await Admin.findByIdAndDelete(req.params.adminId);
96-
if(!admin) return res.status(500).send("something wrong");
97-
return res.status(200).send(admin);
98-
}
99-
100-
module.exports = {
101-
register,
102-
getAllAdmin,
103-
getSingleAdmin,
104-
getMe,
105-
deleteAdmin,
106-
login,
107-
getUpdateAdmin
108-
}
1+
const Admin = require('../models/Admin');
2+
const joi = require('joi');
3+
const bcrypt = require('bcrypt');
4+
5+
const registerValidator = (data) => {
6+
const schema = {
7+
name: joi.string().max(55).required(),
8+
userName: joi.string().max(100).required(),
9+
password: joi.string().required(),
10+
confirmPassword: joi.string().required()
11+
}
12+
13+
return joi.validate(data,schema);
14+
}
15+
16+
const updateValidator = data => {
17+
const schema = {
18+
name: joi.string().max(55).required(),
19+
userName: joi.string().max(100).required(),
20+
oldPassword: joi.string().required(),
21+
newPassword: joi.string().required()
22+
}
23+
return joi.validate(data,schema);
24+
}
25+
26+
const loginValidator = data => {
27+
const schema = {
28+
userName: joi.string().max(256).required(),
29+
password: joi.string().required()
30+
}
31+
return joi.validate(data,schema)
32+
}
33+
34+
const login = async (req,res) => {
35+
const {error} = loginValidator(req.body);
36+
if(error) return res.status(400).send(error.message);
37+
const {userName,password} = req.body;
38+
const admin = await Admin.findOne({userName});
39+
if(!admin) return res.status(400).send("usernaem or password is invalid1");
40+
const isValidPass = await bcrypt.compare(password,admin.password);
41+
if(!isValidPass) return res.status(400).send("username or password is invalid2");
42+
const token = admin.getAdminToken();
43+
return res.header('admin_token',token).status(200).send(token);
44+
}
45+
46+
47+
const register = async (req,res) => {
48+
const {error} = registerValidator(req.body);
49+
if(error) return res.status(400).send(error.message);
50+
51+
const {name,userName,password,confirmPassword} = req.body;
52+
if(password !== confirmPassword) return res.status(400).send("confirm password doesn't match with password !")
53+
const admin = await new Admin({name,userName,password});
54+
const salt = await bcrypt.genSalt(10);
55+
admin.password = await bcrypt.hash(admin.password,salt);
56+
await admin.save();
57+
if(!admin) return res.status(500).send("something wrong");
58+
return res.status(201).send(true);
59+
}
60+
61+
const getUpdateAdmin = async (req,res) => {
62+
const {error} = updateValidator(req.body);
63+
if(error) return res.status(400).send(error.message);
64+
65+
const {name,userName,oldPassword,newPassword} = req.body;
66+
const admin = await Admin.findById(req.admin._id);
67+
const oldPassIsValid = await bcrypt.compare(oldPassword,admin.password);
68+
if(!oldPassIsValid) return res.status(400).send("old password doesn't match!");
69+
const salt = await bcrypt.genSalt(10);
70+
const password = await bcrypt.hash(newPassword,salt);
71+
const updated = await Admin.findByIdAndUpdate({_id: req.admin._id},{$set: {name,userName,password}},{new: true});
72+
if(!updated) return res.status(500).send("something wrong");
73+
return res.status(200).send(updated);
74+
}
75+
76+
const getAllAdmin = async (req,res) => {
77+
const admins = await Admin.find();
78+
if(!admins) return res.status(500).send("something wrong");
79+
return res.status(200).send(admins);
80+
}
81+
82+
const getSingleAdmin = async (req,res) => {
83+
const admin = await Admin.findById(req.params.adminId);
84+
if(!admin) return res.status(500).send("something wrong");
85+
return res.status(200).send(admin);
86+
}
87+
88+
const getMe = async (req,res) => {
89+
const admin = await Admin.findById(req.admin._id);
90+
if(!admin) return res.status(500).send("something wrong");
91+
return res.status(200).send(admin);
92+
}
93+
94+
const deleteAdmin = async (req,res) => {
95+
const admin = await Admin.findByIdAndDelete(req.params.adminId);
96+
if(!admin) return res.status(500).send("something wrong");
97+
return res.status(200).send(admin);
98+
}
99+
100+
module.exports = {
101+
register,
102+
getAllAdmin,
103+
getSingleAdmin,
104+
getMe,
105+
deleteAdmin,
106+
login,
107+
getUpdateAdmin
108+
}

controllers/category.js

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
1-
const Category = require('../models/Category');
2-
3-
const postCategory = async (req,res) => {
4-
const {name} = req.body;
5-
const category = await new Category({name});
6-
if(!category) return res.status(500).send({error: {message: "Something wrong"}})
7-
await category.save()
8-
return res.status(201).send(category);
9-
}
10-
11-
const getAllCategory = async (req,res) => {
12-
const category = await Category.find();
13-
if(!category) return res.status(500).send({error: {message: "Something wrong!"}});
14-
return res.status(200).send(category);
15-
}
16-
17-
const getSingleCategory = async (req,res) => {
18-
const category = await Category.findByID(req.params.id);
19-
if(!category) return res.status(500).send({error: {message: "Something wrong!"}});
20-
return res.status(200).send(category);
21-
}
22-
23-
const getUpdateCategory = async (req,res) => {
24-
const {name} = req.body;
25-
const category = await Category.findByIdAndUpdate(req.params.id,{$set: {name}},{new: true});
26-
if(!category) return res.status(500).send({error: {message: "Something wrong!"}})
27-
return res.status(201).send(category);
28-
}
29-
30-
const deleteCategory = async (req,res) => {
31-
let category = await Category.findByIdAndDelete(req.params.id);
32-
if(!category) return res.status(500).send({error: {message: "Something wrong"}});
33-
return res.status(200).send(category);
34-
}
35-
36-
module.exports = {
37-
postCategory,
38-
getAllCategory,
39-
getSingleCategory,
40-
getUpdateCategory,
41-
deleteCategory
42-
}
1+
const Category = require('../models/Category');
2+
3+
const postCategory = async (req,res) => {
4+
const {name} = req.body;
5+
const category = await new Category({name});
6+
if(!category) return res.status(500).send({error: {message: "Something wrong"}})
7+
await category.save()
8+
return res.status(201).send(category);
9+
}
10+
11+
const getAllCategory = async (req,res) => {
12+
const category = await Category.find();
13+
if(!category) return res.status(500).send({error: {message: "Something wrong!"}});
14+
return res.status(200).send(category);
15+
}
16+
17+
const getSingleCategory = async (req,res) => {
18+
const category = await Category.findByID(req.params.id);
19+
if(!category) return res.status(500).send({error: {message: "Something wrong!"}});
20+
return res.status(200).send(category);
21+
}
22+
23+
const getUpdateCategory = async (req,res) => {
24+
const {name} = req.body;
25+
const category = await Category.findByIdAndUpdate(req.params.id,{$set: {name}},{new: true});
26+
if(!category) return res.status(500).send({error: {message: "Something wrong!"}})
27+
return res.status(201).send(category);
28+
}
29+
30+
const deleteCategory = async (req,res) => {
31+
let category = await Category.findByIdAndDelete(req.params.id);
32+
if(!category) return res.status(500).send({error: {message: "Something wrong"}});
33+
return res.status(200).send(category);
34+
}
35+
36+
module.exports = {
37+
postCategory,
38+
getAllCategory,
39+
getSingleCategory,
40+
getUpdateCategory,
41+
deleteCategory
42+
}

0 commit comments

Comments
 (0)