Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
SunWuyuan committed Aug 27, 2024
1 parent 9ba2351 commit 891a346
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 76 deletions.
2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ model ow_projects {
type String? @default("text") @db.VarChar(32)
licence String? @db.VarChar(32)
authorid Int @db.UnsignedInt
state Int? @default(0) @db.UnsignedTinyInt
state String? @default("private") @db.VarChar(32)
view_count Int? @default(0) @db.UnsignedInt
like_count Int? @default(0)
favo_count Int? @default(0)
Expand Down
2 changes: 1 addition & 1 deletion server/lib/scratch_default_project.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions server/router_admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,11 @@ router.get('/works/scratch/data', function(req, res) {
WHERE='';
NICKNAME = '';//根据昵称查找
if (req.query.w == 'search_state0'){
WHERE = ` WHERE scratch.state=0 `;
WHERE = ` WHERE scratch.state='private' `;
} else if (req.query.w == 'search_state1'){
WHERE = ` WHERE scratch.state=1 `;
WHERE = ` WHERE scratch.state='public' `;
} else if (req.query.w == 'search_state2'){
WHERE = ` WHERE scratch.state=2 `;
WHERE = ` WHERE scratch.state='public' `;
} else if (req.query.w == 'search_workname'){
WHERE = ` WHERE scratch.title LIKE '%${req.query.v}%' `;
} else if (req.query.w == 'search_display_name'){
Expand Down Expand Up @@ -353,11 +353,11 @@ router.get('/works/python/data', function(req, res) {
WHERE='';
NICKNAME = '';//根据昵称查找
if (req.query.w == 'search_state0'){
WHERE = ` WHERE python.state=0 `;
WHERE = ` WHERE python.state='private' `;
} else if (req.query.w == 'search_state1'){
WHERE = ` WHERE python.state=1 `;
WHERE = ` WHERE python.state='public' `;
} else if (req.query.w == 'search_state2'){
WHERE = ` WHERE python.state=2 `;
WHERE = ` WHERE python.state='public' `;
} else if (req.query.w == 'search_workname'){
WHERE = ` WHERE python.title LIKE '%${req.query.v}%' `;
} else if (req.query.w == 'search_display_name'){
Expand Down
26 changes: 13 additions & 13 deletions server/router_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ router.get("/", function (req, res) {
//获取已分享的作品总数:1:普通作品,2:推荐的优秀作品
var SQL =
`SELECT ` +
` (SELECT count(id) FROM ow_projects WHERE state>0 AND type='scratch' ) AS scratch_count, ` +
` (SELECT count(id) FROM ow_projects WHERE state>0 AND type='python') AS python_count `;
` (SELECT count(id) FROM ow_projects WHERE state='public' AND type='scratch' ) AS scratch_count, ` +
` (SELECT count(id) FROM ow_projects WHERE state='public' AND type='python') AS python_count `;
DB.query(SQL, function (err, data) {
if (err) {
// console.error('数据库操作出错:');
Expand All @@ -33,7 +33,7 @@ router.post("/getUserScratchProjects", function (req, res) {
var curr = parseInt(req.body.curr); //当前要显示的页码
var limit = parseInt(req.body.limit); //每页显示的作品数
var userid = parseInt(req.body.userid); //
var SQL = `SELECT id, title,state,view_count,description FROM ow_projects WHERE authorid=${userid} AND state>0 AND type='scratch' ORDER BY view_count DESC LIMIT ${
var SQL = `SELECT id, title,state,view_count,description FROM ow_projects WHERE authorid=${userid} AND state='public' AND type='scratch' ORDER BY view_count DESC LIMIT ${
(curr - 1) * limit
}, ${limit}`;
DB.query(SQL, function (err, data) {
Expand All @@ -50,7 +50,7 @@ router.post("/getUserPythonProjects", function (req, res) {
var curr = parseInt(req.body.curr); //当前要显示的页码
var limit = parseInt(req.body.limit); //每页显示的作品数
var userid = parseInt(req.body.userid); //
var SQL = `SELECT id, title,state,view_count,description FROM ow_projects WHERE authorid=${userid} AND state>0 AND type='python' ORDER BY view_count DESC LIMIT ${
var SQL = `SELECT id, title,state,view_count,description FROM ow_projects WHERE authorid=${userid} AND state='public' AND type='python' ORDER BY view_count DESC LIMIT ${
(curr - 1) * limit
}, ${limit}`;
DB.query(SQL, function (err, data) {
Expand Down Expand Up @@ -104,13 +104,13 @@ router.get("/getuserinfo", async function (req, res) {
scratchcount =await I.prisma.ow_projects.count({
where: {
type: 'scratch',
state: {in:[1,2]},
state: 'public',
},
});
pythoncount =await I.prisma.ow_projects.count({
where: {
type: 'python',
state: {in:[1,2]},
state: 'public',
},
});
if (!user[0]) {
Expand Down Expand Up @@ -145,9 +145,9 @@ router.get("/myprojectcount", function (req, res) {
}
var SQL =
`SELECT ` +
` count(case when state=0 then 1 end) AS state0_count, ` +
` count(case when state=1 then 1 end) AS state1_count, ` +
` count(case when state=2 then 1 end) AS state2_count ` +
` count(case when state='private' then 1 end) AS state0_count, ` +
` count(case when state='public' then 1 end) AS state1_count, ` +
` '' AS state2_count ` +
` FROM ow_projects WHERE authorid=${res.locals["userid"]} AND type='${res.locals.type}'`;

DB.query(SQL, function (err, data) {
Expand Down Expand Up @@ -177,9 +177,9 @@ router.get("/work/info", function (req, res) {
}
var SQL =
`SELECT ` +
` count(case when state=0 then 1 end) AS state0_count, ` +
` count(case when state=1 then 1 end) AS state1_count, ` +
` count(case when state=2 then 1 end) AS state2_count ` +
` count(case when state='private' then 1 end) AS state0_count, ` +
` count(case when state='public' then 1 end) AS state1_count, ` +
`'' AS state2_count ` +
` FROM ow_projects WHERE authorid=${res.locals["userid"]} AND type='${res.locals.type}'`;

DB.query(SQL, function (err, data) {
Expand All @@ -206,7 +206,7 @@ router.get("/projectinfo", function (req, res) {
` ow_users.motto AS author_motto` +
` FROM ow_projects ` +
` LEFT JOIN ow_users ON (ow_users.id=ow_projects.authorid) ` +
` WHERE ow_projects.id=${req.query.id} AND (ow_projects.state>=1 or ow_projects.authorid=${res.locals.userid}) LIMIT 1`;
` WHERE ow_projects.id=${req.query.id} AND (ow_projects.state='public' or ow_projects.authorid=${res.locals.userid}) LIMIT 1`;
DB.query(SQL, function (err, SCRATCH) {
if (err || SCRATCH.length == 0) {
res.locals.tip = { opt: "flash", msg: "项目不存在或未发布" };
Expand Down
26 changes: 14 additions & 12 deletions server/router_my.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ router.get("/project", function (req, res) {
router.get("/scratch", function (req, res) {
var SQL =
`SELECT ` +
` count(case when state=0 then 1 end) AS state0_count, ` +
` count(case when state=1 then 1 end) AS state1_count, ` +
` count(case when state=2 then 1 end) AS state2_count ` +
` count(case when state='private' then 1 end) AS state0_count, ` +
` count(case when state='public' then 1 end) AS state1_count, ` +
` '' AS state2_count ` +
` FROM scratch WHERE authorid=${res.locals["userid"]}`;

DB.query(SQL, function (err, data) {
Expand All @@ -57,9 +57,9 @@ router.get("/scratch", function (req, res) {
router.get("/python", function (req, res) {
var SQL =
`SELECT ` +
` count(case when state=0 then 1 end) AS state0_count, ` +
` count(case when state=1 then 1 end) AS state1_count, ` +
` count(case when state=2 then 1 end) AS state2_count ` +
` count(case when state='private' then 1 end) AS state0_count, ` +
` count(case when state='public' then 1 end) AS state1_count, ` +
` '' AS state2_count ` +
` FROM python WHERE authorid=${res.locals.userid}`;

DB.query(SQL, function (err, data) {
Expand Down Expand Up @@ -115,9 +115,10 @@ router.get("/getPythonProjects", function (req, res) {
});
});

// 弃用
//分享Scratch项目
router.post("/scratch/share", function (req, res) {
var SQL = `UPDATE ow_projects SET state=1 WHERE id=${req.body["id"]} AND authorid=${res.locals.userid} LIMIT 1`;
var SQL = `UPDATE ow_projects SET state='public' WHERE id=${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);
Expand All @@ -128,9 +129,10 @@ router.post("/scratch/share", function (req, res) {
});
});

// 弃用
//分享Scratch项目
router.post("/python/share", function (req, res) {
var SQL = `UPDATE ow_projects SET state=1 WHERE id=${req.body["id"]} AND authorid=${res.locals.userid} LIMIT 1`;
var SQL = `UPDATE ow_projects SET state='public' WHERE id=${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);
Expand All @@ -143,7 +145,7 @@ router.post("/python/share", function (req, res) {

//分享Scratch项目
router.post("/project/share", function (req, res) {
var SQL = `UPDATE ow_projects SET state=1 WHERE id=${req.body["id"]} AND authorid=${res.locals.userid} LIMIT 1`;
var SQL = `UPDATE ow_projects SET state='public' WHERE id=${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);
Expand Down Expand Up @@ -196,7 +198,7 @@ router.post("/project/setdescription", function (req, res) {

//取消分享Scratch项目
router.post("/scratch/noshare", function (req, res) {
var SQL = `UPDATE ow_projects SET state=0 WHERE id=${req.body["id"]} AND authorid=${res.locals.userid} LIMIT 1`;
var SQL = `UPDATE ow_projects SET state='private' WHERE id=${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);
Expand All @@ -209,7 +211,7 @@ router.post("/scratch/noshare", function (req, res) {

//取消分享Scratch项目
router.post("/python/noshare", function (req, res) {
var SQL = `UPDATE ow_projects SET state=0 WHERE id=${req.body["id"]} AND authorid=${res.locals.userid} LIMIT 1`;
var SQL = `UPDATE ow_projects SET state='private' WHERE id=${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);
Expand All @@ -222,7 +224,7 @@ router.post("/python/noshare", function (req, res) {

//取消分享Scratch项目
router.post("/project/noshare", function (req, res) {
var SQL = `UPDATE ow_projects SET state=0 WHERE id=${req.body["id"]} AND authorid=${res.locals.userid} LIMIT 1`;
var SQL = `UPDATE ow_projects SET state='private' WHERE id=${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);
Expand Down
8 changes: 4 additions & 4 deletions server/router_project.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ router.post("/setType", function (req, res) {

//开源项目
router.post("/share", function (req, res) {
var SQL = `UPDATE ow_projects SET state=1 WHERE id=${String(
var SQL = `UPDATE ow_projects SET state='public' WHERE id=${String(
Number(req.body["id"])
)} AND authorid=${res.locals.userid} LIMIT 1`;
DB.query(SQL, function (err, d) {
Expand All @@ -110,7 +110,7 @@ router.get("/getproject/:id", function (req, res) {
var DefaultPython = {
id: 0,
title: "Python新项目",
state: 0,
state: 'private',
src: `import turtle\n\nt = turtle.Turtle()\nt.forward(100)\n\nprint ("Welcome to ZeroCat!")`,
};
if (projectid == 1) {
Expand All @@ -123,12 +123,12 @@ router.get("/getproject/:id", function (req, res) {
} else {
if (!res.locals.login) {
//未登录时,只能打开已发布的作品
SQL = `SELECT * FROM ow_projects WHERE id=${projectid} AND state>0`;
SQL = `SELECT * FROM ow_projects WHERE id=${projectid} AND state='public'`;
} else {
//作品编辑:能够打开一个作品的几种权限:
//1、自己的作品;
//2、开源的作品;
SQL = `SELECT * FROM ow_projects WHERE id=${projectid} AND (authorid=${res.locals.userid} OR state>0)`;
SQL = `SELECT * FROM ow_projects WHERE id=${projectid} AND (authorid=${res.locals.userid} OR state='public')`;
}
}

Expand Down
16 changes: 8 additions & 8 deletions server/router_python.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ router.get("/next", function (req, res) {
//获取已分享的作品总数:1:普通作品,2:推荐的优秀作品
var SQL =
`SELECT ` +
` (SELECT count(id) FROM ow_projects WHERE state>0 ) AS python_count `;
` (SELECT count(id) FROM ow_projects WHERE state='public' ) AS python_count `;
DB.query(SQL, function (err, data) {
if (err) {
// console.error('数据库操作出错:');
Expand All @@ -49,7 +49,7 @@ router.get("/view/getPythonProjects", function (req, res) {
type = "time";
}

var SQL = `SELECT ow_projects.id, ow_projects.title, ow_projects.state,ow_projects.authorid, ow_projects.description,ow_projects.view_count,ow_users.display_name,ow_users.motto FROM ow_projects JOIN ow_users ON ow_projects.authorid = ow_users.id WHERE ow_projects.state > 0 AND ow_projects.type='python' ORDER BY ow_projects.${type} DESC LIMIT ${
var SQL = `SELECT ow_projects.id, ow_projects.title, ow_projects.state,ow_projects.authorid, ow_projects.description,ow_projects.view_count,ow_users.display_name,ow_users.motto FROM ow_projects JOIN ow_users ON ow_projects.authorid = ow_users.id WHERE ow_projects.state='public' AND ow_projects.type='python' ORDER BY ow_projects.${type} DESC LIMIT ${
(curr - 1) * limit
}, ${limit}`;
DB.query(SQL, function (err, data) {
Expand All @@ -72,8 +72,8 @@ router.get("/view/seachPythonProjects", function (req, res) {
if (req.query.searchall == "true") {
searchinfo = "src";
}
//var SQL = `SELECT id, title FROM ow_projects WHERE state>0 AND (${searchinfo} LIKE ?) LIMIT 12`;
var SQL = `SELECT ow_projects.id, ow_projects.title, ow_projects.state,ow_projects.authorid,ow_projects.description,ow_projects.view_count, ow_users.display_name,ow_users.motto FROM ow_projects JOIN ow_users ON ow_projects.authorid = ow_users.id WHERE ow_projects.state>0 AND (${searchinfo} LIKE ?) AND ow_projects.type='${tabelName}'`;
//var SQL = `SELECT id, title FROM ow_projects WHERE state='public' AND (${searchinfo} LIKE ?) LIMIT 12`;
var SQL = `SELECT ow_projects.id, ow_projects.title, ow_projects.state,ow_projects.authorid,ow_projects.description,ow_projects.view_count, ow_users.display_name,ow_users.motto FROM ow_projects JOIN ow_users ON ow_projects.authorid = ow_users.id WHERE ow_projects.state='public' AND (${searchinfo} LIKE ?) AND ow_projects.type='${tabelName}'`;
var WHERE = [`%${req.query.txt}%`];
DB.qww(SQL, WHERE, function (err, data) {
if (err) {
Expand Down Expand Up @@ -121,13 +121,13 @@ router.post('/getWork', function (req, res) {
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`;
SQL = `SELECT * FROM ow_projects WHERE id=${projectid} AND state='public'`;
}else {

//作品编辑:能够打开一个作品的几种权限:
//1、自己的作品;
//2、开源的作品;
SQL = `SELECT * FROM ow_projects WHERE id=${projectid} AND (authorid=${res.locals.userid} OR state>0)`;
SQL = `SELECT * FROM ow_projects WHERE id=${projectid} AND (authorid=${res.locals.userid} OR state='public')`;


}
Expand Down Expand Up @@ -210,7 +210,7 @@ router.post('/publish', function (req, res) {

// python 优秀作品
router.post('/YxLibrary_count', function (req, res) {
var SQL = `SELECT count(id) AS c FROM ow_projects WHERE state=2`;
var SQL = `SELECT count(id) AS c FROM ow_projects WHERE state='public'`;
DB.query(SQL, function (err, COUNT){
if (err) {
res.status(200).send({status:'ok', total: 0});
Expand All @@ -226,7 +226,7 @@ router.post('/YxLibrary_data', function (req, res) {
var page = parseInt(req.body.page);
SQL = `SELECT ow_projects.id, ow_projects.authorid, ow_projects.view_count, ow_projects.time, ow_projects.title, ow_projects.description, ow_users.display_name AS author_display_name FROM ow_projects `+
` LEFT JOIN ow_users ON ow_users.id=ow_projects.authorid `+
` WHERE ow_projects.state=2 ORDER BY ow_projects.view_count DESC LIMIT ${(page-1)*16},${16}`;
` WHERE ow_projects.state='public' ORDER BY ow_projects.view_count DESC LIMIT ${(page-1)*16},${16}`;
DB.query(SQL, function (err, data) {
if (err) {
res.status(200).send({status:'ok', data:[]});
Expand Down
Loading

0 comments on commit 891a346

Please sign in to comment.