1 | package com.example.baza.web;
|
---|
2 |
|
---|
3 |
|
---|
4 | import com.example.baza.model.Avtor;
|
---|
5 | import com.example.baza.model.Izdavac;
|
---|
6 | import com.example.baza.model.Kategorija;
|
---|
7 | import com.example.baza.model.Knigi;
|
---|
8 | import com.example.baza.model.exception.KnigaNotFoundException;
|
---|
9 | import com.example.baza.service.*;
|
---|
10 | import org.springframework.stereotype.Controller;
|
---|
11 | import org.springframework.ui.Model;
|
---|
12 | import org.springframework.web.bind.annotation.*;
|
---|
13 |
|
---|
14 | import java.time.LocalDate;
|
---|
15 | import java.util.List;
|
---|
16 | import java.util.Set;
|
---|
17 |
|
---|
18 | @Controller
|
---|
19 | @RequestMapping("/books")
|
---|
20 | public class KnigiController {
|
---|
21 | private final KnigiService knigiService;
|
---|
22 | private final AvtorService avtorService;
|
---|
23 | private final KategorijaService kategorijaService;
|
---|
24 | private final IzdavacService izdavacService;
|
---|
25 | private final DobavuvacService dobavuvacService;
|
---|
26 |
|
---|
27 | public KnigiController(KnigiService knigiService, AvtorService avtorService, KategorijaService kategorijaService, IzdavacService izdavacService, DobavuvacService dobavuvacService) {
|
---|
28 | this.knigiService = knigiService;
|
---|
29 | this.avtorService = avtorService;
|
---|
30 | this.kategorijaService = kategorijaService;
|
---|
31 | this.izdavacService = izdavacService;
|
---|
32 | this.dobavuvacService = dobavuvacService;
|
---|
33 | }
|
---|
34 | @GetMapping
|
---|
35 | public String getProductPage(@RequestParam(required = false) String error,
|
---|
36 | @RequestParam(required = false) String naslov, Model model){
|
---|
37 | if(error!=null && !error.isEmpty()){
|
---|
38 | model.addAttribute("hasError",true);
|
---|
39 | model.addAttribute("error",error);
|
---|
40 | }
|
---|
41 | if(naslov != null)
|
---|
42 | {
|
---|
43 | //
|
---|
44 | List<Avtor> avtori = this.avtorService.findAll();
|
---|
45 | List<Knigi> knigii = this.knigiService.findByNaslov(naslov);
|
---|
46 | List<Kategorija> kategorii = this.kategorijaService.findAll();
|
---|
47 | model.addAttribute("knigii", knigii);
|
---|
48 | model.addAttribute("avtori", avtori);
|
---|
49 | model.addAttribute("kategorii", kategorii);
|
---|
50 | // model.addAttribute("list", list);
|
---|
51 | }
|
---|
52 | else {
|
---|
53 | this.knigiService.listAll();
|
---|
54 | List<Avtor> avtori = this.avtorService.findAll();
|
---|
55 | List<Knigi> knigii = this.knigiService.listAll();
|
---|
56 | List<Kategorija> kategorii = this.kategorijaService.findAll();
|
---|
57 | model.addAttribute("knigii", knigii);
|
---|
58 | model.addAttribute("avtori", avtori);
|
---|
59 | model.addAttribute("kategorii", kategorii);
|
---|
60 | }
|
---|
61 | return "books";
|
---|
62 | }
|
---|
63 | @GetMapping("/add")
|
---|
64 | public String showAdd() {
|
---|
65 | return "form";
|
---|
66 | }
|
---|
67 | // @DeleteMapping("/delete/{id}")
|
---|
68 | // public String deleteProduct(@PathVariable Integer id){
|
---|
69 | // this.knigiService.delteById(id);
|
---|
70 | // return "redirect:/products";
|
---|
71 | // }
|
---|
72 | @PostMapping("/books")
|
---|
73 | public String create(@RequestParam String naslov,
|
---|
74 | @RequestParam String opis,
|
---|
75 | @RequestParam Izdavac izdavac,
|
---|
76 | @RequestParam Set<Kategorija> kategorijas,
|
---|
77 | @RequestParam Set<Avtor> avtors) {
|
---|
78 | this.knigiService.create(naslov, opis, izdavac, kategorijas, avtors);
|
---|
79 | return "redirect:/books";
|
---|
80 | }
|
---|
81 | }
|
---|