EvaluationSystem:后端业务接口(js同步操作数据库)
1、用户业务接口(services/user.js)
用户相关业务:
- 注册账号
- 登录账号
- 查看用户信息
- 修改个人资料
点击查看代码
// 用户相关业务
const sequelize = require('../shared/sequelize');
const User = sequelize.import('../models/user');
// 注册账号
exports.register = async function (useraccount, password) {
const user = await User.findOne({
where: {useraccount},
});
if (user !== null) {
throw new Error("账号已存在");
}
nickname = 'defaultname';
return User.create({
useraccount,
password,
nickname
});
};
// 登录
exports.login = async function login(useraccount, password) {
const user = await User.findOne({
where: {useraccount}
});
if (user === null || !user.checkPassword(password)) {
throw new Error('账号或密码错误');
}
return user;
};
// 查看用户信息
exports.show = function (useraccount) {
return User.findByPk(useraccount, {
attributes: ['useraccount', 'nickname', 'evalnum']
});
};
// 修改个人资料
exports.changeProfile = function (useraccount, nickname, password) {
return User.update({nickname, password: password || ''}, {where: {useraccount: useraccount}, individualHooks: true});
};
2、数据业务接口(services/data.js)
- 添加一条数据
- 查询一条数据
- 所有数据列表
点击查看代码
const sequelize = require('../shared/sequelize');
const data = sequelize.import('../models/data');
// 批量添加数据(管理员功能)
// 参数 obj = [{数据1名字,dbname, datapath},{数据2,...},...]
exports.publish = async function (obj){
try{
const res = await data.bulkCreate(obj, {
individualHooks: true, // 对每个数据分别执行一遍生命周期函数
});
console.log('插入成功', res.map(res => { res.toJSON() }));
}
catch (e) {
console.log('插入失败', e);
}
}
// 查询一条数据
exports.show = async function (dataname) {
return data.findByPk(dataname);
};
// 所有数据列表
exports.list = async function (page = 1, size = 10) {
return data.findAndCountAll({
limit: size,
offset: (page - 1) * size,
order: [['evalnum', 'DESC']],
});
};
3、测评业务接口(services/ceping.js)
- 添加或更新一条测评数据
- 假删除测试(权限判定)
- 查看指定数据名称的测评列表
- 查看指定用户的测评结果
- 查看指定用户和指定数据名称的测评结果
点击查看代码
// 数据测评
const allocation = require('../alloction');
const sequelize = require('../shared/sequelize');
const dataService = require('./data');
const ceping = sequelize.import('../models/ceping');
const User = sequelize.import('../models/user');
const evalOpt = allocation.evalOpt;
// 测评一条数据
exports.publish = async function (dataname, useraccount, comment, index) {
const targetdata = await dataService.show(dataname);
if (targetdata === null) {
console.log(dataname);
throw new Error('数据不存在');
}
const targetuser = await User.findByPk(useraccount);
if(targetuser === null) {
console.log(useraccount);
throw new Error('用户不存在');
}
const obj = {};
for(key in evalOpt){
obj[evalOpt[key]] = index[key];
}
obj['comment'] = comment;
const targetcepingid = await ceping.findOne({
attributes: ['id'], // 指定返回字段
where:{
dataname,
useraccount
}
});
if(targetcepingid === null){ // 添加测评结果
obj['useraccount'] = useraccount;
obj['dataname'] = dataname;
return ceping.create(obj);
}
else{//更新测评结果
return ceping.update(obj,{
where:{
dataname,
useraccount
}
});
}
};
// 删除一条测评(假删除)
exports.destroy = async function (cepingId, useraccount) {
const target = await ceping.findByPk(cepingId);
if (target === null || target.useraccount !== useraccount) {
throw new Error('你无权删除该评论');
}
return target.destroy();
};
// 查看指定数据测评列表
exports.listByData = async function(dataname, page, size) {
return ceping.findAndCountAll({
where: {dataname},
offset: (page-1) * size,
limit: size,
order: [['id','DESC']]
})
};
// 查看 指定用户 的 测评列表
exports.listByUser = async function(useraccount, page, size) {
return ceping.findAndCountAll({
where: {useraccount},
offset: (page-1) * size,
limit: size,
order: [['id','DESC']]
})
};
// 查看 指定用户 和 指定数据 的测评结果
exports.listByDataAndUser = async function(dataname, useraccount) {
return ceping.findOne({
where:{
dataname,
useraccount
}
})
}
4、接口测试文件(services/test.js)
对上述三种接口进行测试。
点击查看代码
// service-api 测试文件
const dataService = require('./data');
const UserService = require('./user');
const cepingService = require('./ceping');
// 用户接口测试
async function usertest (d) {
switch(d){
case 1:{
console.log('注册测试');
const result1 = await UserService.register('zzc12345', '12345678');
console.log(result1);
}
case 2:{
console.log('登录测试');
const result2 = await UserService.login('zzc12345', '12345678');
console.log(result2);
const result3 = await UserService.login('zzc12345', '12345678');
console.log(result3);
}
case 3:{
console.log('查看测试');
const result4 = await UserService.show('zzc12345');
console.log(result4);
}
break;
}
}
// 数据接口测试
async function datatest (d,obj) {
switch(d) {
case 1:{
console.log('添加数据测试');
const result1 = await dataService.publish(obj);
console.log(result1);
}
break;
case 2:{
console.log('查询一条数据');
const result2 = await dataService.show('audio_1');
console.log(result2);
}
break;
case 3:{
console.log('所有数据')
const result3 = await dataService.list();
console.log(result3);
}
break;
}
}
// 测评接口测试
async function cepingtest (d) {
const index = [1,2,3];
switch(d) {
case 1:{
console.log('添加或更新测试');
const result1 = await cepingService.publish('audio_1','zzc12345',index);
console.log(result1);
}
break;
case 2:{
console.log('假删除测试');
const result2 = await cepingService.destroy(1, 'zzc12345');
console.log(result2);
}
break;
case 3:{
console.log('查看 指定数据 测评列表 测试');
const result3 = await cepingService.listByData('audio_1', 1, 10);
console.log(result3);
}
break;
case 4:{
console.log('查看 指定用户 测评列表 测试');
const result4 = await cepingService.listByUser('zzc12345', 1, 10);
console.log(result4);
}
break;
case 5: {
console.log('查看 指定测评 查询');
const result5 = await cepingService.listByDataAndUser('audio_1','zzc12345');
console.log(result5);
}
break;
}
}
obj = [{
dataname: '3.wav',
dbname: 'sounds',
datapath: 'D:\\projects\\vue\\sounds\\3.wav'
},
{
dataname: '1.wav',
dbname: 'sounds',
datapath: 'D:\\projects\\vue\\sounds\\1.wav'
},
{
dataname: '4.wav',
dbname: 'sounds',
datapath: 'D:\\projects\\vue\\sounds\\4.wav'
},
{
dataname: '2.wav',
dbname: 'sounds',
datapath: 'D:\\projects\\vue\\sounds\\2.wav'
}];
datatest(1,obj);