Changeset 501396e for src/main/java
- Timestamp:
- 02/03/24 15:58:58 (9 months ago)
- Branches:
- master
- Children:
- aea04dd
- Parents:
- 3e572eb
- Location:
- src/main/java/com/example/cookbook
- Files:
-
- 16 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/com/example/cookbook/controller/HomeController.java
r3e572eb r501396e 1 package com.example.cookbook.controller;public class HomeController { 1 package com.example.cookbook.controller; 2 3 4 import com.example.cookbook.model.Recept; 5 import com.example.cookbook.model.Slika; 6 import com.example.cookbook.model.Sostojka; 7 import com.example.cookbook.model.exception.ReceptNeEPronajdenException; 8 import com.example.cookbook.service.ReceptService; 9 import com.example.cookbook.service.SlikiService; 10 import com.example.cookbook.service.SostojkiService; 11 import org.springframework.stereotype.Controller; 12 import org.springframework.ui.Model; 13 import org.springframework.web.bind.annotation.GetMapping; 14 import org.springframework.web.bind.annotation.PathVariable; 15 16 import java.sql.SQLException; 17 import java.util.List; 18 19 @Controller 20 public class HomeController { 21 22 private final ReceptService receptService; 23 private final SlikiService slikiService; 24 25 private final SostojkiService sostojkiService; 26 27 public HomeController(ReceptService receptService, SlikiService slikiService, SostojkiService sostojkiService) { 28 29 this.receptService = receptService; 30 this.slikiService = slikiService; 31 this.sostojkiService = sostojkiService; 32 } 33 34 @GetMapping({"/", "/recepti"}) 35 public String getHomePage(Model model){ 36 37 List<Recept> recepti = null; 38 try { 39 recepti = receptService.listAll(); 40 } catch (SQLException e) { 41 return "redirect:/error-page/SQL%20Exception"; 42 } 43 model.addAttribute("recepti", recepti); 44 return "home"; 45 } 46 47 @GetMapping("/recept/{id}") 48 public String getReceptPage(@PathVariable Long id, Model model){ 49 50 Recept recept = null; 51 List<Slika> sliki; 52 List<Sostojka> sostojki; 53 try { 54 recept = receptService.findById(id); 55 sliki = slikiService.findAllPicById(id); 56 sostojki = sostojkiService.findAllById(id); 57 } catch (SQLException e) { 58 return "redirect:/error-page/SQL%20Exception"; 59 }catch (ReceptNeEPronajdenException e){ 60 return "redirect:/error-page/" + e.getMessage(); 61 } 62 model.addAttribute("recept", recept); 63 model.addAttribute("sliki", sliki); 64 model.addAttribute("sostojki", sostojki); 65 return "recept"; 66 } 67 68 69 @GetMapping("/error-page/{error}") 70 public String getErrorPage(@PathVariable(required = false) String error, Model model){ 71 72 if (error != null){ 73 model.addAttribute("error", error); 74 } 75 return "error-page"; 76 } 2 77 } -
src/main/java/com/example/cookbook/dbConfig/DB.java
r3e572eb r501396e 1 package com.example.cookbook.dbConfig;public class DB { 1 package com.example.cookbook.dbConfig; 2 3 4 import org.springframework.stereotype.Component; 5 6 7 import java.sql.Connection; 8 import java.sql.DriverManager; 9 import java.sql.SQLException; 10 11 @Component 12 public class DB { 13 // private static final String SSH_HOST = "194.149.135.130"; 14 // private static final String SSH_USER = "t_cbdb"; 15 // private static final String SSH_PASSWORD = "b6cd27a3"; 16 17 private static final String DB_URL = "jdbc:postgresql://localhost:9999/db_202324z_va_prj_cbdb"; 18 private static final String DB_USERNAME = "db_202324z_va_prj_cbdb_owner"; 19 private static final String DB_PASSWORD = "d922daf2bfec"; 20 21 22 private static final String LDB_URL = "jdbc:postgresql://localhost:5432/cbdb"; 23 private static final String LDB_USERNAME = "postgres"; 24 private static final String LDB_PASSWORD = "04UF@bak"; 25 26 private static Connection connection = null; 27 28 29 30 31 32 33 private static void setConnection() throws SQLException { 34 35 if (connection == null || connection.isClosed()){ 36 connection = DriverManager.getConnection(LDB_URL, LDB_USERNAME, LDB_PASSWORD); 37 connection.createStatement().execute("set search_path to project"); 38 } 39 } 40 41 public static Connection getConnection() throws SQLException { 42 setConnection(); 43 return connection; 44 } 45 46 47 public static void closeConnection() throws SQLException { 48 if (connection != null){ 49 connection.close(); 50 } 51 } 2 52 } 53 -
src/main/java/com/example/cookbook/model/DostavaDTO.java
r3e572eb r501396e 1 package com.example.cookbook.model;public class Dostava { 1 package com.example.cookbook.model; 2 3 import java.time.LocalDateTime; 4 5 public record DostavaDTO (String recIme, String adresa, String sostojka, LocalDateTime vreme, String telefon){ 6 public DostavaDTO(String recIme, String adresa, String sostojka, LocalDateTime vreme, String telefon) { 7 this.recIme = recIme; 8 this.adresa = adresa; 9 this.sostojka = sostojka; 10 this.vreme = vreme; 11 this.telefon = telefon; 12 } 2 13 } -
src/main/java/com/example/cookbook/model/Recept.java
r3e572eb r501396e 1 1 package com.example.cookbook.model; 2 2 3 public class Recept ForList{3 public class Recept { 4 4 5 5 private Long recId; 6 6 7 7 private String recIme; 8 9 public String getPostapka() {10 return postapka;11 }12 13 public void setPostapka(String postapka) {14 this.postapka = postapka;15 }16 8 17 9 private String postapka; … … 27 19 } 28 20 29 public Recept ForList(Long recId, String recIme, String postapka) {21 public Recept(Long recId, String recIme, String postapka) { 30 22 this.recId = recId; 31 23 this.recIme = recIme; … … 34 26 } 35 27 36 public Recept ForList() {28 public Recept() { 37 29 } 38 30 … … 53 45 } 54 46 47 public String getPostapka() { 48 return postapka; 49 } 55 50 51 public void setPostapka(String postapka) { 52 this.postapka = postapka; 53 } 56 54 } -
src/main/java/com/example/cookbook/model/Slika.java
r3e572eb r501396e 1 package com.example.cookbook.model;public class Slika { 1 package com.example.cookbook.model; 2 3 public class Slika { 4 5 private Integer redenBroj; 6 7 private Long recId; 8 9 private String pic; 10 11 public Slika() { 12 } 13 14 public Slika(Integer redenBroj, Long recId, String pic) { 15 this.redenBroj = redenBroj; 16 this.recId = recId; 17 this.pic = pic; 18 } 19 20 public String getPic() { 21 return pic; 22 } 23 24 public void setPic(String pic) { 25 this.pic = pic; 26 } 27 28 public Integer getRedenBroj() { 29 return redenBroj; 30 } 31 32 public void setRedenBroj(Integer redenBroj) { 33 this.redenBroj = redenBroj; 34 } 35 36 public Long getRecId() { 37 return recId; 38 } 39 40 public void setRecId(Long recId) { 41 this.recId = recId; 42 } 2 43 } -
src/main/java/com/example/cookbook/model/Sostojka.java
r3e572eb r501396e 1 package com.example.cookbook.model;public class Sostojki { 1 package com.example.cookbook.model; 2 3 public class Sostojka { 4 5 private Long sId; 6 7 private String sNaziv; 8 9 public Sostojka(Long sId, String sNaziv) { 10 this.sId = sId; 11 this.sNaziv = sNaziv; 12 } 13 14 public Sostojka() { 15 } 16 17 public Long getsId() { 18 return sId; 19 } 20 21 public void setsId(Long sId) { 22 this.sId = sId; 23 } 24 25 public String getsNaziv() { 26 return sNaziv; 27 } 28 29 public void setsNaziv(String sNaziv) { 30 this.sNaziv = sNaziv; 31 } 2 32 } -
src/main/java/com/example/cookbook/model/exception/ReceptNeEPronajdenException.java
r3e572eb r501396e 1 package com.example.cookbook.model.exception;public class ReceptNeEPronajdenException { 1 package com.example.cookbook.model.exception; 2 3 public class ReceptNeEPronajdenException extends RuntimeException{ 4 public ReceptNeEPronajdenException() { 5 super("Receptot ne e pronajden"); 6 } 2 7 } -
src/main/java/com/example/cookbook/repository/ReceptRepository.java
r3e572eb r501396e 1 package com.example.cookbook.repository;public class ReceptRepository { 1 package com.example.cookbook.repository; 2 3 4 import com.example.cookbook.dbConfig.DB; 5 import com.example.cookbook.model.Recept; 6 import org.springframework.stereotype.Repository; 7 8 import java.sql.*; 9 import java.util.ArrayList; 10 import java.util.List; 11 import java.util.Optional; 12 13 @Repository 14 public class ReceptRepository { 15 16 17 public List<Recept> findAll() throws SQLException { 18 Connection connection = DB.getConnection(); 19 20 Statement stm = connection.createStatement(); 21 String query = "select r.rec_id, r.rec_ime , r.postapka, avg(k.ocena) average\n" + 22 "from recepti r\n" + 23 " left join komentari k\n" + 24 " on r.rec_id = k.rec_id\n" + 25 "group by r.rec_id, r.rec_ime"; 26 ResultSet result = stm.executeQuery(query); 27 28 List<Recept> recepti = new ArrayList<>(); 29 while (result.next()){ 30 Recept recept = new Recept(); 31 32 recept.setRecId(result.getLong("rec_id")); 33 recept.setRecIme(result.getString("rec_ime")); 34 recept.setPostapka(result.getString("postapka")); 35 recept.setSrednaOcena(result.getFloat("average")); 36 37 recepti.add(recept); 38 39 } 40 stm.close(); 41 result.close(); 42 DB.closeConnection(); 43 return recepti; 44 } 45 46 public Optional<Recept> findById(Long recId) throws SQLException { 47 Connection connection = DB.getConnection(); 48 49 String query = "select * from recepti r where r.rec_id = ?"; 50 PreparedStatement prepStm = connection.prepareStatement(query); 51 prepStm.setLong(1, recId); 52 53 ResultSet result = prepStm.executeQuery(); 54 Recept recept = null; 55 if(result.next()){ 56 recept = new Recept(); 57 recept.setRecId(result.getLong("rec_id")); 58 recept.setRecIme(result.getString("rec_ime")); 59 recept.setPostapka(result.getString("postapka")); 60 } 61 result.close(); 62 prepStm.close(); 63 DB.closeConnection(); 64 return Optional.ofNullable(recept); 65 66 } 2 67 } -
src/main/java/com/example/cookbook/repository/SlikiZaReceptRepository.java
r3e572eb r501396e 1 package com.example.cookbook.repository;public class SlikiZaReceptRepository { 1 package com.example.cookbook.repository; 2 3 import com.example.cookbook.dbConfig.DB; 4 import com.example.cookbook.model.Recept; 5 import com.example.cookbook.model.Slika; 6 import org.springframework.stereotype.Repository; 7 8 import java.nio.charset.StandardCharsets; 9 import java.sql.Connection; 10 import java.sql.PreparedStatement; 11 import java.sql.ResultSet; 12 import java.sql.SQLException; 13 import java.util.ArrayList; 14 import java.util.List; 15 16 @Repository 17 public class SlikiZaReceptRepository { 18 19 public List<Slika> findAllForRecipe(Long recId) throws SQLException { 20 Connection connection = DB.getConnection(); 21 22 String query = "select * from sliki s where s.rec_id = ? order by s.reden_broj asc"; 23 PreparedStatement prepStm = connection.prepareStatement(query); 24 prepStm.setLong(1, recId); 25 26 ResultSet result = prepStm.executeQuery(); 27 List<Slika> sliki = new ArrayList<>(); 28 29 while (result.next()){ 30 Slika slika = new Slika(); 31 slika.setRecId(result.getLong("rec_id")); 32 slika.setRedenBroj(result.getInt("reden_broj")); 33 slika.setPic(new String(result.getBytes("slika"), StandardCharsets.UTF_8)); 34 sliki.add(slika); 35 } 36 37 result.close(); 38 prepStm.close(); 39 DB.closeConnection(); 40 41 return sliki; 42 } 2 43 } -
src/main/java/com/example/cookbook/repository/SostojkiRespository.java
r3e572eb r501396e 1 package com.example.cookbook.repository;public class SostojkiRespository { 1 package com.example.cookbook.repository; 2 3 4 import com.example.cookbook.dbConfig.DB; 5 import com.example.cookbook.model.Slika; 6 import com.example.cookbook.model.Sostojka; 7 import org.springframework.stereotype.Repository; 8 9 import java.nio.charset.StandardCharsets; 10 import java.sql.Connection; 11 import java.sql.PreparedStatement; 12 import java.sql.ResultSet; 13 import java.sql.SQLException; 14 import java.util.ArrayList; 15 import java.util.List; 16 17 @Repository 18 public class SostojkiRespository { 19 20 21 public List<Sostojka> findAllByRId(Long rId) throws SQLException { 22 Connection connection = DB.getConnection(); 23 24 String query = "select s.s_id, s_naziv\n" + 25 "from sodrzi\n" + 26 " left join sostojki s on\n" + 27 " sodrzi.s_id = s.s_id\n" + 28 "where rec_id = ?"; 29 30 PreparedStatement prepStm = connection.prepareStatement(query); 31 prepStm.setLong(1, rId); 32 33 ResultSet result = prepStm.executeQuery(); 34 List<Sostojka> sostojki = new ArrayList<>(); 35 36 while (result.next()){ 37 Sostojka sostojka = new Sostojka(); 38 sostojka.setsId(result.getLong("s_id")); 39 sostojka.setsNaziv(result.getString("s_naziv")); 40 sostojki.add(sostojka); 41 } 42 43 result.close(); 44 prepStm.close(); 45 DB.closeConnection(); 46 47 return sostojki; 48 } 2 49 } -
src/main/java/com/example/cookbook/service/ReceptService.java
r3e572eb r501396e 1 package com.example.cookbook.service;public interface ReceptService { 1 package com.example.cookbook.service; 2 3 import com.example.cookbook.model.Recept; 4 5 import java.sql.SQLException; 6 import java.util.List; 7 8 public interface ReceptService { 9 10 List<Recept> listAll() throws SQLException; 11 12 Recept findById(Long id) throws SQLException; 2 13 } -
src/main/java/com/example/cookbook/service/SlikiService.java
r3e572eb r501396e 1 package com.example.cookbook.service;public class SlikiService { 1 package com.example.cookbook.service; 2 3 import com.example.cookbook.model.Slika; 4 5 import java.sql.SQLException; 6 import java.util.List; 7 8 public interface SlikiService { 9 10 List<Slika> findAllPicById(Long recId) throws SQLException; 11 12 2 13 } -
src/main/java/com/example/cookbook/service/SostojkiService.java
r3e572eb r501396e 1 package com.example.cookbook.service;public interface SostojkiService { 1 package com.example.cookbook.service; 2 3 import com.example.cookbook.model.Sostojka; 4 5 import java.sql.SQLException; 6 import java.util.List; 7 8 public interface SostojkiService { 9 10 List<Sostojka> findAllById(Long rId) throws SQLException; 2 11 } -
src/main/java/com/example/cookbook/service/impl/ReceptServiceImpl.java
r3e572eb r501396e 1 package com.example.cookbook.service.impl;public class ReceptServiceImpl { 1 package com.example.cookbook.service.impl; 2 3 import com.example.cookbook.model.Recept; 4 import com.example.cookbook.model.exception.ReceptNeEPronajdenException; 5 import com.example.cookbook.repository.ReceptRepository; 6 import com.example.cookbook.service.ReceptService; 7 import org.springframework.stereotype.Service; 8 9 import java.sql.SQLException; 10 import java.util.List; 11 import java.util.Optional; 12 13 @Service 14 public class ReceptServiceImpl implements ReceptService { 15 16 private final ReceptRepository receptRepository; 17 18 public ReceptServiceImpl(ReceptRepository receptRepository) { 19 this.receptRepository = receptRepository; 20 } 21 22 @Override 23 public List<Recept> listAll() throws SQLException { 24 return receptRepository.findAll(); 25 } 26 27 @Override 28 public Recept findById(Long id) throws SQLException { 29 Recept recept; 30 31 recept = receptRepository.findById(id).orElseThrow(ReceptNeEPronajdenException::new); 32 33 return recept; 34 } 2 35 } -
src/main/java/com/example/cookbook/service/impl/SlikiServiceImpl.java
r3e572eb r501396e 1 package com.example.cookbook.service.impl;public class SlikiService { 1 package com.example.cookbook.service.impl; 2 3 4 import com.example.cookbook.model.Slika; 5 import com.example.cookbook.repository.SlikiZaReceptRepository; 6 import com.example.cookbook.service.SlikiService; 7 import org.springframework.stereotype.Service; 8 9 import java.sql.SQLException; 10 import java.util.List; 11 12 @Service 13 public class SlikiServiceImpl implements SlikiService { 14 15 private final SlikiZaReceptRepository slikiZaReceptRepository; 16 17 public SlikiServiceImpl(SlikiZaReceptRepository slikiZaReceptRepository) { 18 this.slikiZaReceptRepository = slikiZaReceptRepository; 19 } 20 21 @Override 22 public List<Slika> findAllPicById(Long recId) throws SQLException { 23 return slikiZaReceptRepository.findAllForRecipe(recId); 24 } 2 25 } -
src/main/java/com/example/cookbook/service/impl/SostojkiServiceImpl.java
r3e572eb r501396e 1 package com.example.cookbook.service.impl;public class SostojkiServiceImpl { 1 package com.example.cookbook.service.impl; 2 3 4 import com.example.cookbook.model.Sostojka; 5 import com.example.cookbook.repository.SostojkiRespository; 6 import com.example.cookbook.service.SostojkiService; 7 import org.springframework.stereotype.Service; 8 9 import java.sql.SQLException; 10 import java.util.List; 11 12 @Service 13 public class SostojkiServiceImpl implements SostojkiService { 14 15 private final SostojkiRespository sostojkiRepository; 16 17 public SostojkiServiceImpl(SostojkiRespository sostojkiRepository) { 18 this.sostojkiRepository = sostojkiRepository; 19 } 20 21 @Override 22 public List<Sostojka> findAllById(Long rId) throws SQLException { 23 return sostojkiRepository.findAllByRId(rId); 24 } 2 25 }
Note:
See TracChangeset
for help on using the changeset viewer.