-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphotoController.js
158 lines (111 loc) · 3.33 KB
/
photoController.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import Photo from "../models/photoModels.js";
//cloud sonra yaptik
import {v2 as cloudinary} from 'cloudinary'
import fs from 'fs'
//temelde baasit yapiyi kurdiuk geleni req.body attik sonra res statiu sile geri gonderdik
const createPhoto = async (req, res) => {
//burada colud islemi yukleme yaman alir dasboard form nctype="multipart/form-data"ve name image ekledik onemli
const result = await cloudinary.uploader.upload(
req.files.image.tempFilePath,{
use_filename:true,
folder:'lenslight_tr'
}
)
console.log(result)
try {
await Photo.create({
name:req.body.name,
description:req.body.description,
user:res.locals.user._id,
url:result.secure_url,
image_id:result.public_id
});
fs.unlinkSync(req.files.mage.tempFilePath)
res.status(201).redirect('/users/dashboard');
} catch (error) {
res.status(500).json({
succed: false,
error,
});
}
};
//butun fotograflari cagirmak icin
//ilk basta boyle zapariy geneli gormek icin
/**
* const getAllPhotos = async(req,res)=>{
try {
const photos = await Photo.find({})
res.status(200).json({
succed:true,
photos
})
} catch (err) {
}
}
*/
//burda sageerkeli sayfayi ]render ederiz
// res.status(200).render('photos') sonra gelenleri de yuklemekicin r('photos',{photos }) yaoarak yenileri de //yukleriz
const getAllPhotos = async(req,res)=>{
try {
const photos = res.locals.user
? await Photo.find({user:{$ne:res.locals.user._id}})
: await Photo.find({})
res.status(200).render('photos',{
photos,
link: 'photos',
})
} catch (err) {
}
}
//fotografin detailin gitmek icin
//obje oldugundan ({_id : req.body.id}) : iki nokta ile esirledik buna dikkat
const getAPhotos = async(req,res)=>{
try {
const photo = await Photo.findById({ _id: req.params.id }).populate('user')
let isOwner = false;
if (res.locals.user) {
isOwner = photo.user.equals(res.locals.user._id);
}
res.status(200).render('photo',{
photo,
link: 'photos',
isOwner
})
} catch (err) {
}
}
const deletePhoto= async(req,res)=>{
try {
const photo = await Photo.findById(req.params.id)
const photoId =photo.image_id
await cloudinary.uploader.destroy(photoId)
await Photo.findByIdAndRemove({_id : req.params.id})
res.status(200).redirect('/users/dashboard');
} catch (err) {
}
}
const updatePhoto= async(req,res)=>{
try {
const photo = await Photo.findById(req.params.id);
if (req.files) {
const photoId = photo.image_id;
await cloudinary.uploader.destroy(photoId);
const result = await cloudinary.uploader.upload(
req.files.image.tempFilePath,
{
use_filename: true,
folder: 'lenslight_tr',
}
);
photo.url = result.secure_url;
photo.image_id = result.public_id;
fs.unlinkSync(req.files.image.tempFilePath);
}
photo.name = req.body.name;
photo.description = req.body.description;
photo.save();
res.status(200).redirect(`/photos/${req.params.id}`);
} catch (err) {
}
}
export { createPhoto,getAllPhotos,getAPhotos,deletePhoto,updatePhoto };