Changeset f484b14


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

Location:
src/main
Files:
9 added
16 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/com/example/autopartz/config/WebSecurityConfig.java

    r1bd8d1e rf484b14  
    2929                .authorizeRequests()
    3030                .antMatchers("/", "/products", "/services", "/filtered", "/login", "/register","/registerWarehouseman","/finishRegister","/test/*","/access_denied").permitAll()
    31                 .antMatchers("/orders","/repairs","/reviews","/part/*","/currentOrder").hasRole("CLIENT")
     31                .antMatchers("/orders","/repairs","/reviews","/part/*","/currentOrder","/addCarSampleForUser").hasRole("CLIENT")
    3232                .antMatchers("/viewUsers","/approve/*").hasRole("ADMIN")
    3333                .anyRequest()
  • src/main/java/com/example/autopartz/controller/AdminController.java

    r1bd8d1e rf484b14  
    11package com.example.autopartz.controller;
    22
     3import com.example.autopartz.model.Deliveryman;
    34import com.example.autopartz.model.Role;
    45import com.example.autopartz.model.User;
    56import com.example.autopartz.model.Warehouseman;
     7import com.example.autopartz.repository.DeliverymanRepository;
    68import com.example.autopartz.repository.WarehousemanRepository;
    79import com.example.autopartz.service.UserService;
     
    1315import org.springframework.web.bind.annotation.RequestMapping;
    1416
    15 import javax.annotation.security.RolesAllowed;
    1617import javax.servlet.http.HttpServletResponse;
    1718import java.io.IOException;
     
    2526    private final UserService userService;
    2627    private final WarehousemanRepository warehousemanRepository;
     28    private final DeliverymanRepository deliverymanRepository;
    2729
    28     public AdminController(UserService userService, WarehousemanRepository warehousemanRepository) {
     30    public AdminController(UserService userService, WarehousemanRepository warehousemanRepository, DeliverymanRepository deliverymanRepository) {
    2931        this.userService = userService;
    3032        this.warehousemanRepository = warehousemanRepository;
     33        this.deliverymanRepository = deliverymanRepository;
    3134    }
    3235
     
    4447            wh.setEmployed_from(LocalDate.now());
    4548            warehousemanRepository.save(wh);
     49
     50        }
     51        else {
     52            Deliveryman dm = (Deliveryman) userService.findById(id);
     53            dm.setEmployed_from(LocalDate.now());
     54            deliverymanRepository.save(dm);
    4655            try {
    4756                response.sendRedirect("/viewUsers");
  • src/main/java/com/example/autopartz/controller/PartController.java

    r1bd8d1e rf484b14  
    11package com.example.autopartz.controller;
    22
    3 import com.example.autopartz.model.Client;
    4 import com.example.autopartz.model.Order;
    5 import com.example.autopartz.model.Part;
    6 import com.example.autopartz.model.User;
     3import com.example.autopartz.model.*;
    74import com.example.autopartz.model.manytomany.OrderContainsPart;
    8 import com.example.autopartz.repository.OrderContainsPartRepository;
     5import com.example.autopartz.model.manytomany.RsForCm;
     6import com.example.autopartz.repository.*;
    97import com.example.autopartz.service.*;
    108import org.springframework.stereotype.Controller;
     
    1614import javax.servlet.http.HttpSession;
    1715import java.io.IOException;
     16import java.util.ArrayList;
     17import java.util.List;
    1818
    1919@Controller
     
    2626    private final UserService userService;
    2727    private final OrderContainsPartRepository orderContainsPartRepository;
    28     public PartController(PartService partService, RepairShopService repairShopService, PriceService priceService, OrderService orderService, UserService userService, OrderContainsPartRepository orderContainsPartRepository) {
     28    private final DeliveryRepository deliveryRepository;
     29    private final RepairRepository repairRepository;
     30    private final CarSampleRepository carSampleRepository;
     31    private final ServiceBookRepository serviceBookRepository;
     32    private final DeliverymanRepository deliverymanRepository;
     33    private final RsForCmRepository rsForCmRepository;
     34    public PartController(PartService partService, RepairShopService repairShopService, PriceService priceService, OrderService orderService, UserService userService, OrderContainsPartRepository orderContainsPartRepository, DeliveryRepository deliveryRepository, RepairRepository repairRepository, CarSampleRepository carSampleRepository, ServiceBookRepository serviceBookRepository, DeliverymanRepository deliverymanRepository, RsForCmRepository rsForCmRepository) {
    2935        this.partService = partService;
    3036        this.repairShopService = repairShopService;
     
    3339        this.userService = userService;
    3440        this.orderContainsPartRepository = orderContainsPartRepository;
     41        this.deliveryRepository = deliveryRepository;
     42        this.repairRepository = repairRepository;
     43        this.carSampleRepository = carSampleRepository;
     44        this.serviceBookRepository = serviceBookRepository;
     45        this.deliverymanRepository = deliverymanRepository;
     46        this.rsForCmRepository = rsForCmRepository;
    3547    }
    3648    @GetMapping("/{id}")
     
    4456    }
    4557    @GetMapping("/delivery")
    46     public String getDeliveryPage(Model model){
    47         model.addAttribute("repairShops",repairShopService.findAll());
     58    public String getDeliveryPage(Model model, HttpServletRequest request){
     59        Client cl = (Client) userService.findByUsername(request.getRemoteUser());
     60        List<CarSample> cs = carSampleRepository.findAllByClient(cl);
     61        model.addAttribute("cars",cs);
     62        if(cs.size()==0){
     63                model.addAttribute("hasError",true);
     64                model.addAttribute("error","Внеси твоја кола");
     65        }
     66        else {
     67            model.addAttribute("hasError",false);
     68        }
    4869        model.addAttribute("bodyContent","deliveryForPart");
    4970        return "master-template";
    5071    }
    5172    @PostMapping("/repairshopdelivery")
    52     public void setRepairShopDelivery(@RequestParam String name, HttpServletResponse response, HttpSession session){
     73    public void setRepairShopDelivery(@RequestParam Integer vin, HttpServletResponse response, HttpSession session, HttpServletRequest request){
    5374        // insert into project.repair (vin, id_repair_shop, id_service_book) values (1111,3,1)
     75        session.setAttribute("carVin",vin);
     76        try {
     77            response.sendRedirect("/part/chooseRepairShop");
     78        } catch (IOException e) {
     79            throw new RuntimeException(e);
     80        }
     81    }
     82    @GetMapping("/chooseRepairShop")
     83    public String chooseRepairShop(Model model, HttpSession session){
     84        if(session.getAttribute("carVin")==null){
     85            model.addAttribute("hasError",true);
     86            model.addAttribute("error","Внеси твоја кола");
     87        }
     88        else {
     89            model.addAttribute("hasError", false);
     90            CarSample cs = carSampleRepository.findById((Integer) session.getAttribute("carVin")).get();
     91            Integer idCM = cs.getCar().getCar_manufacturer().getID_car_manufacturer();
     92            List<RsForCm> rsForCm = rsForCmRepository.findAllByCmid(idCM);
     93            List<RepairShop> newRepairShopList = new ArrayList<>();
     94            for (RsForCm forCm : rsForCm) {
     95                newRepairShopList.add(repairShopService.getById(forCm.getRsid()));
     96            }
     97            model.addAttribute("authorized", newRepairShopList);
     98        }
     99        model.addAttribute("bodyContent", "chooseRepairShop");
     100        return "master-template";
     101    }
     102    @PostMapping("/chooseRepairShop")
     103    public void chooseRepairShop(@RequestParam Integer rs,HttpSession session, HttpServletResponse response){
     104        Order o = (Order) session.getAttribute("order");
     105        CarSample cs = (CarSample)session.getAttribute("car");
     106        ServiceBook sb = serviceBookRepository.findByCarSample(cs);
     107        RepairShop repairShop = repairShopService.getById(rs);
     108        repairRepository.save(new Repair(o,repairShop,sb));
    54109        session.removeAttribute("order");
    55110        try {
     
    62117    public void setHomeDelivery(@RequestParam String address, HttpServletResponse response, HttpSession session){
    63118        // insert into delivery (delivery_status, delivery_address,id_user,id_order) values ('in progress','Aerodrom',4,1)
     119        Order o = (Order) session.getAttribute("order");
     120        List<Deliveryman> deliverymanList = deliverymanRepository.findAll().stream().filter(dm->dm.getAuthorities().contains(Role.ROLE_DELIVERYMAN)).toList();
     121        int num = deliverymanList.size();
     122        int deliverer =  (int) ((Math.random() * (num)));
     123        Deliveryman dm = deliverymanList.get(deliverer);
     124        deliveryRepository.save(new Delivery("in progress",address,dm,o));
    64125        session.removeAttribute("order");
    65126        try {
  • 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}
  • src/main/java/com/example/autopartz/model/Car.java

    r1bd8d1e rf484b14  
    1818public class Car {
    1919    @Id
    20     Integer ID_car;
     20    @Column(name = "ID_car")
     21    Integer id;
    2122    Integer in_production_since;
    2223    Integer in_production_till;
     
    3233        if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
    3334        Car car = (Car) o;
    34         return ID_car != null && Objects.equals(ID_car, car.ID_car);
     35        return id != null && Objects.equals(id, car.id);
    3536    }
    3637
  • src/main/java/com/example/autopartz/model/CarSample.java

    r1bd8d1e rf484b14  
    3131    Car car;
    3232
     33
     34    public CarSample(Integer vin,Integer year_of_production, Integer engine_power, Integer displacement, String fuel_type, Integer km_driven, Client client, Car car) {
     35        this.vin = vin;
     36        this.year_of_production = year_of_production;
     37        this.engine_power = engine_power;
     38        this.displacement = displacement;
     39        this.fuel_type = fuel_type;
     40        this.km_driven = km_driven;
     41        this.client = client;
     42        this.car = car;
     43    }
     44
    3345    @Override
    3446    public boolean equals(Object o) {
  • src/main/java/com/example/autopartz/model/Delivery.java

    r1bd8d1e rf484b14  
    1717public class Delivery {
    1818    @Id
     19    @GeneratedValue(strategy = GenerationType.IDENTITY)
    1920    Integer ID_delivery;
    2021    @Column(name = "delivery_status")
     
    2829    @OneToOne
    2930    Order order;
     31
     32    public Delivery(String status, String address, Deliveryman deliveryman, Order order) {
     33        this.status = status;
     34        this.address = address;
     35        this.deliveryman = deliveryman;
     36        this.order = order;
     37    }
    3038
    3139    @Override
  • src/main/java/com/example/autopartz/model/Repair.java

    r1bd8d1e rf484b14  
    1717public class Repair {
    1818    @Id
     19    @GeneratedValue(strategy = GenerationType.IDENTITY)
    1920    Integer ID_repair;
    2021    @OneToOne
     
    2728    @JoinColumn(name = "id_service_book")
    2829    ServiceBook serviceBook;
     30
     31    public Repair(Order order, RepairShop repairShop, ServiceBook serviceBook) {
     32        this.order = order;
     33        this.repairShop = repairShop;
     34        this.serviceBook = serviceBook;
     35    }
    2936
    3037    @Override
  • src/main/java/com/example/autopartz/model/ServiceBook.java

    r1bd8d1e rf484b14  
    1919public class ServiceBook {
    2020    @Id
    21     Integer ID_service_book;
     21    @Column(name = "ID_service_book")
     22    @GeneratedValue(strategy = GenerationType.IDENTITY)
     23    Integer id;
    2224    LocalDateTime sb_created_on;
    2325    @OneToOne
    2426    @JoinColumn(name = "vin")
    2527    CarSample carSample;
     28
     29    public ServiceBook(CarSample carSample) {
     30        this.sb_created_on = LocalDateTime.now();
     31        this.carSample = carSample;
     32    }
    2633
    2734    @Override
     
    3037        if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
    3138        ServiceBook that = (ServiceBook) o;
    32         return ID_service_book != null && Objects.equals(ID_service_book, that.ID_service_book);
     39        return id != null && Objects.equals(id, that.id);
    3340    }
    3441
  • src/main/java/com/example/autopartz/repository/RepairShopRepository.java

    r1bd8d1e rf484b14  
    77@Repository
    88public interface RepairShopRepository extends JpaRepository<RepairShop,Integer> {
    9 
     9    RepairShop getRepairShopByName(String name);
    1010}
  • src/main/java/com/example/autopartz/service/CarService.java

    r1bd8d1e rf484b14  
    77public interface CarService {
    88    List<Car> findAll();
     9    Car findById(Integer id);
    910}
  • src/main/java/com/example/autopartz/service/RepairShopService.java

    r1bd8d1e rf484b14  
    77public interface RepairShopService {
    88    List<RepairShop> findAll();
     9    RepairShop getByName(String name);
     10    RepairShop getById(Integer id);
    911}
  • src/main/java/com/example/autopartz/service/impl/CarServiceImpl.java

    r1bd8d1e rf484b14  
    2020        return carRepository.findAll();
    2121    }
     22
     23    @Override
     24    public Car findById(Integer id) {
     25        return carRepository.findById(id).get();
     26    }
    2227}
  • src/main/java/com/example/autopartz/service/impl/RepairShopServiceImpl.java

    r1bd8d1e rf484b14  
    2020        return repairShopRepository.findAll();
    2121    }
     22
     23    @Override
     24    public RepairShop getByName(String name) {
     25        return repairShopRepository.getRepairShopByName(name);
     26    }
     27
     28    @Override
     29    public RepairShop getById(Integer id) {
     30        return repairShopRepository.findById(id).get();
     31    }
    2232}
  • src/main/resources/templates/deliveryForPart.html

    r1bd8d1e rf484b14  
    1313  </form>
    1414  <h3>Достава до овластен сервис</h3>
     15  <h1 th:if="${hasError}" th:text="${error}"></h1>
     16  <div th:if="${!hasError}">
    1517  <form class="form-signin mt-xl-5" method="post" action="/part/repairshopdelivery">
    1618    <p>
    17       <label for="repairShop" class="sr-only">Избор на овластен сервис</label>
    18       <select id="repairShop" required name="name">
    19         <option  th:each="rs : ${repairShops}"
    20                  th:text="${rs.getName()}"
    21                  th:value="${rs.getId()}">
     19      <span>Избор на некоја од твоите коли</span>
     20      <label for="vin" class="sr-only">Избор на некоја од твоите коли</label>
     21      <select id="vin" required name="vin">
     22        <option  th:each="car : ${cars}"
     23                 th:text="${car.getCar().getCartype()}"
     24                 th:value="${car.getVin()}">
    2225        </option>
    2326      </select>
    2427    </p>
    25     <button class="btn btn-lg btn-primary btn-block" type="submit">Достава до сервис</button>
     28    <button class="btn btn-lg btn-primary btn-block" type="submit">Следно</button>
    2629  </form>
     30  </div>
    2731</main>
    2832
  • src/main/resources/templates/fragments/header.html

    r1bd8d1e rf484b14  
    2020            <a class="nav-link active" href="/currentOrder">Моја нарачка</a>
    2121          </li>
     22          </th:block>
     23          <th:block sec:authorize="hasAuthority('ROLE_CLIENT')">
     24            <li class="nav-item m-auto">
     25              <a class="nav-link active" href="/addCarSampleForUser">Додај примерок од кола</a>
     26            </li>
    2227          </th:block>
    2328          <th:block sec:authorize="hasAuthority('ROLE_WAREHOUSEMAN')">
Note: See TracChangeset for help on using the changeset viewer.