Retrieve username for post

This commit is contained in:
Rykkel
2024-07-30 02:21:26 +08:00
parent a76ec634ca
commit 1edf63cc5b
6 changed files with 126 additions and 45 deletions

View File

@@ -17,9 +17,14 @@ module.exports = (sequelize, DataTypes) => {
content: {
type: DataTypes.TEXT,
allowNull: false
}
},
userId: {
type: DataTypes.UUID,
allowNull: false,
},
}, {
tableName: 'posts'
});
return Post;
}

View File

@@ -1,6 +1,6 @@
const express = require('express');
const router = express.Router();
const { Post } = require('../models');
const { Post, User } = require('../models');
const { Op, where } = require("sequelize");
const yup = require("yup");
const multer = require("multer");
@@ -56,7 +56,10 @@ router.post("/", async (req, res) => {
// });
router.get("/", async (req, res) => {
let condition = {};
let condition = {
order: [['createdAt', 'DESC']]
};
let search = req.query.search;
if (search) {
condition[Op.or] = [
@@ -67,11 +70,7 @@ router.get("/", async (req, res) => {
// You can add condition for other columns here
// e.g. condition.columnName = value;
let list = await Post.findAll({
where: condition,
// order option takes an array of items. These items are themselves in the form of [column, direction]
order: [['createdAt', 'DESC']]
});
let list = await Post.findAll(condition);
res.json(list);
});