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
3 changes: 0 additions & 3 deletions .env.example

This file was deleted.

3 changes: 2 additions & 1 deletion api/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const express = require("express");
const router = express.Router();
const testDbRouter = require("./test-db");
const usersRouter = require("./users")

router.use("/test-db", testDbRouter);

router.use("/users", usersRouter);
module.exports = router;
28 changes: 28 additions & 0 deletions api/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const express = require("express")
const router = express.Router();
const { User } = require("../database");

router.get("/", async (req, res) => {
try {
const users = await User.findAll({
attributes: { exclude: ["password_hash"] }
});
res.json(users);
} catch (err) {
res.status(500).json({ error: "Failed to fetch users" });
}
});

router.get("/:id", async (req, res) => {
try {
const user = await User.findByPk(req.params.id, {
attributes: { exclude: ["password_hash"] }
});

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

module.exports = router;
2 changes: 1 addition & 1 deletion database/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { Sequelize } = require("sequelize");
const pg = require("pg");

// Feel free to rename the database to whatever you want!
const dbName = "capstone-1";
const dbName = "capstone-2";

const db = new Sequelize(
process.env.DATABASE_URL || `postgres://localhost:5432/${dbName}`,
Expand Down
29 changes: 29 additions & 0 deletions database/echo_recipients.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const { DataTypes } = require("sequelize");
const db = require("./db");

const Echo_recipients = db.define("echo_recipients", {
echo_id: {
type: DataTypes.INTEGER,
primaryKey: true,
allowNull: false,
references: {
model: 'echoes', // target table name
key: 'id'
},
},

recipient_id: {
type: DataTypes.INTEGER,
primaryKey: true,
allowNull: false,
references: {
model: 'users', // target table name
key: 'id'
},
}
}, {
tableName: 'echo_recipients',
timestamps: false
});

module.exports = Echo_recipients;
29 changes: 29 additions & 0 deletions database/echo_tags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const { DataTypes } = require("sequelize");
const db = require("./db");

const Echo_tags = db.define('echo_tags', {
echo_id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
references: {
model: "echoes", // target table
key: "id"
}
},

tag_id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
references: {
model: "tags",
key: "id"
}
}
}, {
tableName: 'echo_tags',
timestamps: false
});

module.exports = Echo_tags;
26 changes: 26 additions & 0 deletions database/echo_visibility.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const { DataTypes} = require("sequelize");
const db = require("./db");

const Echo_visibility = db.define("echo_visibility", {
echo_id: {
type: DataTypes.INTEGER,
primaryKey: true,
allowNull: false,
},

scope: {
type: DataTypes.ENUM("self", "friends", "public", "custom"),
allowNull: false,

},

note: {
type: DataTypes.TEXT,
allowNull: true
}
}, {
tableName: 'echo_visibility',
timestamps: false,
});

module.exports = Echo_visibility;
62 changes: 62 additions & 0 deletions database/echoes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const { DataTypes } = require("sequelize");
const db = require("./db");

const Echoes = db.define("echoes", {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
},

sender_id: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'users', // target table
key: 'id'
}
},

type: {
type: DataTypes.ENUM("self", "friend", "public"),
allowNull: false,
},

text: {
type: DataTypes.STRING,
allowNull: true,
},

unlock_datetime: {
type: DataTypes.DATE,
allowNull: false
},

is_unlocked: {
type: DataTypes.VIRTUAL,
get() {
const now = new Date();
return now >= this.unlock_datetime;
}
},

is_archived: {
type: DataTypes.BOOLEAN,
defaultValue: false,
allowNull: false,
},

show_sender_name: {
type: DataTypes.BOOLEAN,
defaultValue: false,
allowNull: false,
}

}, {
tableName: 'echoes',
timestamps: true,
createdAt: 'created_at',
updatedAt: 'updated_at',
});

module.exports = Echoes;
48 changes: 48 additions & 0 deletions database/friends.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const { DataTypes } = require("sequelize");
const db = require("./db");

const Friends = db.define("friends", {
id: {
primaryKey: true,
type: DataTypes.INTEGER,
autoIncrement: true
},

user_id: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'users',
key: 'id'
}
},

friend_id: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'users',
key: 'id'
}
},

status: {
type: DataTypes.ENUM('pending', 'accepted', 'blocked'),
allowNull: false,
defaultValue: 'pending'
}
}, {
tableName: 'friends',
timestamps: true,
createdAt: 'created_at',
updatedAt: 'updated_at',
indexes: [
{
unique: true,
fields: ['user_id', 'friend_id'],
name: 'unique_user_friend_pair'
}
]
});

module.exports = Friends;
6 changes: 6 additions & 0 deletions database/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const db = require("./db");
const User = require("./user");
const Echoes = require('./echoes');
const Echo_visibility = require('./echo_visibility');
const Echo_recipients = require('./echo_recipients');
const Echo_tags = require('./echo_tags');



module.exports = {
db,
Expand Down
74 changes: 74 additions & 0 deletions database/media.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const { DataTypes } = require("sequelize");
const db = require("/.db");

const Media = db.define("media", {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},

echo_id: {
type: DataTypes.INTEGER,
allowNull: true,
references: {
model: 'echoes',
key: 'id'
}
},

reply_id: {
type: DataTypes.INTEGER,
allowNull: true,
references: {
model: 'replies',
key: 'id'
}
},

type: {
type: DataTypes.ENUM('image', 'audio', 'video'),
allowNull: false,
},

url: {
type: DataTypes.STRING,
allowNull: false,
validate: {
isUrl: true
}
},

file_size: {
type: DataTypes.INTEGER,
allowNull: false
},

duration_seconds: {
// only applies to audio or video
type: DataTypes.INTEGER,
allowNull: true
},

thumbnail_url: {
// optional thumbnail
type: DataTypes.STRING,
allowNull: true
}
}, {
timestamps: true,
createdAt: 'created_at',
updatedAt: 'updated_at',
validate: {
eitherEchoOrReply() {
if (!this.echo_id && !this.reply_id) {
throw new Error('Media must belong to either an echo or reply.');
}
if (this.echo_id && this.reply_id) {
throw new Error('Media cannot belong to both an echo and a reply.');
}
}
}
});

module.exports = Media;
44 changes: 44 additions & 0 deletions database/reactions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const { DataTypes } = require('sequelize');
const db = require('./db');

const Reactions = db.define('reactions', {
id: {
primaryKey: true,
type: DataTypes.INTEGER,
autoIncrement: true
},

echo_id: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'echoes',
key: 'id'
}
},

user_id: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'users',
key: 'id'
}
},

type: {
type: DataTypes.ENUM('sad', 'funny', 'happy'),
allowNull: false
}
}, {
timestamps: true,
createdAt: 'created_at',
updatedAt: 'updated_at',
indexes: [{
unique: true,
fields: ['user_id', 'echo_id'],
name: 'unique_user_echo_reaction_pair'
}]
});

module.exports = Reactions;
Loading