From ed7f805d265e3162639d329785de0edb8cfc22ff Mon Sep 17 00:00:00 2001 From: Syndamia Date: Thu, 8 Apr 2021 09:19:39 +0300 Subject: Added friends service, from which you could add and remove friends --- src/app/app-constants.module.ts | 1 + src/app/services/friend.service.ts | 44 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/app/services/friend.service.ts (limited to 'src') diff --git a/src/app/app-constants.module.ts b/src/app/app-constants.module.ts index d1bd9f5..67091d9 100644 --- a/src/app/app-constants.module.ts +++ b/src/app/app-constants.module.ts @@ -3,6 +3,7 @@ export class AppConstants { public static API_USER_URL = AppConstants.BASE_API_URL + '/User'; public static API_PROFILE_PICTURE_URL = AppConstants.BASE_API_URL + '/ProfilePicture'; + public static API_FRIENDS_URL = AppConstants.BASE_API_URL + '/Friends'; public static API_USER_LOGIN_URL = AppConstants.API_USER_URL + '/login'; public static API_USER_REGISTER_URL = AppConstants.API_USER_URL + '/register'; diff --git a/src/app/services/friend.service.ts b/src/app/services/friend.service.ts new file mode 100644 index 0000000..6821606 --- /dev/null +++ b/src/app/services/friend.service.ts @@ -0,0 +1,44 @@ +import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http'; +import { Injectable } from '@angular/core'; +import { Guid } from 'guid-typescript'; +import { Observable } from 'rxjs'; +import { AppConstants } from '../app-constants.module'; +import { TokenService } from './token.service'; + +@Injectable({ + providedIn: 'root' +}) +export class FriendService { + constructor(private _http: HttpClient, private _tokenService: TokenService) + { } + + postFriendWithSessionStorageRequest(friendUsername: string): Observable { + const userId = this._tokenService.getUserIdFromSessionStorageToken(); + const token = this._tokenService.getTokenFromSessionStorage(); + + return this.postFriendRequest(userId, token, friendUsername); + } + + deleteFriendWithSessionStorageRequest(friendUsername: string): Observable { + const userId = this._tokenService.getUserIdFromSessionStorageToken(); + const token = this._tokenService.getTokenFromSessionStorage(); + + return this.deleteFriendRequest(userId, token, friendUsername); + } + + postFriendRequest(userId: Guid, authToken: string, friendUsername: string): Observable { + const options = { + params: new HttpParams().set('UserId', userId.toString()).set('FriendUsername', friendUsername), + headers: new HttpHeaders().set('Authorization', 'Bearer ' + authToken) + }; + return this._http.post(AppConstants.API_FRIENDS_URL, {}, options); + } + + deleteFriendRequest(userId: Guid, authToken: string, friendUsername: string): Observable { + const options = { + params: new HttpParams().set('UserId', userId.toString()).set('FriendUsername', friendUsername), + headers: new HttpHeaders().set('Authorization', 'Bearer ' + authToken) + }; + return this._http.delete(AppConstants.API_FRIENDS_URL, options); + } +} -- cgit v1.2.3 From fee6fed64af358ebe410e7d47ea0c8efccec6d4e Mon Sep 17 00:00:00 2001 From: Syndamia Date: Thu, 8 Apr 2021 09:23:56 +0300 Subject: Updated profile component to use friend service and updated html to show only add/remove friend button --- src/app/components/profile/profile.component.html | 9 ++-- src/app/components/profile/profile.component.ts | 52 +++++++++-------------- 2 files changed, 24 insertions(+), 37 deletions(-) (limited to 'src') diff --git a/src/app/components/profile/profile.component.html b/src/app/components/profile/profile.component.html index 1a360fc..2443469 100644 --- a/src/app/components/profile/profile.component.html +++ b/src/app/components/profile/profile.component.html @@ -14,12 +14,9 @@
@{{ user.userName }}
-
- - -
+
diff --git a/src/app/components/profile/profile.component.ts b/src/app/components/profile/profile.component.ts index 83f6517..9914b7f 100644 --- a/src/app/components/profile/profile.component.ts +++ b/src/app/components/profile/profile.component.ts @@ -10,8 +10,7 @@ import { Post } from 'src/models/post.model'; import { FeedService } from 'src/app/services/feed.service'; import { TokenService } from 'src/app/services/token.service'; import { Title } from '@angular/platform-browser'; -import { Friend } from 'src/models/identity/friend.model'; -import { FormBuilder, FormControl, FormGroup } from '@angular/forms'; +import { FriendService } from 'src/app/services/friend.service'; @Component({ selector: 'app-profile', @@ -28,12 +27,10 @@ export class ProfileComponent implements OnInit { public isAdminUser = false; public dataArrived = false; public friendOfUser = false; - public updatingFriendship = false; public user: User; public userPosts: Post[]; - public updateFrienship: FormGroup; - constructor(private _titleService: Title, private _fb: FormBuilder, private _router: Router, private _userService: UserService, private _languageService: LanguageService, private _technologyService: TechnologyService, private _feedService: FeedService, private _location: Location, private _tokenService: TokenService) { + constructor(private _titleService: Title, private _fb: FormBuilder, private _router: Router, private _userService: UserService, private _friendService: FriendService, private _languageService: LanguageService, private _technologyService: TechnologyService, private _feedService: FeedService, private _location: Location, private _tokenService: TokenService) { this._titleService.setTitle(this._title); } @@ -48,10 +45,6 @@ export class ProfileComponent implements OnInit { this.user = this._userService.getDefaultUser(); this.userPosts = []; - this.updateFrienship = this._fb.group({ - password: new FormControl('') - }); - this._userService.getUserByUsernameRequest(this._urlUsername).subscribe({ next: (res: object) => { Object.assign(this.user, res); @@ -141,31 +134,28 @@ export class ProfileComponent implements OnInit { } modifyFriend(): void { - if (this.updatingFriendship) { - this.dataArrived = false; - - this._userService.getUserFromSessionStorageRequest().subscribe({ - next: (result: object) => { - const loggedInUser: User = result as User; - - if (this.friendOfUser) { - loggedInUser.friends = loggedInUser.friends.filter(x => x.userName !== this.user.userName); - } - else { - const newFriend = new Friend(); - newFriend.userName = this.user.userName; - loggedInUser.friends.push(newFriend); - } - - this.updateUserWithNewFriends(loggedInUser); - } - }); + this.dataArrived = false; + if (this.friendOfUser) { + this.removeFriendFromLoggedInUser(); } - this.updatingFriendship = !this.updatingFriendship; + else { + this.addFriendToLoggedInUser(); + } + } + + private addFriendToLoggedInUser(): void { + this._friendService.postFriendWithSessionStorageRequest(this.user.userName).subscribe({ + next: () => { + this.reloadPage(); + }, + error: () => { + this._router.navigate(['/']); + } + }); } - private updateUserWithNewFriends(loggedInUser: User): void { - this._userService.putBareUserFromSessionStorageRequest(loggedInUser, this.updateFrienship.get('password')?.value).subscribe({ + private removeFriendFromLoggedInUser(): void { + this._friendService.deleteFriendWithSessionStorageRequest(this.user.userName).subscribe({ next: () => { this.reloadPage(); }, -- cgit v1.2.3 From 3b778aef651fe9383a281c4c2999f21312ea28ba Mon Sep 17 00:00:00 2001 From: Syndamia Date: Thu, 8 Apr 2021 09:24:10 +0300 Subject: Removed unused bare user requests from user service --- src/app/services/user.service.ts | 17 ----------------- 1 file changed, 17 deletions(-) (limited to 'src') diff --git a/src/app/services/user.service.ts b/src/app/services/user.service.ts index e910ada..690fff5 100644 --- a/src/app/services/user.service.ts +++ b/src/app/services/user.service.ts @@ -45,13 +45,6 @@ export class UserService { return this.putUserRequest(userId, token, updateUserFormGroup, languages, technologies, userRoles, userFriends); } - putBareUserFromSessionStorageRequest(user: User, password: string): Observable { - const userId = this._tokenService.getUserIdFromSessionStorageToken(); - const token = this._tokenService.getTokenFromSessionStorage(); - - return this.putBareUserRequest(userId, token, user, password); - } - deleteUserFromSessionStorageRequest(): Observable { const userId = this._tokenService.getUserIdFromSessionStorageToken(); const token = this._tokenService.getTokenFromSessionStorage(); @@ -133,16 +126,6 @@ export class UserService { return this._http.put(AppConstants.API_USER_URL, body, options); } - putBareUserRequest(userId: Guid, authToken: string, user: User, password: string): Observable { - const body: object = user; - Object.assign(body, { password: password }); - const options = { - params: new HttpParams().set('Id', userId.toString()), - headers: new HttpHeaders().set('Authorization', 'Bearer ' + authToken) - }; - return this._http.put(AppConstants.API_USER_URL, body, options); - } - deleteUserRequest(userId: Guid, authToken: string): Observable { const options = { params: new HttpParams().set('Id', userId.toString()), -- cgit v1.2.3 From 4dbd4298762967657d01d9cd8cd3dd9b31250686 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Thu, 8 Apr 2021 09:25:37 +0300 Subject: Removed bad dependency injection in profile component --- src/app/components/profile/profile.component.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/app/components/profile/profile.component.ts b/src/app/components/profile/profile.component.ts index 9914b7f..f9f64b5 100644 --- a/src/app/components/profile/profile.component.ts +++ b/src/app/components/profile/profile.component.ts @@ -30,7 +30,7 @@ export class ProfileComponent implements OnInit { public user: User; public userPosts: Post[]; - constructor(private _titleService: Title, private _fb: FormBuilder, private _router: Router, private _userService: UserService, private _friendService: FriendService, private _languageService: LanguageService, private _technologyService: TechnologyService, private _feedService: FeedService, private _location: Location, private _tokenService: TokenService) { + constructor(private _titleService: Title, private _router: Router, private _userService: UserService, private _friendService: FriendService, private _languageService: LanguageService, private _technologyService: TechnologyService, private _feedService: FeedService, private _location: Location, private _tokenService: TokenService) { this._titleService.setTitle(this._title); } -- cgit v1.2.3 From bf75b260b2c8b8c11ae3c672ae6378bd24f9f196 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Thu, 8 Apr 2021 09:28:51 +0300 Subject: Improved styling of add/remove friend button --- src/app/components/profile/profile.component.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/app/components/profile/profile.component.html b/src/app/components/profile/profile.component.html index 2443469..c6fdee6 100644 --- a/src/app/components/profile/profile.component.html +++ b/src/app/components/profile/profile.component.html @@ -14,7 +14,7 @@
@{{ user.userName }}
- -- cgit v1.2.3