Changeset 501396e for src/main/java/com


Ignore:
Timestamp:
02/03/24 15:58:58 (9 months ago)
Author:
Blazho <aleksandar.blazhevski@…>
Branches:
master
Children:
aea04dd
Parents:
3e572eb
Message:

added missing files

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 {
     1package com.example.cookbook.controller;
     2
     3
     4import com.example.cookbook.model.Recept;
     5import com.example.cookbook.model.Slika;
     6import com.example.cookbook.model.Sostojka;
     7import com.example.cookbook.model.exception.ReceptNeEPronajdenException;
     8import com.example.cookbook.service.ReceptService;
     9import com.example.cookbook.service.SlikiService;
     10import com.example.cookbook.service.SostojkiService;
     11import org.springframework.stereotype.Controller;
     12import org.springframework.ui.Model;
     13import org.springframework.web.bind.annotation.GetMapping;
     14import org.springframework.web.bind.annotation.PathVariable;
     15
     16import java.sql.SQLException;
     17import java.util.List;
     18
     19@Controller
     20public 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    }
    277}
  • src/main/java/com/example/cookbook/dbConfig/DB.java

    r3e572eb r501396e  
    1 package com.example.cookbook.dbConfig;public class DB {
     1package com.example.cookbook.dbConfig;
     2
     3
     4import org.springframework.stereotype.Component;
     5
     6
     7import java.sql.Connection;
     8import java.sql.DriverManager;
     9import java.sql.SQLException;
     10
     11@Component
     12public 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    }
    252}
     53
  • src/main/java/com/example/cookbook/model/DostavaDTO.java

    r3e572eb r501396e  
    1 package com.example.cookbook.model;public class Dostava {
     1package com.example.cookbook.model;
     2
     3import java.time.LocalDateTime;
     4
     5public 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    }
    213}
  • src/main/java/com/example/cookbook/model/Recept.java

    r3e572eb r501396e  
    11package com.example.cookbook.model;
    22
    3 public class ReceptForList {
     3public class Recept {
    44
    55    private Long recId;
    66
    77    private String recIme;
    8 
    9     public String getPostapka() {
    10         return postapka;
    11     }
    12 
    13     public void setPostapka(String postapka) {
    14         this.postapka = postapka;
    15     }
    168
    179    private String postapka;
     
    2719    }
    2820
    29     public ReceptForList(Long recId, String recIme, String postapka) {
     21    public Recept(Long recId, String recIme, String postapka) {
    3022        this.recId = recId;
    3123        this.recIme = recIme;
     
    3426    }
    3527
    36     public ReceptForList() {
     28    public Recept() {
    3729    }
    3830
     
    5345    }
    5446
     47    public String getPostapka() {
     48        return postapka;
     49    }
    5550
     51    public void setPostapka(String postapka) {
     52        this.postapka = postapka;
     53    }
    5654}
  • src/main/java/com/example/cookbook/model/Slika.java

    r3e572eb r501396e  
    1 package com.example.cookbook.model;public class Slika {
     1package com.example.cookbook.model;
     2
     3public 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    }
    243}
  • src/main/java/com/example/cookbook/model/Sostojka.java

    r3e572eb r501396e  
    1 package com.example.cookbook.model;public class Sostojki {
     1package com.example.cookbook.model;
     2
     3public 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    }
    232}
  • src/main/java/com/example/cookbook/model/exception/ReceptNeEPronajdenException.java

    r3e572eb r501396e  
    1 package com.example.cookbook.model.exception;public class ReceptNeEPronajdenException {
     1package com.example.cookbook.model.exception;
     2
     3public class ReceptNeEPronajdenException extends RuntimeException{
     4    public ReceptNeEPronajdenException() {
     5        super("Receptot ne e pronajden");
     6    }
    27}
  • src/main/java/com/example/cookbook/repository/ReceptRepository.java

    r3e572eb r501396e  
    1 package com.example.cookbook.repository;public class ReceptRepository {
     1package com.example.cookbook.repository;
     2
     3
     4import com.example.cookbook.dbConfig.DB;
     5import com.example.cookbook.model.Recept;
     6import org.springframework.stereotype.Repository;
     7
     8import java.sql.*;
     9import java.util.ArrayList;
     10import java.util.List;
     11import java.util.Optional;
     12
     13@Repository
     14public 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    }
    267}
  • src/main/java/com/example/cookbook/repository/SlikiZaReceptRepository.java

    r3e572eb r501396e  
    1 package com.example.cookbook.repository;public class SlikiZaReceptRepository {
     1package com.example.cookbook.repository;
     2
     3import com.example.cookbook.dbConfig.DB;
     4import com.example.cookbook.model.Recept;
     5import com.example.cookbook.model.Slika;
     6import org.springframework.stereotype.Repository;
     7
     8import java.nio.charset.StandardCharsets;
     9import java.sql.Connection;
     10import java.sql.PreparedStatement;
     11import java.sql.ResultSet;
     12import java.sql.SQLException;
     13import java.util.ArrayList;
     14import java.util.List;
     15
     16@Repository
     17public 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    }
    243}
  • src/main/java/com/example/cookbook/repository/SostojkiRespository.java

    r3e572eb r501396e  
    1 package com.example.cookbook.repository;public class SostojkiRespository {
     1package com.example.cookbook.repository;
     2
     3
     4import com.example.cookbook.dbConfig.DB;
     5import com.example.cookbook.model.Slika;
     6import com.example.cookbook.model.Sostojka;
     7import org.springframework.stereotype.Repository;
     8
     9import java.nio.charset.StandardCharsets;
     10import java.sql.Connection;
     11import java.sql.PreparedStatement;
     12import java.sql.ResultSet;
     13import java.sql.SQLException;
     14import java.util.ArrayList;
     15import java.util.List;
     16
     17@Repository
     18public 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    }
    249}
  • src/main/java/com/example/cookbook/service/ReceptService.java

    r3e572eb r501396e  
    1 package com.example.cookbook.service;public interface ReceptService {
     1package com.example.cookbook.service;
     2
     3import com.example.cookbook.model.Recept;
     4
     5import java.sql.SQLException;
     6import java.util.List;
     7
     8public interface ReceptService {
     9
     10    List<Recept> listAll() throws SQLException;
     11
     12    Recept findById(Long id) throws SQLException;
    213}
  • src/main/java/com/example/cookbook/service/SlikiService.java

    r3e572eb r501396e  
    1 package com.example.cookbook.service;public class SlikiService {
     1package com.example.cookbook.service;
     2
     3import com.example.cookbook.model.Slika;
     4
     5import java.sql.SQLException;
     6import java.util.List;
     7
     8public interface SlikiService {
     9
     10    List<Slika> findAllPicById(Long recId) throws SQLException;
     11
     12
    213}
  • src/main/java/com/example/cookbook/service/SostojkiService.java

    r3e572eb r501396e  
    1 package com.example.cookbook.service;public interface SostojkiService {
     1package com.example.cookbook.service;
     2
     3import com.example.cookbook.model.Sostojka;
     4
     5import java.sql.SQLException;
     6import java.util.List;
     7
     8public interface SostojkiService {
     9
     10    List<Sostojka> findAllById(Long rId) throws SQLException;
    211}
  • src/main/java/com/example/cookbook/service/impl/ReceptServiceImpl.java

    r3e572eb r501396e  
    1 package com.example.cookbook.service.impl;public class ReceptServiceImpl {
     1package com.example.cookbook.service.impl;
     2
     3import com.example.cookbook.model.Recept;
     4import com.example.cookbook.model.exception.ReceptNeEPronajdenException;
     5import com.example.cookbook.repository.ReceptRepository;
     6import com.example.cookbook.service.ReceptService;
     7import org.springframework.stereotype.Service;
     8
     9import java.sql.SQLException;
     10import java.util.List;
     11import java.util.Optional;
     12
     13@Service
     14public 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    }
    235}
  • src/main/java/com/example/cookbook/service/impl/SlikiServiceImpl.java

    r3e572eb r501396e  
    1 package com.example.cookbook.service.impl;public class SlikiService {
     1package com.example.cookbook.service.impl;
     2
     3
     4import com.example.cookbook.model.Slika;
     5import com.example.cookbook.repository.SlikiZaReceptRepository;
     6import com.example.cookbook.service.SlikiService;
     7import org.springframework.stereotype.Service;
     8
     9import java.sql.SQLException;
     10import java.util.List;
     11
     12@Service
     13public 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    }
    225}
  • src/main/java/com/example/cookbook/service/impl/SostojkiServiceImpl.java

    r3e572eb r501396e  
    1 package com.example.cookbook.service.impl;public class SostojkiServiceImpl {
     1package com.example.cookbook.service.impl;
     2
     3
     4import com.example.cookbook.model.Sostojka;
     5import com.example.cookbook.repository.SostojkiRespository;
     6import com.example.cookbook.service.SostojkiService;
     7import org.springframework.stereotype.Service;
     8
     9import java.sql.SQLException;
     10import java.util.List;
     11
     12@Service
     13public 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    }
    225}
Note: See TracChangeset for help on using the changeset viewer.