@@ -124,6 +124,119 @@ export async function createPasswordItem(
124124 return newPasswordItem ;
125125}
126126
127+ export async function updateNoteItem (
128+ id : string ,
129+ newTitle : string ,
130+ newContent : string ,
131+ titleIV : string ,
132+ contentIV : string
133+ ) {
134+ const { userId } = await auth ( )
135+
136+ if ( ! userId ) {
137+ throw new Error ( "Not authenticated" ) ;
138+ }
139+
140+ const user = await prismadb . user . findUnique ( {
141+ where : {
142+ id : userId ,
143+ } ,
144+ include : {
145+ noteItems : true ,
146+ } ,
147+ } ) ;
148+
149+ if ( ! user ) {
150+ throw new Error ( "User not found" ) ;
151+ }
152+
153+ const noteItem = user . noteItems . find ( ( item ) => item . id === id ) ;
154+
155+ if ( ! noteItem ) {
156+ throw new Error ( "Note item not found" ) ;
157+ }
158+
159+ const item = await prismadb . noteItem . update ( {
160+ where : {
161+ id : noteItem . id ,
162+ } ,
163+ data : {
164+ title : newTitle ,
165+ content : newContent ,
166+ titleIV,
167+ contentIV,
168+ } ,
169+ } ) ;
170+
171+ return item ;
172+ }
173+
174+ export async function deleteNoteItem ( id : string ) {
175+ const { userId } = await auth ( )
176+
177+ if ( ! userId ) {
178+ throw new Error ( "Not authenticated" ) ;
179+ }
180+
181+ const user = await prismadb . user . findUnique ( {
182+ where : {
183+ id : userId ,
184+ } ,
185+ include : {
186+ noteItems : true ,
187+ } ,
188+ } ) ;
189+
190+ if ( ! user ) {
191+ throw new Error ( "User not found" ) ;
192+ }
193+
194+ const noteItem = user . noteItems . find ( ( item ) => item . id === id ) ;
195+
196+ if ( ! noteItem ) {
197+ throw new Error ( "Note item not found" ) ;
198+ }
199+
200+ await prismadb . noteItem . delete ( {
201+ where : {
202+ id : noteItem . id ,
203+ } ,
204+ } ) ;
205+
206+ return { success : true } ;
207+ }
208+
209+ export async function createNoteItem (
210+ title : string ,
211+ content : string ,
212+ titleIV : string ,
213+ contentIV : string
214+ ) {
215+ const { userId } = await auth ( )
216+
217+ if ( ! userId ) {
218+ throw new Error ( "Not authenticated" ) ;
219+ }
220+
221+ const newNoteItem = await prismadb . noteItem . create ( {
222+ data : {
223+ title,
224+ content,
225+ titleIV,
226+ contentIV,
227+ updatedAt : new Date ( ) . toISOString ( ) ,
228+ createdAt : new Date ( ) . toISOString ( ) ,
229+ user : {
230+ connect : {
231+ id : userId
232+ }
233+ }
234+ } ,
235+ } ) ;
236+
237+ return newNoteItem ;
238+ }
239+
127240export async function resetVault ( ) {
128241 const { userId } = await auth ( )
129242
@@ -186,7 +299,7 @@ export async function instantiateVault(userId: string, username: string) {
186299 return vault ;
187300}
188301
189- export async function getPasswords ( ) {
302+ export async function getItems ( ) {
190303 const { userId } = await auth ( )
191304
192305 if ( ! userId ) {
@@ -215,3 +328,4 @@ export async function getPasswords() {
215328 } ,
216329 } ) ;
217330}
331+
0 commit comments