From aeeb66b833990be5a7072ab08edd6ba6d74159f3 Mon Sep 17 00:00:00 2001 From: Art Date: Wed, 22 Sep 2021 14:26:50 +0300 Subject: [PATCH] 182. Update profile image back end call (#26) --- .../backend/exception/ExceptionHandling.java | 4 +++- .../src/app/component/user/user.component.ts | 21 +++++++++++++++++-- .../src/app/service/user.service.ts | 6 +++--- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/support-portal-backend/src/main/java/net/shyshkin/study/fullstack/supportportal/backend/exception/ExceptionHandling.java b/support-portal-backend/src/main/java/net/shyshkin/study/fullstack/supportportal/backend/exception/ExceptionHandling.java index b75f411..9c675ab 100644 --- a/support-portal-backend/src/main/java/net/shyshkin/study/fullstack/supportportal/backend/exception/ExceptionHandling.java +++ b/support-portal-backend/src/main/java/net/shyshkin/study/fullstack/supportportal/backend/exception/ExceptionHandling.java @@ -18,6 +18,7 @@ import org.springframework.validation.BindException; import org.springframework.web.HttpRequestMethodNotSupportedException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.multipart.MaxUploadSizeExceededException; import javax.persistence.NoResultException; import java.io.IOException; @@ -65,7 +66,8 @@ public class ExceptionHandling { @ExceptionHandler({ EmailExistsException.class, UsernameExistsException.class, - EmailNotFoundException.class, UserNotFoundException.class + EmailNotFoundException.class, UserNotFoundException.class, + MaxUploadSizeExceededException.class }) public ResponseEntity badRequestExceptionHandler(Exception exception) { return createHttpResponse(BAD_REQUEST, exception.getMessage()); diff --git a/support-portal-frontend/src/app/component/user/user.component.ts b/support-portal-frontend/src/app/component/user/user.component.ts index 91cd098..2bdc75f 100644 --- a/support-portal-frontend/src/app/component/user/user.component.ts +++ b/support-portal-frontend/src/app/component/user/user.component.ts @@ -4,7 +4,7 @@ import {User} from "../../model/user"; import {UserService} from "../../service/user.service"; import {NotificationService} from "../../service/notification.service"; import {NotificationType} from "../../notification/notification-type"; -import {HttpErrorResponse} from "@angular/common/http"; +import {HttpErrorResponse, HttpEvent} from "@angular/common/http"; import {NgForm} from "@angular/forms"; import {CustomHttpResponse} from "../../dto/custom-http-response"; import {AuthenticationService} from "../../service/authentication.service"; @@ -234,6 +234,23 @@ export class UserComponent implements OnInit, OnDestroy { } public onUpdateProfileImage(): void { - + if (!this.profileImage) return; + this.refreshing = true; + const formData = new FormData(); + formData.append("profileImage", this.profileImage); + let user = this.authenticationService.getUserFromLocalStorage(); + let subscription = this.userService.updateProfileImage(user.username, formData) + .subscribe( + (event: HttpEvent) => { + this.notificationService.notify(NotificationType.SUCCESS, `Profile image updated successfully: ${event}`); + this.refreshing = false; + this.getUsers(false); + }, + (errorResponse: HttpErrorResponse) => { + this.sendErrorNotification(errorResponse.error.message); + this.refreshing = false; + } + ); + this.subscriptions.push(subscription); } } diff --git a/support-portal-frontend/src/app/service/user.service.ts b/support-portal-frontend/src/app/service/user.service.ts index a67b0a5..2c9680d 100644 --- a/support-portal-frontend/src/app/service/user.service.ts +++ b/support-portal-frontend/src/app/service/user.service.ts @@ -1,6 +1,6 @@ import {Injectable} from '@angular/core'; import {environment} from "../../environments/environment"; -import {HttpClient, HttpErrorResponse, HttpEvent} from "@angular/common/http"; +import {HttpClient, HttpEvent} from "@angular/common/http"; import {Observable} from "rxjs"; import {User} from "../model/user"; import {CustomHttpResponse} from "../dto/custom-http-response"; @@ -37,9 +37,9 @@ export class UserService { .post(`${this.host}/user/resetPassword/${email}`, null); } - public updateProfileImage(username: string, formData: FormData): Observable> { + public updateProfileImage(username: string, formData: FormData): Observable> { return this.httpClient - .put(`${this.host}/user/${username}/profileImage`, formData, + .put(`${this.host}/user/${username}/profileImage`, formData, { reportProgress: true, observe: 'events'