API Functions for communities and product offers added
This commit is contained in:
82
functions/communityFunctions.js
Normal file
82
functions/communityFunctions.js
Normal file
@@ -0,0 +1,82 @@
|
||||
import {pool} from '../db.js';
|
||||
|
||||
export const createNewCommunity = async (req, res) => {
|
||||
try {
|
||||
const { nombre, descripcion } = req.body;
|
||||
const query = await pool.query(
|
||||
`INSERT INTO comunidad (nombre, descripcion)
|
||||
VALUES ($1, $2)
|
||||
RETURNING *`,
|
||||
[nombre, descripcion]
|
||||
);
|
||||
res.status(201).json(query.rows[0]);
|
||||
} catch (err) {
|
||||
console.error('Error creating community:', err);
|
||||
res.status(500).json({
|
||||
error: 'Error al crear la comunidad: ' + err.message
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export const getCommunityById = async (req, res) => {
|
||||
try {
|
||||
const { id_comunidad } = req.params;
|
||||
const query = await pool.query(
|
||||
`SELECT * FROM comunidad
|
||||
WHERE id_comunidad = $1`,
|
||||
[id_comunidad]
|
||||
);
|
||||
res.json(query.rows[0]);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: err.message });
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
export const getAllCommunities = async (req, res) => {
|
||||
try {
|
||||
const query = await pool.query(
|
||||
`SELECT * FROM comunidad
|
||||
ORDER BY nombre ASC`
|
||||
);
|
||||
res.json(query.rows);
|
||||
}
|
||||
catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: err.message });
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
export const editCommunity = async (req, res) => {
|
||||
try {
|
||||
const { id_comunidad } = req.params;
|
||||
const { nombre, descripcion } = req.body;
|
||||
const query = await pool.query(
|
||||
'UPDATE comunidad SET nombre = $1, descripcion = $2 WHERE id_comunidad = $3 RETURNING *',
|
||||
[nombre, descripcion, id_comunidad]
|
||||
);
|
||||
res.json(query.rows[0]);
|
||||
}
|
||||
catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: err.message });
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
export const deleteCommunity = async (req, res) => {
|
||||
try {
|
||||
const { id_comunidad } = req.params;
|
||||
const query = await pool.query(
|
||||
'DELETE FROM comunidad WHERE id_comunidad = $1 RETURNING *',
|
||||
[id_comunidad]
|
||||
);
|
||||
res.json(query.rows[0]);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: err.message });
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
87
functions/offerFunctions.js
Normal file
87
functions/offerFunctions.js
Normal file
@@ -0,0 +1,87 @@
|
||||
import {pool} from '../db.js';
|
||||
|
||||
export const createNewOffer = async (req, res) => {
|
||||
try {
|
||||
const { nombre, descripcion, precio, id_usuario } = req.body;
|
||||
let imagePath = null;
|
||||
if (req.processedFilename) {
|
||||
imagePath = `/uploads/${req.processedFilename}`;
|
||||
}
|
||||
const query = await pool.query(
|
||||
`INSERT INTO oferta (nombre, descripcion, precio, imagen, fecha_publicacion, id_usuario)
|
||||
VALUES ($1, $2, $3, $4, NOW(), $5)
|
||||
RETURNING *`,
|
||||
[nombre, descripcion, precio, imagePath, id_usuario]
|
||||
);
|
||||
res.status(201).json(query.rows[0]);
|
||||
} catch (err) {
|
||||
console.error('Error creating offer:', err);
|
||||
res.status(500).json({
|
||||
error: 'Error al crear la oferta: ' + err.message
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export const getOfferById = async (req, res) => {
|
||||
try {
|
||||
const { id_oferta } = 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 id_oferta = $1`,
|
||||
[id_oferta]
|
||||
);
|
||||
res.json(query.rows[0]);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: err.message });
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
export const getAllOffers = async (req, res) => {
|
||||
try {
|
||||
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
|
||||
ORDER BY oferta.fecha_publicacion DESC`
|
||||
);
|
||||
res.json(query.rows);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: err.message });
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
export const editOffer = async (req, res) => {
|
||||
try {
|
||||
const { id_oferta } = req.params;
|
||||
const { nombre, descripcion, precio } = req.body;
|
||||
const query = await pool.query(
|
||||
'UPDATE oferta SET nombre = $1, descripcion = $2, precio = $3 WHERE id_oferta = $4 RETURNING *',
|
||||
[nombre, descripcion, precio, id_oferta]
|
||||
);
|
||||
res.json(query.rows[0]);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: err.message });
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
export const deleteOffer = async (req, res) => {
|
||||
try {
|
||||
const { id_oferta } = req.params;
|
||||
const query = await pool.query(
|
||||
'DELETE FROM oferta WHERE id_oferta = $1 RETURNING *',
|
||||
[id_oferta]
|
||||
);
|
||||
res.json(query.rows[0]);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: err.message });
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ import {pool} from '../lib/database.js';
|
||||
|
||||
export const createNewPost = async (req, res) => {
|
||||
try {
|
||||
const { contenido, id_usuario } = req.body;
|
||||
const { contenido, id_usuario, comunidad } = req.body;
|
||||
|
||||
if (!contenido || !id_usuario) {
|
||||
return res.status(400).json({
|
||||
@@ -19,10 +19,10 @@ export const createNewPost = async (req, res) => {
|
||||
|
||||
// Create the post in the database
|
||||
const query = await pool.query(
|
||||
`INSERT INTO posts (contenido, imagen, id_usuario, fecha_publicacion)
|
||||
VALUES ($1, $2, $3, NOW())
|
||||
`INSERT INTO posts (contenido, imagen, id_usuario, fecha_publicacion, comunidad)
|
||||
VALUES ($1, $2, $3, NOW(), $4)
|
||||
RETURNING *`,
|
||||
[contenido, imagePath, id_usuario]
|
||||
[contenido, imagePath, id_usuario, comunidad]
|
||||
);
|
||||
|
||||
// Get the created post with user information
|
||||
@@ -62,6 +62,26 @@ export const getPostById = async (req, res) => {
|
||||
}
|
||||
}
|
||||
|
||||
getPostByComunityId = async (req, res) => {
|
||||
try {
|
||||
const { comunidad } = req.params;
|
||||
const query = await pool.query(
|
||||
`SELECT usuarios.nombre, usuarios.apellido_pa, usuarios.apellido_ma, posts.*
|
||||
FROM posts
|
||||
JOIN usuarios ON posts.id_usuario = usuarios.id_usuario
|
||||
WHERE comunidad = $1
|
||||
ORDER BY fecha_publicacion DESC`,
|
||||
[comunidad]
|
||||
);
|
||||
res.json(query.rows);
|
||||
}
|
||||
catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: err.message });
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
export const deletePost = async (req, res) => {
|
||||
try {
|
||||
const { id_post } = req.params;
|
||||
|
||||
Reference in New Issue
Block a user