[e5fefbd] | 1 | package com.example.medweb.service.impl;
|
---|
| 2 |
|
---|
| 3 |
|
---|
| 4 | import com.example.medweb.model.Pacient;
|
---|
| 5 | import com.example.medweb.model.exceptions.InvalidArgumentsException;
|
---|
| 6 | import com.example.medweb.model.exceptions.InvalidUserCredentialsException;
|
---|
| 7 | import com.example.medweb.repository.PacientRepository;
|
---|
| 8 | import com.example.medweb.service.PacientService;
|
---|
| 9 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
---|
| 10 | import org.springframework.stereotype.Service;
|
---|
| 11 |
|
---|
| 12 | import java.util.List;
|
---|
| 13 | import java.util.Optional;
|
---|
| 14 | import java.util.Random;
|
---|
| 15 |
|
---|
| 16 | @Service
|
---|
| 17 | public class PacientServiceImpl implements PacientService {
|
---|
| 18 |
|
---|
| 19 |
|
---|
| 20 | private final PacientRepository pacientRepository;
|
---|
| 21 |
|
---|
| 22 | public PacientServiceImpl(PacientRepository pacientRepository) {
|
---|
| 23 |
|
---|
| 24 | this.pacientRepository = pacientRepository;
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | @Override
|
---|
| 28 | public List<Pacient> listAll() {
|
---|
| 29 | return this.pacientRepository.findAll();
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | @Override
|
---|
| 33 | public Optional<Pacient> findById(Integer id){
|
---|
| 34 | return this.pacientRepository.findById(id);
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | @Override
|
---|
| 38 | public Optional<Pacient> findPacientByEmailAndPass(String email, String password) {
|
---|
| 39 | return this.pacientRepository.findPacientByEmailAndPass(email, password);
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | @Override
|
---|
| 43 | public Optional<Pacient> findPacientByEmail(String email) {
|
---|
| 44 | return this.pacientRepository.findPacientByEmail(email);
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | @Override
|
---|
| 48 | public void save(Pacient pacient){
|
---|
| 49 | BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
|
---|
| 50 | String plainPass = pacient.getPass();
|
---|
| 51 | String cypherPass = encoder.encode(plainPass);
|
---|
| 52 | pacient.setPass(cypherPass);
|
---|
| 53 | Random random = new Random();
|
---|
| 54 | Integer pacient_id = random.nextInt();
|
---|
| 55 | pacient.setPacient_id(pacient_id);
|
---|
| 56 | this.pacientRepository.save(pacient);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | @Override
|
---|
| 60 | public Pacient login(String email, String pass) {
|
---|
| 61 | if (email == null || email.isEmpty() || pass == null || pass.isEmpty()) {
|
---|
| 62 | throw new InvalidArgumentsException();
|
---|
| 63 | }
|
---|
| 64 | Pacient pacient = this.pacientRepository.findPacientByEmail(email).get();
|
---|
| 65 | BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
|
---|
| 66 | if (encoder.matches(pass, pacient.getPass())) {
|
---|
| 67 | pass = pacient.getPass();
|
---|
| 68 | }
|
---|
| 69 | return pacientRepository.findPacientByEmailAndPass(email, pass)
|
---|
| 70 | .orElseThrow(InvalidUserCredentialsException::new);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | }
|
---|