Changeset 84652fb


Ignore:
Timestamp:
01/07/23 18:37:26 (18 months ago)
Author:
andrejtodorovski <82031894+andrejtodorovski@…>
Branches:
main
Children:
5e7345e
Parents:
37966cf
Message:

Admin views for adding things to the database

Location:
src/main
Files:
7 added
11 edited

Legend:

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

    r37966cf r84652fb  
    3030                .antMatchers("/", "/products", "/services", "/filtered", "/login", "/register","/registerWarehouseman","/finishRegister","/test/*","/access_denied","/carCategoryReport","/partManufacturersReport","/mostPurchasedPart").permitAll()
    3131                .antMatchers("/orders","/repairs","/reviews","/part/*","/currentOrder","/addCarSampleForUser","/repairs/addReview/*").hasRole("CLIENT")
    32                 .antMatchers("/viewUsers","/approve/*","/addPart").hasRole("ADMIN")
     32                .antMatchers("/viewUsers","/approve/*","/addPart","/addCarManufacturer","/addPartManufacturer","/addCategory","/addCar","/addRepairShop","/addWarehouse").hasRole("ADMIN")
    3333                .antMatchers("/myWarehouseReport","myWarehouse").hasRole("WAREHOUSEMAN")
    3434                .anyRequest()
  • src/main/java/com/example/autopartz/controller/AdminController.java

    r37966cf r84652fb  
    55import com.example.autopartz.model.manytomany.PartIsFromCategory;
    66import com.example.autopartz.model.manytomany.PartIsInStockInWarehouse;
     7import com.example.autopartz.model.manytomany.RsForCm;
    78import com.example.autopartz.repository.*;
    89import com.example.autopartz.service.PriceService;
     
    1516import java.io.IOException;
    1617import java.time.LocalDate;
    17 import java.time.LocalDateTime;
    1818import java.util.List;
    1919import java.util.Objects;
     
    2323public class AdminController {
    2424    private final UserService userService;
     25    private final CarManufacturerRepository carManufacturerRepository;
    2526    private final PartIsFromCategoryRepository partIsFromCategoryRepository;
    2627    private final PartIsAppropriateForCarRepository partIsAppropriateForCarRepository;
     
    3435    private final PartManufacturerRepository partManufacturerRepository;
    3536    private final PriceService priceService;
    36 
    37     public AdminController(UserService userService, PartIsFromCategoryRepository partIsFromCategoryRepository, PartIsAppropriateForCarRepository partIsAppropriateForCarRepository, WarehousemanRepository warehousemanRepository, PartIsInStockInWarehouseRepository partIsInStockInWarehouseRepository, DeliverymanRepository deliverymanRepository, CategoryRepository categoryRepository, PartRepository partRepository, WarehouseRepository warehouseRepository, CarRepository carRepository, PartManufacturerRepository partManufacturerRepository, PriceService priceService) {
     37    private final RepairShopRepository repairShopRepository;
     38    private final RsForCmRepository rsForCmRepository;
     39
     40    public AdminController(UserService userService, CarManufacturerRepository carManufacturerRepository, PartIsFromCategoryRepository partIsFromCategoryRepository, PartIsAppropriateForCarRepository partIsAppropriateForCarRepository, WarehousemanRepository warehousemanRepository, PartIsInStockInWarehouseRepository partIsInStockInWarehouseRepository, DeliverymanRepository deliverymanRepository, CategoryRepository categoryRepository, PartRepository partRepository, WarehouseRepository warehouseRepository, CarRepository carRepository, PartManufacturerRepository partManufacturerRepository, PriceService priceService, RepairShopRepository repairShopRepository, RsForCmRepository rsForCmRepository) {
    3841        this.userService = userService;
     42        this.carManufacturerRepository = carManufacturerRepository;
    3943        this.partIsFromCategoryRepository = partIsFromCategoryRepository;
    4044        this.partIsAppropriateForCarRepository = partIsAppropriateForCarRepository;
     
    4852        this.partManufacturerRepository = partManufacturerRepository;
    4953        this.priceService = priceService;
     54        this.repairShopRepository = repairShopRepository;
     55        this.rsForCmRepository = rsForCmRepository;
    5056    }
    5157
     
    115121        }
    116122    }
     123    @GetMapping("/addCarManufacturer")
     124    public String getCarManView(Model model){
     125        model.addAttribute("bodyContent","addCarManufacturer");
     126        return "master-template";
     127    }
     128    @PostMapping("/addCarManufacturer")
     129    public void saveCarManufacturer(@RequestParam String name,@RequestParam String location,
     130                                    Model model, HttpServletResponse response) {
     131        carManufacturerRepository.save(new CarManufacturer(name,location));
     132        try {
     133            response.sendRedirect("/");
     134        } catch (IOException e) {
     135            throw new RuntimeException(e);
     136        }
     137    }
     138    @GetMapping("/addPartManufacturer")
     139    public String getPartManView(Model model){
     140        model.addAttribute("bodyContent","addPartManufacturer");
     141        return "master-template";
     142    }
     143    @PostMapping("/addPartManufacturer")
     144    public void savePartManufacturer(@RequestParam String name,@RequestParam String location,
     145                                    Model model, HttpServletResponse response) {
     146        partManufacturerRepository.save(new PartManufacturer(name,location));
     147        try {
     148            response.sendRedirect("/");
     149        } catch (IOException e) {
     150            throw new RuntimeException(e);
     151        }
     152    }
     153    @GetMapping("/addCategory")
     154    public String getCategoryView(Model model){
     155        model.addAttribute("bodyContent","addCategory");
     156        return "master-template";
     157    }
     158    @PostMapping("/addCategory")
     159    public void saveCategory(@RequestParam String name,
     160                                    Model model, HttpServletResponse response) {
     161        categoryRepository.save(new Category(name));
     162        try {
     163            response.sendRedirect("/");
     164        } catch (IOException e) {
     165            throw new RuntimeException(e);
     166        }
     167    }
     168    @GetMapping("/addCar")
     169    public String getCarView(Model model){
     170        model.addAttribute("bodyContent","addCar");
     171        model.addAttribute("manufacturers",carManufacturerRepository.findAll());
     172        return "master-template";
     173    }
     174    @PostMapping("/addCar")
     175    public void saveCar(@RequestParam Integer since,@RequestParam Integer till,
     176                             @RequestParam String name,@RequestParam Integer mId,
     177                                HttpServletResponse response) {
     178        carRepository.save(new Car(since,till,name,carManufacturerRepository.findById(mId).get()));
     179        try {
     180            response.sendRedirect("/");
     181        } catch (IOException e) {
     182            throw new RuntimeException(e);
     183        }
     184    }
     185    @GetMapping("/addRepairShop")
     186    public String getRepairShopView(Model model){
     187        model.addAttribute("bodyContent","addRepairShop");
     188        model.addAttribute("manufacturers",carManufacturerRepository.findAll());
     189        return "master-template";
     190    }
     191    @PostMapping("/addRepairShop")
     192    public void saveRepairShop(@RequestParam String name,@RequestParam String location,
     193                        @RequestParam String number,@RequestParam Integer carMId,
     194                        HttpServletResponse response) {
     195        RepairShop newRs = new RepairShop(name,location,number,
     196                List.of(carManufacturerRepository.findById(carMId).get()));
     197        repairShopRepository.save(newRs);
     198        rsForCmRepository.save(new RsForCm(newRs.getId(), carMId));
     199        try {
     200            response.sendRedirect("/");
     201        } catch (IOException e) {
     202            throw new RuntimeException(e);
     203        }
     204    }
     205    @GetMapping("/addWarehouse")
     206    public String getWarehouseView(Model model){
     207        model.addAttribute("bodyContent","addWarehouse");
     208        return "master-template";
     209    }
     210    @PostMapping("/addWarehouse")
     211    public void saveWarehouse(@RequestParam String name,
     212                        HttpServletResponse response) {
     213        warehouseRepository.save(new Warehouse(name));
     214        try {
     215            response.sendRedirect("/");
     216        } catch (IOException e) {
     217            throw new RuntimeException(e);
     218        }
     219    }
    117220}
  • src/main/java/com/example/autopartz/controller/PartController.java

    r37966cf r84652fb  
    8989            model.addAttribute("hasError", false);
    9090            CarSample cs = carSampleRepository.findById((Integer) session.getAttribute("carVin")).get();
    91             Integer idCM = cs.getCar().getCar_manufacturer().getID_car_manufacturer();
     91            Integer idCM = cs.getCar().getCar_manufacturer().getId();
    9292            List<RsForCm> rsForCm = rsForCmRepository.findAllByCmid(idCM);
    9393            List<RepairShop> newRepairShopList = new ArrayList<>();
  • src/main/java/com/example/autopartz/model/Car.java

    r37966cf r84652fb  
    1818public class Car {
    1919    @Id
     20    @GeneratedValue(strategy = GenerationType.IDENTITY)
    2021    @Column(name = "ID_car")
    2122    Integer id;
     
    2728    @JoinColumn(name = "id_car_manufacturer")
    2829    CarManufacturer car_manufacturer;
     30
     31    public Car(Integer in_production_since, Integer in_production_till, String cartype, CarManufacturer car_manufacturer) {
     32        this.in_production_since = in_production_since;
     33        this.in_production_till = in_production_till;
     34        this.cartype = cartype;
     35        this.car_manufacturer = car_manufacturer;
     36    }
    2937
    3038    @Override
  • src/main/java/com/example/autopartz/model/CarManufacturer.java

    r37966cf r84652fb  
    77import org.hibernate.Hibernate;
    88
    9 import javax.persistence.Entity;
    10 import javax.persistence.Id;
    11 import javax.persistence.Table;
     9import javax.persistence.*;
    1210import java.util.Objects;
    1311
     
    2018public class CarManufacturer {
    2119    @Id
    22     Integer ID_car_manufacturer;
    23     String cm_name;
     20    @GeneratedValue(strategy = GenerationType.IDENTITY)
     21    @Column(name = "id_car_manufacturer")
     22    Integer id;
     23    @Column(name = "cm_name")
     24    String cmname;
    2425    String cm_country;
     26
     27    public CarManufacturer(String cmname, String cm_country) {
     28        this.cmname = cmname;
     29        this.cm_country = cm_country;
     30    }
    2531
    2632    @Override
     
    2935        if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
    3036        CarManufacturer that = (CarManufacturer) o;
    31         return ID_car_manufacturer != null && Objects.equals(ID_car_manufacturer, that.ID_car_manufacturer);
     37        return id != null && Objects.equals(id, that.id);
    3238    }
    3339
  • src/main/java/com/example/autopartz/model/Category.java

    r37966cf r84652fb  
    1818    @Id
    1919    @Column(name = "id_category")
     20    @GeneratedValue(strategy = GenerationType.IDENTITY)
    2021    Integer id;
    2122    @Column(name = "category_name")
     
    2425    @JoinColumn(name = "id_parent_category")
    2526    Category ID_parent_category;
     27
     28    public Category(String category) {
     29        this.cname = category;
     30    }
    2631
    2732    @Override
  • src/main/java/com/example/autopartz/model/PartManufacturer.java

    r37966cf r84652fb  
    77import org.hibernate.Hibernate;
    88
    9 import javax.persistence.Column;
    10 import javax.persistence.Entity;
    11 import javax.persistence.Id;
    12 import javax.persistence.Table;
     9import javax.persistence.*;
    1310import java.util.Objects;
    1411
     
    2118public class PartManufacturer {
    2219    @Id
     20    @GeneratedValue(strategy = GenerationType.IDENTITY)
    2321    @Column(name = "id_part_manufacturer")
    2422    Integer id;
     
    2624    String name;
    2725    String pm_location;
     26
     27    public PartManufacturer(String name, String location) {
     28        this.name = name;
     29        this.pm_location = location;
     30    }
    2831
    2932    @Override
  • src/main/java/com/example/autopartz/model/RepairShop.java

    r37966cf r84652fb  
    2020    @Id
    2121    @Column(name = "ID_repair_shop")
     22    @GeneratedValue(strategy = GenerationType.IDENTITY)
    2223    Integer id;
    2324    @Column(name = "rs_name")
     
    3435    List<CarManufacturer> carManufacturerList;
    3536
     37    public RepairShop(String name, String location, String number, List<CarManufacturer> carManufacturerList) {
     38        this.name = name;
     39        this.location = location;
     40        this.number = number;
     41        this.carManufacturerList = carManufacturerList;
     42    }
     43
    3644    @Override
    3745    public boolean equals(Object o) {
  • src/main/java/com/example/autopartz/model/Warehouse.java

    r37966cf r84652fb  
    77import org.hibernate.Hibernate;
    88
    9 import javax.persistence.Column;
    10 import javax.persistence.Entity;
    11 import javax.persistence.Id;
     9import javax.persistence.*;
    1210import java.util.Objects;
    1311
     
    2018    @Id
    2119    @Column(name = "id_warehouse")
     20    @GeneratedValue(strategy = GenerationType.IDENTITY)
    2221    Integer id;
    2322    @Column(name = "warehouse_location")
    2423    String location;
     24
     25    public Warehouse(String name) {
     26        this.location = name;
     27    }
    2528
    2629    @Override
  • src/main/java/com/example/autopartz/model/manytomany/RsForCm.java

    r37966cf r84652fb  
    2222    @Id
    2323    Integer cmid;
     24
     25    public RsForCm(Integer rsid, Integer cmid) {
     26        this.rsid = rsid;
     27        this.cmid = cmid;
     28    }
    2429}
  • src/main/resources/templates/fragments/header.html

    r37966cf r84652fb  
    5757          <th:block sec:authorize="hasAuthority('ROLE_ADMIN')">
    5858            <li class="nav-item m-auto">
    59               <a class="nav-link active" href="/addPart">Додај дел во системот</a>
     59              <a class="nav-link active" href="/addPart">Додај дел</a>
     60            </li>
     61          </th:block>
     62          <th:block sec:authorize="hasAuthority('ROLE_ADMIN')">
     63            <li class="nav-item m-auto">
     64              <a class="nav-link active" href="/addCarManufacturer">Додај производител на коли</a>
     65            </li>
     66          </th:block>
     67          <th:block sec:authorize="hasAuthority('ROLE_ADMIN')">
     68            <li class="nav-item m-auto">
     69              <a class="nav-link active" href="/addPartManufacturer">Додај производител на дел</a>
     70            </li>
     71          </th:block>
     72          <th:block sec:authorize="hasAuthority('ROLE_ADMIN')">
     73            <li class="nav-item m-auto">
     74              <a class="nav-link active" href="/addWarehouse">Додај магацин</a>
     75            </li>
     76          </th:block>
     77          <th:block sec:authorize="hasAuthority('ROLE_ADMIN')">
     78            <li class="nav-item m-auto">
     79              <a class="nav-link active" href="/addCar">Додај кола</a>
     80            </li>
     81          </th:block>
     82          <th:block sec:authorize="hasAuthority('ROLE_ADMIN')">
     83            <li class="nav-item m-auto">
     84              <a class="nav-link active" href="/addRepairShop">Додај сервис</a>
     85            </li>
     86          </th:block>
     87          <th:block sec:authorize="hasAuthority('ROLE_ADMIN')">
     88            <li class="nav-item m-auto">
     89              <a class="nav-link active" href="/addCategory">Додај категорија</a>
    6090            </li>
    6191          </th:block>
Note: See TracChangeset for help on using the changeset viewer.