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
17 changes: 14 additions & 3 deletions api/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,23 @@ const { Task, User } = require("../database");

// GET all tasks
router.get("/", async (req, res) => {
// replace this with your own code
res.sendStatus(501);
try {
const result = await Task.findAll();
res.send(result);
} catch (error) {
res.status(501).send("Not implemented");
}
});

// GET a single task by id

// router.get("/:id", async (req, res) => {
// // try {
// // const result = await Task.findByPk(`api/tasks/${id}`);
// // res.send(result);
// // } catch (error) {
// // console.error("Error:", error);
// // }
// });
// Patch a task by id

// Delete a task by id
Expand Down
6 changes: 6 additions & 0 deletions database/db.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
require("dotenv").config();
const { Sequelize } = require("sequelize");
const pg = require("pg");

const db = new Sequelize(
process.env.DB_NAME,
process.env.DB_USER,
process.env.DB_PASSWORD,
process.env.DATABASE_URL || "postgres://localhost:5432/todo_list",
{
host: process.env.DB_HOST,
dialect: process.env.DB_DIALECT,
logging: false, // comment this line to enable logging
}
);
Expand Down
2 changes: 2 additions & 0 deletions database/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const User = require("./user");

// TASK 3: Set up associations here
// What kind of relationship is there between a user and a task?
User.hasMany(Task);
Task.belongsTo(User);

// Export everything needed
module.exports = {
Expand Down
14 changes: 14 additions & 0 deletions database/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@ const db = require("./db");

// TASK 1: Define the Task model here
const Task = db.define("task", {

title: {
type: DataTypes.STRING,
allowNull: false,
},
description: {
type: DataTypes.STRING,
allowNull: false,
},
completed: {
type: DataTypes.BOOLEAN,
defaultValue: false,
},

// You should define the following columns:
// - title: string, required
title: {
Expand Down
6 changes: 4 additions & 2 deletions database/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ const db = require("./db");

// TASK 2: Define the User model here
const User = db.define("user", {
// You should define the following columns:
// - name: string, required
name: {
type: DataTypes.STRING,
allowNull: false,
},
});

module.exports = User;
32 changes: 16 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
"description": "",
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^16.5.0",
"dotenv": "^16.6.1",
"express": "^5.1.0",
"morgan": "^1.10.0",
"pg": "^8.16.2",
"pg": "^8.16.3",
"sequelize": "^6.37.7"
},
"devDependencies": {
Expand Down