From 4c7506d8b22031ff01ebac998a98e86333db532d Mon Sep 17 00:00:00 2001 From: kevinpauer Date: Tue, 5 Apr 2022 19:23:52 +0200 Subject: [PATCH] Add API calls for profile component --- frontend/src/app/Services/bot.service.ts | 15 ---- frontend/src/app/Services/data.service.ts | 36 ---------- .../src/app/Services/profile.service.spec.ts | 16 +++++ frontend/src/app/Services/profile.service.ts | 69 +++++++++++++++++++ .../Views/dashboard/dashboard.component.ts | 3 - .../app/Views/profile/profile.component.html | 2 +- .../app/Views/profile/profile.component.ts | 31 +++++++-- frontend/src/app/app-routing.module.ts | 4 +- 8 files changed, 113 insertions(+), 63 deletions(-) create mode 100644 frontend/src/app/Services/profile.service.spec.ts create mode 100644 frontend/src/app/Services/profile.service.ts diff --git a/frontend/src/app/Services/bot.service.ts b/frontend/src/app/Services/bot.service.ts index 29df378..27c8290 100644 --- a/frontend/src/app/Services/bot.service.ts +++ b/frontend/src/app/Services/bot.service.ts @@ -13,19 +13,4 @@ export class BotService { private http: HttpClient, private tokenStorage: TokenStorageService ) {} - - /** - * @param {string} telegramUserID - * @returns Observable - */ - public createTransaction(telegram_user_id: string): Observable { - return this.http.post(API_URL + 'telegram', { - headers: new HttpHeaders({ - 'Content-Type': 'application/json', - Authorization: 'Bearer ' + this.tokenStorage.getToken(), - }), - responseType: 'text', - telegram_user_id, - }); - } } diff --git a/frontend/src/app/Services/data.service.ts b/frontend/src/app/Services/data.service.ts index 0a5eb26..213e35b 100644 --- a/frontend/src/app/Services/data.service.ts +++ b/frontend/src/app/Services/data.service.ts @@ -16,42 +16,6 @@ export class DataService { private tokenStorage: TokenStorageService ) {} - /** - * @returns Observable - */ - public getUserData(): Observable { - return this.http.get(API_URL + 'user', { - headers: new HttpHeaders({ - 'Content-Type': 'application/json', - Authorization: 'Bearer ' + this.tokenStorage.getToken(), - }), - responseType: 'text', - }); - } - - /** - * @param {string} email - * @param {string} username - * @param {string} password - * @returns Observable - */ - public updateUserData( - email: string, - username: string, - password: string - ): Observable { - return this.http.put(API_URL + 'user', { - headers: new HttpHeaders({ - 'Content-Type': 'application/json', - Authorization: 'Bearer ' + this.tokenStorage.getToken(), - }), - responseType: 'text', - email, - username, - password, - }); - } - /** * @returns Observable */ diff --git a/frontend/src/app/Services/profile.service.spec.ts b/frontend/src/app/Services/profile.service.spec.ts new file mode 100644 index 0000000..2ddf7f2 --- /dev/null +++ b/frontend/src/app/Services/profile.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { ProfileService } from './profile.service'; + +describe('ProfileService', () => { + let service: ProfileService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(ProfileService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/frontend/src/app/Services/profile.service.ts b/frontend/src/app/Services/profile.service.ts new file mode 100644 index 0000000..b23a9b3 --- /dev/null +++ b/frontend/src/app/Services/profile.service.ts @@ -0,0 +1,69 @@ +import { HttpClient, HttpHeaders } from '@angular/common/http'; +import { Injectable } from '@angular/core'; +import { Observable } from 'rxjs'; +import { TokenStorageService } from './token.service'; + +const API_URL = 'https://gruppe1.testsites.info/api/'; + +@Injectable({ + providedIn: 'root', +}) +export class ProfileService { + constructor( + private tokenStorage: TokenStorageService, + private http: HttpClient + ) {} + + /** + * @returns Observable + */ + public getUserData(): Observable { + return this.http.get(API_URL + 'user', { + headers: new HttpHeaders({ + 'Content-Type': 'application/json', + Authorization: 'Bearer ' + this.tokenStorage.getToken(), + }), + responseType: 'text', + }); + } + + /** + * @param {string} username + * @param {number} password + * @returns Observable + */ + public updateProfile(username: string, password: number): Observable { + return this.http.put( + API_URL + 'user', + { + username, + password, + }, + { + headers: new HttpHeaders({ + 'Content-Type': 'application/json', + Authorization: 'Bearer ' + this.tokenStorage.getToken(), + }), + } + ); + } + + /** + * @param {string} telegramUserID + * @returns Observable + */ + public addTelegramId(telegram_user_id: string): Observable { + return this.http.post( + API_URL + 'telegram', + { + telegram_user_id, + }, + { + headers: new HttpHeaders({ + 'Content-Type': 'application/json', + Authorization: 'Bearer ' + this.tokenStorage.getToken(), + }), + } + ); + } +} diff --git a/frontend/src/app/Views/dashboard/dashboard.component.ts b/frontend/src/app/Views/dashboard/dashboard.component.ts index c1dc35e..1ac94a2 100644 --- a/frontend/src/app/Views/dashboard/dashboard.component.ts +++ b/frontend/src/app/Views/dashboard/dashboard.component.ts @@ -53,14 +53,12 @@ export class DashboardComponent implements OnInit { for (let i = 0; i < data.data.length; i++) { STOCK_DATA.push({ count: data.data[i].count, - // price: data.data[i].price, symbol: data.data[i].symbol, time: data.data[i].last_transaction, }); } console.log(STOCK_DATA); this.dataSourceStocks = STOCK_DATA; - //TODO map data on array for display //TODO move to helper service }); this.dataService.getTransactionData().subscribe((response: any) => { @@ -76,7 +74,6 @@ export class DashboardComponent implements OnInit { } console.log(TRANSACTION_DATA); this.dataSourceTransactions = TRANSACTION_DATA; - //TODO map data on array for display //TODO move to helper service }); } diff --git a/frontend/src/app/Views/profile/profile.component.html b/frontend/src/app/Views/profile/profile.component.html index 7bdb5cc..37a5180 100644 --- a/frontend/src/app/Views/profile/profile.component.html +++ b/frontend/src/app/Views/profile/profile.component.html @@ -114,7 +114,7 @@ [formControl]="telegramIdFormControl" [(ngModel)]="userId" required - placeholder="Ex. 123456789" + #telegramId /> Id is required diff --git a/frontend/src/app/Views/profile/profile.component.ts b/frontend/src/app/Views/profile/profile.component.ts index 7350ea9..324d7a8 100644 --- a/frontend/src/app/Views/profile/profile.component.ts +++ b/frontend/src/app/Views/profile/profile.component.ts @@ -1,8 +1,10 @@ +import { i18nMetaToJSDoc } from '@angular/compiler/src/render3/view/i18n/meta'; import { Component, OnInit } from '@angular/core'; import { FormControl, PatternValidator, Validators } from '@angular/forms'; import { MatDialog } from '@angular/material/dialog'; import { BotService } from 'src/app/Services/bot.service'; import { DataService } from 'src/app/Services/data.service'; +import { ProfileService } from 'src/app/Services/profile.service'; import { ConfirmationDialogComponent } from './confirmation-dialog/confirmation-dialog.component'; @Component({ @@ -23,24 +25,40 @@ export class ProfileComponent implements OnInit { form: any = { username: null, email: 'example@web.com', - password: null, + password: 'password', }; constructor( - private botService: BotService, - private dataService: DataService, + private profileService: ProfileService, public dialog: MatDialog ) {} - ngOnInit(): void {} + ngOnInit(): void { + this.profileService.getUserData().subscribe((result) => { + console.log(result); + result = JSON.parse(result); + this.form.username = result.data.username; + this.form.password = result.data.password; + this.userId = result.data.telegram_user_id; + }); + } onSubmit() { - console.log('NASE1'); + if (this.userId != '') { + console.log(this.userId); + this.profileService.addTelegramId(this.userId).subscribe((result) => { + console.log(result); + }); + } } updateUser() { const { username, email, password } = this.form; - console.log('NASE2'); + this.profileService + .updateProfile(this.form.username, this.form.password) + .subscribe((result) => { + console.log(result); + }); } openDialog(action: string) { @@ -57,7 +75,6 @@ export class ProfileComponent implements OnInit { this.updateUser(); } } - console.log(`Dialog result: ${result}`); }); } } diff --git a/frontend/src/app/app-routing.module.ts b/frontend/src/app/app-routing.module.ts index 6d05a46..5d53f26 100644 --- a/frontend/src/app/app-routing.module.ts +++ b/frontend/src/app/app-routing.module.ts @@ -1,5 +1,7 @@ import { NgModule } from '@angular/core'; import { RouterModule, Routes } from '@angular/router'; +import { BotService } from './Services/bot.service'; +import { BotSettingsComponent } from './Views/bot-settings/bot-settings.component'; import { DashboardComponent } from './Views/dashboard/dashboard.component'; import { LoginComponent } from './Views/login/login.component'; import { ProfileComponent } from './Views/profile/profile.component'; @@ -24,7 +26,7 @@ const routes: Routes = [ }, { path: 'settings', - component: ProfileComponent, + component: BotSettingsComponent, }, ];