Some frontend improvements, versioned the backend API

This commit is contained in:
Christian Basler 2017-12-10 20:43:24 +01:00
parent 46f911c075
commit 2bc6abf42c
8 changed files with 74 additions and 37 deletions

View File

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

View File

@ -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();
}));
});

View File

@ -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<string> {
return this.http.get(`${this.baseUrl}/api/v1/status`).pipe(map(data => JSON.stringify(data, null, 2)));
}
getBroadcasts(address: string): Observable<Broadcasts> {
return this.http.get<Broadcasts>(`${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[]
}

View File

@ -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<Broadcasts>;
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<Broadcasts>('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[]
}

View File

@ -1,3 +1,3 @@
<mat-card>
<pre><code>{{status}}</code></pre>
<pre><code>{{status$ | async}}</code></pre>
</mat-card>

View File

@ -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<string>;
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();
}
}

View File

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

View File

@ -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()}}"