-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathline.go
More file actions
74 lines (60 loc) · 1.48 KB
/
line.go
File metadata and controls
74 lines (60 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package treksum
import (
"fmt"
"strings"
"github.com/jackc/pgx"
"go.uber.org/zap"
)
const (
CREATE_LINE_TABLE = `
create table "line" (
"id" serial primary key,
"episode_id" int,
"speaker_id" int,
"line" text,
foreign key ("episode_id") references "episode" ("id"),
foreign key ("speaker_id") references "speaker" ("id")
)
`
INSERT_LINE = `
insert into "line"
("episode_id", "speaker_id", "line")
values
($1, $2, $3)
returning ("id")
`
)
type Line struct {
ID int64 `json:"id"`
Episode *Episode `json:"episode,omitempty"`
Speaker string `json:"speaker"`
Line string `json:"line"`
log *zap.Logger
}
func NewLine(log *zap.Logger, speaker, text string) (l *Line) {
l = &Line{
Speaker: speaker,
log: log,
}
l.AddText(text)
return l
}
// AddText appends text to existing text in a line from the transcript.
func (this *Line) AddText(text string) {
this.Line = strings.TrimSpace(fmt.Sprintf("%s %s", this.Line, text))
}
func (this *Line) String() string {
return fmt.Sprintf("%s: %s", this.Speaker, this.Line)
}
// Save persists the line to the database;
func (this *Line) Save(tx *pgx.Tx) (err error) {
speaker := NewSpeaker(this.Episode.Series, this.Speaker)
if err = speaker.Save(tx); err != nil {
this.log.Warn("failed to save speaker", zap.Error(err))
}
err = tx.QueryRow(INSERT_LINE, this.Episode.ID, speaker.ID, this.Line).Scan(&this.ID)
if err != nil {
this.log.Warn("failed to save line", zap.Error(err))
}
return
}