From 427d4e156bd0f437e913780fd41e9dfcd8dc8bc9 Mon Sep 17 00:00:00 2001 From: Kacper Jarocki Date: Sat, 21 Oct 2023 14:59:54 +0200 Subject: [PATCH] code formating upgraded --- .../java/server/service/AccountsService.java | 113 ++++---- .../java/server/service/AdminService.java | 254 +++++++++--------- .../java/server/service/NotesService.java | 4 - 3 files changed, 189 insertions(+), 182 deletions(-) diff --git a/server/src/main/java/server/service/AccountsService.java b/server/src/main/java/server/service/AccountsService.java index b38c512..d21903b 100644 --- a/server/src/main/java/server/service/AccountsService.java +++ b/server/src/main/java/server/service/AccountsService.java @@ -1,10 +1,10 @@ package server.service; -import server.email.token.EmailConfirmationToken; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; +import server.email.token.EmailConfirmationToken; import server.email.token.EmailConfirmationTokenService; import server.exceptions.RegisterRequestException; import server.model.Accounts; @@ -17,61 +17,68 @@ @Service @RequiredArgsConstructor public class AccountsService { - public final AccountsRepository accountsRepository; - public final EmailConfirmationTokenService emailConfirmationTokenService; - private final PasswordEncoder passwordEncoder; - public List getAccounts() {return accountsRepository.findAll();} - public Accounts getAccount(int id){return accountsRepository.findById(id).orElseThrow();} - public Accounts postAccount(Accounts Acc) - { - return accountsRepository.save(Acc); - } - public Accounts putAccount(Accounts Acc) - { - Accounts editedAcc = accountsRepository.findById(Acc.getAccountId()).orElseThrow(); - editedAcc.setNameUser(Acc.getNameUser()); - editedAcc.setPassword(Acc.getPassword()); - editedAcc.setEmail(Acc.getEmail()); - return editedAcc; - } - public void deleteAccount(Accounts acc) - { - accountsRepository.delete(acc); - } + public final AccountsRepository accountsRepository; + public final EmailConfirmationTokenService emailConfirmationTokenService; + private final PasswordEncoder passwordEncoder; + + public List getAccounts() { + return accountsRepository.findAll(); + } + + public Accounts getAccount(int id) { + return accountsRepository.findById(id).orElseThrow(); + } + + public Accounts postAccount(Accounts Acc) { + return accountsRepository.save(Acc); + } + + public Accounts putAccount(Accounts Acc) { + Accounts editedAcc = accountsRepository.findById(Acc.getAccountId()).orElseThrow(); + editedAcc.setNameUser(Acc.getNameUser()); + editedAcc.setPassword(Acc.getPassword()); + editedAcc.setEmail(Acc.getEmail()); + return editedAcc; + } + + public void deleteAccount(Accounts acc) { + accountsRepository.delete(acc); + } + + public String signUpUser(Accounts accounts) { - public String signUpUser(Accounts accounts) { + String token = UUID.randomUUID().toString(); - String token = UUID.randomUUID().toString(); + EmailConfirmationToken emailConfirmationToken = new EmailConfirmationToken( + token, + LocalDateTime.now(), + LocalDateTime.now().plusMinutes(15), + accounts + ); + emailConfirmationTokenService.saveConfirmationToken( + emailConfirmationToken); + return token; + } - EmailConfirmationToken emailConfirmationToken = new EmailConfirmationToken( - token, - LocalDateTime.now(), - LocalDateTime.now().plusMinutes(15), - accounts - ); - emailConfirmationTokenService.saveConfirmationToken( - emailConfirmationToken); - return token; - } + public int enableAccounts(String email) { + return accountsRepository.enableAccounts(email); + } - public int enableAccounts(String email) { - return accountsRepository.enableAccounts(email); - } + public void editEmail(Accounts acc) { + Accounts editedAcc = accountsRepository.findByNameUser(acc.getNameUser()); + if (!editedAcc.getEmail().equals(acc.getEmail())) { + if (accountsRepository.existsByEmail(acc.getEmail())) { + throw new RegisterRequestException("Email already exists", HttpStatus.BAD_REQUEST); + } + } + editedAcc.setEmail(acc.getEmail()); + accountsRepository.save(editedAcc); + } - public void editEmail(Accounts acc){ - Accounts editedAcc = accountsRepository.findByNameUser(acc.getNameUser()); - if(!editedAcc.getEmail().equals(acc.getEmail())) { - if (accountsRepository.existsByEmail(acc.getEmail())) { - throw new RegisterRequestException("Email already exists", HttpStatus.BAD_REQUEST); - } - } - editedAcc.setEmail(acc.getEmail()); - accountsRepository.save(editedAcc); - } - public void editPassword(Accounts acc){ - Accounts editedAcc = accountsRepository.findByNameUser(acc.getNameUser()); - editedAcc.setPassword(passwordEncoder.encode(acc.getPassword())); - accountsRepository.save(editedAcc); - } + public void editPassword(Accounts acc) { + Accounts editedAcc = accountsRepository.findByNameUser(acc.getNameUser()); + editedAcc.setPassword(passwordEncoder.encode(acc.getPassword())); + accountsRepository.save(editedAcc); + } - } \ No newline at end of file +} \ No newline at end of file diff --git a/server/src/main/java/server/service/AdminService.java b/server/src/main/java/server/service/AdminService.java index c5932c8..1e8d408 100644 --- a/server/src/main/java/server/service/AdminService.java +++ b/server/src/main/java/server/service/AdminService.java @@ -24,136 +24,140 @@ @Service @RequiredArgsConstructor public class AdminService { - private final AccountsRepository accountsRepository; - private final EmailConfirmationTokenService emailConfirmationTokenService; - private final PasswordEncoder passwordEncoder; - private final AccountsService accountsService; - private final JwtService jwtService; - private final EmailSender emailSender; - private final NotesRepository notesRepository; - private final AccountTypeRespository accountTypeRespository; + private final AccountsRepository accountsRepository; + private final EmailConfirmationTokenService emailConfirmationTokenService; + private final PasswordEncoder passwordEncoder; + private final AccountsService accountsService; + private final JwtService jwtService; + private final EmailSender emailSender; + private final NotesRepository notesRepository; + private final AccountTypeRespository accountTypeRespository; - public List> getAccounts(){ - return accountsRepository.getAccounts(); - } + public List> getAccounts() { + return accountsRepository.getAccounts(); + } - public Map getAccountById(int id){return accountsRepository.findAccountById(id);} - public Accounts postAccount(RegisterRequest request) { - AccountType accountType = accountTypeRespository.findByName(request.getAccountTypeName()); - var user = Accounts.builder() - .nameUser(request.getUser_name()) - .email(request.getEmail()) - .register_date(LocalDate.now()) - .accountType(accountType) - .password(passwordEncoder.encode(request.getPassword())) - .role((Role.USER)) - .build(); - if(accountsRepository.existsByEmail(request.getEmail())) - { - throw new RegisterRequestException("Email already exists", HttpStatus.BAD_REQUEST); - } - if(accountsRepository.existsByNameUser(request.getUser_name())) - { - throw new RegisterRequestException("Name already exists",HttpStatus.BAD_REQUEST); - } - accountsRepository.save(user); - var jwtToken = jwtService.generateToken(user); + public Map getAccountById(int id) { + return accountsRepository.findAccountById(id); + } - String token = accountsService.signUpUser(user); + public Accounts postAccount(RegisterRequest request) { + AccountType accountType = accountTypeRespository.findByName(request.getAccountTypeName()); + var user = Accounts.builder() + .nameUser(request.getUser_name()) + .email(request.getEmail()) + .register_date(LocalDate.now()) + .accountType(accountType) + .password(passwordEncoder.encode(request.getPassword())) + .role((Role.USER)) + .build(); + if (accountsRepository.existsByEmail(request.getEmail())) { + throw new RegisterRequestException("Email already exists", HttpStatus.BAD_REQUEST); + } + if (accountsRepository.existsByNameUser(request.getUser_name())) { + throw new RegisterRequestException("Name already exists", HttpStatus.BAD_REQUEST); + } + accountsRepository.save(user); + var jwtToken = jwtService.generateToken(user); - String link = "http://localhost:8090/activation/" + token; + String token = accountsService.signUpUser(user); - user.setUrl_activation(link); + String link = "http://localhost:8090/activation/" + token; - emailSender.send( - request.getEmail(), - buildEmail(request.getUser_name(), link)); - AuthenticationResponse.builder() - .token(jwtToken) - .build(); - return accountsRepository.save(user); - } - public Accounts putAccount(Accounts acc){ - Accounts editedAcc = accountsRepository.findById(acc.getAccountId()).orElseThrow(); - editedAcc.setNameUser(acc.getNameUser()); - editedAcc.setPassword(passwordEncoder.encode(acc.getPassword())); - editedAcc.setEmail(acc.getEmail()); - editedAcc.setRole(acc.getRole()); - return accountsRepository.save(editedAcc); - } - public void deleteAccount(int id){ - accountsRepository.deleteToken(id); - accountsRepository.deleteTag(id); - notesRepository.deleteNotesByAccounts_AccountId(id); - accountsRepository.deleteById(id); - } - private String buildEmail(String name, String link) { - return "
\n" + - "\n" + - "\n" + - "\n" + - " \n" + - " \n" + - " \n" + - " \n" + - "
\n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - "
\n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - "
\n" + - " \n" + - " \n" + - " Confirm your email\n" + - "
\n" + - " \n" + - "
\n" + - " \n" + - "
\n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - "
\n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - "
\n" + - " \n" + - "
\n" + - "\n" + - "\n" + - "\n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - "


\n" + - " \n" + - "

Hi " + name + ",

Thank you for registering. Please click on the below link to activate your account:

Activate Now

\n Link will expire in 15 minutes.

See you soon

" + - " \n" + - "


\n" + - "\n" + - "
"; - } + user.setUrl_activation(link); + + emailSender.send( + request.getEmail(), + buildEmail(request.getUser_name(), link)); + AuthenticationResponse.builder() + .token(jwtToken) + .build(); + return accountsRepository.save(user); + } + + public Accounts putAccount(Accounts acc) { + Accounts editedAcc = accountsRepository.findById(acc.getAccountId()).orElseThrow(); + editedAcc.setNameUser(acc.getNameUser()); + editedAcc.setPassword(passwordEncoder.encode(acc.getPassword())); + editedAcc.setEmail(acc.getEmail()); + editedAcc.setRole(acc.getRole()); + return accountsRepository.save(editedAcc); + } + + public void deleteAccount(int id) { + accountsRepository.deleteToken(id); + accountsRepository.deleteTag(id); + notesRepository.deleteNotesByAccounts_AccountId(id); + accountsRepository.deleteById(id); + } + + private String buildEmail(String name, String link) { + return "
\n" + + "\n" + + "\n" + + "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + "
\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + "
\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + "
\n" + + " \n" + + " \n" + + " Confirm your email\n" + + "
\n" + + " \n" + + "
\n" + + " \n" + + "
\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + "
\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + "
\n" + + " \n" + + "
\n" + + "\n" + + "\n" + + "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + "


\n" + + " \n" + + "

Hi " + name + ",

Thank you for registering. Please click on the below link to activate your account:

Activate Now

\n Link will expire in 15 minutes.

See you soon

" + + " \n" + + "


\n" + + "\n" + + "
"; + } } diff --git a/server/src/main/java/server/service/NotesService.java b/server/src/main/java/server/service/NotesService.java index 6772f58..1606619 100644 --- a/server/src/main/java/server/service/NotesService.java +++ b/server/src/main/java/server/service/NotesService.java @@ -90,17 +90,13 @@ public NotesDTO putNote(NotesDTO note) { } } tagsRepository.deleteByNoteId(noteToUpdate.getNoteId()); - noteToUpdate.setContent(note.content()); noteToUpdate.setTitle(note.title()); noteToUpdate.setModificationDate(LocalDateTime.now()); noteToUpdate.setUrlAddress(note.url_address()); noteToUpdate.setCategory(category); noteToUpdate.setFavorite(note.favorite()); - notesRepository.save(noteToUpdate); - - for (TagDTO temp : note.tags()) { Tags toSave = new Tags();