wuyi 2 жил өмнө
parent
commit
7f3e2f52f1

+ 2 - 0
src/app.module.ts

@@ -16,6 +16,7 @@ import { WeixinModule } from './weixin/weixin.module'
 import { NotifyModule } from './notify/notify.module'
 import { CommissionModule } from './commission/commission.module'
 import { UserBalanceModule } from './user-balance/user-balance.module'
+import { MaskModule } from './mask/mask.module'
 import { WithdrawModule } from './withdraw/withdraw.module'
 import { SysConfigModule } from './sys-config/sys-config.module'
 import { AllExceptionsFilter } from './filters/all-exceptions-filter.filter'
@@ -74,6 +75,7 @@ import { AllExceptionsFilter } from './filters/all-exceptions-filter.filter'
         NotifyModule,
         CommissionModule,
         UserBalanceModule,
+        MaskModule,
         WithdrawModule,
         SysConfigModule
     ],

+ 9 - 0
src/mask/dto/mask.dto.ts

@@ -0,0 +1,9 @@
+import { IsString } from 'class-validator'
+
+export class MaskDto {
+    @IsString()
+    name: string
+
+    @IsString()
+    describe: string
+}

+ 16 - 0
src/mask/entities/mask.entity.ts

@@ -0,0 +1,16 @@
+import { Column, CreateDateColumn, Entity, PrimaryGeneratedColumn } from 'typeorm'
+
+@Entity()
+export class Mask {
+    @PrimaryGeneratedColumn()
+    id: number
+
+    @Column()
+    name: string
+
+    @Column()
+    describe: string
+
+    @CreateDateColumn()
+    createdAt: Date
+}

+ 75 - 0
src/mask/mask.admin.controller.ts

@@ -0,0 +1,75 @@
+import {
+    Controller,
+    Put,
+    Get,
+    Body,
+    Param,
+    HttpStatus,
+    NotFoundException,
+    Delete,
+    BadRequestException,
+    Req,
+    Post
+} from '@nestjs/common'
+import { HasRoles } from '../auth/roles.decorator'
+import { Role } from '../model/role.enum'
+import { ApiBearerAuth, ApiTags } from '@nestjs/swagger'
+import { PageRequest } from '../common/dto/page-request'
+import { Mask } from './entities/mask.entity'
+import { MaskDto } from './dto/mask.dto'
+import { MaskService } from './mask.service'
+
+@ApiTags('mask.admin')
+@Controller('/admin/mask')
+@ApiBearerAuth()
+export class MaskAdminController {
+    constructor(private readonly maskService: MaskService) {}
+
+
+    @Post()
+    public async list(@Body() page: PageRequest<Mask>) {
+        return await this.maskService.findAll(page)
+    }
+
+    @Put()
+    @HasRoles(Role.Admin)
+    public async create(@Body() mask: MaskDto) {
+        return await this.maskService.create(mask)
+    }
+
+    @Get('/get/:maskId')
+    public async get(@Param('maskId') maskId: string) {
+        const mask = await this.maskService.findById(Number(maskId))
+        return mask
+    }
+
+    @Put('/:maskId')
+    @HasRoles(Role.Admin)
+    public async updateMask(@Param('maskId') maskId: string, @Body() maskDto: MaskDto) {
+        try {
+            await this.maskService.updateMask(Number(maskId), maskDto)
+
+            return {
+                message: 'Mask Updated successfully!',
+                status: HttpStatus.OK
+            }
+        } catch (err) {
+            throw new BadRequestException(err, 'Error: Mask not updated!')
+        }
+    }
+
+    @Delete('/:maskId')
+    @HasRoles(Role.Admin)
+    public async deleteMask(@Param('maskId') maskId: number) {
+        try {
+            await this.maskService.deleteMask(maskId)
+            return {
+                message: 'Mask Deleted successfully!',
+                status: HttpStatus.OK
+            }
+        } catch (err) {
+            throw new BadRequestException(err, 'Error: Mask not deleted!')
+        }
+    }
+
+}

+ 23 - 0
src/mask/mask.module.ts

@@ -0,0 +1,23 @@
+import { Module } from '@nestjs/common'
+import { TypeOrmModule } from '@nestjs/typeorm'
+import { Mask } from './entities/mask.entity'
+import { MaskService } from './mask.service'
+import { MaskAdminController } from './mask.admin.controller'
+import { BcryptService } from '../shared/hashing/bcrypt.service'
+import { HashingService } from '../shared/hashing/hashing.service'
+import { SmsModule } from '../sms/sms.module'
+import { MembershipModule } from 'src/membership/membership.module'
+
+@Module({
+    imports: [SmsModule, TypeOrmModule.forFeature([Mask]), MembershipModule],
+    controllers: [MaskAdminController],
+    providers: [
+        {
+            provide: HashingService,
+            useClass: BcryptService
+        },
+        MaskService
+    ],
+    exports: [MaskService]
+})
+export class MaskModule {}

+ 71 - 0
src/mask/mask.service.ts

@@ -0,0 +1,71 @@
+import {
+    Injectable,
+    NotFoundException,
+    HttpException,
+    HttpStatus,
+    BadRequestException,
+    InternalServerErrorException,
+    UnauthorizedException
+} from '@nestjs/common'
+import { Repository, UpdateResult } from 'typeorm'
+import { InjectRepository } from '@nestjs/typeorm'
+import { Mask } from './entities/mask.entity'
+import { MaskDto } from './dto/mask.dto'
+import { HashingService } from '../shared/hashing/hashing.service'
+import { SmsService } from '../sms/sms.service'
+import * as randomstring from 'randomstring'
+import { MembershipService } from '../membership/membership.service'
+import { paginate, Pagination } from 'nestjs-typeorm-paginate'
+import { Role } from 'src/model/role.enum'
+import { PageRequest } from '../common/dto/page-request'
+
+@Injectable()
+export class MaskService {
+    constructor(
+        @InjectRepository(Mask)
+        private readonly maskRepository: Repository<Mask>,
+        private readonly hashingService: HashingService,
+        private readonly smsService: SmsService
+    ) {}
+
+    async findAll(req: PageRequest<Mask>): Promise<Pagination<Mask>> {
+        return await paginate<Mask>(this.maskRepository, req.page, req.search)
+    }
+
+    public async create(maskDto: MaskDto) {
+        return await this.maskRepository.save(maskDto)
+    }
+
+    public async findById(maskId: number): Promise<Mask> {
+        const mask = await this.maskRepository.findOneBy({
+            id: +maskId
+        })
+
+        if (!mask) {
+            throw new NotFoundException(`Mask #${maskId} not found`)
+        }
+
+        return mask
+    }
+
+    public async updateMask(id: number, maskDto: MaskDto): Promise<UpdateResult> {
+        try {
+            const mask = await this.maskRepository.update(
+                {
+                    id: +id
+                },
+                { ...maskDto }
+            )
+
+            return mask
+        } catch (err) {
+            throw new BadRequestException('Mask not updated')
+        }
+    }
+
+    public async deleteMask(id: number): Promise<void> {
+        const mask = await this.findById(id)
+        await this.maskRepository.remove(mask)
+    }
+
+}