package bg.car_wash.areas.user.serviceImpl;

import bg.car_wash.areas.user.exception.UserNotFoundException;
import bg.car_wash.configurations.Errors;
import bg.car_wash.areas.user.entity.User;
import bg.car_wash.areas.user.models.bindingModels.user.UserLoginBindingModel;
import bg.car_wash.areas.user.models.viewModels.user.UserSessionViewModel;
import bg.car_wash.areas.user.repositories.UserRepository;
import bg.car_wash.areas.user.services.UserService;
import bg.car_wash.utils.parser.interfaces.ModelParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserRepository userRepository;

	@Autowired
	private ModelParser modelParser;

	@Override
	public void createUser(User user) {
		this.userRepository.save(user);
	}

	@Override
	public UserSessionViewModel getUserByFullNameAndPassword(UserLoginBindingModel userLoginBindingModel) {
		User user = this.modelParser.convert(userLoginBindingModel, User.class);
		User loggedUser = this.userRepository.findUserByEmailAndPassword(user.getEmail(), user.getPassword());

		return this.modelParser.convert(loggedUser, UserSessionViewModel.class);
	}

	@Override
	public List<User> findAllUsers() {
		List<User> users = this.userRepository.findAll();
		return users;
	}

	@Transactional
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		User user = this.userRepository.findOneByUsername(username);
		if (user == null) {
			throw  new UserNotFoundException();
//			throw new UsernameNotFoundException(Errors.INVALID_CREDENTIALS);
		}

		return user;
	}
}

@@ -73,277 +73,18 @@
 er.e
-xception.UserNotFoundException;%0Aimport bg.car_wash.configurations.Errors;%0Aimport bg.car_wash.areas.user.entity.User;%0Aimport bg.car_wash.areas.user.models.bindingModels.user.UserLoginBindingModel;%0Aimport bg.car_wash.areas.user.models.viewModels.user.UserSessionViewModel
+ntity.User
 ;%0Aim
@@ -192,16 +192,64 @@
 ervice;%0A
+import bg.car_wash.configurations.error.Errors;%0A
 import b
@@ -871,384 +871,8 @@
 %09%7D%0A%0A
-%09@Override%0A%09public UserSessionViewModel getUserByFullNameAndPassword(UserLoginBindingModel userLoginBindingModel) %7B%0A%09%09User user = this.modelParser.convert(userLoginBindingModel, User.class);%0A%09%09User loggedUser = this.userRepository.findUserByEmailAndPassword(user.getEmail(), user.getPassword());%0A%0A%09%09return this.modelParser.convert(loggedUser, UserSessionViewModel.class);%0A%09%7D%0A%0A
 %09@Ov
@@ -1059,24 +1059,21 @@
 (String 
-username
+email
 ) throws
@@ -1148,28 +1148,23 @@
 neBy
-Username(username
+Email(email
 );%0A
+%0A
 %09%09if
@@ -1181,16 +1181,18 @@
 null) %7B%0A
+//
 %09%09%09throw
@@ -1222,18 +1222,16 @@
 tion();%0A
-//
 %09%09%09throw
@@ -1272,27 +1272,22 @@
 ors.
-INVALID_CREDENTIALS
+NOT_FOUND_USER
 );%0A%09

remove method getUserByFullNameAndPassword() and refactoring using email