From 2bc6abf42c2a5fb4120cf2cb0b5c797497844581 Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Sun, 10 Dec 2017 20:43:24 +0100 Subject: [PATCH] Some frontend improvements, versioned the backend API --- frontend/src/app/app.module.ts | 7 +++- frontend/src/app/backend.service.spec.ts | 15 ++++++++ frontend/src/app/backend.service.ts | 38 +++++++++++++++++++ .../src/app/broadcast/broadcast.component.ts | 27 +++---------- frontend/src/app/status/status.component.html | 2 +- frontend/src/app/status/status.component.ts | 18 ++++----- .../ch/dissem/bitmessage/server/Converter.kt | 2 +- .../server/JabitServerController.kt | 2 +- 8 files changed, 74 insertions(+), 37 deletions(-) create mode 100644 frontend/src/app/backend.service.spec.ts create mode 100644 frontend/src/app/backend.service.ts diff --git a/frontend/src/app/app.module.ts b/frontend/src/app/app.module.ts index 043e95f..05e40b6 100644 --- a/frontend/src/app/app.module.ts +++ b/frontend/src/app/app.module.ts @@ -7,9 +7,10 @@ import {RouterModule} from "@angular/router"; import {StatusComponent} from './status/status.component'; import {APP_ROUTES} from "./app.routes"; import {HttpClientModule} from "@angular/common/http"; -import { BroadcastComponent } from './broadcast/broadcast.component'; +import {BroadcastComponent} from './broadcast/broadcast.component'; import {BrowserAnimationsModule} from "@angular/platform-browser/animations"; import {FlexLayoutModule} from "@angular/flex-layout"; +import {BackendService} from "./backend.service"; @NgModule({ declarations: [ @@ -28,7 +29,9 @@ import {FlexLayoutModule} from "@angular/flex-layout"; MatExpansionModule, MatButtonModule ], - providers: [], + providers: [ + BackendService + ], bootstrap: [AppComponent] }) export class AppModule { diff --git a/frontend/src/app/backend.service.spec.ts b/frontend/src/app/backend.service.spec.ts new file mode 100644 index 0000000..c31039d --- /dev/null +++ b/frontend/src/app/backend.service.spec.ts @@ -0,0 +1,15 @@ +import { TestBed, inject } from '@angular/core/testing'; + +import { BackendService } from './backend.service'; + +describe('BackendService', () => { + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [BackendService] + }); + }); + + it('should be created', inject([BackendService], (service: BackendService) => { + expect(service).toBeTruthy(); + })); +}); diff --git a/frontend/src/app/backend.service.ts b/frontend/src/app/backend.service.ts new file mode 100644 index 0000000..c5830cd --- /dev/null +++ b/frontend/src/app/backend.service.ts @@ -0,0 +1,38 @@ +import {Injectable} from '@angular/core'; +import {Observable} from "rxjs/Observable"; +import {HttpClient} from "@angular/common/http"; +import {map} from "rxjs/operators"; + +@Injectable() +export class BackendService { + + private baseUrl = "http://localhost:8080"; + + constructor(private http: HttpClient) { + } + + getStatus(): Observable { + return this.http.get(`${this.baseUrl}/api/v1/status`).pipe(map(data => JSON.stringify(data, null, 2))); + } + + getBroadcasts(address: string): Observable { + return this.http.get(`${this.baseUrl}/api/v1/read/${address}`) + } +} + +export interface Sender { + address: String; + alias: String; +} + +export interface Message { + id: any; + received: number; + subject: string; + body: string; +} + +export interface Broadcasts { + sender: Sender; + messages: Message[] +} diff --git a/frontend/src/app/broadcast/broadcast.component.ts b/frontend/src/app/broadcast/broadcast.component.ts index 6061e26..600a1c4 100644 --- a/frontend/src/app/broadcast/broadcast.component.ts +++ b/frontend/src/app/broadcast/broadcast.component.ts @@ -1,7 +1,8 @@ import {Component, OnInit} from '@angular/core'; import {ActivatedRoute} from "@angular/router"; -import {HttpClient} from "@angular/common/http"; import {Observable} from "rxjs/Observable"; +import {BackendService, Broadcasts} from "../backend.service"; +import {map, switchMap} from "rxjs/operators"; @Component({ selector: 'app-broadcast', @@ -12,29 +13,13 @@ export class BroadcastComponent implements OnInit { broadcasts$: Observable; - constructor(private route: ActivatedRoute, private http: HttpClient) { + constructor(private route: ActivatedRoute, private backend: BackendService) { } ngOnInit() { - let address = this.route.snapshot.params['address']; - this.broadcasts$ = this.http.get('http://localhost:8080/read/' + address) + this.broadcasts$ = this.route.params + .pipe(map(p => p['address'])) + .pipe(switchMap(address => this.backend.getBroadcasts(address))); } } - -class Sender { - address: String; - alias: String; -} - -class Message { - id: any; - received: number; - subject: string; - body: string; -} - -class Broadcasts { - sender: Sender; - messages: Message[] -} diff --git a/frontend/src/app/status/status.component.html b/frontend/src/app/status/status.component.html index 3595779..6963796 100644 --- a/frontend/src/app/status/status.component.html +++ b/frontend/src/app/status/status.component.html @@ -1,3 +1,3 @@ -
{{status}}
+
{{status$ | async}}
diff --git a/frontend/src/app/status/status.component.ts b/frontend/src/app/status/status.component.ts index 2781b71..6ac7f2b 100644 --- a/frontend/src/app/status/status.component.ts +++ b/frontend/src/app/status/status.component.ts @@ -1,22 +1,18 @@ -import {Component, OnInit} from '@angular/core'; -import {HttpClient} from '@angular/common/http'; +import {Component} from '@angular/core'; +import {BackendService} from "../backend.service"; +import {Observable} from "rxjs/Observable"; @Component({ selector: 'app-status', templateUrl: './status.component.html', styleUrls: ['./status.component.scss'] }) -export class StatusComponent implements OnInit { +export class StatusComponent { - status: string; + status$: Observable; - constructor(private http: HttpClient) { - } - - ngOnInit() { - this.http.get('http://localhost:8080/status').subscribe(data => { - this.status = JSON.stringify(data, null, 2); - }); + constructor(backend: BackendService) { + this.status$ = backend.getStatus(); } } diff --git a/src/main/kotlin/ch/dissem/bitmessage/server/Converter.kt b/src/main/kotlin/ch/dissem/bitmessage/server/Converter.kt index 500d329..145c3f4 100644 --- a/src/main/kotlin/ch/dissem/bitmessage/server/Converter.kt +++ b/src/main/kotlin/ch/dissem/bitmessage/server/Converter.kt @@ -48,7 +48,7 @@ object Converter { fun message(plaintext: Plaintext) = Message().apply { id = plaintext.id - received = plaintext.sent + received = plaintext.received subject = plaintext.subject body = plaintext.text } diff --git a/src/main/kotlin/ch/dissem/bitmessage/server/JabitServerController.kt b/src/main/kotlin/ch/dissem/bitmessage/server/JabitServerController.kt index 7a687d2..59ffcfc 100644 --- a/src/main/kotlin/ch/dissem/bitmessage/server/JabitServerController.kt +++ b/src/main/kotlin/ch/dissem/bitmessage/server/JabitServerController.kt @@ -36,6 +36,7 @@ import javax.inject.Inject */ @CrossOrigin @RestController +@RequestMapping("/api/v1") class JabitServerController { @Resource @@ -55,7 +56,6 @@ class JabitServerController { "uri": "${Utils.getURL(identity, true)}" }""" - @RequestMapping(value = ["status"], method = [GET], produces = ["application/json"]) fun status() = "{${ctx.status()}}"