-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexitsurveyresponses.go
105 lines (83 loc) · 2.67 KB
/
exitsurveyresponses.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package database
import (
"context"
_ "embed"
"github.com/jackc/pgx/v4/pgxpool"
)
type ExitSurveyResponse struct {
GuildId uint64 `json:"guild_id,string"`
TicketId int `json:"ticket_id"`
Responses []QuestionResponse `json:"responses"`
}
type QuestionResponse struct {
QuestionId *int `json:"question_id"`
Question *string `json:"question"`
Response string `json:"response"`
}
type ExitSurveyResponses struct {
*pgxpool.Pool
}
func newExitSurveyResponses(db *pgxpool.Pool) *ExitSurveyResponses {
return &ExitSurveyResponses{
db,
}
}
var (
//go:embed sql/exit_survey_responses/schema.sql
exitSurveyResponsesSchema string
//go:embed sql/exit_survey_responses/add_responses.sql
exitSurveyResponsesAdd string
//go:embed sql/exit_survey_responses/get_response_single.sql
exitSurveyResponsesGetSingle string
//go:embed sql/exit_survey_responses/is_form_in_use.sql
exitSurveyResponsesIsInUse string
//go:embed sql/exit_survey_responses/has_response.sql
exitSurveyHasResponse string
)
func (e *ExitSurveyResponses) Schema() string {
return exitSurveyResponsesSchema
}
func (e *ExitSurveyResponses) AddResponses(ctx context.Context, guildId uint64, ticketId int, formId int, responses map[int]string) error {
ctx, cancel := context.WithTimeout(ctx, defaultTransactionTimeout)
defer cancel()
tx, err := e.Begin(ctx)
if err != nil {
return err
}
for questionId, response := range responses {
_, err = tx.Exec(ctx, exitSurveyResponsesAdd, guildId, ticketId, formId, questionId, response)
if err != nil {
return err
}
}
return tx.Commit(ctx)
}
func (e *ExitSurveyResponses) GetResponses(ctx context.Context, guildId uint64, ticketId int) (ExitSurveyResponse, error) {
rows, err := e.Query(ctx, exitSurveyResponsesGetSingle, guildId, ticketId)
if err != nil {
return ExitSurveyResponse{}, err
}
var responses []QuestionResponse
for rows.Next() {
var response QuestionResponse
if err := rows.Scan(&response.QuestionId, &response.Question, &response.Response); err != nil {
return ExitSurveyResponse{}, err
}
responses = append(responses, response)
}
return ExitSurveyResponse{
GuildId: guildId,
TicketId: ticketId,
Responses: responses,
}, nil
}
func (e *ExitSurveyResponses) IsFormInUse(ctx context.Context, guildId uint64, formId int) (bool, error) {
var inUse bool
err := e.QueryRow(ctx, exitSurveyResponsesIsInUse, guildId, formId).Scan(&inUse)
return inUse, err
}
func (e *ExitSurveyResponses) HasResponse(ctx context.Context, guildId uint64, formId int) (bool, error) {
var hasResponse bool
err := e.QueryRow(ctx, exitSurveyHasResponse, guildId, formId).Scan(&hasResponse)
return hasResponse, err
}