diff --git a/src/main/angular/src/app/api/UUID.ts b/src/main/angular/src/app/api/UUID.ts new file mode 100644 index 0000000..403bafe --- /dev/null +++ b/src/main/angular/src/app/api/UUID.ts @@ -0,0 +1,25 @@ +import {CrudService} from "./tools/shopping/crudService"; +import {Subscription} from "rxjs"; + +export abstract class UUID> { + + private sub?: Subscription; + + protected constructor( + crudService: CrudService, + readonly uuid: string, + ) { + if (uuid.length !== 36) { + throw Error(`UUID must be of length 36: uuid="${uuid}", length=${uuid.length}`); + } + this.sub = crudService.subscribe(uuid, next => Object.assign(this, next)); + } + + equals(uuid: T | null | undefined) { + if (uuid === null || uuid === undefined) { + return false; + } + return this.uuid === uuid?.uuid; + } + +} diff --git a/src/main/angular/src/app/api/tools/shopping/Shopping.ts b/src/main/angular/src/app/api/tools/shopping/Shopping.ts new file mode 100644 index 0000000..cfe46af --- /dev/null +++ b/src/main/angular/src/app/api/tools/shopping/Shopping.ts @@ -0,0 +1,23 @@ +import {validateString} from "../../common/validators"; +import {UUID} from "../../UUID"; +import {ShoppingService} from "./shopping.service"; + +export class Shopping extends UUID { + + constructor( + shoppingService: ShoppingService, + uuid: string, + readonly name: string, + ) { + super(shoppingService, uuid); + } + + static fromJson(shoppingService: ShoppingService, json: any): Shopping { + return new Shopping( + shoppingService, + validateString(json.uuid), + validateString(json.name), + ); + } + +} diff --git a/src/main/angular/src/app/api/tools/shopping/crudService.ts b/src/main/angular/src/app/api/tools/shopping/crudService.ts new file mode 100644 index 0000000..f88a2c3 --- /dev/null +++ b/src/main/angular/src/app/api/tools/shopping/crudService.ts @@ -0,0 +1,57 @@ +import {UUID} from "../../UUID"; +import {ApiService} from "../../common/api.service"; +import {FromJson, Next} from "../../common/types"; +import {Page} from "../../common/Page"; +import {Subscription} from "rxjs"; + +export abstract class CrudService> { + + protected constructor( + readonly api: ApiService, + readonly path: any[], + readonly fromJson: FromJson, + ) { + // - + } + + subscribe(uuid: string, next: Next): Subscription { + return this.api.subscribe([...this.path, uuid], this.fromJson, next); + } + + protected modify(uuid: string, key: string, value: any, next?: Next): void { + this.api.postNone([...this.path, key], {uuid: uuid, value: value}, next); + } + + protected getNone(path: string[], next?: Next): void { + this.api.getNone([...this.path, ...path], next); + } + + protected getSingle(path: string[], next?: Next): void { + this.api.getSingle([...this.path, ...path], this.fromJson, next); + } + + protected getList(path: string[], next?: Next): void { + this.api.getList([...this.path, ...path], this.fromJson, next); + } + + protected getPage(path: string[], next?: Next>): void { + this.api.getPage([...this.path, ...path], this.fromJson, next); + } + + protected postNone(path: string[], data: any, next?: Next): void { + this.api.postNone([...this.path, ...path], data, next); + } + + protected postSingle(path: string[], data: any, next?: Next): void { + this.api.postSingle([...this.path, ...path], data, this.fromJson, next); + } + + protected postList(path: string[], data: any, next?: Next): void { + this.api.postList([...this.path, ...path], data, this.fromJson, next); + } + + protected postPage(path: string[], data: any, next?: Next>): void { + this.api.postPage([...this.path, ...path], data, this.fromJson, next); + } + +} diff --git a/src/main/angular/src/app/api/tools/shopping/shopping.service.ts b/src/main/angular/src/app/api/tools/shopping/shopping.service.ts new file mode 100644 index 0000000..08c22b8 --- /dev/null +++ b/src/main/angular/src/app/api/tools/shopping/shopping.service.ts @@ -0,0 +1,44 @@ +import {Injectable} from '@angular/core'; +import {Shopping} from "./Shopping"; +import {ApiService} from "../../common/api.service"; +import {CrudService} from "./crudService"; +import {Next} from "../../common/types"; +import {UserService} from "../../user/user.service"; + +@Injectable({ + providedIn: 'root' +}) +export class ShoppingService extends CrudService { + + constructor( + api: ApiService, + readonly userService: UserService, + ) { + super(api, ['Shopping'], json => Shopping.fromJson(this, json)); + } + + create(next?: Next): void { + this.getSingle(['create'], next); + } + + get(uuid: string, next?: Next): void { + this.postSingle(['get'], uuid, next); + } + + delete(uuid: string, next?: Next): void { + this.postNone(['delete'], uuid, next); + } + + name(uuid: string, name: string, next?: Next): void { + this.modify(uuid, 'name', name, next); + } + + listOwn(next?: Next): void { + if (this.userService.user) { + this.postList(['mine'], this.userService.user, next); + } else { + next && next([]); + } + } + +} diff --git a/src/main/angular/src/app/pages/tools/shopping/shopping/shopping.component.html b/src/main/angular/src/app/pages/tools/shopping/shopping/shopping.component.html new file mode 100644 index 0000000..b3dbcaf --- /dev/null +++ b/src/main/angular/src/app/pages/tools/shopping/shopping/shopping.component.html @@ -0,0 +1 @@ +

shopping works!

diff --git a/src/main/angular/src/app/pages/tools/shopping/shopping/shopping.component.less b/src/main/angular/src/app/pages/tools/shopping/shopping/shopping.component.less new file mode 100644 index 0000000..e69de29 diff --git a/src/main/angular/src/app/pages/tools/shopping/shopping/shopping.component.ts b/src/main/angular/src/app/pages/tools/shopping/shopping/shopping.component.ts new file mode 100644 index 0000000..045b253 --- /dev/null +++ b/src/main/angular/src/app/pages/tools/shopping/shopping/shopping.component.ts @@ -0,0 +1,12 @@ +import { Component } from '@angular/core'; + +@Component({ + selector: 'app-shopping', + standalone: true, + imports: [], + templateUrl: './shopping.component.html', + styleUrl: './shopping.component.less' +}) +export class ShoppingComponent { + +}