Skip to content

Commit 927f476

Browse files
user & comment model added in coments serivice
1 parent 068a176 commit 927f476

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

comments/src/models/comments.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import mongoose from 'mongoose';
2+
import { UserDoc } from './User';
3+
4+
// An interface that describes the prop that are req to create a new Comments
5+
export interface CommentsAttrs {
6+
text: string,
7+
createdAt: number,
8+
user: mongoose.Types.ObjectId,
9+
storyId: number,
10+
child_comment_count: number
11+
}
12+
13+
// An interface that describes the properties that a Comments model has
14+
interface CommentsModel extends mongoose.Model<CommentsDoc> {
15+
build(attrs: CommentsAttrs): CommentsDoc;
16+
}
17+
18+
// An interface that describes the properties that a Comments Document has
19+
export interface CommentsDoc extends mongoose.Document {
20+
text: string,
21+
createdAt: number,
22+
user: UserDoc,
23+
storyId: number,
24+
}
25+
26+
const commentsSchema = new mongoose.Schema(
27+
{
28+
text: {
29+
type: String,
30+
required: true
31+
},
32+
createdAt: {
33+
type: Number
34+
},
35+
user: {
36+
type: mongoose.Types.ObjectId,
37+
required: true,
38+
ref: 'User'
39+
},
40+
storyId: {
41+
type: Number,
42+
required: true
43+
},
44+
child_comment_count: {
45+
type: Number,
46+
default: 0
47+
}
48+
},
49+
{
50+
toJSON: {
51+
transform(doc, ret) {
52+
ret.id = ret._id;
53+
ret._id = undefined;
54+
}
55+
},
56+
versionKey: false
57+
}
58+
);
59+
60+
commentsSchema.statics.build = (attrs: CommentsAttrs) => {
61+
return new Comments(attrs);
62+
};
63+
64+
const Comments = mongoose.model<CommentsDoc, CommentsModel>('Comments', commentsSchema);
65+
66+
export { Comments };
67+

comments/src/models/user.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import mongoose from 'mongoose';
2+
3+
// An interface that describes the prop that are req to create a new User
4+
export interface UserAttrs {
5+
about: string,
6+
created: number,
7+
id: number,
8+
karma: number
9+
}
10+
11+
// An interface that describes the properties that a User model has
12+
interface UserModel extends mongoose.Model<UserDoc> {
13+
build(attrs: UserAttrs): UserDoc;
14+
}
15+
16+
// An interface that describes the properties that a User Document has
17+
export interface UserDoc extends mongoose.Document {
18+
about: string,
19+
createdAt: number,
20+
user: string
21+
}
22+
23+
const userSchema = new mongoose.Schema(
24+
{
25+
about: {
26+
type: String
27+
},
28+
createdAt: {
29+
type: Number,
30+
required: true
31+
},
32+
user: {
33+
type: String,
34+
required: true,
35+
unique: true
36+
},
37+
karma: {
38+
type: Number
39+
}
40+
},
41+
{
42+
toJSON: {
43+
transform(doc, ret) {
44+
ret.id = ret._id;
45+
ret._id = undefined;
46+
}
47+
},
48+
versionKey: false
49+
}
50+
);
51+
52+
userSchema.statics.build = (attrs: UserAttrs) => {
53+
return new User({
54+
about: attrs.about,
55+
createdAt: attrs.created,
56+
user: attrs.id,
57+
karma: attrs.karma
58+
});
59+
};
60+
61+
const User = mongoose.model<UserDoc, UserModel>('User', userSchema);
62+
63+
export { User };
64+

0 commit comments

Comments
 (0)