添加新闻分类的增删改查
This commit is contained in:
parent
fa218c8bdd
commit
f5a2bd00a6
85
src/controller/category.js
Normal file
85
src/controller/category.js
Normal file
@ -0,0 +1,85 @@
|
||||
const Base = require('./base.js');
|
||||
|
||||
module.exports = class extends Base {
|
||||
async addAction() {
|
||||
const name = this.post('name');
|
||||
// eslint-disable-next-line camelcase
|
||||
const news_id = this.post('newsId');
|
||||
const remark = this.post('remark');
|
||||
|
||||
const category = {name, news_id, remark};
|
||||
|
||||
const categoryModel = this.model('category');
|
||||
const insertId = await categoryModel.addCategory(category);
|
||||
|
||||
if (insertId) {
|
||||
this.success({message: '新闻分类添加成功!', id: insertId});
|
||||
} else {
|
||||
this.fail(500, '新闻分类添加失败!');
|
||||
}
|
||||
}
|
||||
|
||||
async editAction() {
|
||||
const id = this.post('id');
|
||||
const name = this.post('name');
|
||||
// eslint-disable-next-line camelcase
|
||||
const news_id = this.post('newsId');
|
||||
const remark = this.post('remark');
|
||||
|
||||
const category = {id, name, news_id, remark};
|
||||
const categoryModel = this.model('category');
|
||||
const affectedRows = await categoryModel.editCategoryById(id, category);
|
||||
if (affectedRows) {
|
||||
this.success({message: '新闻分类修改成功!', affectedRows: affectedRows});
|
||||
} else {
|
||||
this.fail(500, '新闻分类修改失败!');
|
||||
}
|
||||
}
|
||||
|
||||
async deleteAction() {
|
||||
const id = this.post('id');
|
||||
|
||||
const categoryModel = this.model('category');
|
||||
const affectedRows = await categoryModel.deleteCategoryById(id);
|
||||
if (affectedRows) {
|
||||
this.success({message: '新闻分类删除成功!', affectedRows: affectedRows});
|
||||
} else {
|
||||
this.fail(500, '新闻分类删除失败!');
|
||||
}
|
||||
}
|
||||
|
||||
async getCategoryByIdAction() {
|
||||
const id = this.post('id');
|
||||
const categoryModel = this.model('category');
|
||||
const category = await categoryModel.getCategoryById(id);
|
||||
|
||||
if (!think.isEmpty(category)) {
|
||||
this.success(category);
|
||||
} else {
|
||||
this.success({message: '未找到该分类!'});
|
||||
}
|
||||
}
|
||||
|
||||
async getAllCategoriesAction() {
|
||||
const categoryModel = this.model('category');
|
||||
const categories = await categoryModel.getAllCategories();
|
||||
|
||||
if (!think.isEmpty(categories)) {
|
||||
this.success(categories);
|
||||
} else {
|
||||
this.success({message: '当前没有分类!'});
|
||||
}
|
||||
}
|
||||
|
||||
async getCategoriesByConditionAction() {
|
||||
const condition = this.post();
|
||||
const categoryModel = this.model('category');
|
||||
const categories = await categoryModel.getCategoriesByCondition(condition);
|
||||
|
||||
if (!think.isEmpty(categories)) {
|
||||
this.success(categories);
|
||||
} else {
|
||||
this.success({message: '当前条件没有分类!'});
|
||||
}
|
||||
}
|
||||
};
|
8
src/model/base.js
Normal file
8
src/model/base.js
Normal file
@ -0,0 +1,8 @@
|
||||
module.exports = class extends think.Model {
|
||||
// 公共方法示例
|
||||
getDataById(id) {
|
||||
return this.where({ id }).find();
|
||||
}
|
||||
|
||||
// 其他公共方法和属性可以在这里定义
|
||||
};
|
32
src/model/category.js
Normal file
32
src/model/category.js
Normal file
@ -0,0 +1,32 @@
|
||||
const Base = require('./base.js');
|
||||
|
||||
module.exports = class extends Base {
|
||||
addCategory(data) {
|
||||
// 插入数据并返回插入的主键
|
||||
return this.add(data);
|
||||
}
|
||||
|
||||
editCategoryById(id, data) {
|
||||
return this.where({id}).update(data);
|
||||
}
|
||||
|
||||
deleteCategoryById(id) {
|
||||
const del = 1;
|
||||
const data = {del};
|
||||
return this.where({id}).update(data);
|
||||
}
|
||||
|
||||
getCategoryById(id) {
|
||||
const del = 0;
|
||||
return this.where({id, del}).find();
|
||||
}
|
||||
|
||||
getAllCategories() {
|
||||
const del = 0;
|
||||
return this.where({del}).select();
|
||||
}
|
||||
|
||||
getCategoriesByCondition(condition) {
|
||||
return this.where(condition).select();
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue
Block a user