138 lines
3.8 KiB
TypeScript
138 lines
3.8 KiB
TypeScript
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<void> {
|
|
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<Promise<void> | undefined> {
|
|
return this._storage?.set(key, value);
|
|
}
|
|
|
|
public async get(key: string): Promise<Promise<any> | undefined> {
|
|
return this._storage?.get(key);
|
|
}
|
|
|
|
public async remove(key: string): Promise<Promise<void> | undefined> {
|
|
return this._storage?.remove(key);
|
|
}
|
|
|
|
public async getReaderUsers(): Promise<readerUser[]> {
|
|
return (await this._storage?.get('readerUsers')) as readerUser[];
|
|
}
|
|
public async addReaderUser(user: readerUser): Promise<void | undefined> {
|
|
const users = await this.getReaderUsers();
|
|
users.push(user);
|
|
return this.set('readerUsers', users);
|
|
}
|
|
|
|
public async removeReaderUser(user: readerUser): Promise<void | undefined> {
|
|
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<readerUser | undefined> {
|
|
const users: readerUser[] = await this.getReaderUsers();
|
|
return users.find((user: readerUser) => user.fio === fio);
|
|
}
|
|
|
|
public async searchReaderUserById(
|
|
id: number,
|
|
): Promise<readerUser | undefined> {
|
|
const users: readerUser[] = await this.getReaderUsers();
|
|
return users.filter((user: readerUser) => user.id === id)[0];
|
|
}
|
|
|
|
public async getBooksIssued(): Promise<bookIssued[]> {
|
|
return (await this._storage?.get('booksIssued')) as bookIssued[];
|
|
}
|
|
|
|
public async getBookIssuedByBookId(bookId: string): Promise<bookIssued[]> {
|
|
const issueBook = await this.getBooksIssued();
|
|
return issueBook.filter((issue: bookIssued) => {
|
|
return issue.bookId === bookId;
|
|
});
|
|
}
|
|
|
|
public async addBookIssued(issue: bookIssued): Promise<void | undefined> {
|
|
const issueBook = await this.getBooksIssued();
|
|
issueBook.push(issue);
|
|
return this.set('booksIssued', issueBook);
|
|
}
|
|
|
|
public async getBookIssuedUsers(bookId: string): Promise<userIssue[]> {
|
|
const issueBook: bookIssued[] = await this.getBooksIssued();
|
|
const book: Array<bookIssued> = 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(),
|
|
});
|
|
}
|
|
}
|