1 | package com.example.cookbook.repository;
|
---|
2 |
|
---|
3 |
|
---|
4 | import com.example.cookbook.dbConfig.DB;
|
---|
5 | import com.example.cookbook.model.DostavaDTO;
|
---|
6 | import com.example.cookbook.model.Naracka;
|
---|
7 | import com.example.cookbook.model.Recept;
|
---|
8 | import com.example.cookbook.model.Stavka;
|
---|
9 | import org.springframework.stereotype.Repository;
|
---|
10 |
|
---|
11 | import java.sql.*;
|
---|
12 | import java.time.LocalDateTime;
|
---|
13 | import java.util.ArrayList;
|
---|
14 | import java.util.List;
|
---|
15 |
|
---|
16 | @Repository
|
---|
17 | public class NarackiRepository {
|
---|
18 |
|
---|
19 |
|
---|
20 | public List<Naracka> findAll() throws SQLException {
|
---|
21 |
|
---|
22 | Connection connection = DB.getConnection();
|
---|
23 | Statement stm = connection.createStatement();
|
---|
24 | String query = "select vreme, telefon from naracki;";
|
---|
25 |
|
---|
26 | ResultSet result = stm.executeQuery(query);
|
---|
27 | List<Naracka> naracki = new ArrayList<>();
|
---|
28 |
|
---|
29 | while (result.next()){
|
---|
30 | Naracka naracka = new Naracka();
|
---|
31 | naracka.setVreme(result.getObject("vreme", LocalDateTime.class));
|
---|
32 | naracka.setTelefon(result.getString("telefon"));
|
---|
33 | naracki.add(naracka);
|
---|
34 | }
|
---|
35 |
|
---|
36 | DB.closeConnection();
|
---|
37 | result.close();
|
---|
38 | stm.close();
|
---|
39 | return naracki;
|
---|
40 | }
|
---|
41 |
|
---|
42 | public List<Stavka> findByTelAndVreme(String telefon, LocalDateTime vreme) throws SQLException {
|
---|
43 |
|
---|
44 | Connection connection = DB.getConnection();
|
---|
45 | String query = "select sk.vreme, sk.telefon, r.rec_ime, nacin from so_koi sk\n" +
|
---|
46 | " left join recepti r on r.rec_id = sk.rec_id\n" +
|
---|
47 | "where vreme = ? and telefon = ?;";
|
---|
48 | PreparedStatement prepStm = connection.prepareStatement(query);
|
---|
49 |
|
---|
50 | prepStm.setObject(1, vreme);
|
---|
51 | prepStm.setString(2, telefon);
|
---|
52 |
|
---|
53 | ResultSet result = prepStm.executeQuery();
|
---|
54 | List<Stavka> stavki = new ArrayList<>();
|
---|
55 | while (result.next()){
|
---|
56 | Stavka stavka = new Stavka();
|
---|
57 | stavka.setRecIme(result.getString("rec_ime"));
|
---|
58 | stavka.setTelefon(result.getString("telefon"));
|
---|
59 | stavka.setNacin(result.getString("nacin"));
|
---|
60 | stavka.setVreme(result.getObject("vreme", LocalDateTime.class));
|
---|
61 | stavki.add(stavka);
|
---|
62 | }
|
---|
63 | DB.closeConnection();
|
---|
64 | result.close();
|
---|
65 | prepStm.close();
|
---|
66 | return stavki;
|
---|
67 | }
|
---|
68 |
|
---|
69 | public List<DostavaDTO> findDataForDostava(String telefon, LocalDateTime vreme) throws SQLException {
|
---|
70 | Connection connection = DB.getConnection();
|
---|
71 | String query = "select r.rec_ime, l.adresa, 'Готово' sostojka from so_koi sk\n" +
|
---|
72 | " left join recepti r on\n" +
|
---|
73 | " r.rec_id = sk.rec_id\n" +
|
---|
74 | " left join opsluzuva o on\n" +
|
---|
75 | " o.rec_id = r.rec_id\n" +
|
---|
76 | " left join restorani r2 on\n" +
|
---|
77 | " r2.res_id = o.res_id\n" +
|
---|
78 | " left join se_naogja1 sn on\n" +
|
---|
79 | " sn.res_id = r2.res_id\n" +
|
---|
80 | " left join lokacii l on\n" +
|
---|
81 | " l.l_id = sn.l_id\n" +
|
---|
82 | " where vreme = ? and telefon = ? and sk.nacin = 'Готово'\n" +
|
---|
83 | "union\n" +
|
---|
84 | "select r.rec_ime, l.adresa, s2.s_naziv sostojka from so_koi sk\n" +
|
---|
85 | " left join recepti r on\n" +
|
---|
86 | " r.rec_id = sk.rec_id\n" +
|
---|
87 | " left join sodrzi s on\n" +
|
---|
88 | " r.rec_id = s.rec_id\n" +
|
---|
89 | " left join sostojki s2 on\n" +
|
---|
90 | " s2.s_id = s.s_id\n" +
|
---|
91 | " left join ja_ima ji on\n" +
|
---|
92 | " ji.s_id = s2.s_id\n" +
|
---|
93 | " left join prodavnici p on\n" +
|
---|
94 | " p.pro_id = ji.pro_id\n" +
|
---|
95 | " left join se_naogja2 sn on\n" +
|
---|
96 | " sn.pro_id = p.pro_id\n" +
|
---|
97 | " left join lokacii l on\n" +
|
---|
98 | " l.l_id = sn.l_id\n" +
|
---|
99 | " where vreme = ? and telefon = ? and sk.nacin = 'Состојки'";
|
---|
100 | PreparedStatement prepStm = connection.prepareStatement(query);
|
---|
101 |
|
---|
102 | prepStm.setObject(1, vreme);
|
---|
103 | prepStm.setString(2, telefon);
|
---|
104 | prepStm.setObject(3, vreme);
|
---|
105 | prepStm.setString(4, telefon);
|
---|
106 |
|
---|
107 | ResultSet result = prepStm.executeQuery();
|
---|
108 |
|
---|
109 | List<DostavaDTO> dostavaDTOS = new ArrayList<>();
|
---|
110 | while (result.next()){
|
---|
111 | DostavaDTO stavka = new DostavaDTO(
|
---|
112 | result.getString("rec_ime"),
|
---|
113 | result.getString("adresa"),
|
---|
114 | result.getString("sostojka")
|
---|
115 | );
|
---|
116 |
|
---|
117 | dostavaDTOS.add(stavka);
|
---|
118 | }
|
---|
119 | DB.closeConnection();
|
---|
120 | result.close();
|
---|
121 | prepStm.close();
|
---|
122 | return dostavaDTOS;
|
---|
123 | }
|
---|
124 | }
|
---|