Ignore:
Timestamp:
01/05/23 01:31:58 (23 months ago)
Author:
andrejtodorovski <82031894+andrejtodorovski@…>
Branches:
main
Children:
676144b
Parents:
ab952ab
Message:

Added functionalities

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/com/example/autopartz/controller/PartController.java

    rab952ab r7d43957  
    11package com.example.autopartz.controller;
    22
     3import com.example.autopartz.model.Client;
     4import com.example.autopartz.model.Order;
    35import com.example.autopartz.model.Part;
    4 import com.example.autopartz.service.PartService;
    5 import com.example.autopartz.service.PriceService;
    6 import com.example.autopartz.service.RepairShopService;
     6import com.example.autopartz.model.User;
     7import com.example.autopartz.model.manytomany.OrderContainsPart;
     8import com.example.autopartz.repository.OrderContainsPartRepository;
     9import com.example.autopartz.service.*;
    710import org.springframework.stereotype.Controller;
    811import org.springframework.ui.Model;
    912import org.springframework.web.bind.annotation.*;
    1013
     14import javax.servlet.http.HttpServletRequest;
    1115import javax.servlet.http.HttpServletResponse;
     16import javax.servlet.http.HttpSession;
    1217import java.io.IOException;
    1318
     
    1823    private final RepairShopService repairShopService;
    1924    private final PriceService priceService;
    20     public PartController(PartService partService, RepairShopService repairShopService, PriceService priceService) {
     25    private final OrderService orderService;
     26    private final UserService userService;
     27    private final OrderContainsPartRepository orderContainsPartRepository;
     28    public PartController(PartService partService, RepairShopService repairShopService, PriceService priceService, OrderService orderService, UserService userService, OrderContainsPartRepository orderContainsPartRepository) {
    2129        this.partService = partService;
    2230        this.repairShopService = repairShopService;
    2331        this.priceService = priceService;
     32        this.orderService = orderService;
     33        this.userService = userService;
     34        this.orderContainsPartRepository = orderContainsPartRepository;
    2435    }
    2536    @GetMapping("/{id}")
     
    3243        return "master-template";
    3344    }
    34     @GetMapping("/delivery/{id}")
    35     public String getDeliveryPage(@PathVariable Integer id, Model model){
     45    @GetMapping("/delivery")
     46    public String getDeliveryPage(Model model){
    3647        model.addAttribute("repairShops",repairShopService.findAll());
    37         model.addAttribute("partId",id);
    3848        model.addAttribute("bodyContent","deliveryForPart");
    3949        return "master-template";
     
    5767        }
    5868    }
     69    @PostMapping("/addToOrder/{id}")
     70    public void addToOrder(@PathVariable Integer id,@RequestParam Integer quantity, HttpSession session, HttpServletResponse response, HttpServletRequest request){
     71        if(session.getAttribute("order")==null){
     72            User u = userService.findByUsername(request.getRemoteUser());
     73            Order newOrder = orderService.create((Client) u);
     74            session.setAttribute("order",newOrder);
     75        }
     76        Order order = (Order) session.getAttribute("order");
     77        orderContainsPartRepository.save(new OrderContainsPart(id,order.getID_order(),quantity));
     78        try {
     79            response.sendRedirect("/products");
     80        } catch (IOException e) {
     81            throw new RuntimeException(e);
     82        }
     83    }
    5984}
Note: See TracChangeset for help on using the changeset viewer.