Ignore:
Timestamp:
07/07/23 12:14:58 (16 months ago)
Author:
HristijanMitic00 <hristijan.mitic.01@…>
Branches:
main
Parents:
1dd9226
Message:

First commit

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/project/fmo/app/projcetfmo/Web/NarackaController.java

    r1dd9226 rd14176d  
    1 package project.fmo.app.projcetfmo.Web;public class NarackaController {
     1package project.fmo.app.projcetfmo.Web;
     2
     3import org.springframework.stereotype.Controller;
     4import org.springframework.ui.Model;
     5import org.springframework.web.bind.annotation.*;
     6import project.fmo.app.projcetfmo.Model.*;
     7import project.fmo.app.projcetfmo.Service.*;
     8
     9import javax.servlet.http.HttpServletRequest;
     10import java.util.List;
     11
     12@Controller
     13@RequestMapping("/order")
     14public 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
    276}
Note: See TracChangeset for help on using the changeset viewer.