CodingSouls团队项目冲刺(3)-个人概况

团队冲刺第三天:

  1.登录注册数据库设计,后端代码实现:

  1 import * as TypeORM from "typeorm";
  2 import Model from "./common";
  3 
  4 declare var syzoj: any;
  5 
  6 import JudgeState from "./judge_state";
  7 import UserPrivilege from "./user_privilege";
  8 import Article from "./article";
  9 @TypeORM.Entity()
 10 export default class User extends Model {
 11   static cache = true;
 12  @TypeORM.Column({ nullable: true, type: "boolean" })@TypeORM.PrimaryGeneratedColumn()
 13   id: number;
 14 
 15   @TypeORM.Index({ unique: true })
 16   @TypeORM.Column({ nullable: true, type: "varchar", length: 80 })
 17   username: string;
 18 
 19   @TypeORM.Column({ nullable: true, type: "varchar", length: 120 })
 20   email: string;
 21 
 22   @TypeORM.Column({ nullable: true, type: "varchar", length: 120 })
 23   password: string;
 24 
 25   @TypeORM.Column({ nullable: true, type: "varchar", length: 80 })
 26   nickname: string;
 27 
 28   @TypeORM.Column({ nullable: true, type: "text" })
 29   nameplate: string;
 30 
 31   @TypeORM.Column({ nullable: true, type: "text" })
 32   information: string;
 33   static async fromEmail(email): Promise<User> {
 34     return User.findOne({
 35       where: {
 36         email: email
 37       }
 38     });
 39   }
 40 
 41   static async fromName(name): Promise<User> {
 42     return User.findOne({
 43       where: {
 44         username: name
 45       }
 46     });
 47   }
 48 
 49   async isAllowedEditBy(user) {
 50     if (!user) return false;
 51     if (await user.hasPrivilege('manage_user')) return true;
 52     return user && (user.is_admin || this.id === user.id);
 53   }
 54 
 55   getQueryBuilderForACProblems() {
 56     return JudgeState.createQueryBuilder()
 57                      .select(`DISTINCT(problem_id)`)
 58                      .where('user_id = :user_id', { user_id: this.id })
 59                      .andWhere('status = :status', { status: 'Accepted' })
 60                      .andWhere('type != 1')
 61                      .orderBy({ problem_id: 'ASC' })
 62   }
 63 
 64   async refreshSubmitInfo() {
 65     await syzoj.utils.lock(['User::refreshSubmitInfo', this.id], async () => {
 66       this.ac_num = await JudgeState.countQuery(this.getQueryBuilderForACProblems());
 67       this.submit_num = await JudgeState.count({
 68         user_id: this.id,
 69         type: TypeORM.Not(1) // Not a contest submission
 70       });
 71 
 72       await this.save();
 73     });
 74   }
 75 
 76   async getACProblems() {
 77     let queryResult = await this.getQueryBuilderForACProblems().getRawMany();
 78 
 79     return queryResult.map(record => record['problem_id'])
 80   }
 81 
 82   async getArticles() {
 83     return await Article.find({
 84       where: {
 85         user_id: this.id
 86       }
 87     });
 88   }
 89   async setPrivileges(newPrivileges) {
 90     let oldPrivileges = await this.getPrivileges();
 91 
 92     let delPrivileges = oldPrivileges.filter(x => !newPrivileges.includes(x));
 93     let addPrivileges = newPrivileges.filter(x => !oldPrivileges.includes(x));
 94 
 95     for (let privilege of delPrivileges) {
 96       let obj = await UserPrivilege.findOne({ where: {
 97         user_id: this.id,
 98         privilege: privilege
 99       } });
100 
101       await obj.destroy();
102     }
103 
104     for (let privilege of addPrivileges) {
105       let obj = await UserPrivilege.create({
106         user_id: this.id,
107         privilege: privilege
108       });
109 
110       await obj.save();
111     }
112   }
113 
114   async hasPrivilege(privilege) {
115     if (this.is_admin) return true;
116 
117     let x = await UserPrivilege.findOne({ where: { user_id: this.id, privilege: privilege } });
118     return !!x;
119   }
120 
121   async getLastSubmitLanguage() {
122     let a = await JudgeState.findOne({
123       where: {
124         user_id: this.id
125       },
126       order: {
127         submit_time: 'DESC'
128       }
129     });
130     if (a) return a.language;
131 
132     return null;
133   }
134 }
登录注册后端代码

还没写完明天继续。

posted @ 2020-04-17 18:46  DemonSlayer  阅读(156)  评论(0编辑  收藏  举报