RLH-17 - Update Account APIs and implement rename

This commit is contained in:
Bea 2024-11-04 11:22:34 +01:00
parent a87c692a25
commit 5175cb89b5
6 changed files with 91 additions and 14 deletions

View File

@ -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;
}
}

View File

@ -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<String> 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<Boolean> deleteUser(@PathVariable("id") String id) {
UUID uuid = UUID.fromString(id);
boolean deleted = userService.deleteUser(uuid);
return ResponseEntity.ok(deleted);
}
}

View File

@ -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<User> 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<LoginResponse> login(@RequestBody LoginUserDto userDto) throws BadRequestException {
User authenticatedUser = accountService.authenticate(userDto);

View File

@ -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<User> 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<List<User>> getAllUsers() {
List<User> users = userService.getAllUsers();
return ResponseEntity.ok(users);
}
@DeleteMapping(
value = "/delete/{id}",
produces = "application/json")
public ResponseEntity<Boolean> deleteUser(@PathVariable("id") String id) {
UUID uuid = UUID.fromString(id);
boolean deleted = userService.deleteUser(uuid);
return ResponseEntity.ok(deleted);
}
}

View File

@ -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;
}

View File

@ -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();
}
}