Shopping WIP

This commit is contained in:
Patrick Haßel 2024-11-08 15:03:23 +01:00
parent 5912044843
commit c891691dfe
7 changed files with 162 additions and 0 deletions

View File

@ -0,0 +1,25 @@
import {CrudService} from "./tools/shopping/crudService";
import {Subscription} from "rxjs";
export abstract class UUID<T extends UUID<T>> {
private sub?: Subscription;
protected constructor(
crudService: CrudService<T>,
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;
}
}

View File

@ -0,0 +1,23 @@
import {validateString} from "../../common/validators";
import {UUID} from "../../UUID";
import {ShoppingService} from "./shopping.service";
export class Shopping extends UUID<Shopping> {
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),
);
}
}

View File

@ -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<T extends UUID<T>> {
protected constructor(
readonly api: ApiService,
readonly path: any[],
readonly fromJson: FromJson<T>,
) {
// -
}
subscribe(uuid: string, next: Next<T>): Subscription {
return this.api.subscribe([...this.path, uuid], this.fromJson, next);
}
protected modify(uuid: string, key: string, value: any, next?: Next<void>): void {
this.api.postNone([...this.path, key], {uuid: uuid, value: value}, next);
}
protected getNone(path: string[], next?: Next<void>): void {
this.api.getNone([...this.path, ...path], next);
}
protected getSingle(path: string[], next?: Next<T>): void {
this.api.getSingle([...this.path, ...path], this.fromJson, next);
}
protected getList(path: string[], next?: Next<T[]>): void {
this.api.getList([...this.path, ...path], this.fromJson, next);
}
protected getPage(path: string[], next?: Next<Page<T>>): void {
this.api.getPage([...this.path, ...path], this.fromJson, next);
}
protected postNone(path: string[], data: any, next?: Next<void>): void {
this.api.postNone([...this.path, ...path], data, next);
}
protected postSingle(path: string[], data: any, next?: Next<T>): void {
this.api.postSingle([...this.path, ...path], data, this.fromJson, next);
}
protected postList(path: string[], data: any, next?: Next<T[]>): void {
this.api.postList([...this.path, ...path], data, this.fromJson, next);
}
protected postPage(path: string[], data: any, next?: Next<Page<T>>): void {
this.api.postPage([...this.path, ...path], data, this.fromJson, next);
}
}

View File

@ -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<Shopping> {
constructor(
api: ApiService,
readonly userService: UserService,
) {
super(api, ['Shopping'], json => Shopping.fromJson(this, json));
}
create(next?: Next<Shopping>): void {
this.getSingle(['create'], next);
}
get(uuid: string, next?: Next<Shopping>): void {
this.postSingle(['get'], uuid, next);
}
delete(uuid: string, next?: Next<void>): void {
this.postNone(['delete'], uuid, next);
}
name(uuid: string, name: string, next?: Next<void>): void {
this.modify(uuid, 'name', name, next);
}
listOwn(next?: Next<Shopping[]>): void {
if (this.userService.user) {
this.postList(['mine'], this.userService.user, next);
} else {
next && next([]);
}
}
}

View File

@ -0,0 +1 @@
<p>shopping works!</p>

View File

@ -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 {
}