import { Injectable } from '@angular/core'; import { Storage } from '@ionic/storage-angular'; export interface readerUser { id: number; fio: string; registrationDate: number; } export interface bookIssued { bookId: string; userId: number; date: string; dateTo: string; } export interface userIssue { user: readerUser; issue: bookIssued | undefined; } @Injectable({ providedIn: 'root', }) export class StorageService { private _storage: Storage | null = null; constructor(private storage: Storage) { this.init(); } async init(): Promise { this._storage = await this.storage.create(); if ((await this.getBooksIssued()) === null) { await this.set('booksIssued', []); } if ((await this.getReaderUsers()) === null) { await this.set('readerUsers', []); } } public async set( key: string, value: any, ): Promise | undefined> { return this._storage?.set(key, value); } public async get(key: string): Promise | undefined> { return this._storage?.get(key); } public async remove(key: string): Promise | undefined> { return this._storage?.remove(key); } public async getReaderUsers(): Promise { return (await this._storage?.get('readerUsers')) as readerUser[]; } public async addReaderUser(user: readerUser): Promise { const users = await this.getReaderUsers(); users.push(user); return this.set('readerUsers', users); } public async removeReaderUser(user: readerUser): Promise { const users: readerUser[] = await this.getReaderUsers(); const index: number = users.indexOf(user); if (index > -1) { users.splice(index, 1); return await this.set('readerUsers', users); } } public async searchReaderUser(fio: string): Promise { const users: readerUser[] = await this.getReaderUsers(); return users.find((user: readerUser) => user.fio === fio); } public async searchReaderUserById( id: number, ): Promise { const users: readerUser[] = await this.getReaderUsers(); return users.filter((user: readerUser) => user.id === id)[0]; } public async getBooksIssued(): Promise { return (await this._storage?.get('booksIssued')) as bookIssued[]; } public async getBookIssuedByBookId(bookId: string): Promise { const issueBook = await this.getBooksIssued(); return issueBook.filter((issue: bookIssued) => { return issue.bookId === bookId; }); } public async addBookIssued(issue: bookIssued): Promise { const issueBook = await this.getBooksIssued(); issueBook.push(issue); return this.set('booksIssued', issueBook); } public async getBookIssuedUsers(bookId: string): Promise { const issueBook: bookIssued[] = await this.getBooksIssued(); const book: Array = issueBook.filter( (issue: bookIssued) => issue.bookId === bookId, ); const users: readerUser[] = []; for (let i = 0; i < book.length; i++) { let user = await this.searchReaderUserById(book[i].userId); if (user === undefined || user === null) continue; users.push(user); } var usersIssue: userIssue[] = []; for (let i = 0; i < users.length; i++) { let issue: userIssue = { user: users[i], issue: book.filter( (issue: bookIssued) => issue.userId === users[i].id, )[0], }; usersIssue.push(issue); } return usersIssue; } public async addUser(fio: string) { const users: readerUser[] = await this.getReaderUsers(); const lastuserid = users.length === 0 ? -1 : users[users.length - 1].id; await this.addReaderUser({ fio: fio, id: lastuserid + 1, registrationDate: Date.now(), }); } }