mongodb-crud
This commit is contained in:
26
examples/mongodb-crud/backend/src/api/ApiAddPost.ts
Normal file
26
examples/mongodb-crud/backend/src/api/ApiAddPost.ts
Normal 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()
|
||||
})
|
||||
}
|
12
examples/mongodb-crud/backend/src/api/ApiDelPost.ts
Normal file
12
examples/mongodb-crud/backend/src/api/ApiDelPost.ts
Normal 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({});
|
||||
}
|
22
examples/mongodb-crud/backend/src/api/ApiGetPost.ts
Normal file
22
examples/mongodb-crud/backend/src/api/ApiGetPost.ts
Normal 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()
|
||||
}
|
||||
})
|
||||
}
|
22
examples/mongodb-crud/backend/src/api/ApiUpdatePost.ts
Normal file
22
examples/mongodb-crud/backend/src/api/ApiUpdatePost.ts
Normal 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
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user