-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
CommentService.map
61 lines (61 loc) · 6.97 KB
/
CommentService.map
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
{
"mappings": ";;;AAAA,SAASA,KAAK,EAAEC,cAAcC,kBAAkB,QAAQ,WAAW;AACnE,SACEC,kBAAkB,EAClBC,UAAU,EACVC,4BAA4B,EAC5BC,iBAAiB,QACZ,iBAAiB;AACxB,SAASC,WAAW,EAAEC,gBAAgB,QAAQ,mBAAmB;AAEjE,SAASC,OAAO,QAAyB,wCAAwC;AAGjF,SAASC,IAAI,QAAsB,kCAAkC;AAGrE,WAAaC,iBAAN;IACLC,YACqCC,cACHC,WACKC,WACrC;4BAHmCF;yBACHC;0BACKC;IACpC;IAEH,MAAMC,QAAQC,SAAiB,EAA4B;QACzD,MAAMC,UAAU,MAAM,IAAI,CAACL,YAAY,CAACM,QAAQ,CAACF;QAEjD,IAAI,CAACC,SAAS;YACZ,MAAM,IAAIZ,oBAAoB;QAChC,CAAC;QAED,OAAOY,QAAQE,QAAQ,CAAC;IAC1B;IAEA,MAAMC,OACJH,OAAyB,EACzBI,MAAc,EACdC,MAAc,EACY;QAC1B,MAAMC,UAAU,MAAM,IAAI,CAACT,UAAU,CAACU,YAAY;QAElDD,QAAQE,gBAAgB;QAExB,IAAI;YACF,MAAMC,iBAAiB,MAAM,IAAI,CAACd,YAAY,CAACQ,MAAM,CAAC;gBACpD,GAAGH,OAAO;gBACVU,QAAQL;gBACRD;YACF;YAEA,IAAI,CAACK,gBAAgB;gBACnB,MAAM,IAAItB,6BAA6B,2BAA2B;YACpE,CAAC;YAED,MAAM,EAAEwB,cAAa,EAAE,GAAG,MAAM,IAAI,CAACf,SAAS,CAACgB,SAAS,CACtD;gBAAEC,KAAKJ,eAAeL,MAAM;YAAC,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AE,GAAG,MAAM,IAAI,CAACf,SAAS,CAACgB,SAAS,CACtD;gBAAEC,KAAKT;YAAO,GACd;gBACEyB,OAAO;oBAAEd,UAAUhB;gBAAU;YAC/B,GACA;gBAAEkB,kBAAkB,KAAK;YAAC;YAG5B,IAAIU,iBAAiB,KAAKhB,kBAAkB,GAAG;gBAC7C,MAAM,IAAIxB,6BAA6B,2BAA2B;YACpE,CAAC;YAED,MAAMmB,QAAQY,iBAAiB;QACjC,EAAE,OAAOC,OAAO;YACd,MAAMb,QAAQc,gBAAgB;YAE9B,MAAMD,MAAM;QACd,SAAU;YACRb,QAAQe,UAAU;QACpB;IACF;AACF,EAAC;AAtHY5B;IADZP;IAGIG,aAAAA,YAAYE,QAAQuC,IAAI;IACxBzC,aAAAA,YAAYG,KAAKsC,IAAI;IACrBxC,aAAAA;;;eAFgD,iCAAA;eACN,iCAAA;eACM,8CAAA;;GAJxCG",
"names": [
"Model",
"Connection",
"MongooseConnection",
"ForbiddenException",
"Injectable",
"InternalServerErrorException",
"NotFoundException",
"InjectModel",
"InjectConnection",
"Comment",
"Post",
"CommentService",
"constructor",
"commentModel",
"postModel",
"connection",
"getById",
"commentId",
"comment",
"findById",
"populate",
"create",
"postId",
"userId",
"session",
"startSession",
"startTransaction",
"createdComment",
"author",
"modifiedCount",
"updateOne",
"_id",
"$push",
"comments",
"id",
"useFindAndModify",
"commitTransaction",
"error",
"abortTransaction",
"endSession",
"update",
"updatedComment",
"findByIdAndUpdate",
"new",
"delete",
"deletedCount",
"deleteOne",
"$pull",
"name"
],
"sources": [
"../../input/CommentService.ts"
],
"sourcesContent": [
"import { Model, Connection as MongooseConnection } from 'mongoose';\nimport {\n ForbiddenException,\n Injectable,\n InternalServerErrorException,\n NotFoundException,\n} from '@nestjs/common';\nimport { InjectModel, InjectConnection } from '@nestjs/mongoose';\n\nimport { Comment, CommentDocument } from '@server/comment/schemas/CommentSchema';\nimport { CreateCommentDto } from '@server/comment/dto/CreateCommentDto';\nimport { UpdateCommentDto } from '@server/comment/dto/UpdateCommentDto';\nimport { Post, PostDocument } from '@server/post/schemas/PostSchema';\n\n@Injectable()\nexport class CommentService {\n constructor(\n @InjectModel(Comment.name) private commentModel: Model<CommentDocument>,\n @InjectModel(Post.name) private postModel: Model<PostDocument>,\n @InjectConnection() private readonly connection: MongooseConnection,\n ) {}\n\n async getById(commentId: string): Promise<CommentDocument> {\n const comment = await this.commentModel.findById(commentId);\n\n if (!comment) {\n throw new NotFoundException();\n }\n\n return comment.populate('author');\n }\n\n async create(\n comment: CreateCommentDto,\n postId: string,\n userId: string,\n ): Promise<CommentDocument> {\n const session = await this.connection.startSession();\n\n session.startTransaction();\n\n try {\n const createdComment = await this.commentModel.create({\n ...comment,\n author: userId,\n postId,\n });\n\n if (!createdComment) {\n throw new InternalServerErrorException('Comment was not created');\n }\n\n const { modifiedCount } = await this.postModel.updateOne(\n { _id: createdComment.postId },\n {\n $push: { comments: createdComment.id },\n },\n { useFindAndModify: false },\n );\n\n if (modifiedCount === 0) {\n throw new InternalServerErrorException('Comment was not created');\n }\n\n await session.commitTransaction();\n\n return createdComment.populate('author');\n } catch (error) {\n await session.abortTransaction();\n\n throw error;\n } finally {\n session.endSession();\n }\n }\n\n async update(\n commentId: string,\n comment: UpdateCommentDto,\n userId: string,\n ): Promise<CommentDocument> {\n const { author } = await this.getById(commentId);\n\n if (author.id !== userId) {\n throw new ForbiddenException();\n }\n\n const updatedComment = await this.commentModel.findByIdAndUpdate(commentId, comment, {\n new: true,\n });\n\n if (!updatedComment) {\n throw new InternalServerErrorException('Comment was not updated');\n }\n\n return updatedComment.populate('author');\n }\n\n async delete(commentId: string, userId: string): Promise<void> {\n const session = await this.connection.startSession();\n\n session.startTransaction();\n\n try {\n const { author, postId } = await this.getById(commentId);\n\n if (author.id !== userId) {\n throw new ForbiddenException();\n }\n\n const { deletedCount } = await this.commentModel.deleteOne({ _id: commentId });\n\n const { modifiedCount } = await this.postModel.updateOne(\n { _id: postId },\n {\n $pull: { comments: commentId },\n },\n { useFindAndModify: false },\n );\n\n if (deletedCount === 0 || modifiedCount === 0) {\n throw new InternalServerErrorException('Comment was not deleted');\n }\n\n await session.commitTransaction();\n } catch (error) {\n await session.abortTransaction();\n\n throw error;\n } finally {\n session.endSession();\n }\n }\n}\n"
],
"version": 3
}