Skip to content

Commit

Permalink
11
Browse files Browse the repository at this point in the history
  • Loading branch information
SunWuyuan committed Aug 8, 2024
1 parent 7369a82 commit 8f012f6
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 58 deletions.
182 changes: 141 additions & 41 deletions server/router_project.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,169 @@
var express = require('express');
var express = require("express");
var router = express.Router();

var DB = require("./lib/database.js"); // 数据库


router.all('*', function (req, res, next) {
next();
router.all("*", function (req, res, next) {
next();
});
//router.get('/', function (req, res) {})

//保存作品:标题
router.post("/saveProjcetTitle", function (req, res) {
if (!res.locals.login) {
res.status(404);
router.post("/newProjcet", function (req, res) {
if (!res.locals.login) {
res.status(404);
return;
}var INSERT =`INSERT INTO ow_projects (authorid, title,type) VALUES (${res.locals.userid}, ?,?)`;
var SET = [req.body.title,req.body.type]
DB.qww(INSERT, SET, function (err, newproject) {
if (err || newproject.affectedRows==0) {
res.status(200).send({status: "x", msg: "创建失败" });
return;
}
var UPDATE = `UPDATE ow_projects SET title=? WHERE id=${String(Number(req.body.id))} AND authorid=${res.locals.userid} LIMIT 1`;
var VAL = [`${req.body.title}`];
DB.qww(UPDATE, VAL, function (err, SCRATCH) {
if (err) {
res.status(404).send({ status: "err" });
} else {
res.status(200).send({ status: "ok" });
}
});

res.status(200).send({status: "ok", msg: "创建成功", 'id': newproject['insertId']})
});

return;
});
//保存作品:标题
router.post("/saveProjcetTitle", function (req, res) {
if (!res.locals.login) {
res.status(404);
return;
}
var UPDATE = `UPDATE ow_projects SET title=? WHERE id=${String(
Number(req.body.id)
)} AND authorid=${res.locals.userid} LIMIT 1`;
var VAL = [`${req.body.title}`];
DB.qww(UPDATE, VAL, function (err, SCRATCH) {
if (err) {
res.status(404).send({ status: "err" });
} else {
res.status(200).send({ status: "ok" });
}
});
});

//简介
router.post("/setDescription", function (req, res) {
var SET = { description: req.body["description"] };
var SQL = `UPDATE ow_projects SET ? WHERE id=${String(Number(req.body.id))} AND authorid=${res.locals.userid} LIMIT 1`;
DB.qww(SQL, SET, function (err, d) {
if (err) {
res.status(200).send(I.msg_fail);
return;
}
var SET = { description: req.body["description"] };
var SQL = `UPDATE ow_projects SET ? WHERE id=${String(
Number(req.body.id)
)} AND authorid=${res.locals.userid} LIMIT 1`;
DB.qww(SQL, SET, function (err, d) {
if (err) {
res.status(200).send(I.msg_fail);
return;
}

res.status(200).send({ status: "success", msg: "设置成功" });
});
res.status(200).send({ status: "success", msg: "设置成功" });
});
});

//简介
router.post("/setType", function (req, res) {
var SET = { type: req.body["type"] };
var SQL = `UPDATE ow_projects SET ? WHERE id=${String(Number(req.body.id))} AND authorid=${res.locals.userid} LIMIT 1`;
DB.qww(SQL, SET, function (err, d) {
if (err) {
res.status(200).send(I.msg_fail);
return;
}
var SET = { type: req.body["type"] };
var SQL = `UPDATE ow_projects SET ? WHERE id=${String(
Number(req.body.id)
)} AND authorid=${res.locals.userid} LIMIT 1`;
DB.qww(SQL, SET, function (err, d) {
if (err) {
res.status(200).send(I.msg_fail);
return;
}

res.status(200).send({ status: "success", msg: "设置成功" });
});
res.status(200).send({ status: "success", msg: "设置成功" });
});
});

//开源项目
router.post("/share", function (req, res) {
var SQL = `UPDATE ow_projects SET state=1 WHERE id=${String(Number(req.body["id"]))} AND authorid=${res.locals.userid} LIMIT 1`;
DB.query(SQL, function (err, d) {
if (err) {
res.status(200).send(I.msg_fail);
var SQL = `UPDATE ow_projects SET state=1 WHERE id=${String(
Number(req.body["id"])
)} AND authorid=${res.locals.userid} LIMIT 1`;
DB.query(SQL, function (err, d) {
if (err) {
res.status(200).send(I.msg_fail);
return;
}

res.status(200).send({ status: "success", msg: "分享成功" });
});
});

// 从数据库获取作品
router.get("/getproject/:id", function (req, res) {
var projectid = 0;
if (req.params.id && req.params.id > 1) {
projectid = req.params.id;
}

if (projectid == 0 || projectid == 1) {
// 默认作品
//***当把该块注释后,则从数据库加载默认作品***
//从指定文件加载默认作品:BEGIN==========================================
var DefaultPython = {
id: 0,
title: "Python新项目",
state: 0,
src: `import turtle\n\nt = turtle.Turtle()\nt.forward(100)\n\nprint ("Welcome to ZeroCat!")`,
};
if (projectid == 1) {
res.status(200).send({ status: "ok", work: DefaultPython });
return;
}
//从指定文件加载默认作品:END============================================
//*/
SQL = `SELECT * FROM ow_projects WHERE id=1`; //默认作品为1号作品
} else {
if (!res.locals.login) {
//未登录时,只能打开已发布的作品
SQL = `SELECT * FROM ow_projects WHERE id=${projectid} AND state>0`;
} else {
//作品编辑:能够打开一个作品的几种权限:
//1、自己的作品;
//2、开源的作品;
SQL = `SELECT * FROM ow_projects WHERE id=${projectid} AND (authorid=${res.locals.userid} OR state>0)`;
}
}

DB.query(SQL, function (err, WORK) {
if (err || WORK.length == 0) {
res.status(200).send({ status: "x", msg: "作品不存在或无权打开" }); //需要前端内部处理
} else {
res.status(200).send({ status: "ok", work: WORK[0] });

var UPDATE = `UPDATE ow_projects SET view_count=view_count+1 WHERE id=${projectid} LIMIT 1`;
DB.query(UPDATE, function (err, s) {
if (err) {
}
});
}
});
});

//Scratch_play获取源代码数据部分
router.get("/getproject/src/:id", function (req, res) {
var SQL = `SELECT src FROM ow_projects WHERE id=${req.params.id} LIMIT 1`;
DB.query(SQL, function (err, PROJECT) {
if (err) {
return;
}
if (PROJECT.length == 0) {
return;
}
res.status(200).send(PROJECT[0].src);

//浏览数+1
var SQL = `UPDATE ow_projects SET view_count=view_count+1 WHERE id=${req.params.id} LIMIT 1`;
DB.query(SQL, function (err, U) {
if (err || U.affectedRows == 0) {
res.locals.tip = { opt: "flash", msg: "项目不存在或未发布" };
res.render("404.ejs");
return;
}

res.status(200).send({ status: "success", msg: "分享成功" });
});
});
module.exports = router;
});
module.exports = router;
27 changes: 10 additions & 17 deletions server/router_search.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ router.get("/", async function (req, res) {
ordersclist = { up: "desc", down: "asc" };
ordersc = ordersclist[ordersc];
// console.log(ordersc);
searchinfo ={
title: { contains: search.title },
src: search.src != "" ? { contains: search.src } : {},
description: { contains: search.description },
type: { contains: search.type },
state: { in: search.state },
authorid: search.userid != "" ? { equals: Number(search.userid) } : {},
}
var projectresult = await I.prisma.ow_projects.findMany({
orderBy: [
orderby === "view_count"
Expand All @@ -73,14 +81,7 @@ router.get("/", async function (req, res) {
? { id: ordersc }
: {},
],
where: {
title: { contains: search.title },
src: { contains: search.src },
description: { contains: search.description },
type: { contains: search.type },
state: { in: search.state },
authorid: search.userid != "" ? { equals: Number(search.userid) } : {},
},
where: searchinfo,
select: {
id: true,
type: true,
Expand All @@ -95,15 +96,7 @@ router.get("/", async function (req, res) {
take: search.limit,
});
var projectcount = await I.prisma.ow_projects.count({
where: {
title: { contains: search.title },
src: { contains: search.src },
description: { contains: search.description },
type: { contains: search.type },
state: { in: search.state },

authorid: search.userid != "" ? { equals: Number(search.userid) } : {},
},
where: searchinfo,
});
//console.log(projectcount);
const authorIds = new Set(projectresult.map((item) => item.authorid));
Expand Down

0 comments on commit 8f012f6

Please sign in to comment.