1 | package com.example.baza.service.impl;
|
---|
2 |
|
---|
3 | import com.example.baza.model.Chovek2;
|
---|
4 | import com.example.baza.model.exception.InvalidArgumentsException;
|
---|
5 | import com.example.baza.model.exception.InvalidChovekIdException;
|
---|
6 | import com.example.baza.model.exception.InvalidUserCredentialsException;
|
---|
7 | import com.example.baza.repository.ChovekRepository;
|
---|
8 | import com.example.baza.service.ChovekService;
|
---|
9 | import org.springframework.stereotype.Service;
|
---|
10 |
|
---|
11 | import java.util.List;
|
---|
12 |
|
---|
13 | @Service
|
---|
14 | public class ChovekServiceImpl implements ChovekService {
|
---|
15 | private final ChovekRepository chovekRepository;
|
---|
16 |
|
---|
17 | public ChovekServiceImpl(ChovekRepository chovekRepository) {
|
---|
18 | this.chovekRepository = chovekRepository;
|
---|
19 | }
|
---|
20 |
|
---|
21 | @Override
|
---|
22 | public List<Chovek2> listAll() {
|
---|
23 | return this.chovekRepository.findAll();
|
---|
24 | }
|
---|
25 |
|
---|
26 | @Override
|
---|
27 | public Chovek2 findById(Integer id) {
|
---|
28 | return this.chovekRepository.findById(id).orElseThrow(InvalidChovekIdException::new);
|
---|
29 | }
|
---|
30 |
|
---|
31 | @Override
|
---|
32 | public Chovek2 login(String username, String password) {
|
---|
33 | if(username == null || username.isEmpty() || password == null || password.isEmpty())
|
---|
34 | {
|
---|
35 | throw new InvalidArgumentsException();
|
---|
36 | }
|
---|
37 | return chovekRepository.findByUsernameAndPassword(username,password)
|
---|
38 | .orElseThrow(InvalidUserCredentialsException::new);
|
---|
39 | }
|
---|
40 |
|
---|
41 |
|
---|
42 | @Override
|
---|
43 | public Chovek2 create(String ime, String prezime, String email, String telBroj, String adresa, String embg, String username, String password) {
|
---|
44 | return this.chovekRepository.save(new Chovek2(ime,prezime,email,telBroj,adresa,embg,username,password));
|
---|
45 | }//todo : dali postoi dali ne , exception etc etc...
|
---|
46 |
|
---|
47 | @Override
|
---|
48 | public Chovek2 delete(Integer id) {
|
---|
49 | Chovek2 ch=this.findById(id);
|
---|
50 | this.chovekRepository.delete(ch);
|
---|
51 | return ch;
|
---|
52 | }
|
---|
53 | }
|
---|