1 | package com.example.cookbook.repository;
|
---|
2 |
|
---|
3 |
|
---|
4 | import com.example.cookbook.dbConfig.DB;
|
---|
5 | import com.example.cookbook.model.Komentar;
|
---|
6 | import com.example.cookbook.model.Korisnik;
|
---|
7 | import com.example.cookbook.model.TipKorisnik;
|
---|
8 | import org.springframework.stereotype.Repository;
|
---|
9 |
|
---|
10 | import java.sql.*;
|
---|
11 | import java.time.LocalDateTime;
|
---|
12 | import java.util.ArrayList;
|
---|
13 | import java.util.List;
|
---|
14 |
|
---|
15 | @Repository
|
---|
16 | public class KorisniciRepository {
|
---|
17 | public List<Korisnik> findAllByRole(TipKorisnik tipKorisnik) throws SQLException {
|
---|
18 | Connection connection = DB.getConnection();
|
---|
19 |
|
---|
20 | String query;
|
---|
21 |
|
---|
22 | if (tipKorisnik == TipKorisnik.Posetitel){
|
---|
23 | query = "select p.telefon, k.kor_ime, k.prezime\n" +
|
---|
24 | "from posetiteli p\n" +
|
---|
25 | " left join korisnici k on p.telefon = k.telefon";
|
---|
26 | } else {
|
---|
27 | query = "select v.telefon, k.kor_ime, k.prezime\n" +
|
---|
28 | "from vraboteni v\n" +
|
---|
29 | " left join korisnici k on v.telefon = k.telefon";
|
---|
30 | }
|
---|
31 |
|
---|
32 | Statement stm = connection.createStatement();
|
---|
33 |
|
---|
34 |
|
---|
35 | ResultSet result = stm.executeQuery(query);
|
---|
36 | List<Korisnik> korisnici = new ArrayList<>();
|
---|
37 |
|
---|
38 | while (result.next()){
|
---|
39 | Korisnik korisnik = new Korisnik();
|
---|
40 | korisnik.setTelefon(result.getString("telefon"));
|
---|
41 | korisnik.setIme(result.getString("kor_ime"));
|
---|
42 | korisnik.setPrezime(result.getString("prezime"));
|
---|
43 | korisnik.setTipKorisnik(tipKorisnik);
|
---|
44 | korisnici.add(korisnik);
|
---|
45 | }
|
---|
46 |
|
---|
47 | result.close();
|
---|
48 | stm.close();
|
---|
49 | DB.closeConnection();
|
---|
50 |
|
---|
51 | return korisnici;
|
---|
52 | }
|
---|
53 |
|
---|
54 |
|
---|
55 | }
|
---|