@@ -2,7 +2,7 @@ import { openDB, STORES } from "@/db/db";
22import type { Quote } from "@/model/quote.model" ;
33
44const STORE_NAME = STORES . QUOTES ;
5-
5+
66export const getAllQuotes = async ( ) : Promise < Quote [ ] > => {
77 const db = await openDB ( ) ;
88 const transaction = db . transaction ( STORE_NAME , 'readonly' ) ;
@@ -26,30 +26,30 @@ export const getAllQuote = async (quoteId: number): Promise<Quote> => {
2626} ;
2727
2828
29- export const addQuote = async (
30- quote : Quote
31- ) : Promise < Quote > => {
32- const db = await openDB ( )
33- const tx = db . transaction ( STORE_NAME , 'readwrite' )
34- const store = tx . objectStore ( STORE_NAME )
35-
36- return new Promise ( async ( resolve , reject ) => {
37- const { id, ...data } = quote
38- const req = store . add ( data )
29+ export const addQuote = async ( quote : Quote ) : Promise < Quote > => {
30+ const db = await openDB ( ) ;
31+ const tx = db . transaction ( STORE_NAME , 'readwrite' ) ;
32+ const store = tx . objectStore ( STORE_NAME ) ;
3933
40- req . onsuccess = async ( ) => {
41- console . log ( "resol" , quote , req . result )
42- await tx . done
34+ const promise = new Promise < Quote > ( ( resolve , reject ) => {
35+ // IMPORTANT: remove id before add
36+ const { id, ...data } = quote ;
37+ const req = store . add ( {
38+ ...data ,
39+ created_at : new Date ( ) ,
40+ updated_at : new Date ( ) ,
41+ } ) ;
42+ req . onsuccess = ( ) => {
4343 resolve ( {
4444 ...quote ,
45- id : req . result as number ,
46- } )
47- }
48-
49- req . onerror = ( ) => reject ( req . error )
50- } )
51- }
45+ id : req . result as number , // ✅ GENERATED ID
46+ } ) ;
47+ } ;
48+ req . onerror = ( ) => reject ( req . error ) ;
49+ } ) ;
5250
51+ return promise
52+ } ;
5353
5454
5555
@@ -62,18 +62,20 @@ export const updateQuote = async (quote: Quote): Promise<Quote> => {
6262 const tx = db . transaction ( STORE_NAME , 'readwrite' ) ;
6363 const store = tx . objectStore ( STORE_NAME ) ;
6464
65- return new Promise ( async ( resolve , reject ) => {
66- const { id, ...data } = quote
67- const req = store . put ( quote )
68-
65+ const promise = new Promise < Quote > ( async ( resolve , reject ) => {
66+ const req = store . put ( {
67+ ...quote ,
68+ updated_at : new Date ( ) ,
69+ } )
6970 req . onsuccess = async ( ) => {
70- console . log ( "resol" , quote , req . result )
71- await tx . done
72- resolve ( quote )
71+ resolve ( {
72+ ... quote ,
73+ } )
7374 }
74-
7575 req . onerror = ( ) => reject ( req . error )
7676 } )
77+
78+ return promise
7779} ;
7880
7981
0 commit comments