/
notes.go
190 lines (170 loc) · 5.52 KB
/
notes.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package api
import (
"net/http"
"time"
)
const (
NoteTypeJournal = "journal"
NoteTypeProject = "project"
NoteVisibilityPrivate = "private"
NoteVisibilityAdmins = "admins"
)
// Note represents a note that a user can enter into the system, often as a part of a training or project
type Note struct {
ID int64 `json:"id" db:"id"`
UserID int64 `json:"userId" db:"userId"`
CreatedOn string `json:"createdOn" db:"createdOn"`
NoteType string `json:"noteType" db:"noteType"`
ProjectID int64 `json:"projectId" db:"projectId"`
ModuleID int64 `json:"moduleId" db:"moduleId"`
BlockID int64 `json:"blockId" db:"blockId"`
Visibility string `json:"visibility" db:"visibility"`
Title string `json:"title" db:"title"`
Body string `json:"body" db:"body"`
// needed for the gets
ProjectName string `json:"projectName" db:"projectName"`
ModuleName string `json:"moduleName" db:"moduleName"`
BlockName string `json:"blockName" db:"blockName"`
}
// NoteSelectOptions is a helper to store all the possible combinations of values in a selector rather than
// building a whole bunch of strings or funcs
type NoteSelectOptions struct {
NoteType string `json:"noteType"`
ProjectID int64 `json:"projectId"`
ModuleID int64 `json:"moduleId"`
BlockID int64 `json:"blockId"`
}
// CreateNote creates a new note
func CreateNote(input *Note) error {
input.processForDB()
defer input.processForAPI()
res, err := config.DBConnection.NamedExec(`INSERT INTO Notes SET
userId = :userId,
createdOn = :createdOn,
noteType = :noteType,
projectId = :projectId,
moduleId = :moduleId,
blockId = :blockId,
visibility = :visibility,
title = :title,
body = :body`, input)
if err != nil {
return err
}
input.ID, _ = res.LastInsertId()
return nil
}
// UpdateNote updates a single note
func UpdateNote(input *Note) error {
input.processForDB()
defer input.processForAPI()
_, err := config.DBConnection.NamedExec(`UPDATE Notes SET
userId = :userId,
createdOn = :createdOn,
noteType = :noteType,
projectId = :projectId,
moduleId = :moduleId,
blockId = :blockId,
visibility = :visibility,
title = :title,
body = :body
WHERE id = :id`, input)
return err
}
// GetNote gets a single note by its id
func GetNote(noteID int64) (*Note, error) {
note := &Note{}
defer note.processForAPI()
err := config.DBConnection.Get(note, `SELECT * FROM Notes WHERE id = ?`, noteID)
return note, err
}
// GetAllNotesForUser gets non-project notes for a user
func GetAllNotesForUser(userID int64, noteType string, filter *NoteSelectOptions) ([]Note, error) {
notes := []Note{}
filter.process()
var err error
if noteType == "all" || noteType == "" {
err = config.DBConnection.Select(¬es, `SELECT n.*,
IFNULL(p.name, '') AS projectName,
IFNULL(m.name, '') AS moduleName,
IFNULL(b.name, '') AS blockName
FROM Notes n
LEFT JOIN Projects p ON n.projectId = p.id
LEFT JOIN Modules m ON n.moduleId = m.id
LEFT JOIN Blocks b ON n.blockId = b.id
WHERE n.userId = ?
ORDER BY createdOn DESC`, userID)
} else {
err = config.DBConnection.Select(¬es, `SELECT n.*,
IFNULL(p.name, '') AS projectName,
IFNULL(m.name, '') AS moduleName,
IFNULL(b.name, '') AS blockName
FROM Notes n
LEFT JOIN Projects p ON n.projectId = p.id
LEFT JOIN Modules m ON n.moduleId = m.id
LEFT JOIN Blocks b ON n.blockId = b.id
WHERE n.userId = ? AND n.noteType = ? AND n.projectId = ? AND n.moduleId = ? AND n.blockId = ?
ORDER BY createdOn DESC;`, userID, noteType, filter.ProjectID, filter.ModuleID, filter.BlockID)
}
for i := range notes {
notes[i].processForAPI()
}
return notes, err
}
// DeleteNoteByID deletes a single note
func DeleteNoteByID(noteID int64) error {
_, err := config.DBConnection.Exec(`DELETE FROM Notes WHERE id = ?`, noteID)
return err
}
// DeleteNotesForUser deletes all notes tied to a user
func DeleteNotesForUser(userID int64) error {
_, err := config.DBConnection.Exec(`DELETE FROM Notes WHERE userID = ?`, userID)
return err
}
// DeleteJournalNotesForUser deletes all journal notes for a user
func DeleteJournalNotesForUser(userID int64) error {
_, err := config.DBConnection.Exec(`DELETE FROM Notes WHERE userID = ? AND noteType = 'journal'`, userID)
return err
}
// DeleteProjectNotesForUser deletes the notes for a user tied to a specific project
func DeleteProjectNotesForUser(userID, projectID int64) error {
_, err := config.DBConnection.Exec(`DELETE FROM Notes WHERE userId = ? AND projectId = ?`, userID, projectID)
return err
}
// DeleteProjectNotesForProject deletes all notes on a project; should only be called if a project is deleted
func DeleteProjectNotesForProject(projectID int64) error {
_, err := config.DBConnection.Exec(`DELETE FROM Notes WHERE projectId = ?`, projectID)
return err
}
func (input *NoteSelectOptions) process() {
if input.NoteType == NoteTypeJournal {
input.ProjectID = 0
input.ModuleID = 0
input.BlockID = 0
}
}
func (input *Note) processForDB() {
if input.CreatedOn == "" {
input.CreatedOn = time.Now().Format(timeFormatDB)
} else {
input.CreatedOn, _ = parseTimeToTimeFormat(input.CreatedOn, timeFormatDB)
}
if input.NoteType == "" {
input.NoteType = NoteTypeJournal
}
if input.Visibility == "" {
input.Visibility = NoteVisibilityPrivate
}
if input.ProjectID == 0 {
input.ModuleID = 0
input.BlockID = 0
input.NoteType = NoteTypeJournal
}
}
func (input *Note) processForAPI() {
input.CreatedOn, _ = parseTimeToTimeFormat(input.CreatedOn, timeFormatAPI)
}
// Bind binds the data for the HTTP
func (data *Note) Bind(r *http.Request) error {
return nil
}