|
@@ -0,0 +1,112 @@
|
|
|
+export default class TodosController {
|
|
|
+ /**
|
|
|
+ *! [分页获取todo]
|
|
|
+ ** 请求体中包含分页信息:current页码 and count 每页条数 以及可选参数:title 用于检索
|
|
|
+ */
|
|
|
+ static async getTodosByPage(ctx) {
|
|
|
+ let { current, count, title = '', status = 'all' } = ctx.query;
|
|
|
+
|
|
|
+ current = current - 0;
|
|
|
+ count = count - 0;
|
|
|
+
|
|
|
+ let statusList = [0, 1];
|
|
|
+ let start = (current - 1) * count;
|
|
|
+ let userId = ctx.session.user.id;
|
|
|
+
|
|
|
+ switch (status) {
|
|
|
+ case 'active':
|
|
|
+ statusList = [0, 0];
|
|
|
+ break;
|
|
|
+ case 'completed':
|
|
|
+ statusList = [1, 1];
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ statusList = [0, 1];
|
|
|
+ }
|
|
|
+
|
|
|
+ let sql =
|
|
|
+ 'select * from Todos where uid = ? and title like ? and status in (?, ?) order by create_time desc limit ?, ?;';
|
|
|
+ let tsql =
|
|
|
+ 'select count(id) as total from Todos where uid = ? and title like ? and status in (?, ?);';
|
|
|
+ let atsql =
|
|
|
+ 'select count(id) as activeCount from Todos where uid = ? and title like ? and status = 0;';
|
|
|
+
|
|
|
+ let [rows] = await ctx.db.query(sql, [
|
|
|
+ userId,
|
|
|
+ `%${title}%`,
|
|
|
+ ...statusList,
|
|
|
+ start,
|
|
|
+ count - 0,
|
|
|
+ ]);
|
|
|
+
|
|
|
+ let [[{ total = 0 }]] = await ctx.db.query(tsql, [
|
|
|
+ userId,
|
|
|
+ `%${title}%`,
|
|
|
+ ...statusList,
|
|
|
+ ]);
|
|
|
+
|
|
|
+ let [[{ activeCount = 0 }]] = await ctx.db.query(atsql, [
|
|
|
+ userId,
|
|
|
+ `%${title}%`,
|
|
|
+ ]);
|
|
|
+
|
|
|
+ ctx.body = {
|
|
|
+ code: 0,
|
|
|
+ msg: '查询成功',
|
|
|
+ data: {
|
|
|
+ page: {
|
|
|
+ current,
|
|
|
+ count,
|
|
|
+ total,
|
|
|
+ pages: Math.ceil(total / count),
|
|
|
+ activeCount,
|
|
|
+ },
|
|
|
+ list: rows,
|
|
|
+ },
|
|
|
+ };
|
|
|
+ }
|
|
|
+ // 添加todo
|
|
|
+ static async addTodo(ctx) {
|
|
|
+ let { title, description, cover } = ctx.request.body;
|
|
|
+ let uid = ctx.session.user.id;
|
|
|
+
|
|
|
+ const sql =
|
|
|
+ 'insert into Todos(title, description, cover, uid, create_time) values (?, ?, ?, ?, now());';
|
|
|
+
|
|
|
+ let res = await ctx.db.query(sql, [title, description, cover, uid]);
|
|
|
+
|
|
|
+ console.log(res);
|
|
|
+
|
|
|
+ ctx.body = {
|
|
|
+ code: 0,
|
|
|
+ msg: '添加成功',
|
|
|
+ };
|
|
|
+ }
|
|
|
+ static async editTodo(ctx) {
|
|
|
+ let { id, title, description, cover, status } = ctx.request.body;
|
|
|
+ let sql =
|
|
|
+ 'update Todos set title = ?, description = ?, cover = ?, status = ? where id = ?;';
|
|
|
+
|
|
|
+ let res = await ctx.db.query(sql, [title, description, cover, status, id]);
|
|
|
+
|
|
|
+ console.log(res);
|
|
|
+
|
|
|
+ ctx.body = {
|
|
|
+ code: 0,
|
|
|
+ msg: '更新成功',
|
|
|
+ };
|
|
|
+ }
|
|
|
+ static async deleteTodo(ctx) {
|
|
|
+ let todoId = ctx.params.id;
|
|
|
+ let sql = 'delete from Todos where id = ?;';
|
|
|
+
|
|
|
+ let res = await ctx.db.query(sql, [todoId]);
|
|
|
+
|
|
|
+ console.log(res);
|
|
|
+
|
|
|
+ ctx.body = {
|
|
|
+ code: 0,
|
|
|
+ msg: '删除成功',
|
|
|
+ };
|
|
|
+ }
|
|
|
+}
|