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 object from the given database row. 22// see src/database/post.v#search_for_posts for usage. 23@[inline] 24pub fn Post.from_row(res pg.Result, row pg.Row) Post { 25 // curry some arguments for cleanliness 26 c := fn [res, row] (key string) ?string { 27 return util.get_row_col(res, row, key) 28 } 29 ct := fn [res, row] (key string) string { 30 return util.get_row_col_or_throw(res, row, key) 31 } 32 33 // this throws a cgen error when put in Post{} 34 //todo: report this 35 posted_at := time.parse(ct('posted_at')) or { panic(err) } 36 37 return Post{ 38 id: ct('id').int() 39 author_id: ct('author_id').int() 40 replying_to: if c('replying_to') == none { none } else { 41 util.map_or_throw[string, int](ct('replying_to'), |it| it.int()) 42 } 43 title: ct('title') 44 body: ct('body') 45 pinned: util.map_or_throw[string, bool](ct('pinned'), |it| it.bool()) 46 posted_at: posted_at 47 } 48}