1 | package com.example.cookbook.repository;
|
---|
2 |
|
---|
3 |
|
---|
4 | import com.example.cookbook.dbConfig.DB;
|
---|
5 | import com.example.cookbook.model.Komentar;
|
---|
6 | import org.springframework.stereotype.Repository;
|
---|
7 |
|
---|
8 | import java.sql.Connection;
|
---|
9 | import java.sql.PreparedStatement;
|
---|
10 | import java.sql.ResultSet;
|
---|
11 | import java.sql.SQLException;
|
---|
12 | import java.time.LocalDateTime;
|
---|
13 | import java.util.ArrayList;
|
---|
14 | import java.util.List;
|
---|
15 |
|
---|
16 | @Repository
|
---|
17 | public class KomentariRepository {
|
---|
18 |
|
---|
19 | public List<Komentar> findAllByRecId(Long recId) throws SQLException {
|
---|
20 | Connection connection = DB.getConnection();
|
---|
21 |
|
---|
22 | String query = "select k.kom_data, k.telefon, k.rec_id, k.ocena, k.text, korisnici.kor_ime, korisnici.prezime\n" +
|
---|
23 | "from komentari k\n" +
|
---|
24 | " left join korisnici on k.telefon = korisnici.telefon\n" +
|
---|
25 | "where rec_id = ?;";
|
---|
26 |
|
---|
27 | PreparedStatement prepStm = connection.prepareStatement(query);
|
---|
28 | prepStm.setLong(1, recId);
|
---|
29 |
|
---|
30 | ResultSet result = prepStm.executeQuery();
|
---|
31 | List<Komentar> komentari = new ArrayList<>();
|
---|
32 |
|
---|
33 | while (result.next()){
|
---|
34 | Komentar komentar = new Komentar();
|
---|
35 | komentar.setKomData(result.getObject("kom_data", LocalDateTime.class));
|
---|
36 | komentar.setTelefon(result.getString("telefon"));
|
---|
37 | komentar.setRecId(result.getLong("rec_id"));
|
---|
38 | komentar.setOcena(result.getInt("ocena"));
|
---|
39 | komentar.setText(result.getString("text"));
|
---|
40 | komentar.setImePrezime(result.getString("kor_ime") + " " + result.getString("prezime"));
|
---|
41 | komentari.add(komentar);
|
---|
42 | }
|
---|
43 |
|
---|
44 | result.close();
|
---|
45 | prepStm.close();
|
---|
46 | DB.closeConnection();
|
---|
47 |
|
---|
48 | return komentari;
|
---|
49 | }
|
---|
50 | }
|
---|