66 lines
1.1 KiB
JavaScript
66 lines
1.1 KiB
JavaScript
const express = require("express");
|
|
const cors = require("cors");
|
|
const dotenv = require("dotenv");
|
|
const pool = require("./db");
|
|
|
|
dotenv.config();
|
|
|
|
const app = express();
|
|
|
|
app.use(cors());
|
|
app.use(express.json());
|
|
|
|
app.get("/transactions", async (req, res) => {
|
|
try {
|
|
const result = await pool.query(
|
|
"SELECT * FROM transactions ORDER BY created_at DESC"
|
|
);
|
|
|
|
res.json(result.rows);
|
|
|
|
} catch (err) {
|
|
console.error(err.message);
|
|
}
|
|
});
|
|
|
|
app.post("/transactions", async (req, res) => {
|
|
|
|
try {
|
|
|
|
const { title, amount, type } = req.body;
|
|
|
|
const result = await pool.query(
|
|
"INSERT INTO transactions (title, amount, type) VALUES ($1, $2, $3) RETURNING *",
|
|
[title, amount, type]
|
|
);
|
|
|
|
res.json(result.rows[0]);
|
|
|
|
} catch (err) {
|
|
console.error(err.message);
|
|
}
|
|
});
|
|
|
|
app.delete("/transactions/:id", async (req, res) => {
|
|
|
|
try {
|
|
|
|
const { id } = req.params;
|
|
|
|
await pool.query(
|
|
"DELETE FROM transactions WHERE id = $1",
|
|
[id]
|
|
);
|
|
|
|
res.json("transaction deleted");
|
|
|
|
} catch (err) {
|
|
console.error(err.message);
|
|
}
|
|
});
|
|
|
|
app.listen(5000, () => {
|
|
console.log("server running on port 5000");
|
|
});
|
|
|