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

Added functionalities

Location:
src/main/java/com/example/autopartz/controller
Files:
1 added
2 edited

Legend:

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

    rab952ab r7d43957  
    11package com.example.autopartz.controller;
    22
     3import com.example.autopartz.model.Order;
    34import com.example.autopartz.model.User;
     5import com.example.autopartz.model.Warehouse;
     6import com.example.autopartz.repository.OrderContainsPartRepository;
    47import com.example.autopartz.repository.PartsForCarTypeAndCategoryRepository;
    58import com.example.autopartz.repository.RepairShopReviewSummaryRepository;
    6 import com.example.autopartz.service.CarService;
    7 import com.example.autopartz.service.CategoryService;
    8 import com.example.autopartz.service.LoginService;
    9 import com.example.autopartz.service.PartService;
     9import com.example.autopartz.repository.WarehouseRepository;
     10import com.example.autopartz.service.*;
    1011import org.springframework.stereotype.Controller;
    1112import org.springframework.ui.Model;
     
    1617
    1718import javax.servlet.http.HttpServletRequest;
     19import javax.servlet.http.HttpServletResponse;
     20import javax.servlet.http.HttpSession;
     21import java.io.IOException;
     22import java.util.Objects;
    1823
    1924@Controller
     
    2631    private final CategoryService categoryService;
    2732    private final RepairShopReviewSummaryRepository repairShopReviewSummaryRepository;
     33    private final WarehouseRepository warehouseRepository;
     34    private final OrderContainsPartRepository orderContainsPartRepository;
     35    private final OrderService orderService;
    2836
    29     public HomeController(LoginService loginService, PartService partService, PartsForCarTypeAndCategoryRepository partsForCarTypeAndCategoryRepository, CarService carService, CategoryService categoryService, RepairShopReviewSummaryRepository repairShopReviewSummaryRepository) {
     37    public HomeController(LoginService loginService, PartService partService, PartsForCarTypeAndCategoryRepository partsForCarTypeAndCategoryRepository, CarService carService, CategoryService categoryService, RepairShopReviewSummaryRepository repairShopReviewSummaryRepository, WarehouseRepository warehouseRepository,
     38                          OrderContainsPartRepository orderContainsPartRepository, OrderService orderService) {
    3039        this.loginService = loginService;
    3140        this.partService = partService;
     
    3443        this.categoryService = categoryService;
    3544        this.repairShopReviewSummaryRepository = repairShopReviewSummaryRepository;
     45        this.warehouseRepository = warehouseRepository;
     46        this.orderContainsPartRepository = orderContainsPartRepository;
     47        this.orderService = orderService;
    3648    }
    3749
     
    5466        model.addAttribute("services",repairShopReviewSummaryRepository.findAll());
    5567        model.addAttribute("bodyContent","services");
     68        return "master-template";
     69    }
     70    @GetMapping("/currentOrder")
     71    public String getCurrentOrder(Model model,HttpSession session){
     72        if(session.getAttribute("order")==null){
     73            model.addAttribute("hasError",true);
     74            model.addAttribute("error","Нарачката е празна");
     75        }
     76        else {
     77            Order o = (Order) session.getAttribute("order");
     78            model.addAttribute("hasError",false);
     79            model.addAttribute("order",o);
     80            model.addAttribute("parts",orderService.findById(o.getID_order()).getPartList());
     81        }
     82        model.addAttribute("bodyContent","currentOrder");
    5683        return "master-template";
    5784    }
     
    79106    @PostMapping("/register")
    80107    public void handleRegister(@RequestParam String username, @RequestParam String name,
    81                                @RequestParam String password, @RequestParam String email,
    82                                @RequestParam String number){
    83         User u = loginService.register(name,username,email,number,password);
     108                               @RequestParam String password, @RequestParam String rpassword,
     109                               @RequestParam String email, @RequestParam String number,
     110                               @RequestParam String role, HttpServletResponse response, HttpSession session){
     111        System.out.println(username + name + password + rpassword + email + number + role);
     112        if(Objects.equals(role, "warehouseman")){
     113            session.setAttribute("username", username);
     114            session.setAttribute("name", name);
     115            session.setAttribute("password", password);
     116            session.setAttribute("rpassword", rpassword);
     117            session.setAttribute("email", email);
     118            session.setAttribute("number", number);
     119            try {
     120                response.sendRedirect("/registerWarehouseman");
     121            } catch (IOException e) {
     122                throw new RuntimeException(e);
     123            }
     124        }
     125        else {
     126            loginService.register(name, username, email, number, password, role);
     127            try {
     128                response.sendRedirect("/login");
     129            } catch (IOException e) {
     130                throw new RuntimeException(e);
     131            }
     132        }
     133    }
     134    @GetMapping("/registerWarehouseman")
     135    public String getSelectPage(Model model){
     136        model.addAttribute("locations",warehouseRepository.findAll());
     137        model.addAttribute("bodyContent","selectWarehouse");
     138        return "master-template";
     139    }
     140    @PostMapping("/finishRegister")
     141    public void handleWarehousemanRegister(@RequestParam String location,Model model, HttpServletResponse response, HttpSession session){
     142        System.out.println("here?");
     143        String username = (String) session.getAttribute("username");
     144        String name = (String) session.getAttribute("name");
     145        String password = (String) session.getAttribute("password");
     146        String email = (String) session.getAttribute("email");
     147        String number = (String) session.getAttribute("number");
     148        Warehouse warehouse = warehouseRepository.findAllByLocation(location).stream().findFirst().orElseThrow(RuntimeException::new);
     149        loginService.registerWarehouseman(name,username,email,number,password,"warehouseman",warehouse);
     150        try {
     151            response.sendRedirect("/login");
     152        } catch (IOException e) {
     153            throw new RuntimeException(e);
     154        }
    84155    }
    85156}
  • 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.