mongodb-crud

This commit is contained in:
King Wang
2021-06-26 23:41:37 +08:00
parent aa5da74b6a
commit 4a74c31258
36 changed files with 1212 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
import { ApiCall } from "tsrpc";
import { Global } from "../models/Global";
import { ReqAddPost, ResAddPost } from "../shared/protocols/PtlAddPost";
export async function ApiAddPost(call: ApiCall<ReqAddPost, ResAddPost>) {
console.log('cccgasdgasd', call.service.conf)
if (call.service.conf) {
// if (没登录) {
// call.error('你还没登录');
// return;
// }
}
let op = await Global.collection('Post').insertOne({
...call.req.newPost,
create: {
uid: 'xxx',
time: new Date()
},
visitedNum: 0
});
call.succ({
insertedId: op.insertedId.toHexString()
})
}

View File

@@ -0,0 +1,12 @@
import { ObjectID } from 'mongodb';
import { ApiCall } from "tsrpc";
import { Global } from "../models/Global";
import { ReqDelPost, ResDelPost } from "../shared/protocols/PtlDelPost";
export async function ApiDelPost(call: ApiCall<ReqDelPost, ResDelPost>) {
let op = await Global.collection('Post').deleteOne({
_id: new ObjectID(call.req._id)
})
call.succ({});
}

View File

@@ -0,0 +1,22 @@
import { ObjectID } from 'mongodb';
import { ApiCall } from "tsrpc";
import { Global } from "../models/Global";
import { ReqGetPost, ResGetPost } from "../shared/protocols/PtlGetPost";
export async function ApiGetPost(call: ApiCall<ReqGetPost, ResGetPost>) {
let op = await Global.collection('Post').findOne({
_id: new ObjectID(call.req._id)
});
if (!op) {
call.error('Post 不存在');
return;
}
call.succ({
post: {
...op,
_id: op._id.toHexString()
}
})
}

View File

@@ -0,0 +1,22 @@
import { ObjectID } from 'mongodb';
import { ApiCall } from "tsrpc";
import { Global } from "../models/Global";
import { ReqUpdatePost, ResUpdatePost } from "../shared/protocols/PtlUpdatePost";
export async function ApiUpdatePost(call: ApiCall<ReqUpdatePost, ResUpdatePost>) {
let { _id, ...update } = call.req.update;
let op = await Global.collection('Post').updateOne({
_id: new ObjectID(_id)
}, {
$set: update
});
console.log('ssss', {
matchedCount: op.matchedCount
})
call.succ({
matchedCount: op.matchedCount
})
}