diff --git a/app.js b/app.js index 86b4912..3598bc5 100644 --- a/app.js +++ b/app.js @@ -5,6 +5,8 @@ import apiRoutes from './routes/api.js'; import userRoutes from './routes/user.js'; import postRoutes from './routes/posts.js'; import commentRoutes from './routes/comments.js'; +import communityRoutes from './routes/communities.js'; +import offerRoutes from './routes/offers.js'; const app = express(); const port = 3000; @@ -39,6 +41,9 @@ app.use('/api/user', userRoutes); // Rutas de usuarios app.use('/api/post', postRoutes); // Rutas de posts app.use('/api/comments', commentRoutes); // Rutas de comentarios app.use('/uploads', express.static('uploads')); +app.use('/api/offers', express.static('uploads')); +app.use('/api/offers', offerRoutes); // Rutas de ofertas +app.use('/api/communities', communityRoutes); // Rutas de comunidades app.listen(port, () => { console.log(`Example app listening on port ${port}`); diff --git a/functions/communityFunctions.js b/functions/communityFunctions.js index f6770c1..e610817 100644 --- a/functions/communityFunctions.js +++ b/functions/communityFunctions.js @@ -1,4 +1,4 @@ -import {pool} from '../db.js'; +import { pool } from '../lib/database.js'; export const createNewCommunity = async (req, res) => { try { diff --git a/functions/offerFunctions.js b/functions/offerFunctions.js index c016dc7..5c8faf3 100644 --- a/functions/offerFunctions.js +++ b/functions/offerFunctions.js @@ -1,4 +1,4 @@ -import {pool} from '../db.js'; +import {pool} from '../lib/database.js'; export const createNewOffer = async (req, res) => { try { @@ -84,4 +84,24 @@ export const deleteOffer = async (req, res) => { res.status(500).json({ error: err.message }); throw err; } +} + +export const getOffersByUserId = async (req, res) => { + try { + const { id_usuario } = req.params; + const query = await pool.query( + `SELECT usuarios.nombre, usuarios.apellido_pa, usuarios.apellido_ma, oferta.* + FROM oferta + JOIN usuarios ON oferta.id_usuario = usuarios.id_usuario + WHERE usuarios.id_usuario = $1 + ORDER BY oferta.fecha_publicacion DESC`, + [id_usuario] + ); + res.json(query.rows); + } + catch (err) { + console.error(err); + res.status(500).json({ error: err.message }); + throw err; + } } \ No newline at end of file diff --git a/functions/postFunctions.js b/functions/postFunctions.js index 0cc8ede..64f4728 100644 --- a/functions/postFunctions.js +++ b/functions/postFunctions.js @@ -62,7 +62,7 @@ export const getPostById = async (req, res) => { } } -getPostByComunityId = async (req, res) => { +export const getPostByComunityId = async (req, res) => { try { const { comunidad } = req.params; const query = await pool.query( diff --git a/routes/communities.js b/routes/communities.js index 697cc11..e80534d 100644 --- a/routes/communities.js +++ b/routes/communities.js @@ -1,5 +1,5 @@ import {Router} from 'express'; -import { createCommunity, getCommunityById, getAllCommunities, deleteCommunity, editCommunity } from '../functions/communityFunctions.js'; +import { createNewCommunity, getCommunityById, getAllCommunities, deleteCommunity, editCommunity } from '../functions/communityFunctions.js'; const router = Router(); // Validate create community payload @@ -11,8 +11,10 @@ const validateCommunityPayload = (req, res, next) => { next(); }; -router.post('/', validateCommunityPayload, createCommunity); +router.post('/', validateCommunityPayload, createNewCommunity); router.get('/', getAllCommunities); router.get('/:id_comunidad', getCommunityById); router.put('/:id_comunidad', validateCommunityPayload, editCommunity); -router.delete('/:id_comunidad', deleteCommunity); \ No newline at end of file +router.delete('/:id_comunidad', deleteCommunity); + +export default router; \ No newline at end of file diff --git a/routes/offers.js b/routes/offers.js index aacf618..30322ef 100644 --- a/routes/offers.js +++ b/routes/offers.js @@ -1,4 +1,10 @@ import { Router } from "express"; +import path from 'path'; +import multer from 'multer'; +import fs from 'fs/promises'; +import sharp from 'sharp'; +import crypto from 'crypto'; +import { fileTypeFromBuffer } from 'file-type'; import { createNewOffer, getOfferById, deleteOffer, getAllOffers, getOffersByUserId, editOffer } from "../functions/offerFunctions.js"; const router = Router(); @@ -78,4 +84,6 @@ router.get('/', getAllOffers); router.get('/:id_oferta', getOfferById); router.put('/:id_oferta', upload.single('imagen'), validateOfferPayload, processUploadedImage, editOffer); router.delete('/:id_oferta', deleteOffer); -router.get('/user/:id_usuario', getOffersByUserId); \ No newline at end of file +router.get('/user/:id_usuario', getOffersByUserId); + +export default router;