Commit 6346bb2c authored by wuhao's avatar wuhao 🎯

asder

parent 9c090f1e
......@@ -41,6 +41,7 @@ class ItemController extends Controller {
return;
}
const item = await ctx.model.Item.create(ctx.request.body);
// const tags = await ctx.model.service.tags
this.success(item);
}
......
'use strict';
const Controller = require('../core/base_controller');
function toInt(str) {
if (typeof str === 'number') return str;
if (!str) return str;
return parseInt(str, 10) || 0;
}
class TagController extends Controller {
async mutitag() {
const ctx = this.ctx;
const updates = await ctx.service.tag.mutiupdate(
ctx.request.body?.tags ?? []
);
this.success(updates);
}
async index() {
const ctx = this.ctx;
const query = {
project_id: ctx.query.project_id,
};
const result = await ctx.model.Tag.findAll({
where: query
});
this.success(result);
}
async show() {
const ctx = this.ctx;
const result = await ctx.model.Tag.findByPk(toInt(ctx.params.id));
this.success(result);
}
async create() {
const ctx = this.ctx;
console.log(ctx.request.body);
if (!ctx.request.body.tag_name || !ctx.request.body.project_id) {
this.fail('请完善必填项!');
return;
}
const tag = await ctx.model.Tag.create(ctx.request.body);
this.success(tag);
}
async update() {
const ctx = this.ctx;
const id = toInt(ctx.params.id);
const tag = await ctx.model.Tag.findByPk(id);
if (!tag) {
ctx.status = 404;
ctx.body = {
code: 1,
success: false,
msg: '库里没有该条数据',
};
return;
}
await tag.update(ctx.request.body);
this.success(tag);
}
async destroy() {
const ctx = this.ctx;
const id = toInt(ctx.params.id);
const tag = await ctx.model.Tag.findByPk(id);
if (!tag) {
ctx.status = 404;
ctx.body = {
code: 1,
success: false,
msg: '库里没有该条数据',
};
return;
}
await tag.destroy();
this.success({
data: '已删除',
});
}
}
module.exports = TagController;
......@@ -2,7 +2,7 @@
'use strict';
module.exports = app => {
const { STRING, INTEGER, ENUM, DATE, TEXT } = app.Sequelize;
const { STRING, INTEGER, ENUM, DATE, TEXT, JSON } = app.Sequelize;
const Item = app.model.define(
'item',
......@@ -51,6 +51,12 @@ module.exports = app => {
comment: '冗余字段',
unique: true,
},
tags: {
type: JSON,
allowNull: true,
comment: '标签',
unique: true,
},
sort: {
type: INTEGER,
allowNull: true,
......
/* eslint-disable no-unused-vars */
'use strict';
module.exports = app => {
const { STRING, INTEGER, ENUM, DATE, TEXT } = app.Sequelize;
const Tag = app.model.define(
'tag',
{
id: { type: INTEGER, primaryKey: true, autoIncrement: true },
project_id: {
type: INTEGER,
allowNull: true,
comment: '所属项目',
unique: true,
},
mission_id: {
type: INTEGER,
allowNull: true,
comment: '所属任务',
unique: true,
},
tag_name: {
type: STRING(255),
allowNull: true,
comment: '标签名称',
unique: true,
},
other: {
type: STRING(255),
allowNull: true,
comment: '冗余字段',
unique: true,
},
color: {
type: STRING(255),
allowNull: true,
comment: '颜色',
unique: true,
},
created_at: DATE,
updated_at: DATE,
},
{
timestamps: true,
createdAt: 'created_at',
updatedAt: 'updated_at',
}
);
return Tag;
};
// freezeTableName: true, 使用自定义表名
// // 实例对应的表名
// tableName: 'user',
// // 如果需要sequelize帮你维护createdAt,updatedAt和deletedAt必须先启用timestamps功能
// // 将createdAt对应到数据库的created_at字段
// createdAt: 'created_at',
// // 将updatedAt对应到数据库的updated_at字段
// updatedAt: 'updated_at',
// //And deletedAt to be called destroyTime (remember to enable paranoid for this to work)
// deletedAt: false, //'deleted_at',
// //删除数据时不删除数据,而是更新deleteAt字段 如果需要设置为true,则上面的deleteAt字段不能为false,也就是说必须启用
// paranoid: false
......@@ -17,5 +17,8 @@ module.exports = app => {
router.resources('org', '/webtool/v1/org', jwtErr, controller.org);
router.resources('step', '/webtool/v1/step', jwtErr, controller.step);
router.resources('item', '/webtool/v1/item', jwtErr, controller.item);
router.resources('tag', '/webtool/v1/tag', controller.tag);
router.post('/webtool/v1/mutisort', jwtErr, controller.item.mutisort);// 绑定用户组织
router.post('/webtool/v1/mutitag', jwtErr, controller.tag.mutitag);// 绑定用户组织
};
// app/service/user.js
const Service = require('egg').Service;
class TagService extends Service {
async mutiupdate(tagarr) {
const { ctx, app } = this;
const { Op } = app.Sequelize;
let tag;
try {
await ctx.model.Tag.destroy({
where: {
project_id: Array.from(new Set(tagarr.map(it => it.project_id))), // 所有名字为 John 的用户将被删除
},
});
tag = await ctx.model.Tag.bulkCreate(tagarr, {
updateOnDuplicate: [ 'project_id', 'tag_name' ],
});
} catch (error) {
tag = error;
}
return tag;
}
async getbyproject_id(project_id, status) {
const { ctx } = this;
const where =
status == '-1'
? {
project_id,
}
: {
project_id,
status,
};
let tag;
try {
tag = await ctx.model.Tag.findAll({
where,
});
} catch (error) {
tag = error;
}
return tag;
}
}
module.exports = TagService;
......@@ -2,7 +2,7 @@
module.exports = {
async up(queryInterface, Sequelize) {
const { INTEGER, STRING, DATE, ENUM, TEXT } = Sequelize;
const { INTEGER, STRING, DATE, ENUM, TEXT, JSON } = Sequelize;
await queryInterface.createTable('items', {
id: { type: INTEGER, primaryKey: true, autoIncrement: true },
project_id: INTEGER,
......@@ -11,6 +11,7 @@ module.exports = {
status: ENUM('0', '1', '2', '3', '4', '5'),
deadline: DATE,
other: STRING(255),
tags: JSON,
remark: TEXT,
userid: INTEGER,
sort: INTEGER,
......
'use strict';
module.exports = {
async up(queryInterface, Sequelize) {
const { INTEGER, STRING, DATE } = Sequelize;
await queryInterface.createTable('tags', {
id: { type: INTEGER, primaryKey: true, autoIncrement: true },
project_id: INTEGER,
mission_id: INTEGER,
tag_name: STRING(255),
other: STRING(255),
color: STRING(255),
created_at: DATE,
updated_at: DATE,
});
},
async down(queryInterface) {
await queryInterface.dropTable('tags');
},
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment