5.2 用户的增删改查

一、重点

通过管理员的增删改查来实现与角色表的关联;

下边只展示重点,schema,service等完整吗请看下方

二、前后台联调,管理员表的增删改查

1、增加管理员

1.1 admin.controller

@Get('add')
@Render('admin/manager/add')
async add() {
    let result = await this.roleService.find({});
    return { roleResult: result }
}

@Post('doAdd')
async doAdd(@Body() body, @Response() res) {
    console.log('body: ', body);
    if(body.username == '' || body.password.length<2) {
        this.toolsService.error(res, '用户名或密码长度不合法', `/${Config.adminPath}/manager/add`)
    }else {
        // 从数据库查询当前用户名是否存在
        let adminResult = await this.adminService.find({'username': body.username});
        if(adminResult.length > 0) {
            this.toolsService.error(res, '此管理员已存在', `/${Config.adminPath}/manager/add`)
        }else {
            body.password = this.toolsService.getMd5(body.password);
            let result = await this.adminService.add(body);
            console.log('result: ', result);
            if(result) {
                this.toolsService.success(res, `/${Config.adminPath}/manager`)
            }else {
                this.toolsService.error(res, '增加失败', `/${Config.adminPath}/manager/add`)
            }
        }
    }
}

1.2 前端页面

<form action="/<%=config.adminPath%>/manager/doAdd" method="post">
    <ul>
        <li>管理员名称: <input type="text" name="username"/></li>
        <li>管理员密码: <input type="text" name="password"/></li>
        <li>管理员电话: <input type="text" name="mobile"/></li>
        <li>管理员邮箱: <input type="text" name="email"/></li>
        <li>管理员角色:
            <select name="role_id" id="role_id">
                <%for(var i=0; i<roleResult.length; i++) {%>
                    <option value="<%=roleResult[i]._id%>"><%=roleResult[i].title%></option>
                <%}%>
            </select>
        </li>
        <li><button type='submit' class="btn btn-default">提交</button></li>
    </ul>
</form>

2、展示管理员列表(表关联)

2.1 controller

		@Get()
    @Render('admin/manager/index')
    async index() {
        //获取admin表以及role表关联数据
        var result = await this.adminService.getModel().aggregate([
            {
                $lookup: { 
                    from: "role", 
                    localField: "role_id", 
                    foreignField: "_id", 
                    as: "role" 
                }
            }
        ]);
        console.log('result: ', JSON.stringify(result));
        return {
            adminResult: result
        };
    }

2.2 前端页面

<table class="table table-bordered">
    <thead>
        <tr class="th">
            <th>管理员名称</th>
            <th>管理员电话</th>
            <th>管理员邮箱</th>
            <th>管理员角色</th>
            <th class="text-center">操作</th>
        </tr>
    </thead>
    <tbody>
        <%for(var i=0;i<adminResult.length;i++){%>
        <tr>
            <td><%=adminResult[i].username%></td>
            <td><%=adminResult[i].mobile%></td>
            <td><%=adminResult[i].email%></td>
            <!-- adminResult[i].role[0].title -->
            <td><%=adminResult[i].role[0]%></td>
            <td class="text-center">
                <a href="/<%=config.adminPath%>/manager/edit?id=<%=adminResult[i]._id%>">修改</a>  
                <a class="" href="/<%=config.adminPath%>/manager/delete?id=<%=adminResult[i]._id%>">删除</a>
            </td>
        </tr>
        <%}%>
    </tbody>
</table>

2.3 重点注意事项(坑)

若需要用户表和角色表关联,则需要定schema时这样写,否则关联不上

role_id: { type: mongoose.Types.ObjectId },

3、管理员的修改

3.1 controller

		@Get('edit')
    @Render('admin/manager/edit')
    async edit(@Query() query) {
        let roleResult = await this.roleService.find();
        let adminResult = await this.adminService.find({_id: query.id})
        return { 
            roleResult: roleResult,
            adminResult: adminResult[0] 
        }
    }

    @Post('doEdit')
    async doEdit(@Body() body, @Response() res) {
        let id = body._id;
        let password = body.password;
        let mobile = body.mobile;
        let email = body.email;
        let role_id = body.role_id;
        let params:any = {
            mobile, email, role_id
        }
        // 密码不为空时更新密码
        if(password != "") {
            if(password.length <= 2) {
                this.toolsService.error(res, '密码长度不合法', `/${Config.adminPath}/manager/edit?id=${body._id}`)
            }
            params.password = this.toolsService.getMd5(password);  
        }
        let result = await this.adminService.update({'_id': id}, params)
        if(result) {
            this.toolsService.success(res, `/${Config.adminPath}/manager`)
        }
    }

3.2 前端页面

重点:角色的选中

<form action="/<%=config.adminPath%>/manager/doEdit" method="post">
    <ul>
        <input type="hidden" name="_id" value="<%=adminResult._id%>"/>
        <li>管理员名称: <input readonly type="text" name="username" value="<%=adminResult.username%>"/></li>
        <li>管理员密码: <input type="text" name="password"/></li>
        <li>管理员电话: <input type="text" name="mobile" value="<%=adminResult.mobile%>"/></li>
        <li>管理员邮箱: <input type="text" name="email" value="<%=adminResult.email%>"/></li>
        <li>管理员角色:
            <select name="role_id" id="role_id">
                <%for(var i=0; i<roleResult.length; i++) {%>
                    <%if(roleResult[i]._id.toString() == adminResult.role_id){%>
                        <option selected value="<%=roleResult[i]._id%>"><%=roleResult[i].title%></option>
                    <%}else {%>
                        <option value="<%=roleResult[i]._id%>"><%=roleResult[i].title%></option>
                    <%}%>
                <%}%>
            </select>
        </li>
        <li><button type='submit' class="btn btn-default">提交</button></li>
    </ul>
</form>

4、删除管理员

4.1 controller

		@Get('delete')
    async delete(@Query() query, @Response() res ) {
        let result = this.adminService.delete({"_id": query.id});
        this.toolsService.success(res, `/${Config.adminPath}/manager`)
    }

4.2 前端页面

<a class='delete' href="/<%=config.adminPath%>/manager/delete?id=<%=adminResult[i]._id%>">删除</a>

三、完整版

1.完整版的module

import { Module } from '@nestjs/common';
import { MainController } from './main/main.controller';
import { LoginController } from './login/login.controller';
import { ManagerController } from './manager/manager.controller';
import { ToolsService } from '../../service/tools/tools.service';
// 配置schema1
import { MongooseModule } from '@nestjs/mongoose';
import { AdminSchema } from '../../schema/admin.schema'
import { RoleSchema } from '../../schema/role.schema'

// 引入服务
import { AdminService } from '../../service/admin/admin.service'
import { RoleService } from '../../service/role/role.service'
import { RoleController } from './role/role.controller';

@Module({
  imports: [
    MongooseModule.forFeature([
      {
        name: "Admin",
        schema: AdminSchema,
        collection: 'admin'
      },
      {
        name: "Role",
        schema: RoleSchema,
        collection: 'role'
      }
      
    ])
  ],
  controllers: [MainController, LoginController, ManagerController, RoleController],
  providers: [ToolsService, AdminService, RoleService]
})
export class AdminModule { }

2. 完整版的schema

@filename(admin.schema)

import * as mongoose from 'mongoose';
const Schema = mongoose.Schema;
const d = new Date();

export const AdminSchema = new mongoose.Schema({
    username: { type: String },
    password: { type: String },
    mobile: { type: String },
    email: { type: String },
    status: { type: String },
    role_id: { type: mongoose.Types.ObjectId },
    add_time: { 
        type: Number,
        default: d.getTime() 
    },
    is_super: { type: Number },
})

3.完整版的service

import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { AdminInterface } from '../../interface/admin.interface'

@Injectable()
export class AdminService {
    constructor(@InjectModel('Admin') private adminModel) { }

    async find(json: AdminInterface = {}) {
        let result = await this.adminModel.find(json);
        return result;
    }

    async add(json: AdminInterface) {
        let role = new this.adminModel(json);
        let result = await role.save();
        return result;
    }

    async update(json1: AdminInterface, json2: AdminInterface) {
        let result = await this.adminModel.updateOne(json1, json2);
        return result;
    }

    async delete(json: AdminInterface) {
        let result = await this.adminModel.deleteOne(json);
        return result;
    }

    getModel() {
        return this.adminModel
    }
} 

4.完整版的interface

@filename(admin.interface)

export interface AdminInterface {
    _id?: String;
    username?: String;
    password?: String;
    mobile?: String;
    email?: String;
    status?: String;
    role_id?: String;
    add_time?: Number;
    is_super?: Number;
}

5.完整版的controller

import { Controller, Get, Render, Post, Body, Response } from '@nestjs/common';
import { Config } from '../../../config/config';
import { AdminService } from '../../../service/admin/admin.service'
import { RoleService } from '../../../service/role/role.service';
import { ToolsService } from '../../../service/tools/tools.service';

@Controller(`${Config.adminPath}/manager`)
export class ManagerController {
    constructor(
        private adminService: AdminService,
        private roleService: RoleService,
        private toolsService: ToolsService
    ) { }

    @Get()
    @Render('admin/manager/index')
    async index() {
        return { }
    }

    @Get('add')
    @Render('admin/manager/add')
    async add() {
        let result = await this.roleService.find({});
        return { roleResult: result }
    }

    @Post('doAdd')
    async doAdd(@Body() body, @Response() res) {
        console.log('body: ', body);
        if(body.username == '' || body.password.length<2) {
            this.toolsService.error(res, '用户名或密码长度不合法', `/${Config.adminPath}/manager/add`)
        }else {
            // 从数据库查询当前用户名是否存在
            let adminResult = await this.adminService.find({'username': body.username});
            if(adminResult.length > 0) {
                this.toolsService.error(res, '此管理员已存在', `/${Config.adminPath}/manager/add`)
            }else {
                body.password = this.toolsService.getMd5(body.password);
                let result = await this.adminService.add(body);
                console.log('result: ', result);
                if(result) {
                    this.toolsService.success(res, `/${Config.adminPath}/manager`)
                }else {
                    this.toolsService.error(res, '增加失败', `/${Config.adminPath}/manager/add`)
                }
            }
        }
    }

    @Get('edit')
    @Render('admin/manager/edit')
    edit() {
        return {}
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值