Files
pathtoglory_blog/app/server/controllers/Post.controller.js

36 lines
786 B
JavaScript

import client from "../db/index.js";
export default class PostController {
static async getAll(options) {
let queryText = "SELECT * FROM posts";
if (options && options.order === "asc") {
queryText += ' ORDER BY created_at DESC';
}
try {
const res = await client.query(queryText);
return res.rows;
} catch (err) {
console.log(err);
return [];
}
}
static async get(id) {
const res = await client.query({
text: "SELECT * FROM posts WHERE id=$1",
values: [id]
});
return res.rows[0];
}
static async create(title, content) {
const res = await client.query({
text: "INSERT INTO posts(title, content) VALUES($1, $2)",
values: [title, content]
});
console.log(res.rows);
}
}