|
1 | | -import { db } from "../Data/Db.ts"; |
2 | | -import { ObjectId } from "mongo.ts"; |
| 1 | +import { db } from '../Data/Db.ts'; |
| 2 | +import { ObjectId } from 'mongo.ts'; |
| 3 | +import { User } from '../User/User.ts'; |
3 | 4 |
|
4 | 5 | type PostRecord = { |
5 | | - _id?: ObjectId; |
6 | | - title: string; |
7 | | - body: string; |
8 | | - published?: boolean; |
| 6 | + _id?: ObjectId; |
| 7 | + authorId?: ObjectId; |
| 8 | + title: string; |
| 9 | + body: string; |
| 10 | + published?: boolean; |
9 | 11 | }; |
10 | 12 |
|
11 | 13 | export class Post { |
12 | | - constructor({ _id, title, body, published = true }: PostRecord) { |
13 | | - this.id = _id; |
14 | | - this.title = title; |
15 | | - this.body = body; |
16 | | - this.published = published; |
17 | | - } |
18 | | - |
19 | | - id?: ObjectId; |
20 | | - title: string; |
21 | | - body: string; |
22 | | - published?: boolean; |
23 | | - |
24 | | - static collection = db.collection("Post"); |
25 | | - |
26 | | - async save() { |
27 | | - let rec; |
28 | | - if (this.id) { |
29 | | - rec = await Post.collection.updateOne({ _id: this.id }, { $set: this }); |
30 | | - } else { |
31 | | - rec = await Post.collection.insertOne(this); |
| 14 | + id?: ObjectId; |
| 15 | + authorId?: ObjectId; |
| 16 | + title: string; |
| 17 | + body: string; |
| 18 | + published?: boolean; |
| 19 | + |
| 20 | + constructor({ _id, authorId, title, body, published = true }: PostRecord) { |
| 21 | + this.id = _id; |
| 22 | + this.title = title; |
| 23 | + this.body = body; |
| 24 | + this.published = published; |
| 25 | + this.authorId = authorId; |
32 | 26 | } |
33 | 27 |
|
34 | | - const saved = new Post({ ...this, ...rec }); |
35 | | - Object.assign(this, saved); |
36 | | - return saved; |
37 | | - } |
38 | | - |
39 | | - static async find(query?: any) { |
40 | | - const records = await Post.collection.find(query).toArray(); |
41 | | - return records.map((record) => new Post(record as PostRecord)); |
42 | | - } |
43 | | - |
44 | | - static async load(_id: string) { |
45 | | - const record = await Post.collection.findOne({ _id: new ObjectId(_id) }); |
46 | | - if (typeof record == "undefined") return null; |
47 | | - return new Post(record as PostRecord); |
48 | | - } |
| 28 | + static collection = db.collection('Post'); |
| 29 | + |
| 30 | + async save() { |
| 31 | + let rec; |
| 32 | + if (this.id) { |
| 33 | + rec = await Post.collection.updateOne( |
| 34 | + { _id: this.id }, |
| 35 | + { $set: this }, |
| 36 | + ); |
| 37 | + } else { |
| 38 | + rec = await Post.collection.insertOne(this); |
| 39 | + } |
| 40 | + |
| 41 | + const saved = new Post({ ...this, ...rec }); |
| 42 | + Object.assign(this, saved); |
| 43 | + return saved; |
| 44 | + } |
| 45 | + |
| 46 | + static async find(query?: any) { |
| 47 | + const records = await Post.collection.find(query).toArray(); |
| 48 | + return records.map((record) => new Post(record as PostRecord)); |
| 49 | + } |
| 50 | + |
| 51 | + static async load(_id: string) { |
| 52 | + const record = await Post.collection.findOne({ |
| 53 | + _id: new ObjectId(_id), |
| 54 | + }); |
| 55 | + if (typeof record == 'undefined') return null; |
| 56 | + return new Post(record as PostRecord); |
| 57 | + } |
| 58 | + |
| 59 | + static async delete(_id: string) { |
| 60 | + return await Post.collection.deleteOne({ _id: new ObjectId(_id) }); |
| 61 | + } |
49 | 62 | } |
0 commit comments