- Timestamp:
- 02/03/24 18:08:44 (9 months ago)
- Branches:
- master
- Children:
- cab1b7d
- Parents:
- aea04dd
- Location:
- src/main
- Files:
-
- 5 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/com/example/cookbook/controller/HomeController.java
raea04dd rd4d8fb9 2 2 3 3 4 import com.example.cookbook.model.Komentar; 5 import com.example.cookbook.model.Recept; 6 import com.example.cookbook.model.Slika; 7 import com.example.cookbook.model.Sostojka; 4 import com.example.cookbook.model.*; 8 5 import com.example.cookbook.model.exception.ReceptNeEPronajdenException; 9 import com.example.cookbook.service.KomentariService; 10 import com.example.cookbook.service.ReceptService; 11 import com.example.cookbook.service.SlikiService; 12 import com.example.cookbook.service.SostojkiService; 6 import com.example.cookbook.service.*; 13 7 import org.springframework.stereotype.Controller; 14 8 import org.springframework.ui.Model; 15 9 import org.springframework.web.bind.annotation.GetMapping; 16 10 import org.springframework.web.bind.annotation.PathVariable; 11 import org.springframework.web.bind.annotation.PostMapping; 12 import org.springframework.web.bind.annotation.RequestParam; 17 13 18 14 import java.sql.SQLException; 15 import java.time.LocalDateTime; 19 16 import java.util.List; 20 17 … … 29 26 private final KomentariService komentariService; 30 27 28 private final KorisniciService korisniciService; 29 31 30 public HomeController(ReceptService receptService, 32 31 SlikiService slikiService, 33 SostojkiService sostojkiService, KomentariService komentariService ) {32 SostojkiService sostojkiService, KomentariService komentariService, KorisniciService korisniciService) { 34 33 35 34 this.receptService = receptService; … … 37 36 this.sostojkiService = sostojkiService; 38 37 this.komentariService = komentariService; 38 this.korisniciService = korisniciService; 39 39 } 40 40 … … 59 59 List<Sostojka> sostojki; 60 60 List<Komentar> komentari; 61 List<Korisnik> posetiteli; 61 62 try { 62 63 recept = receptService.findById(id); … … 64 65 sostojki = sostojkiService.findAllById(id); 65 66 komentari = komentariService.findAllById(id); 67 posetiteli = korisniciService.findAllByRole(TipKorisnik.Posetitel); 66 68 } catch (SQLException e) { 67 69 return "redirect:/error-page/SQL%20Exception"; … … 73 75 model.addAttribute("sostojki", sostojki); 74 76 model.addAttribute("komentari", komentari); 77 model.addAttribute("posetiteli", posetiteli); 75 78 return "recept"; 79 } 80 81 @PostMapping("/komentar/{id}") 82 public String processKomentar(@PathVariable Long id, 83 @RequestParam Integer ocena, 84 @RequestParam String komentar, 85 @RequestParam String telefon){ 86 if (id == null){ 87 return "redirect:/error-page/Ne%20dodavanje%20komentar(Identifikatorot%20na%20receptot%20ne%20postoi)"; 88 } 89 try { 90 komentariService.add(telefon, id, ocena, komentar); 91 } catch (SQLException e) { 92 return "redirect:/error-page/SQL%20Exception"; 93 }catch (IllegalArgumentException e){ 94 return "redirect:/error-page/Illegal%20Arguments"; 95 } 96 return "redirect:/recept/" + id; 76 97 } 77 98 -
src/main/java/com/example/cookbook/model/Komentar.java
raea04dd rd4d8fb9 24 24 public void setImePrezime(String imePrezime) { 25 25 this.imePrezime = imePrezime; 26 } 27 28 public Komentar(LocalDateTime komData, String telefon, Long recId, Integer ocena, String text) { 29 this.komData = komData; 30 this.telefon = telefon; 31 this.recId = recId; 32 this.ocena = ocena; 33 this.text = text; 26 34 } 27 35 -
src/main/java/com/example/cookbook/repository/KomentariRepository.java
raea04dd rd4d8fb9 6 6 import org.springframework.stereotype.Repository; 7 7 8 import java.sql.Connection; 9 import java.sql.PreparedStatement; 10 import java.sql.ResultSet; 11 import java.sql.SQLException; 8 import java.sql.*; 12 9 import java.time.LocalDateTime; 13 10 import java.util.ArrayList; … … 48 45 return komentari; 49 46 } 47 48 public void save(Komentar komentar) throws SQLException { 49 Connection connection = DB.getConnection(); 50 51 String query = "insert into komentari (kom_data, telefon, rec_id, ocena, text) values \n" + 52 " (?, ?, ?, ?, ?);"; 53 54 PreparedStatement prepStm = connection.prepareStatement(query); 55 prepStm.setObject(1, komentar.getKomData()); 56 prepStm.setString(2, komentar.getTelefon()); 57 prepStm.setLong(3, komentar.getRecId()); 58 prepStm.setInt(4, komentar.getOcena()); 59 prepStm.setString(5, komentar.getText()); 60 61 prepStm.executeUpdate(); 62 63 prepStm.close(); 64 DB.closeConnection(); 65 } 50 66 } -
src/main/java/com/example/cookbook/service/KomentariService.java
raea04dd rd4d8fb9 9 9 10 10 List<Komentar> findAllById(Long recId) throws SQLException; 11 12 void add(String telefon, Long id, Integer ocena, String komentar) throws SQLException; 11 13 } -
src/main/java/com/example/cookbook/service/impl/KomentariServiceImpl.java
raea04dd rd4d8fb9 8 8 9 9 import java.sql.SQLException; 10 import java.time.LocalDateTime; 10 11 import java.util.List; 11 12 … … 23 24 return komentariRepository.findAllByRecId(recId); 24 25 } 26 27 @Override 28 public void add(String telefon, Long id, Integer ocena, String komentar) throws SQLException { 29 if (telefon.isBlank() || ocena == null){ 30 throw new IllegalArgumentException(); 31 } 32 komentariRepository.save(new Komentar(LocalDateTime.now(), telefon, id, ocena, komentar)); 33 } 25 34 } -
src/main/resources/templates/recept.html
raea04dd rd4d8fb9 83 83 <select id="telefon" name="telefon" required> 84 84 <option selected></option> 85 <option th:each="tel : ${telefoni}" th:text="tel" th:value="tel"></option> 85 <option th:each="posetitel : ${posetiteli}" 86 th:text="${posetitel.telefon}" 87 th:value="${posetitel.telefon}"></option> 86 88 </select> 87 89 </div>
Note:
See TracChangeset
for help on using the changeset viewer.