a mini social media app for small communities
1module entity
2
3import db.pg
4import time
5import util
6
7pub struct Post {
8pub mut:
9 id int @[primary; sql: serial]
10 author_id int
11 replying_to ?int
12
13 title string
14 body string
15
16 pinned bool
17 nsfw bool
18
19 posted_at time.Time = time.now()
20}
21
22// Post.from_row creates a post object from the given database row.
23// see src/database/post.v#search_for_posts for usage.
24@[inline]
25pub fn Post.from_row(res pg.Result, row pg.Row) Post {
26 // curry some arguments for cleanliness
27 c := fn [res, row] (key string) ?string {
28 return util.get_row_col(res, row, key)
29 }
30 ct := fn [res, row] (key string) string {
31 return util.get_row_col_or_throw(res, row, key)
32 }
33
34 // this throws a cgen error when put in Post{}
35 //todo: report this
36 posted_at := time.parse(ct('posted_at')) or { panic(err) }
37 nsfw := util.map_or_throw[string, bool](ct('nsfw'), |it| it.bool())
38
39 return Post{
40 id: ct('id').int()
41 author_id: ct('author_id').int()
42 replying_to: if c('replying_to') == none { none } else {
43 util.map_or_throw[string, int](ct('replying_to'), |it| it.int())
44 }
45 title: ct('title')
46 body: ct('body')
47 pinned: util.map_or_throw[string, bool](ct('pinned'), |it| it.bool())
48 nsfw: nsfw
49 posted_at: posted_at
50 }
51}