Skip to content

Commit

Permalink
事务
Browse files Browse the repository at this point in the history
  • Loading branch information
Y-io committed Feb 19, 2019
1 parent 01fe4da commit a089b84
Show file tree
Hide file tree
Showing 31 changed files with 406 additions and 80 deletions.
4 changes: 4 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { HttpErrorFilter } from './core';
import { LoggingInterceptor } from './core';
import { CategoryModule } from './modules/category/category.module';
import { PostModule } from './modules/post/post.module';
import { CourseModule } from './modules/course/course.module';
import { CourseSupplierModule } from './modules/course-supplier/course-supplier.module';

@Module({
imports: [
Expand All @@ -29,6 +31,8 @@ import { PostModule } from './modules/post/post.module';
}),
UserModule,
CategoryModule,
CourseModule,
CourseSupplierModule,
PostModule,
],
providers: [
Expand Down
11 changes: 1 addition & 10 deletions src/auth/auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { GqlExecutionContext } from '@nestjs/graphql';
import { ROLES_DEFINITION } from 'src/core';
import { AuthService } from './auth.service';
import { User } from 'src/modules/user/user.interface';
import { RoleEnum } from 'src/modules/base.enum';
import { RoleEnum } from 'src/modules/base.object';

@Injectable()
export class AuthGurad implements CanActivate {
Expand Down Expand Up @@ -43,15 +43,6 @@ export class AuthGurad implements CanActivate {
if (user.userName === 'sadmin' || user.role === RoleEnum.SuperAdmin)
return true;

// if (user && !!user.role.length) {
// let len: number = 0;

// user.roles.forEach(v => {
// if (roles.includes(v)) len++;
// });

// return len !== 0;
// }
return true;
}
}
2 changes: 1 addition & 1 deletion src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class AuthService {
const decodedToken = <{ _id: string; userName: string }>(
jwt.verify(token, process.env.SECRET)
);
console.log(decodedToken);

const user: User = await this.userService.findUserById(decodedToken._id);

return user;
Expand Down
5 changes: 5 additions & 0 deletions src/core/decorators/user.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createParamDecorator } from '@nestjs/common';

export const User = createParamDecorator((data, req) => {
return data ? req.user[data] : req.user;
});
13 changes: 0 additions & 13 deletions src/modules/base.enum.ts
Original file line number Diff line number Diff line change
@@ -1,13 +0,0 @@
// 状态
export enum StatusEnum {
Examine = 'Examine', // 审核
Banned = 'Banned', // 禁止
Normal = 'Normal', // 正常
}

// 权限列表
export enum RoleEnum {
SuperAdmin = 'SuperAdmin', // 超级管理员
Admin = 'Admin', // 管理员
User = 'User', // 普通用户
}
19 changes: 11 additions & 8 deletions src/modules/base.object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@
// 状态列表
export const StatusArr = ['Examine', 'Banned', 'Normal'];

// 权限列表
export enum StatusEnum {}
for (const i in StatusArr) {
StatusEnum[StatusArr[i]] = StatusArr[i];
// 状态
export enum StatusEnum {
Examine = 'Examine', // 审核
Banned = 'Banned', // 禁止
Normal = 'Normal', // 正常
}

// 状态列表
export const RoleArr = ['SuperAdmin', 'Admin', 'User'];
export const RoleArr = ['SuperAdmin', 'Admin', 'Supplier', 'User'];

// 权限列表
export enum ArrEnum {}
for (const i in RoleArr) {
ArrEnum[RoleArr[i]] = RoleArr[i];
export enum RoleEnum {
SuperAdmin = 'SuperAdmin', // 超级管理员
Admin = 'Admin', // 管理员
Supplier = 'Supplier', // 机构
User = 'User', // 普通用户
}
6 changes: 6 additions & 0 deletions src/modules/category/category.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Controller, Get } from '@nestjs/common';

@Controller()
export class CategoryController {
// constructor() {}
}
31 changes: 27 additions & 4 deletions src/modules/category/category.module.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,37 @@
import { Module } from '@nestjs/common';
import { Module, OnModuleInit } from '@nestjs/common';
import { CategoryResolver } from './category.resolver';
import { CategoryService } from './category.service';
import { MongooseModule } from '@nestjs/mongoose';
import { MongooseModule, InjectModel } from '@nestjs/mongoose';
import { CategorySchema } from './category.schema';
import { CategoryController } from './category.controller';
import { Model } from 'mongoose';
import { Category } from './category.interface';

@Module({
imports: [
MongooseModule.forFeature([{ name: 'Category', schema: CategorySchema }]),
],
controllers: [],
controllers: [CategoryController],
providers: [CategoryResolver, CategoryService],
})
export class CategoryModule {}
export class CategoryModule implements OnModuleInit {
constructor(
@InjectModel('Category') private readonly categoryModel: Model<Category>,
) {}
async onModuleInit() {
await this.createCategory();
}
private async createCategory() {
let category = await this.categoryModel.findOne({ name: '舞蹈' });
if (!category) {
category = await this.categoryModel.create({ name: '舞蹈' });
await category.save();

category = await this.categoryModel.create({ name: '艺术体操' });
await category.save();

category = await this.categoryModel.create({ name: '美术' });
await category.save();
}
}
}
2 changes: 1 addition & 1 deletion src/modules/category/category.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CategoryService } from './category.service';

import { Category } from './category.interface';
import { Roles } from 'src/core';
import { RoleEnum } from '../base.enum';
import { RoleEnum } from '../base.object';

@Resolver()
export class CategoryResolver {
Expand Down
8 changes: 0 additions & 8 deletions src/modules/category/category.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@ export const CategorySchema = new Schema(
parent: { type: Schema.Types.ObjectId, ref: 'Category', default: null },
children: [{ type: Schema.Types.ObjectId, ref: 'Category' }],
paths: [String],
created: {
type: Date,
default: new Date(),
},
updated: {
type: Date,
default: new Date(),
},
},
schemaOptions,
);
Expand Down
61 changes: 61 additions & 0 deletions src/modules/course-supplier/course-supplier.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {
Controller,
Get,
Post,
Body,
Put,
Query,
Delete,
Param,
} from '@nestjs/common';
import { ICourseSupplier, CourseSupplier } from './course-supplier.interface';
import { Roles } from 'src/core';
import { RoleEnum } from '../base.object';
import { CourseSupplierService } from './course-supplier.service';
import { CommonResult } from '../base.interface';
import { User } from 'src/core/decorators/user.decorator';
import { User as IUer } from '../user/user.interface';

@Controller('courseSupplier')
export class CourseSupplierController {
constructor(private readonly courseSupplierService: CourseSupplierService) {}

@Post()
@Roles(RoleEnum.SuperAdmin, RoleEnum.Supplier)
async createCourseSupplier(
@User() user: IUer,
@Body() body: ICourseSupplier,
): Promise<CommonResult<CourseSupplier>> {
const data = await this.courseSupplierService.createCourseSupplier(
user,
body,
);
return { code: 200, message: '创建成功', data };
}

@Get()
@Roles(RoleEnum.SuperAdmin, RoleEnum.Admin, RoleEnum.Supplier)
async findCourseSupplier(): Promise<CommonResult<CourseSupplier[]>> {
const data = await this.courseSupplierService.findCourseSupplier();
return { code: 200, message: '查询成功', data };
}

@Put(':id')
@Roles(RoleEnum.Supplier)
async updataCourseSupplierById(
@Param('id') id: string,
@User() user: IUer,
@Body() body: CourseSupplier,
): Promise<CommonResult> {
await this.courseSupplierService.updataCourseSupplierById(id, user, body);
return { code: 200, message: '更新成功' };
}

// @Delete(':id')
// @Roles(RoleEnum.SuperAdmin, RoleEnum.Supplier)
// async deleteCourseSupplierById(
// @Param('id') id: string,
// ): Promise<CommonResult> {
// const;
// }
}
32 changes: 32 additions & 0 deletions src/modules/course-supplier/course-supplier.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { BaseInterface } from '../base.interface';
import { User } from '../user/user.interface';
import * as mongoose from 'mongoose';

export interface CourseSupplier extends BaseInterface {
name: string; // 名称
status: string; // normal:正常, examine:审核, banned:禁止
content: string[]; // 文字内容
contentImage: string[]; // 图片内容
user: User; // 作者,关联用户文档
recycle: boolean; // 是否加入回收站
top: boolean; // 置顶
// comment: [ObjectId]; // 评论
minPeople: number; // 课程容纳最低人数
maxPeople: number; // 课程容纳最高人数
price: number; // 价格
discountPrice: number; // 零售的折扣价,用于业务员除外谈单
banner: [string]; // 海报列表
sex: number; // 0:不限, 1:男, 2:女
}

export interface ICourseSupplier {
_id: mongoose.Types.ObjectId; // id
name: string; // 名称
sex: number; // 0:不限, 1:男, 2:女
minPeople: number; // 课程容纳最低人数
maxPeople: number; // 课程容纳最高人数
price: number; // 价格
banner: [string]; // 海报列表
content: string[]; // 文字内容
contentImage: string[]; // 图片内容
}
18 changes: 18 additions & 0 deletions src/modules/course-supplier/course-supplier.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Module } from '@nestjs/common';
import { CourseSupplierController } from './course-supplier.controller';
import { CourseSupplierService } from './course-supplier.service';
import { MongooseModule } from '@nestjs/mongoose';
import { CourseSupplierSchema } from './course-supplier.schema';
import { UserSchema } from '../user/user.schema';

@Module({
imports: [
MongooseModule.forFeature([
{ name: 'CourseSupplier', schema: CourseSupplierSchema },
{ name: 'User', schema: UserSchema },
]),
],
controllers: [CourseSupplierController],
providers: [CourseSupplierService],
})
export class CourseSupplierModule {}
23 changes: 23 additions & 0 deletions src/modules/course-supplier/course-supplier.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as mongoose from 'mongoose';
import { schemaOptions } from '../base.schema';
const { Schema } = mongoose;

export const CourseSupplierSchema = new Schema(
{
name: String,
status: String,
content: [String], // 文字内容
contentImage: [String], // 图片内容
user: { type: Schema.Types.ObjectId, ref: 'User' },
recycle: Boolean,
top: Boolean,
// comment: [ObjectId],
minPeople: Number,
maxPeople: Number,
price: Number,
discountPrice: Number,
banner: [String],
sex: Number,
},
schemaOptions,
);
Loading

0 comments on commit a089b84

Please sign in to comment.