Changeset d14176d for src/main/java/project/fmo/app/projcetfmo/Web
- Timestamp:
- 07/07/23 12:14:58 (19 months ago)
- Branches:
- main
- Parents:
- 1dd9226
- Location:
- src/main/java/project/fmo/app/projcetfmo/Web
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/project/fmo/app/projcetfmo/Web/DostavaController.java
r1dd9226 rd14176d 1 package project.fmo.app.projcetfmo.Web;public class DostavaController { 1 package project.fmo.app.projcetfmo.Web; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.ui.Model; 5 import org.springframework.web.bind.annotation.GetMapping; 6 import org.springframework.web.bind.annotation.PathVariable; 7 import org.springframework.web.bind.annotation.PostMapping; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 import project.fmo.app.projcetfmo.Model.Dostava; 10 import project.fmo.app.projcetfmo.Model.Dostavuvac; 11 import project.fmo.app.projcetfmo.Model.Korisnik; 12 import project.fmo.app.projcetfmo.Service.DostavaService; 13 import project.fmo.app.projcetfmo.Service.DostavuvaceService; 14 import project.fmo.app.projcetfmo.Service.KorisnikService; 15 import project.fmo.app.projcetfmo.Service.NarackaService; 16 17 import javax.servlet.http.HttpServletRequest; 18 import java.util.List; 19 20 @Controller 21 @RequestMapping("/delivery") 22 public class DostavaController { 23 24 private final DostavaService dostavaService; 25 private final DostavuvaceService dostavuvaceService; 26 private final KorisnikService korisnikService; 27 private final NarackaService narackaService; 28 29 public DostavaController(DostavaService dostavaService, DostavuvaceService dostavuvaceService, KorisnikService korisnikService, NarackaService narackaService) { 30 this.dostavaService = dostavaService; 31 this.dostavuvaceService = dostavuvaceService; 32 this.korisnikService = korisnikService; 33 this.narackaService = narackaService; 34 } 35 36 37 @GetMapping 38 public String getDelivery(Model model, HttpServletRequest req){ 39 String username = req.getRemoteUser(); 40 Korisnik korisnik = korisnikService.findbyImePrezime(username); 41 Dostavuvac dostavuvac = dostavuvaceService.findById(korisnik.getIdKorisnik()); 42 List<Dostava> dostavaList = dostavaService.findByIdKorisnikAndStatus(dostavuvac, "za dostava"); 43 model.addAttribute("user", korisnik); 44 model.addAttribute("orders", narackaService.findAll()); 45 model.addAttribute("delivers", dostavaList); 46 model.addAttribute("bodyContent", "delivery"); 47 return "master_template"; 48 } 49 50 @PostMapping("/{id}") 51 public String OrderDelivered(@PathVariable Integer id){ 52 dostavaService.edit(id); 53 return "redirect:/delivery"; 54 } 55 2 56 } -
src/main/java/project/fmo/app/projcetfmo/Web/HomeController.java
r1dd9226 rd14176d 9 9 @Controller 10 10 @RequestMapping({"/","/home"}) 11 public class StoreController {11 public class HomeController { 12 12 13 13 14 14 @GetMapping 15 public String getHomePage(){ 16 return "home"; 15 public String getHomePage(Model model){ 16 model.addAttribute("bodyContent", "home"); 17 return "master_template"; 17 18 } 18 19 20 @GetMapping("/access_denied") 21 public String getAccessDeniedPage(Model model) { 22 model.addAttribute("bodyContent", "access_denied"); 23 return "master-template"; 24 } 25 26 19 27 } -
src/main/java/project/fmo/app/projcetfmo/Web/LoginController.java
r1dd9226 rd14176d 1 package project.fmo.app.projcetfmo.Web;public class LoginController { 1 package project.fmo.app.projcetfmo.Web; 2 3 4 import org.springframework.stereotype.Controller; 5 import org.springframework.ui.Model; 6 import org.springframework.web.bind.annotation.GetMapping; 7 import org.springframework.web.bind.annotation.PostMapping; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 import project.fmo.app.projcetfmo.Model.Korisnik; 10 import project.fmo.app.projcetfmo.Model.exception.InvalidUserCredentialsException; 11 import project.fmo.app.projcetfmo.Service.AuthService; 12 13 import javax.servlet.http.HttpServletRequest; 14 15 @Controller 16 @RequestMapping("/login") 17 public class LoginController { 18 19 private final AuthService authService; 20 21 public LoginController(AuthService authService) { 22 this.authService = authService; 23 } 24 25 @GetMapping 26 public String getLoginPage(Model model) { 27 model.addAttribute("bodyContent","login"); 28 return "master-template"; 29 } 30 31 @PostMapping 32 public String login(HttpServletRequest request, Model model) { 33 Korisnik user = null; 34 try{ 35 user = this.authService.login(request.getParameter("username"), 36 request.getParameter("password")); 37 request.getSession().setAttribute("user", user); 38 return "redirect:/home"; 39 } 40 catch (InvalidUserCredentialsException exception) { 41 model.addAttribute("hasError", true); 42 model.addAttribute("error", exception.getMessage()); 43 return "login"; 44 } 45 } 2 46 } -
src/main/java/project/fmo/app/projcetfmo/Web/LogoutController.java
r1dd9226 rd14176d 1 package project.fmo.app.projcetfmo.Web;public class LogoutController { 1 package project.fmo.app.projcetfmo.Web; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.GetMapping; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 7 import javax.servlet.http.HttpServletRequest; 8 9 @Controller 10 @RequestMapping("/logout") 11 public class LogoutController { 12 13 @GetMapping 14 public String logout(HttpServletRequest request) { 15 request.getSession().invalidate(); 16 return "redirect:/"; 17 } 2 18 } -
src/main/java/project/fmo/app/projcetfmo/Web/NarackaController.java
r1dd9226 rd14176d 1 package project.fmo.app.projcetfmo.Web;public class NarackaController { 1 package project.fmo.app.projcetfmo.Web; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.ui.Model; 5 import org.springframework.web.bind.annotation.*; 6 import project.fmo.app.projcetfmo.Model.*; 7 import project.fmo.app.projcetfmo.Service.*; 8 9 import javax.servlet.http.HttpServletRequest; 10 import java.util.List; 11 12 @Controller 13 @RequestMapping("/order") 14 public class NarackaController { 15 16 private final NarackaService narackaService; 17 private final NarackaSodrziProduktService narackaSodrziProduktService; 18 private final KorisnikService korisnikService; 19 private final KupuvacService kupuvacService; 20 private final CenaService cenaService; 21 22 private final ProduktService produktService; 23 24 public NarackaController(NarackaService narackaService, NarackaSodrziProduktService narackaSodrziProduktService, KorisnikService korisnikService, KupuvacService kupuvacService, CenaService cenaService, ProduktService produktService) { 25 this.narackaService = narackaService; 26 this.narackaSodrziProduktService = narackaSodrziProduktService; 27 this.korisnikService = korisnikService; 28 this.kupuvacService = kupuvacService; 29 this.cenaService = cenaService; 30 this.produktService = produktService; 31 } 32 33 @GetMapping 34 public String getShoppingCartPage(@RequestParam(required = false) String error, 35 HttpServletRequest req, 36 Model model) { 37 if (error != null && !error.isEmpty()) { 38 model.addAttribute("hasError", true); 39 model.addAttribute("error", error); 40 } 41 String username = req.getRemoteUser(); 42 Korisnik korisnik = korisnikService.findbyImePrezime(username); 43 Kupuvac kupuvac = kupuvacService.findById(korisnik.getIdKorisnik()); 44 Naracka naracka = this.narackaService.findByIdKorisniktAndStatus(kupuvac.getIdKorisnik(),"aktivna"); 45 if(naracka == null){ 46 model.addAttribute("bodyContent", "home"); 47 return "master_template"; 48 } 49 List<NarackaSodrziProdukt> narackaSodrziProdukt = narackaSodrziProduktService.findByIdNaracka(naracka.getIdNaracka()); 50 List<Produkt> produktList = produktService.findAll(); 51 model.addAttribute("order", naracka); 52 model.addAttribute("products", produktList); 53 model.addAttribute("po", narackaSodrziProdukt); 54 model.addAttribute("prices", cenaService.findAll()); 55 model.addAttribute("bodyContent", "cart"); 56 return "master_template"; 57 } 58 59 @PostMapping("/{id}/success") 60 public String processOrder(@PathVariable Integer id,HttpServletRequest req,@RequestParam(required = false) String address){ 61 Naracka n = narackaService.findByIdNaracka(id); 62 String username = req.getRemoteUser(); 63 Korisnik korisnik = korisnikService.findbyImePrezime(username); 64 Kupuvac kupuvac = kupuvacService.findById(korisnik.getIdKorisnik()); 65 if(address!=null) { 66 narackaService.edit(0, "se sprema", address, kupuvac.getIdKorisnik(), n.getIdNaracka()); 67 } 68 else { 69 narackaService.edit(0, "se sprema", "/", kupuvac.getIdKorisnik(), n.getIdNaracka()); 70 } 71 return "redirect:/products"; 72 } 73 74 75 2 76 } -
src/main/java/project/fmo/app/projcetfmo/Web/ProductController.java
r1dd9226 rd14176d 1 package project.fmo.app.projcetfmo.Web;public class ProductController { 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 2 185 } -
src/main/java/project/fmo/app/projcetfmo/Web/RegisterController.java
r1dd9226 rd14176d 1 package project.fmo.app.projcetfmo.Web;public class RegisterController { 1 package project.fmo.app.projcetfmo.Web; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.ui.Model; 5 import org.springframework.web.bind.annotation.GetMapping; 6 import org.springframework.web.bind.annotation.PostMapping; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.RequestParam; 9 import project.fmo.app.projcetfmo.Service.KorisnikService; 10 11 @Controller 12 @RequestMapping("/register") 13 public class RegisterController { 14 15 private final KorisnikService korisnikService; 16 17 18 public RegisterController(KorisnikService korisnikService) { 19 this.korisnikService = korisnikService; 20 } 21 22 23 @GetMapping 24 public String getRegisterPage(@RequestParam(required = false) String error, Model model) { 25 if(error != null && !error.isEmpty()) { 26 model.addAttribute("hasError", true); 27 model.addAttribute("error", error); 28 } 29 model.addAttribute("bodyContent","register"); 30 return "master_template"; 31 } 32 33 @PostMapping 34 public String register(@RequestParam String name_surname, 35 @RequestParam String password, 36 @RequestParam String repeatedPassword, 37 @RequestParam String mail, 38 @RequestParam String number) { 39 try{ 40 if(korisnikService.findbyImePrezime(name_surname) != null) throw new RuntimeException(); 41 this.korisnikService.register(name_surname, password, repeatedPassword, mail, number); 42 return "redirect:/home"; 43 } catch (RuntimeException r) { 44 return "redirect:/register?error=InvalidRegistration"; 45 } 46 } 47 48 49 2 50 } -
src/main/java/project/fmo/app/projcetfmo/Web/StatisticsController.java
r1dd9226 rd14176d 1 package project.fmo.app.projcetfmo.Web;public class StatisticsController { 1 package project.fmo.app.projcetfmo.Web; 2 3 import org.springframework.data.domain.Page; 4 import org.springframework.stereotype.Controller; 5 import org.springframework.ui.Model; 6 import org.springframework.web.bind.annotation.GetMapping; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import project.fmo.app.projcetfmo.Model.Korisnik; 9 import project.fmo.app.projcetfmo.Model.Produkt; 10 import project.fmo.app.projcetfmo.Service.KorisnikService; 11 import project.fmo.app.projcetfmo.Service.ProduktService; 12 13 import java.util.ArrayList; 14 import java.util.List; 15 16 @Controller 17 @RequestMapping("/statistics") 18 public class StatisticsController { 19 private final ProduktService produktService; 20 private final KorisnikService korisnikService; 21 22 public StatisticsController(ProduktService produktService, KorisnikService korisnikService) { 23 this.produktService = produktService; 24 this.korisnikService = korisnikService; 25 } 26 27 @GetMapping 28 public String getStatistics(Model model){ 29 List<Object> korisnikList = korisnikService.statistics2(); 30 List<String> ime = new ArrayList<>(); 31 List<String> id = new ArrayList<>(); 32 List<Integer> vkupno_naracki = new ArrayList<>(); 33 List<Float> prosecna_suma_na_naracka_za_site_kupuvaci = new ArrayList<>(); 34 List<Integer> vkupno_suma_od_site_naracki_na_kupuvacot = new ArrayList<>(); 35 List<Integer> vkupno_naracki_nad_prosek_na_korisnikot = new ArrayList<>(); 36 List<Integer> vkupno_suma_na_naracki_nad_prosek_za_korisnikot = new ArrayList<>(); 37 // for(int i =0 ; i< korisnikList.size();i++) { 38 // ime.add(korisnikList.get(i)[0].toString()); 39 // } 40 List<Object> produktList = produktService.statistics1(); 41 model.addAttribute("products", produktList); 42 model.addAttribute("users", korisnikList); 43 model.addAttribute("bodyContent", "statistics"); 44 return "master_template"; 45 46 } 2 47 }
Note:
See TracChangeset
for help on using the changeset viewer.