Skip to content

Commit

Permalink
fixed pictures
Browse files Browse the repository at this point in the history
  • Loading branch information
wurzy committed Feb 6, 2021
1 parent 8c960af commit bd7aaf2
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
6 changes: 6 additions & 0 deletions api-server/controllers/noticia.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ module.exports.atualizarEstado = idRecurso => {
{$set: {'recurso.estado': 'Indisponível'}})
}

module.exports.atualizarFoto = (idUser,foto) => {
return Noticia.updateMany(
{"autor.id": idUser},
{$set: {'autor.foto': foto}})
}

module.exports.noticiasUtilizador = id =>{
return Noticia
.find({idAutor: id})
Expand Down
14 changes: 14 additions & 0 deletions api-server/controllers/publicacao.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ module.exports.pubsUtilizador = (id) => {
.exec()
}

module.exports.atualizarFoto = (idAutor,foto) => {
return Publicacao.updateMany(
{id_autor: idAutor},
{$set: {foto: foto}}
)
}

module.exports.atualizarFotoComments = (idAutor,foto) => {
return Publicacao.updateMany(
{comments: {$elemMatch: {id_autor: idAutor}}},
{$set: {"comments.$.foto": foto}}
)
}

module.exports.inserir = t => {
var novo = new Publicacao(t)
return novo.save()
Expand Down
7 changes: 7 additions & 0 deletions api-server/routes/noticias.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,11 @@ router.post('/atualizarEstado/:id', function(req, res){
.catch(e => res.status(500).jsonp({error: e}))
})

// Atualizar foto dos utilizadores nas noticias
router.post('/atualizarFoto/:id', function(req, res){
Noticia.atualizarFoto(req.params.id, req.body.foto)
.then(dados => res.status(201).jsonp({dados}))
.catch(e => res.status(500).jsonp({error: e}))
})

module.exports = router;
10 changes: 10 additions & 0 deletions api-server/routes/publicacao.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ router.post('/', function(req,res){
.catch(e => res.status(500).jsonp({error: e}))
})

router.post('/atualizarFoto/:id', function(req, res){
Publicacao.atualizarFoto(req.params.id, req.body.foto)
.then(dados => {
Publicacao.atualizarFotoComments(req.params.id, req.body.foto)
.then(dados => res.status(200).jsonp(dados))
.catch(e => res.status(500).jsonp({error: e}))
})
.catch(e => res.status(500).jsonp({error: e}))
})

router.post('/comentar/:id', function(req,res){
console.log(req.params.id)
console.log(req.body)
Expand Down
15 changes: 13 additions & 2 deletions app-server/routes/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,19 @@ router.post('/:id/editar/imagem/', upload.single('foto'), function(req,res,next)
var token = aux.unveilToken(req.cookies.token)

if ((token.nivel == 'produtor' || token.nivel == 'admin') && token._id == req.params.id) {
axios.put('http://localhost:8001/users/imagem/' + req.params.id +'?token=' + req.cookies.token, {foto: req.file.path.replace("public","").replace(/\\/g,"/"), _id: token._id})
.then(dados => res.redirect("/perfil"))
var foto = req.file.path.replace("public","").replace(/\\/g,"/")
axios.put('http://localhost:8001/users/imagem/' + req.params.id +'?token=' + req.cookies.token, {foto: foto, _id: token._id})
.then(dados => {
axios.post('http://localhost:8001/noticias/atualizarFoto/' + token._id + '?token=' + req.cookies.token, {foto: foto})
.then(dados => {
axios.post('http://localhost:8001/publicacoes/atualizarFoto/' + token._id + '?token=' + req.cookies.token, {foto: foto})
.then(dados => {
res.redirect("/perfil")
})
.catch(error => res.render('error', {error}))
})
.catch(error => res.render('error', {error}))
})
.catch(error => res.render('error', {error}))
}
else res.redirect('/perfil/' + req.params.id)
Expand Down

0 comments on commit bd7aaf2

Please sign in to comment.