Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .env.example

This file was deleted.

31 changes: 30 additions & 1 deletion api/users.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const express = require("express");
const router = express.Router();
const { Task, User } = require("../database");
const { User } = require("../database");

// GET all users
router.get("/", async (req, res) => {
Expand Down Expand Up @@ -49,4 +49,33 @@ router.post("/", async (req, res) => {
}
});

router.patch("/:id", async (req, res) => {
try {
const user = await User.findByPk(req.params.id);
if (!user) {
return res.status(404).send({ error: "User not found" });
}
await user.update(req.body);

res.send(user);
} catch (error) {
res.status(500).json({ error: "Failed to update user" });
}
});

// DELETE a user by id
router.delete("/:id", async (req, res) => {
try {
const user = await User.findByPk(req.params.id);
if (!user) {
return res.status(404).send({ error: "User not found" });
}
await user.destroy();
res.status(204).send();
} catch (error) {
res.status(500).json({ error: "Failed to delete user" });
}
});


module.exports = router;
3 changes: 2 additions & 1 deletion database/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const pg = require("pg");
const db = new Sequelize(
process.env.DATABASE_URL || "postgres://localhost:5432/todo_list",
{
logging: false, // comment this line to enable logging
dialect: "postgres",
logging: false, // optional // comment this line to enable logging
}
);

Expand Down
1 change: 1 addition & 0 deletions database/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const User = db.define("user", {
name: {
type: DataTypes.STRING,
allowNull: false,
unique: true
},
});

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@
"optionalDependencies": {
"win-node-env": "^0.6.1"
}
}
}