1 | package project.fmo.app.projcetfmo.Web;
|
---|
2 |
|
---|
3 |
|
---|
4 | import org.springframework.format.annotation.DateTimeFormat;
|
---|
5 | import org.springframework.stereotype.Controller;
|
---|
6 | import org.springframework.ui.Model;
|
---|
7 | import org.springframework.web.bind.annotation.*;
|
---|
8 | import project.fmo.app.projcetfmo.Model.*;
|
---|
9 | import project.fmo.app.projcetfmo.Service.*;
|
---|
10 |
|
---|
11 |
|
---|
12 | import java.sql.Date;
|
---|
13 | import java.text.ParseException;
|
---|
14 |
|
---|
15 | import java.time.LocalDate;
|
---|
16 | import java.util.List;
|
---|
17 |
|
---|
18 |
|
---|
19 | @Controller
|
---|
20 | @RequestMapping("/products")
|
---|
21 | public class ProductController {
|
---|
22 |
|
---|
23 | private final ProduktService produktService;
|
---|
24 | private final KategorijaService kategorijaService;
|
---|
25 | private final ProduktEOdKategorijaService produktEOdKategorijaService;
|
---|
26 | private final CenaService cenaService;
|
---|
27 | private final NarackaService narackaService;
|
---|
28 | private final NarackaSodrziProduktService narackaSodrziProduktService;
|
---|
29 | private final KupuvacService kupuvacService;
|
---|
30 | private final KorisnikService korisnikService;
|
---|
31 |
|
---|
32 | public ProductController(ProduktService produktService, KategorijaService kategorijaService, ProduktEOdKategorijaService produktEOdKategorijaService, CenaService cenaService, NarackaService narackaService, NarackaSodrziProduktService narackaSodrziProduktService, KupuvacService kupuvacService, KorisnikService korisnikService) {
|
---|
33 | this.produktService = produktService;
|
---|
34 | this.kategorijaService = kategorijaService;
|
---|
35 | this.produktEOdKategorijaService = produktEOdKategorijaService;
|
---|
36 | this.cenaService = cenaService;
|
---|
37 | this.narackaService = narackaService;
|
---|
38 | this.narackaSodrziProduktService = narackaSodrziProduktService;
|
---|
39 | this.kupuvacService = kupuvacService;
|
---|
40 | this.korisnikService = korisnikService;
|
---|
41 | }
|
---|
42 |
|
---|
43 | @GetMapping
|
---|
44 | public String getProductPage(Model model) {
|
---|
45 | List<Produkt> products = this.produktService.findAll();
|
---|
46 | model.addAttribute("products", products);
|
---|
47 | model.addAttribute("kat", kategorijaService.findAll());
|
---|
48 | model.addAttribute("categories", produktEOdKategorijaService.findAll());
|
---|
49 | model.addAttribute("prices", cenaService.findAll());
|
---|
50 | model.addAttribute("bodyContent", "products");
|
---|
51 | return "master_template";
|
---|
52 | }
|
---|
53 |
|
---|
54 | @GetMapping("/{id}")
|
---|
55 | public String getProductInfo(Model model, @PathVariable Integer id){
|
---|
56 | Produkt p = produktService.findById(id).orElse(null);
|
---|
57 | Cena c = cenaService.findByIdProdukt(id);
|
---|
58 | ProduktEOdKategorija pk = produktEOdKategorijaService.findByIdProdukt(id);
|
---|
59 | List<Kategorija> k = kategorijaService.findAll();
|
---|
60 | Kategorija kat = null;
|
---|
61 | for ( Kategorija kategorija : k){
|
---|
62 | if (kategorija.getIdKategorija() == pk.getIdKategorija()){
|
---|
63 | kat=kategorija;
|
---|
64 | }
|
---|
65 | }
|
---|
66 | model.addAttribute("product", p);
|
---|
67 | model.addAttribute("price", c);
|
---|
68 | model.addAttribute("category",pk);
|
---|
69 | if(kat != null) {
|
---|
70 | model.addAttribute("kat", kat);
|
---|
71 | }
|
---|
72 | model.addAttribute("bodyContent", "product_info");
|
---|
73 | return "master_template";
|
---|
74 | }
|
---|
75 |
|
---|
76 | @PostMapping("{id}/add-to-cart")
|
---|
77 | public String addProductToCart(@PathVariable Integer id,
|
---|
78 | @RequestParam String idKorisnik,
|
---|
79 | @RequestParam int cena,
|
---|
80 | @RequestParam int quantity){
|
---|
81 | Korisnik kOrisnik = korisnikService.findbyImePrezime(idKorisnik);
|
---|
82 | Kupuvac k = kupuvacService.findById(kOrisnik.getIdKorisnik());
|
---|
83 | Naracka n = narackaService.findByIdKorisniktAndStatus(k.getIdKorisnik(),"aktivna");
|
---|
84 | if (n == null){
|
---|
85 | //Naracka narackaNova = new Naracka(cena*quantity, Date.valueOf(LocalDate.now()), "aktivna", "/", k);
|
---|
86 | Naracka narackaNova = narackaService.save(cena*quantity, Date.valueOf(LocalDate.now()), "aktivna", "/", k);
|
---|
87 | NarackaSodrziProdukt nsp = new NarackaSodrziProdukt(id,narackaNova.getIdNaracka(),quantity,cena);
|
---|
88 | narackaSodrziProduktService.save(id,narackaNova.getIdNaracka(),quantity,cena);
|
---|
89 |
|
---|
90 | return "redirect:/products";
|
---|
91 | }
|
---|
92 | else {
|
---|
93 | narackaService.edit(cena*quantity,n.getStatus(),n.getAdresa(),k.getIdKorisnik(),n.getIdNaracka());
|
---|
94 | NarackaSodrziProdukt narackaSodrziProdukt = narackaSodrziProduktService.findByIdProduktAndIdNaracka(id,n.getIdNaracka());
|
---|
95 | if(narackaSodrziProdukt == null){
|
---|
96 | NarackaSodrziProdukt nova = new NarackaSodrziProdukt(id,n.getIdNaracka(),quantity,cena);
|
---|
97 | narackaSodrziProduktService.save(id,n.getIdNaracka(),quantity,cena);
|
---|
98 | }
|
---|
99 | else{
|
---|
100 | narackaSodrziProduktService.edit(id,n.getIdNaracka(),quantity,cena);
|
---|
101 | }
|
---|
102 | return "redirect:/products";
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | @DeleteMapping("/delete/{id}")
|
---|
107 | public String deleteProduct(@PathVariable Integer id,Model model) {
|
---|
108 | if(narackaSodrziProduktService.findByIdProdukt(id).size() != 0){
|
---|
109 | model.addAttribute("bodyContent", "not_valid_delete");
|
---|
110 | return "master_template";
|
---|
111 | }
|
---|
112 | this.cenaService.deleteByIdProdukt(id);
|
---|
113 | this.produktEOdKategorijaService.deleteByIdProdukt(id);
|
---|
114 | this.produktService.deleteById(id);
|
---|
115 | return "redirect:/products";
|
---|
116 | }
|
---|
117 |
|
---|
118 | @GetMapping("/edit-form/{id}")
|
---|
119 | public String editProductPage(@PathVariable Integer id, Model model) {
|
---|
120 | if (this.produktService.findById(id).isPresent()) {
|
---|
121 | Produkt product = this.produktService.findById(id).get();
|
---|
122 | Cena c = cenaService.findByIdProdukt(product.getIdProdukt());
|
---|
123 | ProduktEOdKategorija produktEOdKategorija = produktEOdKategorijaService.findByIdProdukt(product.getIdProdukt());
|
---|
124 | model.addAttribute("chosen", produktEOdKategorija);
|
---|
125 | model.addAttribute("price", c);
|
---|
126 | List<Kategorija> categories = this.kategorijaService.findAll();
|
---|
127 | model.addAttribute("categories", categories);
|
---|
128 | model.addAttribute("product", product);
|
---|
129 | model.addAttribute("bodyContent", "add_product");
|
---|
130 | return "master_template";
|
---|
131 | }
|
---|
132 | return "redirect:/products";
|
---|
133 | }
|
---|
134 |
|
---|
135 | @GetMapping("/add-form")
|
---|
136 | public String addProductPage(Model model) {
|
---|
137 | List<Kategorija> categories = this.kategorijaService.findAll();
|
---|
138 | model.addAttribute("categories", categories);
|
---|
139 | model.addAttribute("bodyContent", "add_product");
|
---|
140 | return "master_template";
|
---|
141 | }
|
---|
142 |
|
---|
143 | @PostMapping("/add")
|
---|
144 | public String saveProduct(
|
---|
145 | @RequestParam(required = false) Integer id,
|
---|
146 | @RequestParam String name,
|
---|
147 | @RequestParam(required = false) String description,
|
---|
148 | @RequestParam Integer category,
|
---|
149 | @RequestParam Date date,
|
---|
150 | @RequestParam(required = false) String dateTo,
|
---|
151 | @RequestParam int price) throws ParseException {
|
---|
152 | if(dateTo.length() != 0) {
|
---|
153 | Date d = Date.valueOf(dateTo);
|
---|
154 |
|
---|
155 | if (id != null) {
|
---|
156 | this.produktService.edit(id, name, description);
|
---|
157 | this.cenaService.edit(id, date, d, price);
|
---|
158 | this.produktEOdKategorijaService.edit(id, category);
|
---|
159 |
|
---|
160 | } else {
|
---|
161 | Produkt p = this.produktService.save(name, description);
|
---|
162 | this.cenaService.save(p.getIdProdukt(), date, d, price);
|
---|
163 | this.produktEOdKategorijaService.save(p.getIdProdukt(), category);
|
---|
164 | }
|
---|
165 | }
|
---|
166 | else{
|
---|
167 | if (id != null) {
|
---|
168 | this.produktService.edit(id, name, description);
|
---|
169 | this.cenaService.edit(id, date, null, price);
|
---|
170 | this.produktEOdKategorijaService.edit(id, category);
|
---|
171 |
|
---|
172 | } else {
|
---|
173 | Produkt p = this.produktService.save(name, description);
|
---|
174 | this.cenaService.save(p.getIdProdukt(), date, null, price);
|
---|
175 | this.produktEOdKategorijaService.save(p.getIdProdukt(), category);
|
---|
176 | }
|
---|
177 |
|
---|
178 | }
|
---|
179 | return "redirect:/products";
|
---|
180 | }
|
---|
181 |
|
---|
182 |
|
---|
183 |
|
---|
184 |
|
---|
185 | }
|
---|