Changes between Initial Version and Version 1 of Transactions


Ignore:
Timestamp:
08/25/25 13:23:11 (8 days ago)
Author:
221007
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Transactions

    v1 v1  
     1== Трансакции
     2
     3Употребата на трансакции беше од голема корист за операции кои требаа да се извршат или сите или да падне, за лесно да се најде грешката. Во мојата апликација истото го употребив во неколку наврати, кои ќе ги покажам подолу.
     4
     5=== 1. Трансакција при регистрација на нов корисник
     6
     7{{{
     8 @Transactional
     9    public void register_user(String name, String surname, String email, String password, String password_confirmation) {
     10        if (email == null || email.isEmpty() || password == null || password.isEmpty() || password_confirmation == null || password_confirmation.isEmpty()) {
     11            throw new NoExistingCredentialsException("Credentials cannot be empty.");
     12        }
     13        if (!password.equals(password_confirmation)) {
     14            throw new IllegalArgumentException("Passwords do not match. Check the mistake and try again.");
     15        }
     16        if (reportiumUserRepository.findByEmail(email).isPresent()) {
     17            throw new UserAlreadyExistsException(String.format("User with this email '%s' already exists.", email));
     18        }
     19        //creating the user
     20        ReportiumUser new_application_user = new ReportiumUser();
     21        new_application_user.setActive(true);
     22        new_application_user.setName(name);
     23        new_application_user.setSurname(surname);
     24        new_application_user.setEmail(email);
     25        new_application_user.setPasswordHash(passwordEncoder.encode(password));
     26        new_application_user.setCreatedAt(LocalDateTime.now());
     27        ReportiumUser savedUser = reportiumUserRepository.save(new_application_user);
     28        //I have a trigger that creates the user profile
     29        UserProfile user_profile = userProfileRepository.findByReportiumUser(savedUser).get();
     30        UserProfileLog userProfileLog = new UserProfileLog();
     31        userProfileLog.setUserProfile(user_profile);
     32        userProfileLog.setChangedAt(LocalDateTime.now());
     33        String description = String.format("New user <%s %s> with mail '%s' has been registered.", savedUser.getName(), savedUser.getSurname(), savedUser.getEmail());
     34        userProfileLog.setChangeDescription(description);
     35        profileLogRepository.save(userProfileLog);
     36    }
     37}}}
     38
     39
     40=== 2.
     41
     42
     43=== 3.