API Functions for communities and product offers added
This commit is contained in:
@@ -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