/
tx_presentation.go
76 lines (62 loc) · 1.72 KB
/
tx_presentation.go
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
75
76
package persistence
import (
"context"
"encoding/json"
"github.com/aleale2121/interactive-presentation/internal/constant/model"
"github.com/google/uuid"
)
func (store *SQLStore) GetCurrentPoll(ctx context.Context, id uuid.UUID) (model.CurrentPoll, error) {
var result model.CurrentPoll
err := store.execTx(ctx, func(q *Queries) error {
poll, err := q.GetPoll(ctx, id)
if err != nil {
return err
}
result.ID = poll.ID.String()
result.Question = poll.Question
var optionRows []model.Option
err = json.Unmarshal(poll.Options, &optionRows)
if err != nil {
return err
}
result.Options = optionRows
return err
})
return result, err
}
func (store *SQLStore) UpdateCurrentPollToForwardTx(ctx context.Context, id uuid.UUID) (model.CurrentPoll, error) {
var result model.CurrentPoll
err := store.execTx(ctx, func(q *Queries) error {
// Update the current poll index for the presentation.
nextPoll, err := q.GetNextPoll(ctx, id)
if err != nil {
return err
}
result.ID = nextPoll.ID.String()
result.Question = nextPoll.Question
err = json.Unmarshal(nextPoll.Options, &result.Options)
if err != nil {
return err
}
return err
})
return result, err
}
func (store *SQLStore) UpdateCurrentPollToPreviousTx(ctx context.Context, id uuid.UUID) (model.CurrentPoll, error) {
var result model.CurrentPoll
err := store.execTx(ctx, func(q *Queries) error {
// Update the current poll index for the presentation.
nextPoll, err := q.GetPreviousPoll(ctx, id)
if err != nil {
return err
}
result.ID = nextPoll.ID.String()
result.Question = nextPoll.Question
err = json.Unmarshal(nextPoll.Options, &result.Options)
if err != nil {
return err
}
return err
})
return result, err
}