Skip to content

Commit

Permalink
feat(shared): 新增user模块 附带服务,schema配置,interface定义
Browse files Browse the repository at this point in the history
  • Loading branch information
jiayisheji committed Aug 23, 2018
1 parent fe85858 commit 76ec404
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/shared/mongodb/user/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './user.service';
export * from './user.module';
export * from './user.interface';
39 changes: 39 additions & 0 deletions src/shared/mongodb/user/user.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { BaseInterface } from '../base.interface';

export interface User extends BaseInterface {
name: string; // 显示名字
loginname: string; // 登录名
pass: string; // 密码
age: number; // 年龄
email: string; // 邮箱
active: boolean; // 是否激活
collect_topic_count: number; // 收集话题数
topic_count: number; // 发布话题数
score: number; // 积分
is_star: boolean; //
is_block: boolean; // 是否黑名单
url: string; // 个人主页
location: string; // 位置
profile_image_url: string;
signature: string; // 签名
profile: string; // 描述
weibo: string; // 微博地址
avatar: string; // 头像地址
githubId: string; // github id 绑定github获取的
githubUsername: string; // github 用户名 绑定github获取的
githubAccessToken: string; // github AccessToken 绑定github获取的
reply_count: number; // 回复数
follower_count: number; // 粉丝
following_count: number; // 关注
collect_tag_count: number; // 收集标签数
level: string; // 等级
receive_reply_mail: boolean; // 收到回复邮件
receive_at_mail: boolean; // 收到的邮件
from_wp: boolean; // 来源
retrieve_time: number; // 检索时间
retrieve_key: string; // 检索key
accessToken: string; // AccessToken
is_admin?: boolean; // 是否是管理员
avatar_url?: string; // 头像地址
isAdvanced?: boolean; // 是否高级用户
}
12 changes: 12 additions & 0 deletions src/shared/mongodb/user/user.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Module } from '@nestjs/common';
import { UserService } from './user.service';
import { MongooseModule } from '@nestjs/mongoose';
import { UserSchema } from './user.schema';

@Module({
imports: [
MongooseModule.forFeature([{ name: 'User', schema: UserSchema }]),
],
providers: [UserService],
})
export class UserModule {}
82 changes: 82 additions & 0 deletions src/shared/mongodb/user/user.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { Schema } from 'mongoose';
import { schemaOptions } from '../base.schema';
import { User } from './user.interface';
import * as utility from 'utility';

export const UserSchema = new Schema({
name: { type: String },
loginname: { type: String },
pass: { type: String },
email: { type: String },
url: { type: String },
profile_image_url: { type: String },
location: { type: String },
signature: { type: String },
profile: { type: String },
weibo: { type: String },
avatar: { type: String },
githubId: { type: String },
githubUsername: { type: String },
githubAccessToken: { type: String },
is_block: { type: Boolean, default: false },

score: { type: Number, default: 0 },
topic_count: { type: Number, default: 0 },
reply_count: { type: Number, default: 0 },
follower_count: { type: Number, default: 0 },
following_count: { type: Number, default: 0 },
collect_tag_count: { type: Number, default: 0 },
collect_topic_count: { type: Number, default: 0 },
is_star: { type: Boolean },
level: { type: String },
active: { type: Boolean, default: false },

receive_reply_mail: { type: Boolean, default: false },
receive_at_mail: { type: Boolean, default: false },
from_wp: { type: Boolean },

retrieve_time: { type: Number },
retrieve_key: { type: String },

accessToken: { type: String },
}, schemaOptions);

// 设置索引
UserSchema.index({ loginname: 1 }, { unique: true });
UserSchema.index({ email: 1 }, { unique: true });
UserSchema.index({ score: -1 });
UserSchema.index({ githubId: 1 });
UserSchema.index({ accessToken: 1 });

// 设置虚拟属性
UserSchema.virtual('avatar_url').get(function() {
let url =
this.avatar ||
`https://gravatar.com/avatar/${utility.md5(this.email.toLowerCase())}?size=48`;

// www.gravatar.com 被墙
url = url.replace('www.gravatar.com', 'gravatar.com');

// 让协议自适应 protocol,使用 `//` 开头
if (url.indexOf('http:') === 0) {
url = url.slice(5);
}

// 如果是 github 的头像,则限制大小
if (url.indexOf('githubusercontent') !== -1) {
url += '&s=120';
}
return url;
});

UserSchema.virtual('isAdvanced').get(function() {
// 积分高于 700 则认为是高级用户
return this.score > 700 || this.is_star;
});

// 设置保存中间件
UserSchema.pre('save', function(next) {
const now = new Date();
(this as User).update_at = now;
next();
});
15 changes: 15 additions & 0 deletions src/shared/mongodb/user/user.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Injectable } from '@nestjs/common';
import { BaseService } from '../base.service';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { User } from './user.interface';

@Injectable()
export class UserService extends BaseService<User> {
constructor(
@InjectModel('User') private readonly userModel: Model<User>,
) {
super();
this._model = userModel;
}
}

0 comments on commit 76ec404

Please sign in to comment.