From 5175cb89b5af2ab1b1f1da473cfb86d05c442d33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Mon, 4 Nov 2024 11:22:34 +0100 Subject: [PATCH] RLH-17 - Update Account APIs and implement rename --- .../dtos/EditUsernameAccountDto.java | 25 +++++++++++ .../resources/AccountResource.java | 44 +++++++++++++++++++ .../releasehive/resources/AuthResource.java | 5 ++- .../releasehive/resources/UserResource.java | 15 ++----- .../releasehive/services/AccountService.java | 3 ++ .../services/AccountServiceImpl.java | 13 ++++++ 6 files changed, 91 insertions(+), 14 deletions(-) create mode 100644 src/main/java/wtf/beatrice/releasehive/dtos/EditUsernameAccountDto.java create mode 100644 src/main/java/wtf/beatrice/releasehive/resources/AccountResource.java diff --git a/src/main/java/wtf/beatrice/releasehive/dtos/EditUsernameAccountDto.java b/src/main/java/wtf/beatrice/releasehive/dtos/EditUsernameAccountDto.java new file mode 100644 index 0000000..cfc0175 --- /dev/null +++ b/src/main/java/wtf/beatrice/releasehive/dtos/EditUsernameAccountDto.java @@ -0,0 +1,25 @@ +package wtf.beatrice.releasehive.dtos; + +import java.util.UUID; + +public class EditUsernameAccountDto +{ + private String username; + private UUID uuid; + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public UUID getUuid() { + return uuid; + } + + public void setUuid(UUID uuid) { + this.uuid = uuid; + } +} diff --git a/src/main/java/wtf/beatrice/releasehive/resources/AccountResource.java b/src/main/java/wtf/beatrice/releasehive/resources/AccountResource.java new file mode 100644 index 0000000..30adb34 --- /dev/null +++ b/src/main/java/wtf/beatrice/releasehive/resources/AccountResource.java @@ -0,0 +1,44 @@ +package wtf.beatrice.releasehive.resources; + +import org.apache.coyote.BadRequestException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; +import wtf.beatrice.releasehive.dtos.EditUsernameAccountDto; +import wtf.beatrice.releasehive.services.AccountService; +import wtf.beatrice.releasehive.services.UserService; + +import java.util.UUID; + +@RestController +@RequestMapping("/api/v1/account") +public class AccountResource { + + private final AccountService accountService; + private final UserService userService; + + public AccountResource( + @Autowired AccountService accountService, + @Autowired UserService userService) { + this.accountService = accountService; + this.userService = userService; + } + + @PostMapping( + value="/edit", + produces= MediaType.TEXT_PLAIN_VALUE) + public ResponseEntity register(@RequestBody EditUsernameAccountDto userDto) throws BadRequestException { + String username = accountService.changeUsername(userDto); + return ResponseEntity.ok(username); + } + + @DeleteMapping( + value = "/delete/{id}", + produces = MediaType.TEXT_PLAIN_VALUE) + public ResponseEntity deleteUser(@PathVariable("id") String id) { + UUID uuid = UUID.fromString(id); + boolean deleted = userService.deleteUser(uuid); + return ResponseEntity.ok(deleted); + } +} diff --git a/src/main/java/wtf/beatrice/releasehive/resources/AuthResource.java b/src/main/java/wtf/beatrice/releasehive/resources/AuthResource.java index df16dbe..90e5d28 100644 --- a/src/main/java/wtf/beatrice/releasehive/resources/AuthResource.java +++ b/src/main/java/wtf/beatrice/releasehive/resources/AuthResource.java @@ -2,6 +2,7 @@ package wtf.beatrice.releasehive.resources; import org.apache.coyote.BadRequestException; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @@ -30,7 +31,7 @@ public class AuthResource { @PostMapping( value="/register", - produces="application/json") + produces= MediaType.APPLICATION_JSON_VALUE) public ResponseEntity register(@RequestBody RegisterUserDto userDto) throws BadRequestException { User user = accountService.register(userDto); return ResponseEntity.ok(user); @@ -38,7 +39,7 @@ public class AuthResource { @PostMapping( value="/login", - produces="application/json") + produces= MediaType.APPLICATION_JSON_VALUE) public ResponseEntity login(@RequestBody LoginUserDto userDto) throws BadRequestException { User authenticatedUser = accountService.authenticate(userDto); diff --git a/src/main/java/wtf/beatrice/releasehive/resources/UserResource.java b/src/main/java/wtf/beatrice/releasehive/resources/UserResource.java index f0f02a3..d365fb3 100644 --- a/src/main/java/wtf/beatrice/releasehive/resources/UserResource.java +++ b/src/main/java/wtf/beatrice/releasehive/resources/UserResource.java @@ -2,6 +2,7 @@ package wtf.beatrice.releasehive.resources; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; @@ -25,7 +26,7 @@ public class UserResource @GetMapping( value = "/me", - produces = "application/json") + produces= MediaType.APPLICATION_JSON_VALUE) public ResponseEntity authenticatedUser() { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); @@ -36,19 +37,9 @@ public class UserResource @GetMapping( value = "/all", - produces = "application/json") + produces= MediaType.APPLICATION_JSON_VALUE) public ResponseEntity> getAllUsers() { List users = userService.getAllUsers(); return ResponseEntity.ok(users); } - - @DeleteMapping( - value = "/delete/{id}", - produces = "application/json") - public ResponseEntity deleteUser(@PathVariable("id") String id) { - UUID uuid = UUID.fromString(id); - boolean deleted = userService.deleteUser(uuid); - return ResponseEntity.ok(deleted); - } - } diff --git a/src/main/java/wtf/beatrice/releasehive/services/AccountService.java b/src/main/java/wtf/beatrice/releasehive/services/AccountService.java index 2663426..e6912f4 100644 --- a/src/main/java/wtf/beatrice/releasehive/services/AccountService.java +++ b/src/main/java/wtf/beatrice/releasehive/services/AccountService.java @@ -1,6 +1,7 @@ package wtf.beatrice.releasehive.services; import org.apache.coyote.BadRequestException; +import wtf.beatrice.releasehive.dtos.EditUsernameAccountDto; import wtf.beatrice.releasehive.dtos.LoginUserDto; import wtf.beatrice.releasehive.dtos.RegisterUserDto; import wtf.beatrice.releasehive.models.User; @@ -11,4 +12,6 @@ public interface AccountService User register(RegisterUserDto user) throws BadRequestException; User authenticate(LoginUserDto user) throws BadRequestException; + + String changeUsername(EditUsernameAccountDto editData) throws BadRequestException; } diff --git a/src/main/java/wtf/beatrice/releasehive/services/AccountServiceImpl.java b/src/main/java/wtf/beatrice/releasehive/services/AccountServiceImpl.java index 95a4633..1433ead 100644 --- a/src/main/java/wtf/beatrice/releasehive/services/AccountServiceImpl.java +++ b/src/main/java/wtf/beatrice/releasehive/services/AccountServiceImpl.java @@ -4,9 +4,11 @@ import org.apache.coyote.BadRequestException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; import wtf.beatrice.releasehive.config.InternalConfiguration; +import wtf.beatrice.releasehive.dtos.EditUsernameAccountDto; import wtf.beatrice.releasehive.dtos.LoginUserDto; import wtf.beatrice.releasehive.dtos.RegisterUserDto; import wtf.beatrice.releasehive.models.User; @@ -102,4 +104,15 @@ public class AccountServiceImpl implements AccountService { .orElseThrow(); } + @Override + public String changeUsername(EditUsernameAccountDto editData) { + User user = userRepository + .findById(editData.getUuid()) + .orElseThrow(() -> new UsernameNotFoundException("User not found")); + + user.setUsername(editData.getUsername()); + userRepository.save(user); + return user.getUsername(); + } + }