Ignore:
Timestamp:
02/04/22 23:22:13 (3 years ago)
Author:
Test <matonikolov77@…>
Branches:
main
Children:
5b447b0
Parents:
3ded84d
Message:

Added CRUD for movies,persons,discussion,replies,genres
Added ajaxcalls

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/com/wediscussmovies/project/service/impl/UserServiceImpl.java

    r3ded84d re0ef1b1  
    11package com.wediscussmovies.project.service.impl;
    22
    3 import com.wediscussmovies.project.model.*;
    4 import com.wediscussmovies.project.model.exception.PasswordsDontMatchException;
    5 import com.wediscussmovies.project.model.exception.UserWithEmailAlreadyExists;
    6 import com.wediscussmovies.project.model.exception.UserWithUsernameAlreadyExists;
     3import com.wediscussmovies.project.model.exception.InvalidArgumentsException;
     4import com.wediscussmovies.project.model.exception.PasswordsDoNotMatchException;
     5import com.wediscussmovies.project.model.exception.UserNotExistException;
     6import com.wediscussmovies.project.model.User;
     7import com.wediscussmovies.project.model.exception.UsernameAlreadyExistsException;
    78import com.wediscussmovies.project.repository.UserRepository;
    89import com.wediscussmovies.project.service.UserService;
     10import org.springframework.security.core.userdetails.UserDetails;
     11import org.springframework.security.core.userdetails.UsernameNotFoundException;
     12import org.springframework.security.crypto.password.PasswordEncoder;
    913import org.springframework.stereotype.Service;
    10 
    11 import javax.servlet.http.HttpServletRequest;
    12 import java.util.Optional;
    1314
    1415@Service
    1516public class UserServiceImpl implements UserService {
     17
    1618    private final UserRepository userRepository;
     19    private final PasswordEncoder passwordEncoder;
    1720
    18     public UserServiceImpl(UserRepository userRepository) {
     21
     22    public UserServiceImpl(UserRepository userRepository, PasswordEncoder passwordEncoder) {
    1923        this.userRepository = userRepository;
     24        this.passwordEncoder = passwordEncoder;
    2025    }
    2126
    2227    @Override
    23     public Optional<User> login(String email, String password) {
    24         Optional<User> userEmail = userRepository.findByEmailAndPassword(email, password);
    25         Optional<User> userUsername = userRepository.findByUsernameAndPassword(email, password);
    26         if(userEmail.isPresent())
    27             return userEmail;
    28         else if(userUsername.isPresent())
    29             return userUsername;
    30         else
    31             return Optional.empty();
     28    public User findByUsername(String username) {
     29        return this.userRepository.findByUsername(username).orElseThrow(() -> new UserNotExistException(username));
     30    }
     31
     32
     33
     34    @Override
     35    public User register( String email, String username, String password, String confirmPassword
     36                                  , String name, String surname) {
     37
     38        if ( username.isEmpty() || password.isEmpty())
     39            throw new InvalidArgumentsException();
     40        if (!password.equals(confirmPassword))
     41            throw new PasswordsDoNotMatchException();
     42        if(this.userRepository.findByUsername(username).isPresent())
     43            throw new UsernameAlreadyExistsException(username);
     44        User user = new User(email,username,passwordEncoder.encode(password),name,surname);
     45        return userRepository.save(user);
     46
    3247    }
    3348
    3449    @Override
    35     public Optional<User> register(HttpServletRequest request, String email, String password, String confirmPassword,
    36                                    String username, String name, String surname) {
    37         if(email == null || email.isEmpty() || password == null || password.isEmpty() ||
    38                 confirmPassword == null || confirmPassword.isEmpty() || username == null || username.isEmpty() ||
    39                 name == null || name.isEmpty() || surname == null || surname.isEmpty()) {
    40             request.getSession().setAttribute("error", "Not all of the fields had a value in them, check again.");
    41             return Optional.empty();
    42         }
    43         if(userRepository.findByUsername(username).isPresent()){
    44             request.getSession().setAttribute("error", new UserWithUsernameAlreadyExists(username).getMessage());
    45             return Optional.empty();
    46         }
    47         if(userRepository.findByEmail(email).isPresent()){
    48             request.getSession().setAttribute("error", new UserWithEmailAlreadyExists(email).getMessage());
    49             return Optional.empty();
    50         }
    51         if(!password.equals(confirmPassword)){
    52             request.getSession().setAttribute("error", new PasswordsDontMatchException().getMessage());
    53             return Optional.empty();
    54         }
    55         User user = new User(email, username, password, name, surname);
    56         return Optional.of(userRepository.save(user));
     50    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
     51        return this.findByUsername(username);
    5752    }
    5853}
Note: See TracChangeset for help on using the changeset viewer.