Testing branch #47
@@ -12,6 +12,7 @@ import com.faf223.expensetrackerfaf.util.exceptions.UserNotCreatedException;
|
||||
import com.faf223.expensetrackerfaf.util.exceptions.UserNotFoundException;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.security.core.Authentication;
|
||||
@@ -80,5 +81,14 @@ public class UserController {
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,48 +1,48 @@
|
||||
package com.faf223.expensetrackerfaf.model;
|
||||
package com.faf223.expensetrackerfaf.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.*;
|
||||
import jakarta.persistence.*;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.List;
|
||||
|
||||
@Entity(name = "users")
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class User {
|
||||
@Id
|
||||
@Column(name = "user_uuid")
|
||||
@GeneratedValue(strategy = GenerationType.UUID)
|
||||
private String userUuid;
|
||||
@Entity(name = "users")
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class User {
|
||||
@Id
|
||||
@Column(name = "user_uuid")
|
||||
@GeneratedValue(strategy = GenerationType.UUID)
|
||||
private String userUuid;
|
||||
|
||||
@Column(name = "name")
|
||||
@NotNull(message = "First name must not be null")
|
||||
@NotEmpty(message = "First name must not be empty")
|
||||
private String firstName;
|
||||
@Column(name = "name")
|
||||
@NotNull(message = "First name must not be null")
|
||||
@NotEmpty(message = "First name must not be empty")
|
||||
private String firstName;
|
||||
|
||||
@Column(name = "surname")
|
||||
@NotNull(message = "Last name must not be null")
|
||||
@NotEmpty(message = "Last name must not be empty")
|
||||
private String lastName;
|
||||
@Column(name = "surname")
|
||||
@NotNull(message = "Last name must not be null")
|
||||
@NotEmpty(message = "Last name must not be empty")
|
||||
private String lastName;
|
||||
|
||||
@Column(name = "username")
|
||||
@NotNull(message = "Username must not be null")
|
||||
@NotEmpty(message = "Username must not be empty")
|
||||
private String username;
|
||||
@Column(name = "username")
|
||||
@NotNull(message = "Username must not be null")
|
||||
@NotEmpty(message = "Username must not be empty")
|
||||
private String username;
|
||||
|
||||
@Transient
|
||||
// @NotNull(message = "Password must not be null")
|
||||
// @NotEmpty(message = "Password must not be empty")
|
||||
private String password;
|
||||
@Transient
|
||||
// @NotNull(message = "Password must not be null")
|
||||
// @NotEmpty(message = "Password must not be empty")
|
||||
private String password;
|
||||
|
||||
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
|
||||
@ToString.Exclude
|
||||
private List<Expense> expenses;
|
||||
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
|
||||
@ToString.Exclude
|
||||
private List<Expense> expenses;
|
||||
|
||||
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
|
||||
@ToString.Exclude
|
||||
private List<Income> incomes;
|
||||
}
|
||||
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
|
||||
@ToString.Exclude
|
||||
private List<Income> incomes;
|
||||
}
|
||||
|
||||
@@ -12,4 +12,6 @@ public interface CredentialRepository extends JpaRepository<Credential, Long> {
|
||||
Optional<Credential> findByEmail(String email);
|
||||
|
||||
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> 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.repository.CredentialRepository;
|
||||
import com.faf223.expensetrackerfaf.repository.UserRepository;
|
||||
import jakarta.transaction.Transactional;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@@ -36,4 +37,20 @@ public class UserService {
|
||||
}
|
||||
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