Skip to content

feat: #6

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

Open
wants to merge 2 commits into
base: master
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
1,206 changes: 1,206 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
},
"dependencies": {
"express": "^4.16.3",
"mysql2": "^1.6.5",
"pg": "^7.4.3",
"pg-hstore": "^2.3.2",
"sequelize": "^4.38.0",
Expand Down
37 changes: 22 additions & 15 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ app.get('/users', (req: Request, res: Response) => {
.catch(err => res.status(500).json({ err: ['oops', err] }));
});

app.get('/comments', (req: Request, res: Response) => {
/* app.get('/comments', (req: Request, res: Response) => {
db.Comment.findAll()
.then((comments: CommentInstance[]) => res.status(200).json({ comments }))
.catch(err => res.status(500).json({ err: ['oops', err] }));
});
}); */

app.get('/posts', (req: Request, res: Response) => {
/* app.get('/posts', (req: Request, res: Response) => {
db.Post.findAll()
.then((posts: PostInstance[]) => res.status(200).json({ posts }))
.catch(err => res.status(500).json({ err: ['oops', err] }));
});

*/
// GET all users that upvoted a comment
app.get('/comments/:id/upvoters', (req: Request, res: Response) => {
/* app.get('/comments/:id/upvoters', (req: Request, res: Response) => {
db.Comment.findById(req.params.id)
.then(comment => comment.getUpvoters())
.then(upvoters =>
Expand All @@ -46,31 +46,38 @@ app.get('/comments/:id/upvoters', (req: Request, res: Response) => {
})
)
.catch(err => res.status(500).json({ err: ['oops', err] }));
});
}); */

// GET all posts a user has authored
app.get('/users/:id/posts', (req: Request, res: Response) => {
db.User.findById(req.params.id)
.then(user => user.getPosts())
.then(posts =>
.then(posts =>
res.status(200).json({ posts })
)
.catch(err => res.status(500).json({ err: ['oops', err] }))
});

// POST create new dummy user with posts
// POST create new dummy user with noposts
app.post('/seed/user', (req: Request, res: Response) => {
db.User.create({
name: 'John Doe',
name: 'hwd'
})
.then(user => res.status(201).json({ user }))
.catch(err => res.status(500).json({ err: ['oops', err] }))
})

// POST create new dummy user with posts
app.post('/seed/userWithPosts', (req: Request, res: Response) => {
db.User.create({
name: 'yunyou123',
posts: [
{
name: 'post1',
title: 'Croissants are tasty',
text: 'I recently ate a croissant from France. It was nice.',
category: 'croissants'
},
{
name: 'post2',
title: 'my fav techno music',
text: 'I love the song TECHNO by TechnoGang.',
category: 'techno'
Expand Down Expand Up @@ -100,15 +107,15 @@ app.post('/seed/user/:user_id/upvotes', async (req: Request, res: Response) => {
});

// POST add upvoter
app.post('/comment/:comment_id/upvoter/:upvoter_id', (req: Request, res: Response) => {
/* app.post('/comment/:comment_id/upvoter/:upvoter_id', (req: Request, res: Response) => {
db.Comment.findById(req.params.comment_id)
.then(comment => comment.addUpvoter(req.params.upvoter_id))
.catch(err => res.status(500).json({ err: ['oops', err] }))
});

*/
// GET all upvoted comments for specified users (using eager loading)
// userIds go in query params, e.g. /upvoted_comments?userIds=[1,2,3]
app.get('/upvoted_comments', (req: Request, res: Response) => {
/* app.get('/upvoted_comments', (req: Request, res: Response) => {
console.log(req.query.userIds);
const userIds = JSON.parse(req.query.userIds);
db.User.findAll({
Expand All @@ -125,7 +132,7 @@ app.get('/upvoted_comments', (req: Request, res: Response) => {
res.status(200).json({ upvotedComments: comments });
})
.catch(err => res.status(500).json({ err: ['oops', err] }))
});
}); */

app.listen(3000, () => {
console.log('App listening on port 3000');
Expand Down
8 changes: 4 additions & 4 deletions src/config/sequelizeConfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"database": "tutorial",
"username": "postgres",
"password": null,
"database": "blog_sys",
"username": "root",
"password": "root123",
"params": {
"host": "localhost",
"dialect": "postgres",
"dialect": "mysql",
"operatorsAliases": false
}
}
12 changes: 4 additions & 8 deletions src/models/Post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import { SequelizeAttributes } from 'typings/SequelizeAttributes';

export interface PostAttributes {
id?: number;
name: string;
title: string;
text: string;
category: 'tech' | 'croissants' | 'techno';
createdAt?: Date;
updatedAt?: Date;
comments?: CommentAttributes[] | CommentAttributes['id'][];
author?: UserAttributes | UserAttributes['id'];
/* comments?: CommentAttributes[] | CommentAttributes['id'][];
author?: UserAttributes | UserAttributes['id']; */
};

export interface PostInstance extends Sequelize.Instance<PostAttributes>, PostAttributes {
Expand All @@ -29,14 +28,11 @@ export interface PostInstance extends Sequelize.Instance<PostAttributes>, PostAt

getAuthor: Sequelize.BelongsToGetAssociationMixin<UserInstance>;
setAuthor: Sequelize.BelongsToSetAssociationMixin<UserInstance, UserInstance['id']>;
createAuthor: Sequelize.BelongsToCreateAssociationMixin<UserAttributes>;
createAuthor: Sequelize.BelongsToCreateAssociationMixin<UserAttributes,UserInstance>;
};

export const PostFactory = (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes): Sequelize.Model<PostInstance, PostAttributes> => {
const attributes: SequelizeAttributes<PostAttributes> = {
name: {
type: DataTypes.STRING
},
title: {
type: DataTypes.STRING
},
Expand All @@ -51,7 +47,7 @@ export const PostFactory = (sequelize: Sequelize.Sequelize, DataTypes: Sequelize
const Post = sequelize.define<PostInstance, PostAttributes>('Post', attributes);

Post.associate = models => {
Post.hasMany(models.Comment, { as: 'comments' });
// Post.hasMany(models.Comment, { as: 'comments' });
Post.belongsTo(models.User, { as: 'author', foreignKey: 'AuthorId' });
};

Expand Down
8 changes: 3 additions & 5 deletions src/models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ export interface UserAttributes {
name: string;
createdAt?: Date;
updatedAt?: Date;
comments?: CommentAttributes[] | CommentAttributes['id'][];
posts?: PostAttributes[] | PostAttributes['id'][];
upvotedComments?: CommentAttributes[] | CommentAttributes['id'][];
};

export interface UserInstance extends Sequelize.Instance<UserAttributes>, UserAttributes {
Expand Down Expand Up @@ -58,12 +56,12 @@ export const UserFactory = (sequelize: Sequelize.Sequelize, DataTypes: Sequelize
const User = sequelize.define<UserInstance, UserAttributes>('User', attributes);

User.associate = models => {
User.hasMany(models.Comment, { foreignKey: 'AuthorId', as: 'comments' });
// User.hasMany(models.Comment, { foreignKey: 'AuthorId', as: 'comments' });
User.hasMany(models.Post, { foreignKey: 'AuthorId', as: 'posts' });
User.belongsToMany(models.Comment, {
/* User.belongsToMany(models.Comment, {
through: 'PostUpvotes',
as: 'upvotedComments'
});
}); */
};

return User;
Expand Down
4 changes: 2 additions & 2 deletions src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Sequelize from 'sequelize';
import { DbInterface } from'typings/DbInterface';
import { UserFactory } from './User';
import { PostFactory } from './Post';
import { CommentFactory } from './Comment';
// import { CommentFactory } from './Comment';

export const createModels = (sequelizeConfig: any): DbInterface => {
const { database, username, password, params } = sequelizeConfig;
Expand All @@ -11,7 +11,7 @@ export const createModels = (sequelizeConfig: any): DbInterface => {
const db: DbInterface = {
sequelize,
Sequelize,
Comment: CommentFactory(sequelize, Sequelize),
// Comment: CommentFactory(sequelize, Sequelize),
Post: PostFactory(sequelize, Sequelize),
User: UserFactory(sequelize, Sequelize)
};
Expand Down
2 changes: 1 addition & 1 deletion src/typings/DbInterface/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { UserAttributes, UserInstance } from 'models/User';
export interface DbInterface {
sequelize: Sequelize.Sequelize;
Sequelize: Sequelize.SequelizeStatic;
Comment: Sequelize.Model<CommentInstance, CommentAttributes>;
// Comment: Sequelize.Model<CommentInstance, CommentAttributes>;
Post: Sequelize.Model<PostInstance, PostAttributes>;
User: Sequelize.Model<UserInstance, UserAttributes>;
}