[2a5d6a3] | 1 | package com.wediscussmovies.project.service.impl;
|
---|
| 2 |
|
---|
| 3 | import com.wediscussmovies.project.model.User;
|
---|
| 4 | import com.wediscussmovies.project.model.exception.PasswordsDontMatchException;
|
---|
| 5 | import com.wediscussmovies.project.model.exception.UserWithEmailAlreadyExists;
|
---|
| 6 | import com.wediscussmovies.project.model.exception.UserWithUsernameAlreadyExists;
|
---|
| 7 | import com.wediscussmovies.project.repository.UserRepository;
|
---|
| 8 | import com.wediscussmovies.project.service.UserService;
|
---|
| 9 | import org.springframework.http.HttpRequest;
|
---|
| 10 | import org.springframework.stereotype.Service;
|
---|
| 11 |
|
---|
| 12 | import javax.servlet.http.HttpServletRequest;
|
---|
| 13 | import java.util.Optional;
|
---|
| 14 |
|
---|
| 15 | @Service
|
---|
| 16 | public class UserServiceImpl implements UserService {
|
---|
| 17 | private final UserRepository userRepository;
|
---|
| 18 |
|
---|
| 19 | public UserServiceImpl(UserRepository userRepository) {
|
---|
| 20 | this.userRepository = userRepository;
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | @Override
|
---|
| 24 | public Optional<User> login(String email, String password) {
|
---|
| 25 | Optional<User> userEmail = userRepository.findByEmailAndPassword(email, password);
|
---|
| 26 | Optional<User> userUsername = userRepository.findByUsernameAndPassword(email, password);
|
---|
| 27 | if(userEmail.isPresent())
|
---|
| 28 | return userEmail;
|
---|
| 29 | else if(userUsername.isPresent())
|
---|
| 30 | return userUsername;
|
---|
| 31 | else
|
---|
| 32 | return Optional.empty();
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | @Override
|
---|
| 36 | public Optional<User> register(HttpServletRequest request, String email, String password, String confirmPassword,
|
---|
| 37 | String username, String name, String surname) {
|
---|
| 38 | if(email == null || email.isEmpty() || password == null || password.isEmpty() ||
|
---|
| 39 | confirmPassword == null || confirmPassword.isEmpty() || username == null || username.isEmpty() ||
|
---|
| 40 | name == null || name.isEmpty() || surname == null || surname.isEmpty()) {
|
---|
| 41 | request.getSession().setAttribute("error", "Not all of the fields had a value in them, check again.");
|
---|
| 42 | return Optional.empty();
|
---|
| 43 | }
|
---|
| 44 | if(userRepository.findByUsername(username).isPresent()){
|
---|
| 45 | request.getSession().setAttribute("error", new UserWithUsernameAlreadyExists(username).getMessage());
|
---|
| 46 | return Optional.empty();
|
---|
| 47 | }
|
---|
| 48 | if(userRepository.findByEmail(email).isPresent()){
|
---|
| 49 | request.getSession().setAttribute("error", new UserWithEmailAlreadyExists(email).getMessage());
|
---|
| 50 | return Optional.empty();
|
---|
| 51 | }
|
---|
| 52 | if(!password.equals(confirmPassword)){
|
---|
| 53 | request.getSession().setAttribute("error", new PasswordsDontMatchException().getMessage());
|
---|
| 54 | return Optional.empty();
|
---|
| 55 | }
|
---|
| 56 | User user = new User(email, username, password, name, surname);
|
---|
| 57 | return Optional.of(userRepository.save(user));
|
---|
| 58 | }
|
---|
| 59 | }
|
---|