Ignore:
Timestamp:
01/06/23 02:51:45 (23 months ago)
Author:
andrejtodorovski <82031894+andrejtodorovski@…>
Branches:
main
Children:
d4b888e
Parents:
1bd8d1e
Message:

Added full functionality for creating order

File:
1 edited

Legend:

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

    r1bd8d1e rf484b14  
    11package com.example.autopartz.controller;
    22
    3 import com.example.autopartz.repository.OrdersForUserRepository;
    4 import com.example.autopartz.repository.RepairsForUserRepository;
    5 import com.example.autopartz.repository.ReviewsForUserRepository;
     3import com.example.autopartz.model.CarSample;
     4import com.example.autopartz.model.Client;
     5import com.example.autopartz.model.ServiceBook;
     6import com.example.autopartz.repository.*;
     7import com.example.autopartz.service.CarService;
    68import com.example.autopartz.service.UserService;
    79import org.springframework.stereotype.Controller;
    810import org.springframework.ui.Model;
    911import org.springframework.web.bind.annotation.GetMapping;
    10 import org.springframework.web.bind.annotation.PathVariable;
     12import org.springframework.web.bind.annotation.PostMapping;
    1113import org.springframework.web.bind.annotation.RequestMapping;
     14import org.springframework.web.bind.annotation.RequestParam;
    1215
    1316import javax.servlet.http.HttpServletRequest;
     17import javax.servlet.http.HttpServletResponse;
     18import java.io.IOException;
    1419
    1520@Controller
     
    2025    private final ReviewsForUserRepository reviewsForUserRepository;
    2126    private final UserService userService;
    22     public UserController(OrdersForUserRepository ordersForUserRepository, RepairsForUserRepository repairsForUserRepository, ReviewsForUserRepository reviewsForUserRepository, UserService userService) {
     27    private final CarService carService;
     28    private final CarSampleRepository carSampleRepository;
     29    private final ServiceBookRepository serviceBookRepository;
     30    public UserController(OrdersForUserRepository ordersForUserRepository, RepairsForUserRepository repairsForUserRepository, ReviewsForUserRepository reviewsForUserRepository, UserService userService, CarService carService, CarSampleRepository carSampleRepository, ServiceBookRepository serviceBookRepository) {
    2331        this.ordersForUserRepository = ordersForUserRepository;
    2432        this.repairsForUserRepository = repairsForUserRepository;
    2533        this.reviewsForUserRepository = reviewsForUserRepository;
    2634        this.userService = userService;
     35        this.carService = carService;
     36        this.carSampleRepository = carSampleRepository;
     37        this.serviceBookRepository = serviceBookRepository;
    2738    }
    2839    @GetMapping("orders")
     
    4758        return "master-template";
    4859    }
     60    @GetMapping("/addCarSampleForUser")
     61    public String addCarSampleForUser(Model model){
     62        model.addAttribute("bodyContent","addCarSampleForUser");
     63        model.addAttribute("cars",carService.findAll());
     64        return "master-template";
     65    }
     66    @PostMapping("/addCarSampleForUser")
     67    public void addCarSampleForUser(@RequestParam Integer vin, @RequestParam Integer year, @RequestParam Integer power,
     68                                    @RequestParam Integer displacement, @RequestParam String fuel,
     69                                    @RequestParam Integer km, @RequestParam Integer cartype,
     70                                    HttpServletRequest request, HttpServletResponse response){
     71        CarSample cs = new CarSample(vin,year,power,displacement,fuel,km, (Client) userService.findByUsername(request.getRemoteUser()),carService.findById(cartype));
     72        carSampleRepository.save(cs);
     73        serviceBookRepository.save(new ServiceBook(cs));
     74        try {
     75            response.sendRedirect("/");
     76        } catch (IOException e) {
     77            throw new RuntimeException(e);
     78        }
     79    }
    4980}
Note: See TracChangeset for help on using the changeset viewer.