package bg.car_wash.areas.car.controller;

import bg.car_wash.areas.car.models.bindingModel.CarBindingModel;
import bg.car_wash.configurations.site.PageTitle;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.Valid;

@Controller
@RequestMapping("/car")
public class CarController {

	@GetMapping("/all")
	public String getAllCarsPage(Model model) {
		model.addAttribute("pageTitle", PageTitle.CAR_ALL_PAGE);
		return "car/car-all";
	}

	@GetMapping("/add")
	public String addCarForWorkPage(
			Model model,
			@Valid @ModelAttribute CarBindingModel carBindingModel,
			BindingResult bindingResult) {
		model.addAttribute("pageTitle", PageTitle.CAR_ADD_PAGE);
		return "car/car-add";
	}

	@GetMapping("/edit")
	public String editCarForWorkPage(Model model) {
		model.addAttribute("pageTitle", PageTitle.CAR_EDIT_PAGE);
		return "car/car-edit";
	}

	@GetMapping("/delete")
	public String deleteCarForWorkPage(Model model) {
		model.addAttribute("pageTitle", PageTitle.CAR_DELETE_PAGE);
		return "car/car-edit";
	}
}

@@ -36,16 +36,57 @@
 oller;%0A%0A
+import bg.car_wash.areas.car.entity.Car;%0A
 import b
@@ -166,37 +166,245 @@
 ash.
-configurations.site.PageTitle
+areas.car.models.viewModel.CarViewModel;%0Aimport bg.car_wash.areas.car.service.CarService;%0Aimport bg.car_wash.configurations.site.PageTitle;%0Aimport org.modelmapper.ModelMapper;%0Aimport org.springframework.beans.factory.annotation.Autowired
 ;%0Aim
@@ -663,16 +663,76 @@
 ribute;%0A
+import org.springframework.web.bind.annotation.PostMapping;%0A
 import o
@@ -817,16 +817,68 @@
 n.Valid;
+%0Aimport java.util.LinkedList;%0Aimport java.util.List;
 %0A%0A@Contr
@@ -937,16 +937,108 @@
 ller %7B%0A%0A
+%09@Autowired%0A%09private CarService carService;%0A%0A%09@Autowired%0A%09private ModelMapper modelMapper;%0A%0A
 %09@GetMap
@@ -1146,32 +1146,293 @@
 .CAR_ALL_PAGE);%0A
+%0A%09%09List%3CCar%3E cars = this.carService.findAllCars();%0A%09%09List%3CCarViewModel%3E carsViewModel = new LinkedList%3C%3E();%0A%09%09for (Car car : cars) %7B%0A%09%09%09carsViewModel.add(this.modelMapper.map(car, CarViewModel.class));%0A%09%09%7D%0A%0A%09%09model.addAttribute(%22carViewModel%22, carsViewModel);%0A%0A
 %09%09return %22car/ca
@@ -1690,24 +1690,518 @@
 r-add%22;%0A%09%7D%0A%0A
+%09@PostMapping(%22/add%22)%0A%09public String addCar(%0A%09%09%09Model model,%0A%09%09%09@Valid @ModelAttribute CarBindingModel carBindingModel,%0A%09%09%09BindingResult bindingResult) %7B%0A%09%09model.addAttribute(%22pageTitle%22, PageTitle.CAR_ADD_PAGE);%0A%0A%09%09if(bindingResult.hasErrors()) %7B%0A%09%09%09return %22car/car-add%22;%0A%09%09%7D%0A%0A%09%09//TODO get customer from DB%0A%09%09Car car = this.modelMapper.map(carBindingModel, Car.class);%0A%09%09car.setCarModelName(carBindingModel.getCarModel());%0A%0A%09%09this.carService.createCar(car);%0A%0A%09%09return %22redirect:/car/all%22;%0A%09%7D%0A%0A
 %09@GetMapping

update "all" and "add" paths in car controller