Skip to content

Commit

Permalink
feat: setting crud
Browse files Browse the repository at this point in the history
  • Loading branch information
SolidZORO committed May 24, 2020
1 parent f4cd1b7 commit 800f107
Show file tree
Hide file tree
Showing 22 changed files with 633 additions and 535 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class CreateCategoryInput {
@IsNotEmpty()
slug!: string;

parent_id?: string;
parent_id?: string | null;

description?: string;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
import { IsOptional } from 'class-validator';
import { Field, InputType, Int } from '@nestjs/graphql';

import { Category } from '@leaa/common/src/entrys';

@InputType()
export class UpdateCategoryInput {
@IsOptional()
@Field(() => String)
name?: string;

@IsOptional()
@Field(() => String)
slug?: string;

@IsOptional()
@Field(() => String, { nullable: true })
parent_id?: string | null;

@IsOptional()
@Field(() => String, { nullable: true })
description?: string;

// @IsOptional()
// @Field(() => Category, { nullable: true })
parent?: Category | null;
}
15 changes: 8 additions & 7 deletions packages/_leaa-common/src/dtos/setting/update-settings.input.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { IsNotEmpty } from 'class-validator';
import { Field, InputType } from '@nestjs/graphql';

@InputType()
export class UpdateSettingsInput {
@IsNotEmpty()
@Field(() => String)
id!: string;
settings!: {
id: string;
value: string;
}[];

@IsNotEmpty()
@Field(() => String)
value!: string;
// id!: string;
//
// @IsNotEmpty()
// value!: string;
}
8 changes: 2 additions & 6 deletions packages/_leaa-common/src/entrys/_base.entity.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
import { PrimaryGeneratedColumn, Column } from 'typeorm';
import { ObjectType, Field, Int } from '@nestjs/graphql';
import { Exclude } from 'class-transformer';

@ObjectType()
export class Base {
@PrimaryGeneratedColumn('uuid')
@Field(() => String)
id!: string;

//

@Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
@Field(() => Date)
created_at!: Date;

@Column({ type: 'timestamp', nullable: true, onUpdate: 'CURRENT_TIMESTAMP' })
@Field(() => Date, { nullable: true })
updated_at?: Date;

// SOFT DELETE
// https://github.com/typeorm/typeorm/issues/534
@Exclude({ toPlainOnly: true })
@Column({ type: 'timestamp', nullable: true })
@Field(() => Date, { nullable: true })
deleted_at?: Date;
}
15 changes: 3 additions & 12 deletions packages/_leaa-common/src/entrys/category.entity.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,31 @@
import { ObjectType, Field } from '@nestjs/graphql';

import { Index, Entity, Tree, Column, TreeChildren, TreeParent } from 'typeorm';

import { Base } from '@leaa/common/src/entrys';

@Entity('categories')
@Index('categories_name_unique', ['name'], { unique: true })
@Index('categories_slug_unique', ['slug'], { unique: true })
@ObjectType()
@Tree('nested-set')
export class Category extends Base {
@Column({ type: 'varchar', length: 32, unique: true })
@Field(() => String, { nullable: true })
@Column({ type: 'varchar', length: 64, unique: true })
name!: string | null;

@Column({ type: 'varchar', length: 32, unique: true })
@Field(() => String)
// @Index()
@Column({ type: 'varchar', length: 64, unique: true })
slug!: string;

@Column({ type: 'varchar', nullable: true })
@Field(() => String, { nullable: true })
parent_id!: string | null;

@Column({ type: 'text', nullable: true })
@Field(() => String, { nullable: true })
description?: string | null;

@TreeChildren()
@Field(() => [Category], { nullable: true })
children?: Category[] | null;

@TreeParent()
@Field(() => Category, { nullable: true })
parent?: Category | null;

// Virtual Field (not in DB)
// @Field(() => String, { nullable: true })
// key?: string | null;
}
11 changes: 0 additions & 11 deletions packages/_leaa-common/src/entrys/setting.entity.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,31 @@
import { Index, Entity, Column } from 'typeorm';
import { ObjectType, Field, Int } from '@nestjs/graphql';

import { Base } from '@leaa/common/src/entrys';

@Entity('settings')
@Index('settings_name_unique', ['name'], { unique: true })
@Index('settings_slug_unique', ['slug'], { unique: true })
@ObjectType()
export class Setting extends Base {
@Column({ type: 'varchar', length: 32, unique: true })
@Field(() => String)
name!: string;

@Column({ type: 'varchar', length: 32, unique: true })
@Field(() => String, { nullable: true })
slug?: string;

@Column({ type: 'varchar', length: 32 })
@Field(() => String, { nullable: true })
type!: string;

@Column({ type: 'text', nullable: true })
@Field(() => String, { nullable: true })
description?: string;

@Column({ type: 'text', nullable: true })
@Field(() => String, { nullable: true })
options?: string;

@Column({ type: 'text', nullable: true })
@Field(() => String, { nullable: true })
value?: string;

@Column({ type: 'int', default: 0 })
@Field(() => Int)
sort!: number;

@Column({ type: 'int', default: 0 })
@Field(() => Int)
private!: number;
}
4 changes: 0 additions & 4 deletions packages/_leaa-common/src/entrys/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,8 @@ export class User extends Base {

// Virtual Field (not in DB)
permissions?: Permission[];

flatPermissions?: string[];

authToken?: string;

authExpiresIn?: number;

avatar?: Attachment;
}
4 changes: 2 additions & 2 deletions packages/leaa-api/src/modules/category/category.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ export class CategoryController implements CrudController<Category> {
@Override('createOneBase')
@UseGuards(JwtGuard, PermissionsGuard)
@Permissions('category.item-create')
createOne(@ParsedRequest() req: CrudRequest, @ParsedBody() dto: Category & CreateCategoryInput): Promise<Category> {
createOne(@ParsedRequest() req: CrudRequest, @ParsedBody() dto: Category | CreateCategoryInput): Promise<Category> {
return this.service.createOne(req, dto);
}

@Override('updateOneBase')
@UseGuards(JwtGuard, PermissionsGuard)
@Permissions('category.item-update')
updateOne(@ParsedRequest() req: CrudRequest, @ParsedBody() dto: Category & UpdateCategoryInput): Promise<Category> {
updateOne(@ParsedRequest() req: CrudRequest, @ParsedBody() dto: Category | UpdateCategoryInput): Promise<Category> {
return this.service.updateOne(req, dto);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/leaa-api/src/modules/category/category.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class CategoryService extends TypeOrmCrudService<Category> {
super(categoryRepo);
}

async createOne(req: CrudRequest, dto: Category & CreateCategoryInput): Promise<Category> {
async createOne(req: CrudRequest, dto: Category | CreateCategoryInput): Promise<Category> {
const nextDto = {
...dto,
parent: await this.formatParentId(dto.parent_id),
Expand All @@ -25,7 +25,7 @@ export class CategoryService extends TypeOrmCrudService<Category> {
return super.createOne(req, nextDto);
}

async updateOne(req: CrudRequest, dto: Category & UpdateCategoryInput): Promise<Category> {
async updateOne(req: CrudRequest, dto: Category | UpdateCategoryInput): Promise<Category> {
const nextDto = {
...dto,
parent: await this.formatParentId(dto.parent_id),
Expand Down
4 changes: 3 additions & 1 deletion packages/leaa-api/src/modules/seed/seed.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ import { LoggerService } from '@leaa/api/src/modules/logger/logger.service';
//
// for Test
if (process.argv.includes('--test')) {
await insertAllAboutAuth();
// clear
// await getRepository('Category').clear();
await seedService.insertCategory();
}
})();
8 changes: 4 additions & 4 deletions packages/leaa-api/src/modules/seed/seed.data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,14 +261,14 @@ export const rolesToUserSeed = [
];

interface ICreateCategoryInput extends CreateCategoryInput {
seedParentSlug: string;
seedParentSlug?: string;
}

// prettier-ignore
export const categorySeed: ICreateCategoryInput[] = [
{ name: 'Articles', description: '文章分类', slug: 'articles', seedParentSlug: '' },
{ name: 'Products', description: '商品分类', slug: 'products', seedParentSlug: '' },
{ name: 'Brands', description: '品牌分类', slug: 'brands', seedParentSlug: '' },
{ name: 'Articles', description: '文章分类', slug: 'articles' },
{ name: 'Products', description: '商品分类', slug: 'products' },
{ name: 'Brands', description: '品牌分类', slug: 'brands' },
//
{ name: 'Frontend', slug: 'frontend', seedParentSlug: 'articles' },
{ name: 'Backend', slug: 'backend', seedParentSlug: 'articles' },
Expand Down
23 changes: 13 additions & 10 deletions packages/leaa-api/src/modules/seed/seed.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable no-restricted-syntax */
/* eslint-disable no-await-in-loop */

import { Repository } from 'typeorm';
import { Injectable } from '@nestjs/common';
import { PermissionService } from '@leaa/api/src/modules/permission/permission.service';
Expand Down Expand Up @@ -68,15 +67,15 @@ export class SeedService {

async insertUsers() {
for (const i of usersSeed) {
const item = await this.userService.createOne(req, i);
const item = await this.userService.createOne(req, i as any);

console.log(item);
}
}

async insertRandomUsers() {
for (const i of randomSersSeed) {
await this.userService.createOne(req, i);
await this.userService.createOne(req, i as any);
}
}

Expand Down Expand Up @@ -114,17 +113,21 @@ export class SeedService {

async insertCategory() {
for (const i of categorySeed) {
let parentId = '';
let parent = null;
// eslint-disable-next-line @typescript-eslint/camelcase
let parent_id = null;

if (i.seedParentSlug) {
const category = await this.categoryService.getOneBySlug(i.seedParentSlug);
parentId = category?.id || '';
parent = await this.categoryService.getOneBySlug(i?.seedParentSlug);
// eslint-disable-next-line @typescript-eslint/camelcase
parent_id = parent?.id;
await console.log('insertCategory >>>>>>>>>>>>>>', i.seedParentSlug, parent);
}

// const parent_id = await this.categoryService.createCategory(i);
const item = await this.categoryService.createOne(req, {
...i,
parent_id: parentId,
parent,
parent_id,
});

console.log(item);
Expand All @@ -133,7 +136,7 @@ export class SeedService {

async insertArticle() {
for (const i of articleSeed) {
const item = await this.articleService.createOne(req, i);
const item = await this.articleService.createOne(req, i as any);

console.log(item);
}
Expand All @@ -157,7 +160,7 @@ export class SeedService {

async insertSetting() {
for (const i of settingSeed) {
const item = await this.settingService.createSetting(i);
const item = await this.settingService.createOne(req, i as any);

console.log(item);
}
Expand Down
63 changes: 63 additions & 0 deletions packages/leaa-api/src/modules/setting/setting.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Controller, UseGuards, Get, Put, Post, Body } from '@nestjs/common';
import { Crud, CrudController, Override, ParsedRequest, ParsedBody, CrudRequest } from '@nestjsx/crud';

import { Permissions } from '@leaa/api/src/decorators';
import { CreateSettingInput, UpdateSettingInput, UpdateSettingsInput } from '@leaa/common/src/dtos/setting';
import { JwtGuard, PermissionsGuard } from '@leaa/api/src/guards';
import { Setting } from '@leaa/common/src/entrys';

import { SettingService } from './setting.service';

@Crud({
model: { type: Setting },
params: {
id: {
field: 'id',
type: 'uuid',
primary: true,
},
},
query: {
maxLimit: 1000,
alwaysPaginate: true,
sort: [{ field: 'sort', order: 'ASC' }],
// join: {
// tags: { eager: true },
// categories: { eager: true },
// },
},
routes: {
// getManyBase: { decorators: [Permissions('setting.list-read')] },
// getOneBase: { decorators: [Permissions('setting.item-read')] },
createOneBase: { decorators: [UseGuards(JwtGuard, PermissionsGuard), Permissions('setting.item-create')] },
updateOneBase: { decorators: [UseGuards(JwtGuard, PermissionsGuard), Permissions('setting.item-update')] },
deleteOneBase: {
decorators: [UseGuards(JwtGuard, PermissionsGuard), Permissions('setting.item-delete')],
returnDeleted: true,
},
},
dto: {
create: CreateSettingInput,
update: UpdateSettingInput,
},
})
@Controller('/settings')
export class SettingController implements CrudController<Setting> {
constructor(public service: SettingService) {}

@Override('updateOneBase')
@UseGuards(JwtGuard, PermissionsGuard)
@Permissions('setting.item-update')
updateOne(@ParsedRequest() req: CrudRequest, @ParsedBody() dto: UpdateSettingInput): Promise<Setting> {
return this.service.updateOne(req, dto);
}

//

@Post('batch')
@UseGuards(JwtGuard, PermissionsGuard)
@Permissions('setting.item-update')
batchUpdate(@Body() dto: UpdateSettingsInput): Promise<Setting[]> {
return this.service.batchUpdate(dto);
}
}

0 comments on commit 800f107

Please sign in to comment.