1 | package com.example.villadihovo.service.impl.loginImpl;
|
---|
2 |
|
---|
3 | import com.example.villadihovo.exceptions.InvalidArgumentException;
|
---|
4 | import com.example.villadihovo.exceptions.InvalidUserCredentialsException;
|
---|
5 | import com.example.villadihovo.exceptions.PasswordDoNotMatchException;
|
---|
6 | import com.example.villadihovo.model.users.UserTable;
|
---|
7 | import com.example.villadihovo.repository.users.UserRepository;
|
---|
8 | import com.example.villadihovo.service.login.AuthService;
|
---|
9 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
10 | import org.springframework.stereotype.Service;
|
---|
11 |
|
---|
12 | @Service
|
---|
13 | public class AuthServiceImpl implements AuthService {
|
---|
14 |
|
---|
15 | @Autowired
|
---|
16 | private UserRepository userTableRepository;
|
---|
17 |
|
---|
18 | @Override
|
---|
19 | public UserTable login(String username, String password) {
|
---|
20 | if(username == null || username.isEmpty() || password == null || password.isEmpty())
|
---|
21 | {
|
---|
22 | return userTableRepository.findByUsernameAndPassword(username, password).orElseThrow(InvalidArgumentException::new);
|
---|
23 | } else {
|
---|
24 | return userTableRepository.findByUsernameAndPassword(username, password).orElseThrow(InvalidUserCredentialsException::new);
|
---|
25 | }
|
---|
26 | }
|
---|
27 |
|
---|
28 |
|
---|
29 | @Override
|
---|
30 | public UserTable register(String embg, String address, String email, String username, String password, String repeatPassword, String full_name, String phone_number) {
|
---|
31 | if(username == null || username.isEmpty() || password == null || password.isEmpty())
|
---|
32 | {
|
---|
33 | throw new InvalidArgumentException();
|
---|
34 | }
|
---|
35 | if(!password.equals(repeatPassword))
|
---|
36 | {
|
---|
37 | throw new PasswordDoNotMatchException();
|
---|
38 | }
|
---|
39 | UserTable userTable = new UserTable(embg, address, email, password, username, full_name, phone_number);
|
---|
40 | return userTableRepository.save(userTable);
|
---|
41 | }
|
---|
42 | }
|
---|