Skip to content

Commit 8348057

Browse files
committed
casi
1 parent 4df5354 commit 8348057

Some content is hidden

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

50 files changed

+1681
-72
lines changed

api/app.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ app.use((req, res, next) => {
2828

2929

3030

31+
3132
//rutas
3233

3334
app.use('/api', userRoutes)

api/controllers/follow.js

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,16 @@ function getFollowingUsers(req, res){
5555

5656
if(!follows) return res.status(404).send({message: 'no estas siguiendo a ningun usuario'})
5757

58-
return res.status(200).send({
59-
total: total,
60-
pages: Math.ceil(total/itemsPrePage),
61-
follows
62-
})
58+
followUserIds(req.user.sub).then((value) => {
59+
return res.status(200).send({
60+
total: total,
61+
pages: Math.ceil(total/itemsPrePage),
62+
userFollowMe: value.followed,
63+
userFollowing: value.following,
64+
follows
65+
66+
})
67+
})
6368
})
6469
}
6570

@@ -85,14 +90,46 @@ function getFollowedUser(req, res){
8590

8691
if(!follows) return res.status(404).send({message: 'no te sigue ningun usuario'})
8792

88-
return res.status(200).send({
89-
total: total,
90-
pages: Math.ceil(total/itemsPrePage),
91-
follows
92-
})
93+
followUserIds(req.user.sub).then((value) => {
94+
return res.status(200).send({
95+
total: total,
96+
pages: Math.ceil(total/itemsPrePage),
97+
userFollowMe: value.followed,
98+
userFollowing: value.following,
99+
follows
100+
101+
})
102+
})
103+
93104
})
94105
}
95106

107+
async function followUserIds(user_id){
108+
var following = await Follow.find({'user':user_id}).select({'_id':0, '__v':0, 'user':0})
109+
110+
var followed = await Follow.find({'followed':user_id}).select({'_id':0, '__v':0, 'followed':0})
111+
112+
var followingClean = [];
113+
114+
following.forEach((follow)=>{
115+
followingClean.push(follow.followed);
116+
});
117+
118+
119+
var followedClean = [];
120+
121+
followed.forEach((follow)=>{
122+
followedClean.push(follow.user);
123+
124+
});
125+
126+
return {
127+
following: followingClean,
128+
followed: followedClean
129+
}
130+
131+
}
132+
96133
//devolver listado de usuarios
97134
function getMyFollows(req, res){
98135
var userId = req.user.sub;

api/controllers/publication.js

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,46 @@ function savePublication(req, res){
4242
})
4343
}
4444

45+
function getPublicationsUser(req, res){
46+
var page = 1;
47+
48+
if(req.params.page){
49+
page = req.params.page;
50+
}
51+
var user = req.user.sub;
52+
if(req.params.user){
53+
var user = req.params.user;
54+
}
55+
56+
var itemsPrePage = 4;
57+
58+
Publication.find({user: user,}).sort('-createdAt').populate('user').paginate(page, itemsPrePage, (err, publications, total) => {
59+
60+
if(err){
61+
return res.status(500).send({
62+
message: 'error al devolver publicaciones'
63+
})
64+
}
65+
if(!publications){
66+
return res.status(404).send({
67+
message: 'no hay publicaciones'
68+
})
69+
}
70+
71+
res.status(200).send({
72+
totalItems : total,
73+
pages: Math.ceil(total/itemsPrePage),
74+
page: page,
75+
itemsPrePage: itemsPrePage,
76+
publications
77+
})
78+
79+
80+
})
81+
}
82+
83+
84+
4585
function getPublications(req, res){
4686
var page = 1;
4787

@@ -51,19 +91,25 @@ function getPublications(req, res){
5191

5292
var itemsPrePage = 4;
5393

94+
95+
5496
Follow.find({user: req.user.sub}).populate('followed').exec((err, follows) => {
5597
if(err){
5698
return res.status(500).send({
5799
message: 'error al volver seguimientos'
58100
})
59101
}
102+
60103
var followsClean = [];
61104

62105
follows.forEach((follow) => {
63-
followsClean.push(follows.followed)
106+
followsClean.push(follow.followed)
64107
})
65108

66-
Publication.find({user: {'$in': followsClean}}).sort('-createdAt').populate('user').paginate(page, itemsPrePage, (err, publications, total) => {
109+
followsClean.push(req.user.sub)
110+
111+
Publication.find({user: {"$in": followsClean}}).sort('-createdAt').populate('user').paginate(page, itemsPrePage, (err, publications, total) => {
112+
67113
if(err){
68114
return res.status(500).send({
69115
message: 'error al devolver publicaciones'
@@ -74,13 +120,18 @@ function getPublications(req, res){
74120
message: 'no hay publicaciones'
75121
})
76122
}
77-
return res.status(200).send({
123+
124+
res.status(200).send({
78125
totalItems : total,
79126
pages: Math.ceil(total/itemsPrePage),
80127
page: page,
128+
itemsPrePage: itemsPrePage,
81129
publications
82130
})
131+
console.log(followsClean)
132+
83133
})
134+
84135
})
85136
}
86137

@@ -111,17 +162,13 @@ function deletePublication(req, res){
111162
message: 'error al borrar publicaciones'
112163
})
113164
}
114-
if(!publication){
115-
return res.status(404).send({
116-
message: 'no se a borrado la publicacion'
117-
})
118-
}
165+
119166
return res.status(200).send({message: 'publicacion eliminada'})
120167
})
121168
}
122169

123170
function uploadImage(req, res){
124-
var userId = req.params.id;
171+
var publicationId = req.params.id;
125172

126173

127174
if(req.files){
@@ -131,12 +178,17 @@ function uploadImage(req, res){
131178
var extSplit = fileName.split('\.');
132179
var fileExt = extSplit[1];
133180

181+
if(userId != req.user.sub){
182+
return removeFilesOfUploads(res, filePath, 'no tienes permiso para actualizar ese usuario')
183+
}
184+
134185
if(fileExt == 'png' || fileExt == 'jpg' || fileExt == 'jpeg' || fileExt == 'gif'){
135-
Publication.findOne({"user": req.user.sub , '_id':publicationId}).exec((err, publication) => {
186+
Publication.findOne({"user": req.user.sub , "_id":publicationId}).exec((err, publication) => {
136187
if(publication){
137188
//actualizar documento de la publicacion
138189

139190
Publication.findByIdAndUpdate(publicationId, {file: fileName}, {new:true},(err, publicationUpdated)=>{
191+
140192
if(err) return res.status(500).send({
141193
message: 'no tienes permiso para actualizar ese usuario'
142194
})
@@ -186,6 +238,7 @@ function getImageFile(req, res){
186238
module.exports = {
187239
savePublication,
188240
getPublications,
241+
getPublicationsUser,
189242
getPublication,
190243
deletePublication,
191244
uploadImage,

api/controllers/user.js

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -121,24 +121,40 @@ function getUser(req, res){
121121
})
122122
}
123123

124-
return res.status(200).send({user, follow})
124+
followThisUser(req.user.sub, userId).then(value => {
125+
user.password = undefined;
126+
console.log(req.user.sub)
127+
console.log(userId)
128+
console.log(value)
129+
return res.status(200).send({
130+
user,
131+
following: value.following,
132+
followed: value.followed
133+
})
134+
135+
})
136+
125137
})
126138

127139
})
128140
}
129141

130142
async function followThisUser(identityUserId, userId){
131-
var following = await Follow.findOne({'user':identityUserId, 'followed':userId}).exec((err,follow)=>{
132-
if(err) return handleError(err);
143+
var following = await Follow.findOne({'user':identityUserId, 'followed':userId}).exec().then(follow => {
133144
return follow;
134-
})
135-
var followed = await Follow.findOne({'user':userId, 'followed':identityUserId}).exec((err,follow)=>{
145+
}).catch(err=>{
136146
if(err) return handleError(err);
147+
})
148+
149+
var followed = await Follow.findOne({'user':userId, 'followed':identityUserId}).exec().then(follow=>{
137150
return follow;
151+
}).catch(err =>{
152+
if(err) return handleError(err);
138153
})
139-
return{
140-
following: following,
141-
followed: followed
154+
155+
return {
156+
following: following,
157+
followed: followed
142158
}
143159
}
144160

@@ -202,25 +218,20 @@ function getCounters(req, res){
202218
if(req.params.id){
203219
userId = req.params.id;
204220
}
205-
getCountFollow(req.params.id).then((value)=>{
221+
getCountFollow(userId).then((value)=>{
206222
return res.status(200).send(value)
207223
})
208224

225+
209226
}
210227

211228
async function getCountFollow(userId){
212-
var following = await Follow.count({'user': userId}).exec((err,count)=>{
213-
if(err) return handleError(err)
214-
return count;
215-
})
216-
var followed = await Follow.count({'followed':userId}).exec((err, count)=>{
217-
if(err) return handleError(err)
218-
return count;
219-
})
220-
var publications = await Publication.count({'user': userId}).exec((err, count)=>{
221-
if(err) return handleError(err);
222-
return count;
223-
})
229+
var following = await Follow.count({"user": userId})
230+
231+
console.log(following)
232+
var followed = await Follow.count({"followed":userId})
233+
234+
var publications = await Publication.count({"user": userId})
224235

225236
return {
226237
following: following,
@@ -301,6 +312,7 @@ function updateImage(req, res){
301312
if(!userUpdated) return res.status(404).send({
302313
message: 'no sea podido actualizar el usuario'
303314
})
315+
304316
return res.status(200).send({
305317
user: userUpdated
306318
})

api/routes/publication.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ var mdUpload = multipart({uploadDir: './uploads/publication'});
77

88
api.post('/publication', mdAuth.ensureAuth, PublicationControllers.savePublication);
99
api.get('/publications/:page?',mdAuth.ensureAuth, PublicationControllers.getPublications);
10+
api.get('/publications-user/:user/:page?',mdAuth.ensureAuth, PublicationControllers.getPublicationsUser);
1011
api.get('/publication/:id',mdAuth.ensureAuth, PublicationControllers.getPublication);
1112
api.delete('/publication/:id',mdAuth.ensureAuth, PublicationControllers.deletePublication);
1213
api.post('/upload-image-pub/:id', [mdAuth.ensureAuth, mdUpload], PublicationControllers.uploadImage);
13-
api.post('/upload-image-pub/:imageFile', mdAuth.ensureAuth, PublicationControllers.getImageFile);
14+
api.get('/get-image-pub/:imageFile', PublicationControllers.getImageFile);
1415

1516

1617
module.exports = api;
522 KB
Loading
522 KB
Loading
90.6 KB
Loading
316 KB
Loading
130 KB
Loading

0 commit comments

Comments
 (0)