Skip to content

Commit 6c0629b

Browse files
committed
Added upload image feature
1 parent 76e2d57 commit 6c0629b

13 files changed

+76
-20
lines changed

.gitignore

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
node_modules
2-
config
1+
node_modules

models/designer.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ var designerSchema = new mongoose.Schema({
5454
},
5555
imageLink:{
5656
type:String,
57-
trim:true,
58-
validate:[validator.isURL, "Please enter URL in a valid format"]
57+
trim:true
58+
},
59+
imagePath:{
60+
type:String
5961
}
6062
},{
6163
timestamps:true

models/designerMaker.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,10 @@ var designerMakerSchema = new mongoose.Schema({
8383
},
8484
imageLink:{
8585
type:String,
86-
trim:true,
87-
validate:[validator.isURL, "Please enter URL in a valid format"]
86+
trim:true
87+
},
88+
imagePath:{
89+
type:String
8890
}
8991
},{
9092
timestamps:true

models/maker.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ var makerSchema = new mongoose.Schema({
5858
},
5959
imageLink:{
6060
type:String,
61-
trim:true,
62-
validate:[validator.isURL, "Please enter URL in a valid format"]
61+
trim:true
62+
},
63+
imagePath:{
64+
type:String
6365
}
6466
},{
6567
timestamps:true

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
"multer": "^1.4.2",
3232
"path": "^0.12.7",
3333
"sharp": "^0.25.2",
34-
"validator": "^13.0.0",
35-
"nodemon": "^2.0.2"
34+
"validator": "^13.0.0"
3635
}
3736
}

public/css/main.css

+6-1
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,9 @@ label{
3131

3232
small{
3333
color: aliceblue;
34-
}
34+
}
35+
36+
/* .image_button{
37+
position: absolute;
38+
font-size: 50px;
39+
} */

routes/users.js

+49-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var storage = multer.diskStorage({
2121
cb(null, 'uploads/');
2222
},
2323
filename: function (req, file, cb) {
24-
cb(null, new Date().toISOString() + file.originalname);
24+
cb(null, Date.now() + file.originalname);
2525
}
2626

2727
});
@@ -78,8 +78,24 @@ router.get('/designerMaker', (req, res) => {
7878

7979
router.post('/designer', upload.single('designerImage'), async (req, res) => {
8080
console.log(req.file);
81+
var imagePath = "";
82+
if (req.file) {
83+
imagePath=req.file.path;
84+
}
85+
86+
//creating body for object
87+
var data = {
88+
email: req.body.email,
89+
password: req.body.password,
90+
capacity: req.body.capacity,
91+
category: req.body.category,
92+
training: req.body.training,
93+
imageLink: req.body.imageLink,
94+
imagePath
95+
}
96+
8197
try {
82-
var designer = new Designer(req.body);
98+
var designer = new Designer(data);
8399
await designer.save();
84100

85101
//To display flash message
@@ -103,9 +119,21 @@ router.post('/designer', upload.single('designerImage'), async (req, res) => {
103119
// ─── CREATE A MAKER ──────────────────────────────────────────────────────────
104120
//
105121

106-
router.post('/maker',convert, async (req, res) => {
122+
router.post('/maker',convert, upload.single('makerImage'), async (req, res) => {
123+
124+
//creating body for object
125+
var data = {
126+
email: req.body.email,
127+
password: req.body.password,
128+
capacity: req.body.capacity,
129+
materials: req.body.materials,
130+
location: req.body.location,
131+
imageLink: req.body.imageLink,
132+
imagePath: req.file.path
133+
}
134+
107135
try {
108-
var maker = new Maker(req.body);
136+
var maker = new Maker(data);
109137
await maker.save();
110138

111139
//To display flash message
@@ -129,9 +157,24 @@ router.post('/maker',convert, async (req, res) => {
129157
// ─── CREATE A DESIGNER+MAKER ──────────────────────────────────────────────────────────
130158
//
131159

132-
router.post('/designerMaker',convert, async (req, res) => {
160+
router.post('/designerMaker',convert,upload.single('designerMakerImage'), async (req, res) => {
161+
162+
//creating body for object
163+
var data = {
164+
email: req.body.email,
165+
password: req.body.password,
166+
makerCapacity: req.body.makerCapacity,
167+
designerCapacity: req.body.designerCapacity,
168+
category: req.body.category,
169+
material: req.body.material,
170+
training: req.body.training,
171+
location: req.body.location,
172+
imageLink: req.body.imageLink,
173+
imagePath: req.file.path
174+
}
175+
133176
try {
134-
var designerMaker = new DesignerMaker(req.body);
177+
var designerMaker = new DesignerMaker(data);
135178
await designerMaker.save();
136179

137180
//To display flash message

uploads/1586249536235image12.jpeg

579 KB
Loading

uploads/1586249633273image12.jpeg

579 KB
Loading
File renamed without changes.
Binary file not shown.
-68.3 KB
Binary file not shown.

views/designer.ejs

+7-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
<!--
3838
-- FORM STARTS
3939
-->
40-
<form action="/designer" method="POST">
40+
<form action="/designer" method="POST" enctype="multipart/form-data">
4141
<div class="row">
4242
<div class=" col-md-6 form-group">
4343
<label for="email">Email address</label>
@@ -78,8 +78,12 @@
7878
<input type="url" name="imageLink" class="form-control" id="imageLink" placeholder="Link to the Image">
7979
</div>
8080

81-
<input type="submit" value="Submit" class="btn btn-primary mb-2 float-right">
82-
<a class="btn btn-primary float-left" href="/" role="button">Go Home</a>
81+
<div class="d-flex justify-content-between">
82+
<a class="col-3 btn btn-primary " href="/" role="button">Go Home</a>
83+
<input type="file" class="image_button" name="designerImage">
84+
<input type="submit" value="Submit" class="col-3 btn btn-primary">
85+
86+
</div>
8387

8488
</form>
8589
</div>

0 commit comments

Comments
 (0)