forked from anmil/quicknote
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnote.go
130 lines (112 loc) · 2.75 KB
/
note.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
// Quicknote stores and searches tens of thousands of short notes.
//
// Copyright (C) 2017 Andrew Miller <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package quicknote
import (
"encoding/json"
"fmt"
"strings"
"time"
)
// Note types
var (
Basic = "basic"
URL = "url"
NoteTypes = []string{
Basic,
URL,
}
)
// Always a good idea to have a upper limit
const MaxStringLen = 131070
// Note is our main struct for storing
// notes and their meta data.
type Note struct {
ID int64
Created time.Time
Modified time.Time
Type string
Title string
Body string
Book *Book
Tags []*Tag
}
// NewNote returns a new Note
func NewNote() *Note {
return &Note{}
}
func (n *Note) String() string {
bk := ""
if n.Book != nil {
bk = n.Book.Name
}
tags := ""
if len(n.Tags) > 0 {
tags = strings.Join(n.GetTagStringArray(), ", ")
}
return fmt.Sprintf("<Note ID: %d Title: %s Book: %s Tags: %s>", n.ID, n.Title, bk, tags)
}
// GetTagStringArray returns a list of the note's tag names
func (n *Note) GetTagStringArray() []string {
tags := make([]string, 0, len(n.Tags))
for _, tag := range n.Tags {
tags = append(tags, tag.Name)
}
return tags
}
func (n *Note) GetTagIDsArray() []int64 {
ids := make([]int64, len(n.Tags))
for idx, tag := range n.Tags {
ids[idx] = tag.ID
}
return ids
}
// MarshalJSON customer json Marshaler
func (n *Note) MarshalJSON() ([]byte, error) {
tags := make([]string, len(n.Tags))
for idx, tag := range n.Tags {
tags[idx] = tag.Name
}
return json.Marshal(&struct {
ID int64 `json:"id"`
Created time.Time `json:"created"`
Modified time.Time `json:"modified"`
Type string `json:"type"`
Title string `json:"title"`
Body string `json:"body"`
Book string `json:"book"`
Tags []string `json:"tags"`
}{
ID: n.ID,
Created: n.Created,
Modified: n.Modified,
Type: n.Type,
Title: n.Title,
Body: n.Body,
Book: n.Book.Name,
Tags: tags,
})
}
type Notes []*Note
func (n Notes) Len() int {
return len(n)
}
func (n Notes) Less(i, j int) bool {
return n[i].ID < n[j].ID
}
func (n Notes) Swap(i, j int) {
n[i], n[j] = n[j], n[i]
}