Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Frontend rewrite #131

Open
wants to merge 16 commits into
base: develop
Choose a base branch
from
Open
Prev Previous commit
Next Next commit
Fixing dependencies in component code using dependency injection
lieberlois committed Mar 18, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 609a411eb96c2aa23a0f65d04f641580ab308466
25 changes: 17 additions & 8 deletions stack/application/frontend/src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -4,16 +4,16 @@ import { HttpClientModule } from '@angular/common/http';
import { FormsModule } from '@angular/forms';

import { AppComponent } from './app.component';
import { UploadComponent } from './features/upload';
import { DisplayComponent } from './features/display';
import { RandomComponent } from './features/random';
import { DeleteComponent } from './features/delete';
import { OrchestrateComponent } from './features/orchestrate';
import { UploadComponent, UploadService } from './features/upload';
import { DisplayComponent, DisplayService } from './features/display';
import { RandomComponent, RandomService } from './features/random';
import { DeleteComponent, DeleteService } from './features/delete';
import { OrchestrateComponent, OrchestrateService } from './features/orchestrate';
import { AppRoutingModule } from './app-routing.module';
import { DashboardComponent } from './features/dashboard';
import { AlbumComponent, PreviewComponent, PreviewService } from './features/album';
import { AlbumComponent, AlbumService, PreviewComponent, PreviewService } from './features/album';
import { ToolsComponent } from './features/tools';
import { TrafficGeneratorComponent } from "./features/trafficgen";
import { TrafficGeneratorComponent, TrafficgenService } from "./features/trafficgen";

@NgModule({
declarations: [
@@ -35,7 +35,16 @@ import { TrafficGeneratorComponent } from "./features/trafficgen";
HttpClientModule,
FormsModule
],
providers: [PreviewService],
providers: [
AlbumService,
DeleteService,
DisplayService,
OrchestrateService,
RandomService,
TrafficgenService,
UploadService,
PreviewService
],
bootstrap: [AppComponent]
})
export class AppModule {
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {Component, OnInit} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {environment} from "../../../../../environments/environment";
import {PreviewComponent} from "../preview/preview.component";
import {PreviewService} from "../../services/preview.service";
import { Component, OnInit } from '@angular/core';
import { PreviewComponent } from "../preview/preview.component";
import { PreviewService } from "../../services/preview.service";
import { AlbumService } from '../../services/album.service';

@Component({
selector: 'app-album',
@@ -11,9 +10,10 @@ import {PreviewService} from "../../services/preview.service";
})
export class AlbumComponent implements OnInit {

constructor(private http: HttpClient,
private previewService: PreviewService) {
}
public constructor(
private readonly albumService: AlbumService,
private readonly previewService: PreviewService
) {}

ngOnInit(): void {
this.retrieveImages()
@@ -91,7 +91,7 @@ export class AlbumComponent implements OnInit {
}

async retrieveImages() {
this.data = await this.http.get<Array<Image>>(environment.backend.imageholder + '/api/images').toPromise();
this.data = await this.albumService.getImages();
if (this.data.length > 0) {
this.images = this.data;
this.showPreviews(this.data, this.firstImageDisplayed)
3 changes: 2 additions & 1 deletion stack/application/frontend/src/app/features/album/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./components/album/album.component";
export * from "./components/preview/preview.component";
export * from "./services/preview.service";
export * from "./services/preview.service";
export * from "./services/album.service";
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { HttpClient, HttpHeaders } from '@angular/common/http';
import {Injectable} from '@angular/core';
import {environment} from "../../../../environments/environment";
import { Image } from '../../../shared/models'


@Injectable()
export class AlbumService {

public constructor(private readonly httpClient: HttpClient) {}

public async getImages(): Promise<any> {
return this.httpClient.get<Array<Image>>(environment.backend.imageholder + '/api/images').toPromise();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Component, OnInit} from '@angular/core';
import {HttpClient} from "@angular/common/http";
import {environment} from "../../../../../environments/environment";
import { DeleteService } from '../../services/delete.service';

@Component({
selector: 'app-delete',
@@ -10,8 +11,7 @@ import {environment} from "../../../../../environments/environment";

export class DeleteComponent implements OnInit {

constructor(private http: HttpClient) {
}
public constructor(private readonly deleteService: DeleteService) {}

ngOnInit(): void {
this.retrieveImages();
@@ -32,7 +32,7 @@ export class DeleteComponent implements OnInit {
return
}
try {
res = await this.http.delete(environment.backend.imageholder + '/api/images/' + this.deleteId, {responseType: 'text'}).toPromise();
res = await this.deleteService.deleteImageById(this.deleteId);
} catch (e) {
let info = document.getElementById("info");
info.innerText = "Failed to delete " + this.deleteId;
@@ -54,7 +54,7 @@ export class DeleteComponent implements OnInit {
async deleteAll() {
let res;
try {
res = await this.http.post(environment.backend.imageholder + '/api/images/delete/all', null, {responseType: 'text'}).toPromise();
res = await this.deleteService.deleteAllImages();
} catch (e) {
let info = document.getElementById("info");
info.innerText = "Failed to delete all images";
@@ -78,7 +78,7 @@ export class DeleteComponent implements OnInit {
}

async retrieveImages() {
let data = await this.http.get<Array<Image>>(environment.backend.imageholder + '/api/images').toPromise();
let data = await this.deleteService.getImages();
if (data.length > 0) {
document.getElementById("preview").hidden = false;
this.images = data;
@@ -104,7 +104,7 @@ export class DeleteComponent implements OnInit {
}

async showImage(id: string) {
let data = await this.http.get(environment.backend.imagethumbnail + '/api/images/' + id, {responseType: 'blob'}).toPromise();
let data = await this.deleteService.getImageById(id);
if (data != null) {
this.displayImage = this.createImageFromBlob(data);
}
3 changes: 2 additions & 1 deletion stack/application/frontend/src/app/features/delete/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './components/delete/delete.component';
export * from './components/delete/delete.component';
export * from './services/delete.service';
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { HttpClient, HttpHeaders } from '@angular/common/http';
import {Injectable} from '@angular/core';
import {environment} from "../../../../environments/environment";
import { Image } from '../../../shared/models'


@Injectable()
export class DeleteService {

public constructor(private readonly httpClient: HttpClient) {}

public async deleteImageById(deleteId: string): Promise<any> {
return this.httpClient.delete(environment.backend.imageholder + '/api/images/' + deleteId, {responseType: 'text'}).toPromise();
}

public async deleteAllImages(): Promise<any> {
return this.httpClient.post(environment.backend.imageholder + '/api/images/delete/all', null, {responseType: 'text'}).toPromise();
}

public async getImages(): Promise<any> {
return this.httpClient.get<Array<Image>>(environment.backend.imageholder + '/api/images').toPromise();
}

public async getImageById(id: string): Promise<any> {
return this.httpClient.get(environment.backend.imagethumbnail + '/api/images/' + id, {responseType: 'blob'}).toPromise();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Component, OnInit} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {environment} from "../../../../../environments/environment";
import { DisplayService } from '../../services/display.service';
import { Image } from '../../../../shared/models';

@Component({
selector: 'app-display',
@@ -9,8 +10,7 @@ import {environment} from "../../../../../environments/environment";
})
export class DisplayComponent implements OnInit {

constructor(private http: HttpClient) {
}
public constructor(private readonly displayService: DisplayService) {}

ngOnInit(): void {
this.retrieveImages();
@@ -22,7 +22,7 @@ export class DisplayComponent implements OnInit {
selectedLink: string;

async retrieveImages() {
let data = await this.http.get<Array<Image>>(environment.backend.imageholder + '/api/images').toPromise();
let data = await this.displayService.getImages();
if (data.length > 0) {
document.getElementById("preview").hidden = false;
this.images = data;
@@ -55,7 +55,7 @@ export class DisplayComponent implements OnInit {
}

async showImage(id: string, hideId: boolean) {
let data = await this.http.get(environment.backend.imageholder + '/api/images/' + id, {responseType: 'blob'}).toPromise();
let data = await this.displayService.getImageById(id);
if (data != null) {
this.displayImage = this.createImageFromBlob(data);
this.selectedLink = environment.backend.imageholder + '/api/images/' + id;
@@ -76,9 +76,3 @@ export class DisplayComponent implements OnInit {
}

}

interface Image {
id: String;
contentType: String;
name: String;
}
3 changes: 2 additions & 1 deletion stack/application/frontend/src/app/features/display/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './components/display/display.component';
export * from './components/display/display.component';
export * from './services/display.service';
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { HttpClient, HttpHeaders } from '@angular/common/http';
import {Injectable} from '@angular/core';
import {environment} from "../../../../environments/environment";
import { Image } from '../../../shared/models'


@Injectable()
export class DisplayService {

public constructor(private readonly httpClient: HttpClient) {}

public async getImages(): Promise<any> {
return this.httpClient.get<Array<Image>>(environment.backend.imageholder + '/api/images').toPromise();
}

public async getImageById(id: string): Promise<any> {
return this.httpClient.get(environment.backend.imageholder + '/api/images/' + id, {responseType: 'blob'}).toPromise();
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { Component, OnInit } from '@angular/core';
import { HttpClient, HttpHeaders, HttpResponse } from '@angular/common/http';
import { HttpHeaders, HttpResponse } from '@angular/common/http';
import { environment } from "../../../../../environments/environment";
import { OrchestrateService } from '../../services/orchestrate.service';
import { InfoType } from '../../../../shared/enums'

@Component({
selector: 'app-orchestrate',
templateUrl: './orchestrate.component.html',
styleUrls: ['./orchestrate.component.css']
})
export class OrchestrateComponent implements OnInit {
constructor(private http: HttpClient) {
public constructor(private readonly orchestrateService: OrchestrateService) {
}

ngOnInit(): void {
@@ -22,7 +24,7 @@ export class OrchestrateComponent implements OnInit {
selectedLink: string;

async retrieveImages(id: string) {
let data = await this.http.get<Array<Image>>(environment.backend.imageholder + '/api/images').toPromise();
let data = await this.orchestrateService.getImages();
if (data.length > 0) {
document.getElementById("preview").hidden = false;
this.images = data;
@@ -37,7 +39,7 @@ export class OrchestrateComponent implements OnInit {
}

async showPreview(id: string) {
let data = await this.http.get(environment.backend.imagethumbnail + '/api/images/' + id, { responseType: 'blob' }).toPromise();
let data = await this.orchestrateService.getImageById(id);
if (data != null) {
this.displayId = id;
let reader = new FileReader();
@@ -78,14 +80,12 @@ export class OrchestrateComponent implements OnInit {
return
}

let transformationRequest = this.buildJson(formInput);
let transformationRequestString = JSON.stringify(transformationRequest)
let headers = new HttpHeaders();
headers = headers.set('Content-Type', 'application/json; charset=utf-8');
let res;
const transformationRequest = this.buildJson(formInput);
const transformationRequestString = JSON.stringify(transformationRequest)

let res: any;
try {
res = await this.http.post(environment.backend.imageorchestrator + '/api/images/transform', transformationRequestString,
{ observe: "response", headers: headers, responseType: 'blob' }).toPromise();
res = await this.orchestrateService.sendTransformationRequest(transformationRequestString)
} catch (e) {
console.log(e)
this.hideTransformed()
@@ -162,12 +162,6 @@ export class OrchestrateComponent implements OnInit {
}
}

interface Image {
id: String;
contentType: String;
name: String;
}

enum TransformationType {
Copy link

@BlacCello BlacCello Oct 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can think about moving those classes into separate file(s)

rotate = "rotate",
grayscale = "grayscale",
@@ -193,10 +187,3 @@ class TransformationRequest {
}
}

enum InfoType {
warning,
danger,
success
}


Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './components/orchestrate/orchestrate.component';
export * from './components/orchestrate/orchestrate.component';
export * from './services/orchestrate.service';
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { HttpClient, HttpHeaders } from '@angular/common/http';
import {Injectable} from '@angular/core';
import {environment} from "../../../../environments/environment";
import { Image } from '../../../shared/models'


@Injectable()
export class OrchestrateService {

public constructor(private readonly httpClient: HttpClient) {}

public async getImages(): Promise<any> {
return this.httpClient.get<Array<Image>>(environment.backend.imageholder + '/api/images').toPromise();
}

public async getImageById(id: string): Promise<any> {
return this.httpClient.get(environment.backend.imagethumbnail + '/api/images/' + id, { responseType: 'blob' }).toPromise();
}

public async sendTransformationRequest(transformationRequestString: string): Promise<any> {
let headers = new HttpHeaders();
headers = headers.set('Content-Type', 'application/json; charset=utf-8');

return this.httpClient.post(environment.backend.imageorchestrator + '/api/images/transform', transformationRequestString,
{ observe: "response", headers: headers, responseType: 'blob' }).toPromise();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Component, OnInit} from '@angular/core';
import {environment} from "../../../../environments/environment";
import {HttpClient} from "@angular/common/http";
import { RandomService } from '../services/random.service';

@Component({
selector: 'app-random',
@@ -9,27 +9,26 @@ import {HttpClient} from "@angular/common/http";
})
export class RandomComponent implements OnInit {

constructor(private http: HttpClient) {
public constructor(private readonly randomService: RandomService) {
}

ngOnInit() {
this.changeImage()
}

public displayImage;
private randomImageUrl: string = environment.backend.imageholder + '/api/images/random';

async changeImage() {
let data;
public async changeImage() {
let data: Blob;
try {
data = await this.http.get(this.randomImageUrl, {responseType: 'blob'}).toPromise();
data = await this.randomService.getRandomImage();
} catch (e) {
RandomComponent.info("Failed retrieving random image")
this.info("Failed retrieving random image")
}
if (data != undefined) {
RandomComponent.info("")
this.info("")
} else {
RandomComponent.info("No images found")
this.info("No images found")
}
this.displayImage = this.createImageFromBlob(data);
}
@@ -44,7 +43,7 @@ export class RandomComponent implements OnInit {
}
}

private static info(text: string) {
private info(text: string): void {
document.getElementById("image").hidden = text.length > 0;
document.getElementById("info").innerText = text;
}
3 changes: 2 additions & 1 deletion stack/application/frontend/src/app/features/random/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './components/random.component';
export * from './components/random.component';
export * from './services/random.service';
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { HttpClient } from '@angular/common/http';
import {Injectable} from '@angular/core';
import {environment} from "../../../../environments/environment";


@Injectable()
export class RandomService {

public constructor(private readonly httpClient: HttpClient) {}

private randomImageUrl: string = environment.backend.imageholder + '/api/images/random';

public async getRandomImage(): Promise<Blob> {
return this.httpClient.get(this.randomImageUrl, {responseType: 'blob'}).toPromise();
}
}
Original file line number Diff line number Diff line change
@@ -8,10 +8,7 @@ import {environment} from "../../../../environments/environment";
})
export class ToolsComponent {

constructor() {
}

getToolURL(configValue: string) {
public getToolURL(configValue: string): string {
if (configValue.startsWith(":")) {
return window.location.protocol + "//" + window.location.hostname + configValue;
} else {
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component, OnInit } from '@angular/core';
import { HttpClient } from "@angular/common/http";
import { environment } from "../../../../environments/environment";
import { Component } from '@angular/core';
import { TrafficgenService } from '../services/trafficgen.service';
import { InfoType } from '../../../shared/enums';

@Component({
selector: 'app-trafficgen',
@@ -10,16 +10,16 @@ import { environment } from "../../../../environments/environment";

export class TrafficGeneratorComponent {

constructor(private http: HttpClient) {
public constructor(private readonly trafficgenService: TrafficgenService) {
}

async uploadAll() {
try {
await this.http.post(environment.backend.trafficgen + '/api/traffic/image/upload', null, { responseType: 'text' }).toPromise();
TrafficGeneratorComponent.info("Upload of all images triggered successfully.", InfoType.success);
await this.trafficgenService.uploadAll();
this.info("Upload of all images triggered successfully.", InfoType.success);
} catch (e) {
console.log(e)
TrafficGeneratorComponent.info("Failed to trigger upload of all images", InfoType.danger);
this.info("Failed to trigger upload of all images", InfoType.danger);
}
}

@@ -30,34 +30,28 @@ export class TrafficGeneratorComponent {
transformationsPerSecond = 1
console.log("Defaulting to 1 transformation request per second.")
}
await this.http.post(environment.backend.trafficgen + '/api/traffic/image/transform/start?transformationsPerSecond=' + transformationsPerSecond, null, { responseType: 'text' }).toPromise();
TrafficGeneratorComponent.info("Transformation traffic request sent successfully.", InfoType.success);
await this.trafficgenService.sendTransformationRequest(transformationsPerSecond);
this.info("Transformation traffic request sent successfully.", InfoType.success);
} catch (e) {
console.log(e)
TrafficGeneratorComponent.info("Failed to send transformation traffic request", InfoType.danger);
this.info("Failed to send transformation traffic request", InfoType.danger);
}
}

async stopTransformationTraffic() {
try {
await this.http.post(environment.backend.trafficgen + '/api/traffic/image/transform/stop', null, { responseType: 'text' }).toPromise();
TrafficGeneratorComponent.info("Stop transformation request sent successfully.", InfoType.success);
await this.trafficgenService.stopTransformationTraffic();
this.info("Stop transformation request sent successfully.", InfoType.success);
} catch (e) {
console.log(e)
TrafficGeneratorComponent.info("Failed to send stop transformation traffic request", InfoType.danger);
this.info("Failed to send stop transformation traffic request", InfoType.danger);
}
}

private static info(text: string, type: InfoType) {
private info(text: string, type: InfoType) {
let info = <HTMLInputElement>document.getElementById("info");
info.hidden = false
info.value = text
info.className = "fade-in btn-block btn-" + InfoType[type] + " dima-btn"
}
}

enum InfoType {
warning,
danger,
success
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './components/trafficgen.component';
export * from './components/trafficgen.component';
export * from './services/trafficgen.service';
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { HttpClient } from '@angular/common/http';
import {Injectable} from '@angular/core';
import {environment} from "../../../../environments/environment";


@Injectable()
export class TrafficgenService {

public constructor(private readonly httpClient: HttpClient) {}

public async uploadAll(): Promise<any> {
return this.httpClient.post(environment.backend.trafficgen + '/api/traffic/image/upload', null, { responseType: 'text' }).toPromise();
}

public async sendTransformationRequest(transformationsPerSecond: number): Promise<any> {
return await this.httpClient.post(environment.backend.trafficgen + '/api/traffic/image/transform/start?transformationsPerSecond=' + transformationsPerSecond, null, { responseType: 'text' }).toPromise();
}

public async stopTransformationTraffic(): Promise<any> {
return await this.httpClient.post(environment.backend.trafficgen + '/api/traffic/image/transform/stop', null, { responseType: 'text' }).toPromise();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import {Component} from '@angular/core';
import {UploadService} from "../services/upload.service";

class ImageSnippet {
constructor(public src: string, public file: File) {
}
}
import { Component } from '@angular/core';
import { InfoType } from '../../../shared/enums';
import { ImageSnippet } from '../models/image-snippet.model';
import { UploadService } from "../services/upload.service";

@Component({
selector: 'upload.component',
@@ -13,47 +10,49 @@ class ImageSnippet {
})
export class UploadComponent {

selectedFile: ImageSnippet;
public selectedFile: ImageSnippet;

constructor(private uploadService: UploadService) {
}
public constructor(private uploadService: UploadService) {}

processFile(imageInput: any, nameInput: any) {
public processFile(imageInput: any, nameInput: any) {
const file: File = imageInput.files[0];
const reader = new FileReader();

reader.addEventListener('load', (event: any) => {
this.selectedFile = new ImageSnippet(event.target.result, file);
this.selectedFile = {
src: event.target.result,
file: file
}
});

reader.readAsDataURL(file);
UploadComponent.infoHide()
this.infoHide()
nameInput.value = ""
}

uploadImage(nameInput: any) {
public uploadImage(nameInput: any) {
const name: string = nameInput.value;
UploadComponent.infoHide()
this.infoHide()
if (this.selectedFile == undefined) {
UploadComponent.info("No image selected", null, InfoType.warning)
this.info("No image selected", null, InfoType.warning)
return
}

this.uploadService.uploadImage(this.selectedFile.file, name).subscribe(
(res) => {
UploadComponent.info("Upload successful", res, InfoType.success)
this.info("Upload successful", res, InfoType.success)
},
(err) => {
console.log(err)
UploadComponent.info("Upload failed", null, InfoType.danger)
this.info("Upload failed", null, InfoType.danger)
})
}

private static infoHide() {
private infoHide(): void {
document.getElementById("info").hidden = true
}

private static info(text: string, id: string, type: InfoType) {
private info(text: string, id: string, type: InfoType) {
let info = <HTMLInputElement>document.getElementById("info");
info.hidden = false
if (id != null) {
@@ -65,10 +64,6 @@ export class UploadComponent {
}
}

enum InfoType {
warning,
danger,
success
}



Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface ImageSnippet {
src: string;
file: File;
}
1 change: 1 addition & 0 deletions stack/application/frontend/src/app/shared/enums/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './info-type.enum';
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum InfoType {
warning,
danger,
success
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface Image {
id: String;
contentType: String;
name: String;
}
1 change: 1 addition & 0 deletions stack/application/frontend/src/app/shared/models/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './image.model';