From a2faa116c716a12af55e9921a19b823c45aadba0 Mon Sep 17 00:00:00 2001 From: PARK BYUNG JUN Date: Fri, 5 Aug 2022 08:58:41 +0000 Subject: [PATCH] web-storage is added --- src/app/app.module.ts | 2 + .../core/web-storage/config/module-config.ts | 1 + src/app/core/web-storage/config/token.ts | 5 ++ src/app/core/web-storage/services/index.ts | 9 ++ .../services/web-local-storage.service.ts | 25 ++++++ .../services/web-session-storage.service.ts | 25 ++++++ .../services/web-storage.service.ts | 87 +++++++++++++++++++ .../core/web-storage/web-storage.module.ts | 20 +++++ .../member/user/components/list.component.ts | 5 +- .../member/services/identity.service.ts | 32 ++++++- 10 files changed, 204 insertions(+), 7 deletions(-) create mode 100644 src/app/core/web-storage/config/module-config.ts create mode 100644 src/app/core/web-storage/config/token.ts create mode 100644 src/app/core/web-storage/services/index.ts create mode 100644 src/app/core/web-storage/services/web-local-storage.service.ts create mode 100644 src/app/core/web-storage/services/web-session-storage.service.ts create mode 100644 src/app/core/web-storage/services/web-storage.service.ts create mode 100644 src/app/core/web-storage/web-storage.module.ts diff --git a/src/app/app.module.ts b/src/app/app.module.ts index cc1acfa..02ea0c3 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -17,6 +17,7 @@ import { AppComponent } from 'app/app.component'; import { appRoutes } from 'app/app.routing'; import { NatsModule } from 'app/core/nats/nats.module'; +import { WebStorageModule } from 'app/core/web-storage/web-storage.module'; import { environment } from 'environments/environment'; @@ -50,6 +51,7 @@ const routerConfig: ExtraOptions = { MatIconModule, NatsModule.forRoot(environment.nats), + WebStorageModule.forRoot({}), MemberModule.forRoot(), ], diff --git a/src/app/core/web-storage/config/module-config.ts b/src/app/core/web-storage/config/module-config.ts new file mode 100644 index 0000000..b7d7971 --- /dev/null +++ b/src/app/core/web-storage/config/module-config.ts @@ -0,0 +1 @@ +export interface ModuleConfig {} diff --git a/src/app/core/web-storage/config/token.ts b/src/app/core/web-storage/config/token.ts new file mode 100644 index 0000000..cba1139 --- /dev/null +++ b/src/app/core/web-storage/config/token.ts @@ -0,0 +1,5 @@ +import { InjectionToken } from '@angular/core'; + +export const _MODULE_CONFIG = new InjectionToken( + '@bet/beteran web-storage config of module' +); diff --git a/src/app/core/web-storage/services/index.ts b/src/app/core/web-storage/services/index.ts new file mode 100644 index 0000000..c343c54 --- /dev/null +++ b/src/app/core/web-storage/services/index.ts @@ -0,0 +1,9 @@ +import { Type } from '@angular/core'; + +import { WebLocalStorageService } from './web-local-storage.service'; +import { WebSessionStorageService } from './web-session-storage.service'; + +export const SERVICES: Type[] = [ + WebLocalStorageService, + WebSessionStorageService, +]; diff --git a/src/app/core/web-storage/services/web-local-storage.service.ts b/src/app/core/web-storage/services/web-local-storage.service.ts new file mode 100644 index 0000000..45de16d --- /dev/null +++ b/src/app/core/web-storage/services/web-local-storage.service.ts @@ -0,0 +1,25 @@ +import { Inject, Injectable } from '@angular/core'; + +import { ModuleConfig } from '../config/module-config'; +import { _MODULE_CONFIG } from '../config/token'; +import { WebStorageService } from './web-storage.service'; + +@Injectable({ + providedIn: 'root', +}) +export class WebLocalStorageService extends WebStorageService { + /** + * Constructor + */ + constructor() { + super(localStorage); + } + + // ----------------------------------------------------------------------------------------------------- + // @ Accessors + // ----------------------------------------------------------------------------------------------------- + + // ----------------------------------------------------------------------------------------------------- + // @ Public methods + // ----------------------------------------------------------------------------------------------------- +} diff --git a/src/app/core/web-storage/services/web-session-storage.service.ts b/src/app/core/web-storage/services/web-session-storage.service.ts new file mode 100644 index 0000000..793f88b --- /dev/null +++ b/src/app/core/web-storage/services/web-session-storage.service.ts @@ -0,0 +1,25 @@ +import { Inject, Injectable } from '@angular/core'; + +import { ModuleConfig } from '../config/module-config'; +import { _MODULE_CONFIG } from '../config/token'; +import { WebStorageService } from './web-storage.service'; + +@Injectable({ + providedIn: 'root', +}) +export class WebSessionStorageService extends WebStorageService { + /** + * Constructor + */ + constructor() { + super(sessionStorage); + } + + // ----------------------------------------------------------------------------------------------------- + // @ Accessors + // ----------------------------------------------------------------------------------------------------- + + // ----------------------------------------------------------------------------------------------------- + // @ Public methods + // ----------------------------------------------------------------------------------------------------- +} diff --git a/src/app/core/web-storage/services/web-storage.service.ts b/src/app/core/web-storage/services/web-storage.service.ts new file mode 100644 index 0000000..c963608 --- /dev/null +++ b/src/app/core/web-storage/services/web-storage.service.ts @@ -0,0 +1,87 @@ +import { BehaviorSubject, Observable } from 'rxjs'; + +import { ModuleConfig } from '../config/module-config'; +import { _MODULE_CONFIG } from '../config/token'; + +export abstract class WebStorageService { + private __storage: Storage; + private __subjects: Map>; + + /** + * Constructor + */ + constructor(storage: Storage) { + this.__storage = storage; + this.__subjects = new Map>(); + } + + // ----------------------------------------------------------------------------------------------------- + // @ Accessors + // ----------------------------------------------------------------------------------------------------- + + // ----------------------------------------------------------------------------------------------------- + // @ Public methods + // ----------------------------------------------------------------------------------------------------- + /** + * watch data of given key + * @param key + * @param defaultValue + */ + watch(key: string, defaultValue: any = null): Observable { + if (!this.__subjects.has(key)) { + this.__subjects.set(key, new BehaviorSubject(defaultValue)); + } + let item = this.__storage.getItem(key); + if (item !== null) { + item = JSON.parse(item); + } + (this.__subjects.get(key) as BehaviorSubject).next(item); + return (this.__subjects.get(key) as BehaviorSubject).asObservable(); + } + + /** + * get data of given key + * @param key + */ + get(key: string): any { + let item = this.__storage.getItem(key); + if (item !== null) { + item = JSON.parse(item); + } + return item; + } + + /** + * set value on given key + * @param key + * @param value + */ + set(key: string, value: any) { + this.__storage.setItem(key, JSON.stringify(value)); + if (!this.__subjects.has(key)) { + this.__subjects.set(key, new BehaviorSubject(value)); + } else { + (this.__subjects.get(key) as BehaviorSubject).next(value); + } + } + + /** + * remove given key + * @param key + */ + remove(key: string) { + if (this.__subjects.has(key)) { + (this.__subjects.get(key) as BehaviorSubject).complete(); + this.__subjects.delete(key); + } + this.__storage.removeItem(key); + } + + /** + * clear all available keys + */ + clear() { + this.__subjects.clear(); + this.__storage.clear(); + } +} diff --git a/src/app/core/web-storage/web-storage.module.ts b/src/app/core/web-storage/web-storage.module.ts new file mode 100644 index 0000000..e3be4d6 --- /dev/null +++ b/src/app/core/web-storage/web-storage.module.ts @@ -0,0 +1,20 @@ +import { ModuleWithProviders, NgModule } from '@angular/core'; +import { ModuleConfig } from './config/module-config'; +import { _MODULE_CONFIG } from './config/token'; + +import { SERVICES } from './services'; + +@NgModule({}) +export class WebStorageRootModule {} + +@NgModule({}) +export class WebStorageModule { + public static forRoot( + config: ModuleConfig + ): ModuleWithProviders { + return { + ngModule: WebStorageRootModule, + providers: [{ provide: _MODULE_CONFIG, useValue: config }, ...SERVICES], + }; + } +} diff --git a/src/app/modules/admin/member/user/components/list.component.ts b/src/app/modules/admin/member/user/components/list.component.ts index 81edd10..e06e603 100644 --- a/src/app/modules/admin/member/user/components/list.component.ts +++ b/src/app/modules/admin/member/user/components/list.component.ts @@ -198,10 +198,7 @@ export class ListComponent implements OnInit, AfterViewInit, OnDestroy { __onClickSearch(): void { this.__isSearchOpened = !this.__isSearchOpened; - console.log( - 'kkkk', - this._identityService.checkUsernameForDuplication('sdfsdfsdfsf') - ); + console.log('kkkk', this._identityService.captcha()); } /** diff --git a/src/app/modules/polyglot/member/services/identity.service.ts b/src/app/modules/polyglot/member/services/identity.service.ts index 158bc12..086efa3 100644 --- a/src/app/modules/polyglot/member/services/identity.service.ts +++ b/src/app/modules/polyglot/member/services/identity.service.ts @@ -2,11 +2,18 @@ import { Injectable } from '@angular/core'; import { NatsService } from 'app/core/nats/services/nats.service'; import * as nats from 'nats.ws'; +import { Error } from 'app/modules/protobuf/protobuf/rpc/error_pb'; + import { CheckUsernameForDuplicationRequest, CheckUsernameForDuplicationResponse, + CaptchaRequest, + CaptchaResponse, } from 'app/modules/protobuf/c2se/common/identity_pb'; -import { SUBJECT_CHECK_USERNAME_FOR_DUPLICATION } from 'app/modules/protobuf/c2se/backend/identity_pb'; +import { + SUBJECT_CHECK_USERNAME_FOR_DUPLICATION, + SUBJECT_CAPTCHA, +} from 'app/modules/protobuf/c2se/backend/identity_pb'; @Injectable({ providedIn: 'root', @@ -39,8 +46,27 @@ export class IdentityService { .then((result) => { console.log('success', result, result.getDuplicated()); }) - .catch((e) => { - console.log('failed', e); + .catch((e: Error) => { + console.log('failed', e.getCode(), e.getMessage(), e.getData()); + }); + }); + } + + captcha(): Promise { + return new Promise((resolve, reject) => { + let req = new CaptchaRequest(); + + this.__natsService + .request( + SUBJECT_CAPTCHA, + req.serializeBinary(), + CaptchaResponse.deserializeBinary + ) + .then((result) => { + console.log('success', result, result.getToken(), result.getImage()); + }) + .catch((e: Error) => { + console.log('failed', e.getCode(), e.getMessage(), e.getData()); }); }); }