import { Injectable } from '@angular/core' ; import Dexie from 'dexie' ; import { environment } from '@env/environment' ; import { IQuestion } from '@app/core/interfaces' ; class ZCEDatabase extends Dexie { public questionTable ! : Dexie . Table < IQuestion , number > ; constructor ( ) { super ( 'PHP_ZCE_DB' ) ; const questionTable = '++id,type,*category,difficulty,*questionRows,*answerRows,_version' ; this . version ( 1 ) . stores ( { questionTable } ) ; } } @ Injectable ( { providedIn : 'root' } ) export class IndexedDbQuizService { private db : ZCEDatabase ; constructor ( ) { this . db = new ZCEDatabase ( ) ; } addQuestion ( question ) { question . _version = environment . appVersion ; delete question . _isValidated ; [ 0 , 1 , 2 , 3 ] . forEach ( ( value , index , array ) => { delete question . answerRows [ index ] . _isCheckedByUser ; } ) ; return this . db . questionTable . add ( question ) ; } getQuestionById ( id ) { return this . db . questionTable . get ( id ) ; } async clearQuestionTableByVersion ( ) { const promise = this . db . questionTable . where ( '_version' ) . notEqual ( environment . appVersion ) . delete ( ) ; return await promise ; } clearQuestionTable ( ) { return this . db . questionTable . clear ( ) ; } updateQuestion ( question ) { return this . db . questionTable . put ( question ) ; } deleteQuestion ( id ) { return this . db . questionTable . delete ( id ) ; } getAllQuestions ( ) { return this . db . questionTable . toArray ( ) ; } }