Add user deleting
This commit is contained in:
@@ -12,6 +12,7 @@ import com.faf223.expensetrackerfaf.util.exceptions.UserNotCreatedException;
|
|||||||
import com.faf223.expensetrackerfaf.util.exceptions.UserNotFoundException;
|
import com.faf223.expensetrackerfaf.util.exceptions.UserNotFoundException;
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.security.access.prepost.PreAuthorize;
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.Authentication;
|
||||||
@@ -80,5 +81,14 @@ public class UserController {
|
|||||||
|
|
||||||
return ResponseEntity.ok(userMapper.toDto(users));
|
return ResponseEntity.ok(userMapper.toDto(users));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/delete/{username}")
|
||||||
|
@PreAuthorize("hasRole('ADMIN')")
|
||||||
|
public ResponseEntity<Void> deleteUserByUsername(@PathVariable String username) {
|
||||||
|
|
||||||
|
userService.deleteByUsername(username);
|
||||||
|
|
||||||
|
return ResponseEntity.status(HttpStatus.OK).build();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -38,11 +38,11 @@ public class User {
|
|||||||
// @NotEmpty(message = "Password must not be empty")
|
// @NotEmpty(message = "Password must not be empty")
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
|
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
|
||||||
@ToString.Exclude
|
@ToString.Exclude
|
||||||
private List<Expense> expenses;
|
private List<Expense> expenses;
|
||||||
|
|
||||||
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
|
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
|
||||||
@ToString.Exclude
|
@ToString.Exclude
|
||||||
private List<Income> incomes;
|
private List<Income> incomes;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,4 +12,6 @@ public interface CredentialRepository extends JpaRepository<Credential, Long> {
|
|||||||
Optional<Credential> findByEmail(String email);
|
Optional<Credential> findByEmail(String email);
|
||||||
|
|
||||||
Optional<Credential> findByUser(User user);
|
Optional<Credential> findByUser(User user);
|
||||||
|
|
||||||
|
void deleteByEmail(String email);
|
||||||
}
|
}
|
||||||
@@ -9,4 +9,6 @@ public interface UserRepository extends JpaRepository<User, String> {
|
|||||||
Optional<User> getUserByUserUuid(String userUuid);
|
Optional<User> getUserByUserUuid(String userUuid);
|
||||||
|
|
||||||
Optional<User> findByUsername(String username);
|
Optional<User> findByUsername(String username);
|
||||||
|
|
||||||
|
void deleteByUsername(String username);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import com.faf223.expensetrackerfaf.model.Credential;
|
|||||||
import com.faf223.expensetrackerfaf.model.User;
|
import com.faf223.expensetrackerfaf.model.User;
|
||||||
import com.faf223.expensetrackerfaf.repository.CredentialRepository;
|
import com.faf223.expensetrackerfaf.repository.CredentialRepository;
|
||||||
import com.faf223.expensetrackerfaf.repository.UserRepository;
|
import com.faf223.expensetrackerfaf.repository.UserRepository;
|
||||||
|
import jakarta.transaction.Transactional;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@@ -36,4 +37,20 @@ public class UserService {
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void deleteByUsername(String username) {
|
||||||
|
|
||||||
|
Optional<User> user = userRepository.findByUsername(username);
|
||||||
|
if (user.isPresent()) {
|
||||||
|
|
||||||
|
Optional<Credential> credential = credentialRepository.findByUser(user.get());
|
||||||
|
|
||||||
|
if (credential.isPresent()) {
|
||||||
|
|
||||||
|
credentialRepository.deleteByEmail(credential.get().getEmail());
|
||||||
|
userRepository.deleteByUsername(username);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user