团队冲刺第十天:
讨论区后端开发:后端讨论区,将评论与回复分开实现。
1 import * as TypeORM from "typeorm"; 2 import Model from "./common"; 3 4 import User from "./user"; 5 import Problem from "./problem"; 6 import ArticleComment from "./article-comment"; 7 8 declare var syzoj: any; 9 10 @TypeORM.Entity() 11 export default class Article extends Model { 12 static cache = false; 13 14 @TypeORM.PrimaryGeneratedColumn() 15 id: number; 16 17 @TypeORM.Column({ nullable: true, type: "varchar", length: 80 }) 18 title: string; 19 20 @TypeORM.Column({ nullable: true, type: "mediumtext" }) 21 content: string; 22 23 @TypeORM.Index() 24 @TypeORM.Column({ nullable: true, type: "integer" }) 25 user_id: number; 26 27 @TypeORM.Index() 28 @TypeORM.Column({ nullable: true, type: "integer" }) 29 problem_id: number; 30 31 @TypeORM.Column({ nullable: true, type: "integer" }) 32 public_time: number; 33 34 @TypeORM.Column({ nullable: true, type: "integer" }) 35 update_time: number; 36 37 @TypeORM.Index() 38 @TypeORM.Column({ nullable: true, type: "integer" }) 39 sort_time: number; 40 41 @TypeORM.Column({ default: 0, type: "integer" }) 42 comments_num: number; 43 44 @TypeORM.Column({ default: true, type: "boolean" }) 45 allow_comment: boolean; 46 47 @TypeORM.Index() 48 @TypeORM.Column({ nullable: true, type: "boolean" }) 49 is_notice: boolean; 50 51 user?: User; 52 problem?: Problem; 53 54 async loadRelationships() { 55 this.user = await User.findById(this.user_id); 56 } 57 58 async isAllowedEditBy(user) { 59 return user && (user.is_admin || this.user_id === user.id); 60 } 61 62 async isAllowedCommentBy(user) { 63 return user && (this.allow_comment || user.is_admin || this.user_id === user.id); 64 } 65 66 async resetReplyCountAndTime() { 67 await syzoj.utils.lock(['Article::resetReplyCountAndTime', this.id], async () => { 68 this.comments_num = await ArticleComment.count({ article_id: this.id }); 69 if (this.comments_num === 0) { 70 this.sort_time = this.public_time; 71 } else { 72 this.sort_time = (await ArticleComment.findOne({ 73 where: { article_id: this.id }, 74 order: { public_time: "DESC" } 75 })).public_time; 76 } 77 await this.save(); 78 }); 79 } 80 };
1 import * as TypeORM from "typeorm"; 2 import Model from "./common"; 3 4 import User from "./user"; 5 import Article from "./article"; 6 7 @TypeORM.Entity() 8 export default class ArticleComment extends Model { 9 @TypeORM.PrimaryGeneratedColumn() 10 id: number; 11 12 @TypeORM.Column({ nullable: true, type: "text" }) 13 content: string; 14 15 @TypeORM.Index() 16 @TypeORM.Column({ nullable: true, type: "integer" }) 17 article_id: number; 18 19 @TypeORM.Index() 20 @TypeORM.Column({ nullable: true, type: "integer" }) 21 user_id: number; 22 23 @TypeORM.Column({ nullable: true, type: "integer" }) 24 public_time: number; 25 26 user?: User; 27 article?: Article; 28 29 async loadRelationships() { 30 this.user = await User.findById(this.user_id); 31 this.article = await Article.findById(this.article_id); 32 } 33 34 async isAllowedEditBy(user) { 35 await this.loadRelationships(); 36 return user && (user.is_admin || this.user_id === user.id || user.id === this.article.user_id); 37 } 38 };