From f5a2bd00a6b2704fb6c8fd34ac383bc7b10050e9 Mon Sep 17 00:00:00 2001 From: huangge1199 Date: Tue, 6 Aug 2024 11:59:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=B0=E9=97=BB=E5=88=86?= =?UTF-8?q?=E7=B1=BB=E7=9A=84=E5=A2=9E=E5=88=A0=E6=94=B9=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controller/category.js | 85 ++++++++++++++++++++++++++++++++++++++ src/model/base.js | 8 ++++ src/model/category.js | 32 ++++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 src/controller/category.js create mode 100644 src/model/base.js create mode 100644 src/model/category.js diff --git a/src/controller/category.js b/src/controller/category.js new file mode 100644 index 0000000..8c542f1 --- /dev/null +++ b/src/controller/category.js @@ -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: '当前条件没有分类!'}); + } + } +}; diff --git a/src/model/base.js b/src/model/base.js new file mode 100644 index 0000000..096daaa --- /dev/null +++ b/src/model/base.js @@ -0,0 +1,8 @@ +module.exports = class extends think.Model { + // 公共方法示例 + getDataById(id) { + return this.where({ id }).find(); + } + + // 其他公共方法和属性可以在这里定义 +}; diff --git a/src/model/category.js b/src/model/category.js new file mode 100644 index 0000000..0809681 --- /dev/null +++ b/src/model/category.js @@ -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(); + } +};