1 | package com.example.service.impl;
|
---|
2 |
|
---|
3 | import com.example.exceptions.InvalidUserCredentialsException;
|
---|
4 | import com.example.exceptions.InvalidUsernameOrPasswordException;
|
---|
5 | import com.example.exceptions.PasswordsDoNotMatchException;
|
---|
6 | import com.example.exceptions.UsernameAlreadyExistsException;
|
---|
7 | import com.example.model.*;
|
---|
8 | import com.example.model.Enumerations.Role;
|
---|
9 | import com.example.repository.UserRepository;
|
---|
10 | import com.example.service.AuthService;
|
---|
11 | import org.springframework.stereotype.Service;
|
---|
12 |
|
---|
13 | @Service
|
---|
14 | public class AuthServiceImpl implements AuthService {
|
---|
15 | private final UserRepository userRepository;
|
---|
16 |
|
---|
17 | public AuthServiceImpl(UserRepository userRepository) {
|
---|
18 | this.userRepository = userRepository;
|
---|
19 | }
|
---|
20 |
|
---|
21 | @Override
|
---|
22 | public User login(String username, String password) {
|
---|
23 | return userRepository.findByUsernameAndPassword(username,
|
---|
24 | password).orElseThrow(InvalidUserCredentialsException::new);
|
---|
25 | }
|
---|
26 |
|
---|
27 | @Override
|
---|
28 | public void registerUser(String name, String username, String number, String password,
|
---|
29 | String rpassword, String role) {
|
---|
30 | if (username == null || username.isEmpty() || password == null || password.isEmpty())
|
---|
31 | throw new InvalidUsernameOrPasswordException();
|
---|
32 | if (!password.equals(rpassword))
|
---|
33 | throw new PasswordsDoNotMatchException();
|
---|
34 | if (this.userRepository.findByUsername(username).isPresent())
|
---|
35 | throw new UsernameAlreadyExistsException(username);
|
---|
36 | userRepository.save(new User(name, username, number, password, Role.ROLE_USER));
|
---|
37 | }
|
---|
38 |
|
---|
39 | @Override
|
---|
40 | public void registerBand(String name, String username, String number, String password,
|
---|
41 | String rpassword, String role, Integer price) {
|
---|
42 | if (username == null || username.isEmpty() || password == null || password.isEmpty())
|
---|
43 | throw new InvalidUsernameOrPasswordException();
|
---|
44 | if (!password.equals(rpassword))
|
---|
45 | throw new PasswordsDoNotMatchException();
|
---|
46 | if (this.userRepository.findByUsername(username).isPresent())
|
---|
47 | throw new UsernameAlreadyExistsException(username);
|
---|
48 | userRepository.save(new Band(name, username, number, password, price, Role.ROLE_BAND));
|
---|
49 | }
|
---|
50 |
|
---|
51 | @Override
|
---|
52 | public void registerCatering(String name, String username, String number, String password,
|
---|
53 | String rpassword, String role,
|
---|
54 | Integer price, String address) {
|
---|
55 | if (username == null || username.isEmpty() || password == null || password.isEmpty())
|
---|
56 | throw new InvalidUsernameOrPasswordException();
|
---|
57 | if (!password.equals(rpassword))
|
---|
58 | throw new PasswordsDoNotMatchException();
|
---|
59 | if (this.userRepository.findByUsername(username).isPresent())
|
---|
60 | throw new UsernameAlreadyExistsException(username);
|
---|
61 | userRepository.save(new Catering(name, username, number, password, price, address, Role.ROLE_CATERING));
|
---|
62 | }
|
---|
63 |
|
---|
64 | @Override
|
---|
65 | public void registerClient(String name, String username, String number, String password,
|
---|
66 | String rpassword, String role) {
|
---|
67 | if (username == null || username.isEmpty() || password == null || password.isEmpty())
|
---|
68 | throw new InvalidUsernameOrPasswordException();
|
---|
69 | if (!password.equals(rpassword))
|
---|
70 | throw new PasswordsDoNotMatchException();
|
---|
71 | if (this.userRepository.findByUsername(username).isPresent())
|
---|
72 | throw new UsernameAlreadyExistsException(username);
|
---|
73 | userRepository.save(new Client(name, username, number, password, Role.ROLE_CLIENT));
|
---|
74 | }
|
---|
75 |
|
---|
76 | @Override
|
---|
77 | public void registerPhotographer(String name, String username, String number, String password,
|
---|
78 | String rpassword, String role, Integer price, String portfolio) {
|
---|
79 | if (username == null || username.isEmpty() || password == null || password.isEmpty())
|
---|
80 | throw new InvalidUsernameOrPasswordException();
|
---|
81 | if (!password.equals(rpassword))
|
---|
82 | throw new PasswordsDoNotMatchException();
|
---|
83 | if (this.userRepository.findByUsername(username).isPresent())
|
---|
84 | throw new UsernameAlreadyExistsException(username);
|
---|
85 | userRepository.save(new Photographer(name, username, number, password, price, portfolio, Role.ROLE_PHOTOGRAPHER));
|
---|
86 | }
|
---|
87 |
|
---|
88 | @Override
|
---|
89 | public void registerWaiter(String name, String username, String number, String password,
|
---|
90 | String rpassword, String role, Integer free_day,
|
---|
91 | Integer experience, Catering catering) {
|
---|
92 | if (username == null || username.isEmpty() || password == null || password.isEmpty())
|
---|
93 | throw new InvalidUsernameOrPasswordException();
|
---|
94 | if (!password.equals(rpassword))
|
---|
95 | throw new PasswordsDoNotMatchException();
|
---|
96 | if (this.userRepository.findByUsername(username).isPresent())
|
---|
97 | throw new UsernameAlreadyExistsException(username);
|
---|
98 | userRepository.save(new Waiter(name, username, number, password, free_day, experience, catering, Role.ROLE_WAITER));
|
---|
99 | }
|
---|
100 |
|
---|
101 | // @Override
|
---|
102 | // public void register(String name, String username, String number, String password,
|
---|
103 | // String rpassword, Role role) {
|
---|
104 | // if (username==null || username.isEmpty() || password==null || password.isEmpty())
|
---|
105 | // throw new InvalidUsernameOrPasswordException();
|
---|
106 | // if (!password.equals(rpassword))
|
---|
107 | // throw new PasswordsDoNotMatchException();
|
---|
108 | // if(this.userRepository.findByUsername(username).isPresent())
|
---|
109 | // throw new UsernameAlreadyExistsException(username);
|
---|
110 | // userRepository.save(new User(name, username, number, password));
|
---|
111 | // }
|
---|
112 | } |
---|