Changeset d4d8fb9


Ignore:
Timestamp:
02/03/24 18:08:44 (9 months ago)
Author:
Blazho <aleksandar.blazhevski@…>
Branches:
master
Children:
cab1b7d
Parents:
aea04dd
Message:

Posetitelot komentira i dava ocena za recept(https://develop.finki.ukim.mk/projects/cbdb/wiki/useCase11)

Location:
src/main
Files:
5 added
6 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/com/example/cookbook/controller/HomeController.java

    raea04dd rd4d8fb9  
    22
    33
    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;
     4import com.example.cookbook.model.*;
    85import 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;
     6import com.example.cookbook.service.*;
    137import org.springframework.stereotype.Controller;
    148import org.springframework.ui.Model;
    159import org.springframework.web.bind.annotation.GetMapping;
    1610import org.springframework.web.bind.annotation.PathVariable;
     11import org.springframework.web.bind.annotation.PostMapping;
     12import org.springframework.web.bind.annotation.RequestParam;
    1713
    1814import java.sql.SQLException;
     15import java.time.LocalDateTime;
    1916import java.util.List;
    2017
     
    2926    private final KomentariService komentariService;
    3027
     28    private final KorisniciService korisniciService;
     29
    3130    public HomeController(ReceptService receptService,
    3231                          SlikiService slikiService,
    33                           SostojkiService sostojkiService, KomentariService komentariService) {
     32                          SostojkiService sostojkiService, KomentariService komentariService, KorisniciService korisniciService) {
    3433
    3534        this.receptService = receptService;
     
    3736        this.sostojkiService = sostojkiService;
    3837        this.komentariService = komentariService;
     38        this.korisniciService = korisniciService;
    3939    }
    4040
     
    5959        List<Sostojka> sostojki;
    6060        List<Komentar> komentari;
     61        List<Korisnik> posetiteli;
    6162        try {
    6263            recept = receptService.findById(id);
     
    6465            sostojki = sostojkiService.findAllById(id);
    6566            komentari = komentariService.findAllById(id);
     67            posetiteli = korisniciService.findAllByRole(TipKorisnik.Posetitel);
    6668        } catch (SQLException e) {
    6769            return "redirect:/error-page/SQL%20Exception";
     
    7375        model.addAttribute("sostojki", sostojki);
    7476        model.addAttribute("komentari", komentari);
     77        model.addAttribute("posetiteli", posetiteli);
    7578        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;
    7697    }
    7798
  • src/main/java/com/example/cookbook/model/Komentar.java

    raea04dd rd4d8fb9  
    2424    public void setImePrezime(String imePrezime) {
    2525        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;
    2634    }
    2735
  • src/main/java/com/example/cookbook/repository/KomentariRepository.java

    raea04dd rd4d8fb9  
    66import org.springframework.stereotype.Repository;
    77
    8 import java.sql.Connection;
    9 import java.sql.PreparedStatement;
    10 import java.sql.ResultSet;
    11 import java.sql.SQLException;
     8import java.sql.*;
    129import java.time.LocalDateTime;
    1310import java.util.ArrayList;
     
    4845        return komentari;
    4946    }
     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    }
    5066}
  • src/main/java/com/example/cookbook/service/KomentariService.java

    raea04dd rd4d8fb9  
    99
    1010    List<Komentar> findAllById(Long recId) throws SQLException;
     11
     12    void add(String telefon, Long id, Integer ocena, String komentar) throws SQLException;
    1113}
  • src/main/java/com/example/cookbook/service/impl/KomentariServiceImpl.java

    raea04dd rd4d8fb9  
    88
    99import java.sql.SQLException;
     10import java.time.LocalDateTime;
    1011import java.util.List;
    1112
     
    2324        return komentariRepository.findAllByRecId(recId);
    2425    }
     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    }
    2534}
  • src/main/resources/templates/recept.html

    raea04dd rd4d8fb9  
    8383                <select id="telefon" name="telefon" required>
    8484                    <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>
    8688                </select>
    8789            </div>
Note: See TracChangeset for help on using the changeset viewer.