@@ -99,6 +99,17 @@ def put(self, note_id):
99
99
100
100
return {}
101
101
102
+ def delete (self , note_id ):
103
+ children = getResults ("select note_id from notes_tree where note_pid = ?" , [note_id ])
104
+
105
+ for child in children :
106
+ self .delete (child ['note_id' ])
107
+
108
+ delete ("notes_tree" , note_id )
109
+ delete ("notes" , note_id )
110
+
111
+ conn .commit ()
112
+
102
113
api .add_resource (Notes , '/notes/<string:note_id>' )
103
114
104
115
class NotesChildren (Resource ):
@@ -107,7 +118,29 @@ def post(self, parent_note_id):
107
118
108
119
noteId = '' .join (random .SystemRandom ().choice (string .ascii_uppercase + string .digits ) for _ in range (22 ))
109
120
110
- now = math .floor (time .time ());
121
+ if parent_note_id == "root" :
122
+ parent_note_id = ""
123
+
124
+ new_note_pos = 0
125
+
126
+ if note ['target' ] == 'into' :
127
+ res = getSingleResult ('select max(note_pos) as max_note_pos from notes_tree where note_pid = ?' , [parent_note_id ])
128
+ max_note_pos = res ['max_note_pos' ]
129
+
130
+ if max_note_pos is None : # no children yet
131
+ new_note_pos = 0
132
+ else :
133
+ new_note_pos = max_note_pos + 1
134
+ elif note ['target' ] == 'after' :
135
+ after_note = getSingleResult ('select note_pos from notes_tree where note_id = ?' , [note ['target_note_id' ]])
136
+
137
+ new_note_pos = after_note ['note_pos' ] + 1
138
+
139
+ execute ('update notes_tree set note_pos = note_pos + 1 where note_pid = ? and note_pos > ?' , [parent_note_id , after_note ['note_pos' ]])
140
+ else :
141
+ raise Exception ('Unknown target: ' + note ['target' ])
142
+
143
+ now = math .floor (time .time ())
111
144
112
145
insert ("notes" , {
113
146
'note_id' : noteId ,
@@ -120,13 +153,10 @@ def post(self, parent_note_id):
120
153
'is_finished' : 0
121
154
})
122
155
123
- if parent_note_id == "root" :
124
- parent_note_id = ""
125
-
126
156
insert ("notes_tree" , {
127
157
'note_id' : noteId ,
128
158
'note_pid' : parent_note_id ,
129
- 'note_pos' : 0 ,
159
+ 'note_pos' : new_note_pos ,
130
160
'is_expanded' : 0
131
161
})
132
162
@@ -143,15 +173,39 @@ def put(self, note_id, after_note_id):
143
173
after_note = getSingleResult ("select * from notes_tree where note_id = ?" , [after_note_id ])
144
174
145
175
if after_note <> None :
176
+ execute ("update notes_tree set note_pos = note_pos + 1 where note_pid = ? and note_pos > ?" , [after_note ['note_pid' ], after_note ['note_pos' ]])
177
+
146
178
execute ("update notes_tree set note_pid = ?, note_pos = ? where note_id = ?" , [after_note ['note_pid' ], after_note ['note_pos' ] + 1 , note_id ])
147
179
148
180
conn .commit ()
149
181
150
182
api .add_resource (MoveAfterNote , '/notes/<string:note_id>/moveAfter/<string:after_note_id>' )
151
183
184
+ class MoveBeforeNote (Resource ):
185
+ def put (self , note_id , before_note_id ):
186
+ before_note = getSingleResult ("select * from notes_tree where note_id = ?" , [before_note_id ])
187
+
188
+ if before_note <> None :
189
+ execute ("update notes_tree set note_pos = note_pos + 1 where note_id = ?" , [before_note_id ])
190
+
191
+ execute ("update notes_tree set note_pid = ?, note_pos = ? where note_id = ?" , [before_note ['note_pid' ], before_note ['note_pos' ], note_id ])
192
+
193
+ conn .commit ()
194
+
195
+ api .add_resource (MoveBeforeNote , '/notes/<string:note_id>/moveBefore/<string:before_note_id>' )
196
+
152
197
class MoveToNote (Resource ):
153
198
def put (self , note_id , parent_id ):
154
- execute ("update notes_tree set note_pid = ? where note_id = ?" , [parent_id , note_id ])
199
+ res = getSingleResult ('select max(note_pos) as max_note_pos from notes_tree where note_pid = ?' , [parent_id ])
200
+ max_note_pos = res ['max_note_pos' ]
201
+ new_note_pos = 0
202
+
203
+ if max_note_pos is None : # no children yet
204
+ new_note_pos = 0
205
+ else :
206
+ new_note_pos = max_note_pos + 1
207
+
208
+ execute ("update notes_tree set note_pid = ?, note_pos = ? where note_id = ?" , [parent_id , new_note_pos , note_id ])
155
209
156
210
conn .commit ()
157
211
0 commit comments