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
18 posted_at time.Time = time.now()
19}
20
21// Post.from_row creates a post from the given database row.
22// see src/database/post.v#search_for_posts for usage.
23@[inline]
24pub fn Post.from_row(row pg.Row) Post {
25 // this throws a cgen error when put in Post{}
26 //todo: report this
27 posted_at := time.parse(util.or_throw[string](row.vals[6])) or { panic(err) }
28
29 return Post{
30 id: util.or_throw[string](row.vals[0]).int()
31 author_id: util.or_throw[string](row.vals[1]).int()
32 replying_to: if row.vals[2] == none { ?int(none) } else {
33 util.map_or_throw[string, int](row.vals[2], |it| it.int())
34 }
35 title: util.or_throw[string](row.vals[3])
36 body: util.or_throw[string](row.vals[4])
37 pinned: util.map_or_throw[string, bool](row.vals[5], |it| it.bool())
38 posted_at: posted_at
39 }
40}