From e57a669ebd28a9afb9ed609d772756ae5098fa0a Mon Sep 17 00:00:00 2001 From: Kyle9410-Chen Date: Mon, 2 Dec 2024 00:44:46 +0800 Subject: [PATCH] feat: add getting user profile image --- .../commonground/backend/service/user/UserController.java | 5 +++++ .../tw/commonground/backend/service/user/UserService.java | 6 ++++++ .../backend/service/user/entity/UserRepository.java | 2 ++ 3 files changed, 13 insertions(+) diff --git a/src/main/java/tw/commonground/backend/service/user/UserController.java b/src/main/java/tw/commonground/backend/service/user/UserController.java index c4fb3a5..9b93d1b 100644 --- a/src/main/java/tw/commonground/backend/service/user/UserController.java +++ b/src/main/java/tw/commonground/backend/service/user/UserController.java @@ -62,4 +62,9 @@ public ResponseEntity userSetup(@Valid @RequestBody UserSetupReque UserResponse response = UserMapper.toResponse(userEntity); return ResponseEntity.ok(response); } + + @GetMapping(value = "/user/profile-image/{username}", produces = "image/png") + public ResponseEntity getProfileImage(@Valid @NotBlank @PathVariable String username) { + return ResponseEntity.ok(userService.getProfileImage(username)); + } } diff --git a/src/main/java/tw/commonground/backend/service/user/UserService.java b/src/main/java/tw/commonground/backend/service/user/UserService.java index e61b6c2..99f3ba3 100644 --- a/src/main/java/tw/commonground/backend/service/user/UserService.java +++ b/src/main/java/tw/commonground/backend/service/user/UserService.java @@ -89,4 +89,10 @@ public FullUserEntity getMe(String email) { Optional userEntityOptional = userRepository.findUserEntityByEmail(email); return userEntityOptional.orElseThrow(() -> new EntityNotFoundException("User", "email", email)); } + + public byte[] getProfileImage(String username) { + return userRepository.getUserEntityByUsername(username).orElseThrow( + () -> new EntityNotFoundException("User", "email", username) + ).getProfileImage(); + } } diff --git a/src/main/java/tw/commonground/backend/service/user/entity/UserRepository.java b/src/main/java/tw/commonground/backend/service/user/entity/UserRepository.java index 1e96c79..b0837fe 100644 --- a/src/main/java/tw/commonground/backend/service/user/entity/UserRepository.java +++ b/src/main/java/tw/commonground/backend/service/user/entity/UserRepository.java @@ -13,6 +13,8 @@ public interface UserRepository extends CrudRepository { Optional findUserEntityByUsername(String username); + Optional getUserEntityByUsername(String username); + SimpleUserEntity findByEmail(String email); @Query("SELECT u.id FROM UserEntity u WHERE u.uuid = ?1")