Serge Bazanski | 5e695e8 | 2021-03-07 14:29:40 +0000 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "fmt" |
| 7 | "time" |
| 8 | |
| 9 | _ "github.com/lib/pq" |
| 10 | ) |
| 11 | |
| 12 | // app is the model of the oodviewer app. |
| 13 | // The data modeled is a K/V map from string ('Term') to list of entries. |
| 14 | type app struct { |
| 15 | db *sql.DB |
| 16 | } |
| 17 | |
| 18 | // term represents a key in the K/V map of the model. |
| 19 | type term struct { |
| 20 | // Name of the term, the 'K' of the K/V map. |
| 21 | Name string |
| 22 | // Count of entries (len(V) of the K/V map). |
| 23 | Entries uint64 |
| 24 | } |
| 25 | |
| 26 | // entry is an element contained under a term. A list of entries ([]entry) is |
| 27 | // the 'V' of the K/V map. |
| 28 | type entry struct { |
| 29 | Entry string `json:"entry"` |
| 30 | Added int64 `json:"added"` |
| 31 | Author string `json:"author"` |
| 32 | } |
| 33 | |
| 34 | // newApp returns an instantiated app given a lib/pq postgres connection |
| 35 | // string. |
| 36 | func newApp(postgres string) (*app, error) { |
| 37 | db, err := sql.Open("postgres", flagPostgres) |
| 38 | if err != nil { |
| 39 | return nil, fmt.Errorf("Open: %v", err) |
| 40 | } |
| 41 | |
| 42 | return &app{ |
| 43 | db: db, |
| 44 | }, nil |
| 45 | } |
| 46 | |
| 47 | // getTerms returns all terms stored in the database. |
| 48 | func (a *app) getTerms(ctx context.Context) ([]term, error) { |
| 49 | rows, err := a.db.QueryContext(ctx, ` |
| 50 | SELECT |
| 51 | _term._name, |
| 52 | count(_entry._text) |
| 53 | FROM |
| 54 | _term |
| 55 | LEFT JOIN _entry |
| 56 | ON |
| 57 | _entry._term_oid = _term._oid |
| 58 | GROUP BY _term._oid |
| 59 | ORDER BY _term._name |
| 60 | `) |
| 61 | if err != nil { |
| 62 | return nil, err |
| 63 | } |
| 64 | var res []term |
| 65 | for rows.Next() { |
| 66 | var name string |
| 67 | var count uint64 |
| 68 | if err := rows.Scan(&name, &count); err != nil { |
| 69 | return nil, err |
| 70 | } |
| 71 | res = append(res, term{ |
| 72 | Name: name, |
| 73 | Entries: count, |
| 74 | }) |
| 75 | } |
| 76 | if err := rows.Err(); err != nil { |
| 77 | return nil, err |
| 78 | } |
| 79 | return res, err |
| 80 | } |
| 81 | |
| 82 | // getEntries returns all entries of a given term stored in the database. |
| 83 | func (a *app) getEntries(ctx context.Context, name string) ([]entry, error) { |
| 84 | rows, err := a.db.QueryContext(ctx, ` |
| 85 | SELECT |
| 86 | _entry._text, |
| 87 | _entry._added_at, |
| 88 | _entry._added_by |
| 89 | FROM |
| 90 | _term |
| 91 | LEFT JOIN _entry |
| 92 | ON _entry._term_oid = _term._oid |
| 93 | WHERE lower(_term._name) = lower($1) |
| 94 | ORDER BY _entry._added_at |
| 95 | `, name) |
| 96 | if err != nil { |
| 97 | return nil, err |
| 98 | } |
| 99 | var res []entry |
| 100 | for rows.Next() { |
| 101 | var text string |
| 102 | var added time.Time |
| 103 | var author string |
| 104 | if err := rows.Scan(&text, &added, &author); err != nil { |
| 105 | return nil, err |
| 106 | } |
| 107 | res = append(res, entry{ |
| 108 | Entry: text, |
| 109 | Added: added.Unix(), |
| 110 | Author: author, |
| 111 | }) |
| 112 | } |
| 113 | if err := rows.Err(); err != nil { |
| 114 | return nil, err |
| 115 | } |
| 116 | return res, err |
| 117 | } |