source: src/main/java/com/example/medweb/service/impl/PacientServiceImpl.java

Last change on this file was e5fefbd, checked in by Anita Terziska <63020646+Nit4e@…>, 2 years ago

initial commit

  • Property mode set to 100644
File size: 2.4 KB
RevLine 
[e5fefbd]1package com.example.medweb.service.impl;
2
3
4import com.example.medweb.model.Pacient;
5import com.example.medweb.model.exceptions.InvalidArgumentsException;
6import com.example.medweb.model.exceptions.InvalidUserCredentialsException;
7import com.example.medweb.repository.PacientRepository;
8import com.example.medweb.service.PacientService;
9import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
10import org.springframework.stereotype.Service;
11
12import java.util.List;
13import java.util.Optional;
14import java.util.Random;
15
16@Service
17public 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}
Note: See TracBrowser for help on using the repository browser.