Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

7/22 updates #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
36 changes: 32 additions & 4 deletions MCO2/controllers/postController.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,45 @@
const db = require('../models/db.js');
const User = require('../models/UserModel.js');
const Post = require('../models/PostModel.js');

const postController = {

getFavicon: function (req, res) {
res.status(204);
},

getPostPage: async function (req, res) {
// var query for logged in (boolean)
// var projection
var query = {_id: req.params.postID};
var poster = {username: req.params.username};

// fields to be returned
var projection = 'postID title username votes date description comments';
var userProjection = 'icon';

var result = await db.findOne(Post, query, projection);
var posterIcon = await db.findOne(User, poster, userProjection);

var commenterUsername = result.comments.username;

if(result != null || userResult != null) {
var details = {
votes: result.votes,
title: result.title,
description: result.description,
username: result.username,
date: result.date,
commentVotes: result.comments.votes,
commenterUsername: commenterUsername,
commentDesc: result.comments.description,

icon: posterIcon.icon,
commenterIcon: await db.findOne(User, commenterUsername, userProjection)
};

// var result = await db.findOne
res.render('post-page-u', details);
};


res.render('post-page');
}
}

Expand Down
68 changes: 36 additions & 32 deletions MCO2/controllers/profileController.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,53 @@ const User = require('../models/UserModel.js');

*/

// const db = require('../models/db.js');
const db = require('../models/db.js');
const User = require('../models/UserModel.js');
const Post = require('../models/PostModel.js');

const profileController = {
getProfile: async function (req, res) {

// query where `idNum` is equal to URL parameter `idNum`
var u = req.params.username;
var query = {username: u};
// var u = req.params.username;
var query = {username: req.params.username};

// fields to be returned
// posts
var projection = 'username displayName bio icon banner';
var postProjection = 'id title votes date comments';

/*
calls the function findOne()
defined in the `database` object in `../models/db.js`
this function searches the collection `users`
based on the value set in object `query`
the third parameter is a string containing fields to be returned
*/
db.findOne('users', query, function (result) {
if(result != null) {
var details = {
username: result.username,
displayName: result.displayName,
bio: result.bio,
icon: result.icon,
banner: result.banner
};

// render `../views/profile.hbs`
res.render('profile-page', details);
}

/*
if the user does not exist in the database
render the error page
*/
else {
res.render('error');
}
});
var result = await db.findOne(User, query, projection);
var postResult = await db.findOne(Post, query, postProjection);

if(result != null || postResult != null) {

// var commentCt = postResult.comments;
var details = {

username: result.username,
displayName: result.displayName,
bio: result.bio,
icon: result.icon,
banner: result.banner,
postID: postResult._id,
title: postResult.title,
votes: postResult.votes,
date: postResult.date,
numComments: postResult.comments.length
};

res.render('profile-page', details);
}

else {
res.render('error', {collectionType: "user"});
}

},

getError: async function (req, res) {
res.render('error', {collectionType: "user"});
}
}

Expand Down
71 changes: 65 additions & 6 deletions MCO2/controllers/timelineController.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,76 @@
const db = require('../models/db.js');
const User = require('../models/UserModel.js');
const Post = require('../models/PostModel.js');

const timelineController = {

getFavicon: function (req, res) {
res.status(204);
},

getTimelineReg: async function (req, res) {
// var query for logged in (boolean)
// var projection
getTimeline: async function (req, res) {
// var query = db.getCollectionInfos();

var projection = 'id title username votes date comments';
var postProjection = 'id title votes date comments';

// var result = await db.findMany(User, query, projection);
// var postResult = await db.findMany(Post, query, postProjection);
var result = await User.find();
var postResult = await Post.find();

if(result != null || postResult != null){
var details = {
username: result.username,
displayName: result.displayName,
bio: result.bio,
icon: result.icon,
banner: result.banner,
postID: postResult._id,
title: postResult.title,
votes: postResult.votes,
date: postResult.date,
// numComments: postResult.comments.length
};

// var result = await db.findOne
res.render('index', details);
}

else{
res.render('error', {collectionType: "page"});
}

// // switch (params.sortBy) {
// // case 'popular':
// // post2.sort((a, b) => {
// // return b.votes - a.votes;
// // });
// // break;
// // case 'recent':
// // post2.sort((a, b) => {
// // return new Date(b.date) - new Date(a.date);
// // });
// // break;
// // case 'unpopular':
// // post2.sort((a, b) => {
// // return a.votes - b.votes;
// // });
// // break;
// // case 'old':
// // post2.sort((a, b) => {
// // return new Date(a.date) - new Date(b.date);
// // });
// // break;
// // default:
// // post2.sort((a, b) => {
// // return a.id - b.id;
// // });
// // }

},

res.render('timeline');
getError: async function (req,res){
res.render('error', {collectiontype: "user"});
}
}

Expand Down
44 changes: 36 additions & 8 deletions MCO2/models/PostModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ var mongoose = require('mongoose'),

// defines the schema for collection `posts`
var PostSchema = new mongoose.Schema({
id: {
type: Number,
required: true
},

title: {
type: String,
required: false
Expand Down Expand Up @@ -40,10 +37,41 @@ var PostSchema = new mongoose.Schema({
type: String,
required: true
},
comments: {
type: Schema.Types.ObjectId,
ref: 'Comment'
}
comments: [
// type: Schema.Types.ObjectId,
// ref: 'Comment'
{

username: {
type: String,
required: true
},
votes: {
type: Number,
required: true
},
date: {
type: Date,
required: true
},
clickvote: {
type: Boolean,
required: true
},
dirvotes: {
type: Boolean,
required: true
},
deleted: {
type: Boolean,
required: true
},
description: {
type: String,
required: true
}
}
]
});

/*
Expand Down
12 changes: 8 additions & 4 deletions MCO2/models/UserModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ var UserSchema = new mongoose.Schema({
required: true
},
icon: {
data: Buffer,
contentType: String
// data: Buffer,
// contentType: String
type: String,
required: true
},
banner: {
data: Buffer,
contentType: String
// data: Buffer,
// contentType: String
type: String,
required: true
}
});

Expand Down
Loading