refactor(backend): fix typos, dead debug logs, and bare orElseThrow calls
- UserService: remove debug log dumping all DB groups ("Groupds in DB"),
fix indentation of createUserOrUpdate, clean up log messages
- DocumentService: fix typo reciever → receiver in searchDocuments parameter,
remove broken log.info("Tags", tags) with missing format specifier,
replace bare orElseThrow() with DomainException in updateDocumentTags
and createDocument, remove what-comments on Lombok annotations
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -29,8 +29,8 @@ import java.util.UUID;
|
|||||||
import static org.raddatz.familienarchiv.repository.DocumentSpecifications.*;
|
import static org.raddatz.familienarchiv.repository.DocumentSpecifications.*;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor // Lombok: Erzeugt Constructor für 'final' Felder (Dependency Injection)
|
@RequiredArgsConstructor
|
||||||
@Slf4j // Lombok: Logging
|
@Slf4j
|
||||||
public class DocumentService {
|
public class DocumentService {
|
||||||
|
|
||||||
private final DocumentRepository documentRepository;
|
private final DocumentRepository documentRepository;
|
||||||
@@ -102,8 +102,10 @@ public class DocumentService {
|
|||||||
.filter(s -> !s.isEmpty())
|
.filter(s -> !s.isEmpty())
|
||||||
.toList();
|
.toList();
|
||||||
}
|
}
|
||||||
updateDocumentTags(doc.getId(), tags);
|
UUID savedId = doc.getId();
|
||||||
doc = documentRepository.findById(doc.getId()).orElseThrow();
|
updateDocumentTags(savedId, tags);
|
||||||
|
doc = documentRepository.findById(savedId)
|
||||||
|
.orElseThrow(() -> DomainException.notFound(ErrorCode.DOCUMENT_NOT_FOUND, "Document not found after save: " + savedId));
|
||||||
|
|
||||||
// Sender
|
// Sender
|
||||||
if (dto.getSenderId() != null) {
|
if (dto.getSenderId() != null) {
|
||||||
@@ -180,7 +182,8 @@ public class DocumentService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Document updateDocumentTags(UUID docId, List<String> tagNames) {
|
public Document updateDocumentTags(UUID docId, List<String> tagNames) {
|
||||||
Document doc = documentRepository.findById(docId).orElseThrow();
|
Document doc = documentRepository.findById(docId)
|
||||||
|
.orElseThrow(() -> DomainException.notFound(ErrorCode.DOCUMENT_NOT_FOUND, "Document not found: " + docId));
|
||||||
|
|
||||||
Set<Tag> newTags = new HashSet<>();
|
Set<Tag> newTags = new HashSet<>();
|
||||||
|
|
||||||
@@ -217,12 +220,11 @@ public class DocumentService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 1. Allgemeine Suche (für das Suchfeld im Frontend)
|
// 1. Allgemeine Suche (für das Suchfeld im Frontend)
|
||||||
public List<Document> searchDocuments(String text, LocalDate from, LocalDate to, UUID sender, UUID reciever, List<String> tags) {
|
public List<Document> searchDocuments(String text, LocalDate from, LocalDate to, UUID sender, UUID receiver, List<String> tags) {
|
||||||
log.info("Tags", tags);
|
|
||||||
Specification<Document> spec = Specification.where(hasText(text))
|
Specification<Document> spec = Specification.where(hasText(text))
|
||||||
.and(isBetween(from, to))
|
.and(isBetween(from, to))
|
||||||
.and(hasSender(sender))
|
.and(hasSender(sender))
|
||||||
.and(hasReceiver(reciever))
|
.and(hasReceiver(receiver))
|
||||||
.and(hasTags(tags));
|
.and(hasTags(tags));
|
||||||
|
|
||||||
// Immer sortiert nach Datum
|
// Immer sortiert nach Datum
|
||||||
|
|||||||
@@ -30,49 +30,45 @@ public class UserService {
|
|||||||
private final UserGroupRepository groupRepository;
|
private final UserGroupRepository groupRepository;
|
||||||
private final PasswordEncoder passwordEncoder;
|
private final PasswordEncoder passwordEncoder;
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public AppUser createUserOrUpdate(CreateUserRequest request) {
|
public AppUser createUserOrUpdate(CreateUserRequest request) {
|
||||||
log.info("Versuche neuen User anzulegen: {}", request.getUsername());
|
log.info("Creating or updating user: {}", request.getUsername());
|
||||||
|
|
||||||
Set<UserGroup> groups = new HashSet<>();
|
Set<UserGroup> groups = new HashSet<>();
|
||||||
if (request.getGroupIds() != null && !request.getGroupIds().isEmpty()) {
|
if (request.getGroupIds() != null && !request.getGroupIds().isEmpty()) {
|
||||||
List<UserGroup> foundGroups = groupRepository.findAllById(request.getGroupIds());
|
groups.addAll(groupRepository.findAllById(request.getGroupIds()));
|
||||||
groups.addAll(foundGroups);
|
}
|
||||||
|
|
||||||
|
Optional<AppUser> existingUser = userRepository.findByUsername(request.getUsername());
|
||||||
|
AppUser user;
|
||||||
|
|
||||||
|
if (existingUser.isPresent()) {
|
||||||
|
log.info("User exists, updating: {}", request.getUsername());
|
||||||
|
user = existingUser.get().updateFromRequest(request, passwordEncoder, groups);
|
||||||
|
} else {
|
||||||
|
log.info("Creating new user: {}", request.getUsername());
|
||||||
|
user = AppUser.builder()
|
||||||
|
.username(request.getUsername())
|
||||||
|
.email(request.getEmail())
|
||||||
|
.password(passwordEncoder.encode(request.getInitialPassword()))
|
||||||
|
.groups(groups)
|
||||||
|
.enabled(true)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
return userRepository.save(user);
|
||||||
}
|
}
|
||||||
log.info("GroupsIds {}", groups.toString());
|
|
||||||
log.info("Groupds in DB {}", groupRepository.findAll().toString());
|
|
||||||
|
|
||||||
Optional<AppUser> dbUser = userRepository.findByUsername(request.getUsername());
|
|
||||||
AppUser user;
|
|
||||||
|
|
||||||
if (dbUser.isPresent()) {
|
|
||||||
log.info("Found user in DB. Will update.");
|
|
||||||
user = dbUser.get().updateFromRequest(request, passwordEncoder, groups);
|
|
||||||
} else {
|
|
||||||
log.info("Creating new user.");
|
|
||||||
user = AppUser.builder()
|
|
||||||
.username(request.getUsername())
|
|
||||||
.email(request.getEmail())
|
|
||||||
.password(passwordEncoder.encode(request.getInitialPassword()))
|
|
||||||
.groups(groups)
|
|
||||||
.enabled(true)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
log.info("Saving new user {}", user.toString());
|
|
||||||
return userRepository.save(user);
|
|
||||||
}
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public void deleteUser(UUID userId) {
|
public void deleteUser(UUID userId) {
|
||||||
log.info("Delete user {}", userId);
|
|
||||||
|
|
||||||
AppUser user = userRepository.findById(userId)
|
AppUser user = userRepository.findById(userId)
|
||||||
.orElseThrow(() -> DomainException.notFound(ErrorCode.USER_NOT_FOUND, String.format("No user found for id %s", userId)));
|
.orElseThrow(() -> DomainException.notFound(ErrorCode.USER_NOT_FOUND, "No user found for id: " + userId));
|
||||||
userRepository.delete(user);
|
userRepository.delete(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
public AppUser findByUsername(String username) {
|
public AppUser findByUsername(String username) {
|
||||||
return userRepository.findByUsername(username).orElseThrow(
|
return userRepository.findByUsername(username)
|
||||||
() -> DomainException.notFound(ErrorCode.USER_NOT_FOUND, String.format("No user found for username %s", username)));
|
.orElseThrow(() -> DomainException.notFound(ErrorCode.USER_NOT_FOUND, "No user found for username: " + username));
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<AppUser> getAllUsers() {
|
public List<AppUser> getAllUsers() {
|
||||||
|
|||||||
Reference in New Issue
Block a user