Changeset 7d43957 for src


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
Files:
10 added
21 edited

Legend:

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

    rab952ab r7d43957  
    66import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
    77import org.springframework.security.crypto.password.PasswordEncoder;
     8import org.thymeleaf.extras.springsecurity5.dialect.SpringSecurityDialect;
    89
    910@SpringBootApplication
     
    1718        return new BCryptPasswordEncoder(10);
    1819    }
    19 
     20    @Bean
     21    public SpringSecurityDialect securityDialect() {
     22        return new SpringSecurityDialect();
     23    }
    2024
    2125}
  • src/main/java/com/example/autopartz/config/WebSecurityConfig.java

    rab952ab r7d43957  
    2828        http.csrf().disable()
    2929                .authorizeRequests()
    30                 .antMatchers("/", "/products", "/services", "/filtered", "/login", "/register").permitAll()
    31                 .antMatchers("/orders","/repairs","/reviews","/part/*").hasRole("CLIENT")
     30                .antMatchers("/", "/products", "/services", "/filtered", "/login", "/register","/registerWarehouseman","/finishRegister","/test/*").permitAll()
     31                .antMatchers("/orders","/repairs","/reviews","/part/*","/currentOrder").hasRole("CLIENT")
    3232                .anyRequest()
    3333                .authenticated()
  • 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}
  • src/main/java/com/example/autopartz/model/Client.java

    rab952ab r7d43957  
    1919@Entity
    2020public class Client extends User{
     21    public Client(String username, String name, String email, String password, String number) {
     22        super(username, name, email, password, number);
     23    }
     24
    2125    @Override
    2226    public boolean equals(Object o) {
  • src/main/java/com/example/autopartz/model/Deliveryman.java

    rab952ab r7d43957  
    2323public class Deliveryman extends User{
    2424    LocalDate employed_from;
     25    public static final LocalDate defaultEmployedFrom = LocalDate.of(2020,1,1);
     26
     27    public Deliveryman(String username, String name, String email, String password, String number) {
     28        super(username, name, email, password, number);
     29        this.employed_from = defaultEmployedFrom;
     30    }
    2531
    2632    @Override
     
    3844    @Override
    3945    public Collection<? extends GrantedAuthority> getAuthorities() {
    40         return Collections.singletonList(Role.ROLE_DELIVERYMAN);
     46        if(Objects.equals(employed_from, defaultEmployedFrom))
     47            return Collections.singletonList(Role.ROLE_PENDING_DELIVERYMAN);
     48        else
     49            return Collections.singletonList(Role.ROLE_DELIVERYMAN);
    4150    }
    4251}
  • src/main/java/com/example/autopartz/model/Order.java

    rab952ab r7d43957  
    2020public class Order {
    2121    @Id
     22    @GeneratedValue(strategy = GenerationType.IDENTITY)
    2223    Integer ID_order;
    2324    String order_status;
     
    3233    @ToString.Exclude
    3334    List<Part> partList;
     35
     36    public Order(Client user) {
     37        this.order_status = "created";
     38        this.user = user;
     39        this.order_date = LocalDateTime.now();
     40    }
    3441
    3542    @Override
  • src/main/java/com/example/autopartz/model/Repair.java

    rab952ab r7d43957  
    77import org.hibernate.Hibernate;
    88
    9 import javax.persistence.Entity;
    10 import javax.persistence.Id;
    11 import javax.persistence.JoinColumn;
    12 import javax.persistence.ManyToOne;
     9import javax.persistence.*;
    1310import java.util.Objects;
    1411
     
    2118    @Id
    2219    Integer ID_repair;
    23     @ManyToOne
    24     @JoinColumn(name = "vin")
    25     CarSample carSample;
     20    @OneToOne
     21    @JoinColumn(name = "id_order")
     22    Order order;
    2623    @ManyToOne
    2724    @JoinColumn(name = "id_repair_shop")
  • src/main/java/com/example/autopartz/model/Role.java

    rab952ab r7d43957  
    66public enum Role implements GrantedAuthority {
    77
    8     ROLE_USER,ROLE_CLIENT, ROLE_ADMIN, ROLE_WAREHOUSEMAN, ROLE_DELIVERYMAN;
     8    ROLE_USER,ROLE_CLIENT, ROLE_ADMIN, ROLE_WAREHOUSEMAN, ROLE_DELIVERYMAN, ROLE_PENDING_WAREHOUSEMAN,ROLE_PENDING_DELIVERYMAN;
    99
    1010    @Override
  • src/main/java/com/example/autopartz/model/User.java

    rab952ab r7d43957  
    2424public class User implements UserDetails {
    2525    @Id
     26    @GeneratedValue(strategy = GenerationType.IDENTITY)
    2627    Integer ID_user;
    2728    String username;
  • src/main/java/com/example/autopartz/model/Warehouse.java

    rab952ab r7d43957  
    77import org.hibernate.Hibernate;
    88
     9import javax.persistence.Column;
    910import javax.persistence.Entity;
    1011import javax.persistence.Id;
     
    1920    @Id
    2021    Integer ID_warehouse;
    21     String warehouse_location;
     22    @Column(name = "warehouse_location")
     23    String location;
    2224
    2325    @Override
  • src/main/java/com/example/autopartz/model/Warehouseman.java

    rab952ab r7d43957  
    2323public class Warehouseman extends User{
    2424    LocalDate employed_from;
     25    public static final LocalDate defaultEmployedFrom = LocalDate.of(2020,1,1);
    2526    @ManyToOne
    2627    @JoinColumn(name = "id_warehouse")
    2728    Warehouse warehouse;
     29
     30    public Warehouseman(String username, String name, String email, String password, String number, Warehouse warehouse) {
     31        super(username, name, email, password, number);
     32        this.employed_from=defaultEmployedFrom;
     33        this.warehouse= warehouse;
     34    }
    2835
    2936    @Override
     
    4148    @Override
    4249    public Collection<? extends GrantedAuthority> getAuthorities() {
    43         return Collections.singletonList(Role.ROLE_WAREHOUSEMAN);
     50        if(employed_from==defaultEmployedFrom)
     51            return Collections.singletonList(Role.ROLE_PENDING_WAREHOUSEMAN);
     52        else
     53            return Collections.singletonList(Role.ROLE_WAREHOUSEMAN);
    4454    }
    4555}
  • src/main/java/com/example/autopartz/model/views/RepairsForUser.java

    rab952ab r7d43957  
    3333    @Id
    3434    String partname;
    35     Integer vin;
     35    Integer orderid;
    3636    String rsname;
    3737}
  • src/main/java/com/example/autopartz/service/LoginService.java

    rab952ab r7d43957  
    22
    33import com.example.autopartz.model.User;
     4import com.example.autopartz.model.Warehouse;
    45
    56public interface LoginService {
    6     User register(String name, String username, String email, String number, String password);
     7    void register(String name, String username, String email, String number, String password, String role);
    78    User login(String username, String password);
     9    void registerWarehouseman(String name, String username, String email, String number, String password, String role, Warehouse warehouse);
    810}
  • src/main/java/com/example/autopartz/service/impl/LoginServiceImpl.java

    rab952ab r7d43957  
    11package com.example.autopartz.service.impl;
    22
    3 import com.example.autopartz.model.User;
     3import com.example.autopartz.model.*;
    44import com.example.autopartz.repository.UserRepository;
    55import com.example.autopartz.service.LoginService;
    66import org.springframework.stereotype.Service;
     7
     8import java.util.Objects;
    79
    810@Service
     
    1517
    1618    @Override
    17     public User register(String name, String username, String email, String number, String password) {
    18         return userRepository.save(new User(username,name,email,password,number));
     19    public void register(String name, String username, String email, String number, String password, String role) {
     20        if (Objects.equals(role, "client")) {
     21            userRepository.save(new Client(username, name, email, password, number));
     22        }
     23        else {
     24            userRepository.save(new Deliveryman(username, name, email, password, number));
     25        }
    1926    }
    2027
     
    2431    }
    2532
     33    @Override
     34    public void registerWarehouseman(String name, String username, String email, String number, String password, String role, Warehouse warehouse) {
     35        userRepository.save(new Warehouseman(username, name, email, password, number, warehouse));
     36
     37    }
     38
    2639//    @Override
    2740//    public User findByUsername(String username) {
  • src/main/resources/templates/fragments/header.html

    rab952ab r7d43957  
    1 <header xmlns:sec="http://www.w3.org/1999/xhtml">
     1<header xmlns:sec="http://www.w3.org/1999/xhtml" >
    22  <nav class="navbar navbar-expand-md navbar-dark bg-dark">
    33    <div class="container">
     
    1616            <a class="nav-link active" href="/services">Сервиси</a>
    1717          </li>
     18          <th:block sec:authorize="hasAuthority('ROLE_CLIENT')">
     19          <li class="nav-item m-auto">
     20            <a class="nav-link active" href="/currentOrder">Моја нарачка</a>
     21          </li>
     22          </th:block>
    1823        </ul>
    1924
  • src/main/resources/templates/master-template.html

    rab952ab r7d43957  
    11<!DOCTYPE html>
    2 <html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
     2<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security"
     3>
    34<head>
    45  <meta charset="UTF-8"/>
  • src/main/resources/templates/partinfo.html

    rab952ab r7d43957  
    44<p th:text="${part.getManufacturer().getName()}"></p>
    55<p><span th:text="${amount}"></span><span> денари</span></p>
    6 <form th:action="@{'/part/delivery/{id}' (id=${part.getId()}) }">
     6<form method="post" th:action="@{'/part/addToOrder/{id}' (id=${part.getId()}) }">
    77    <label>
    8         <input type="number" required min="1" placeholder="Количина"/>
     8        <input type="number" name="quantity" required min="1" placeholder="Количина"/>
    99    </label>
    10     <button type="submit">Купи</button>
     10    <button class="btn btn-primary btn-block btn-lg" type="submit">Додај во нарачка</button>
    1111</form>
    1212</div>
  • src/main/resources/templates/products.html

    rab952ab r7d43957  
    1 <div>
     1<div xmlns:sec="http://www.thymeleaf.org/extras/spring-security"
     2>
    23<header>
     4    <th:block sec:authorize="hasAuthority('ROLE_CLIENT')">
    35    <a th:href="${'/orders'}">Мои нарачки</a>
    46    <a th:href="${'/repairs'}">Мои поправки</a>
    57    <a th:href="${'/reviews'}">Мои критики</a>
     8    </th:block>
    69</header>
    710<main>
  • src/main/resources/templates/register.html

    rab952ab r7d43957  
    2828                <input type="password" id="rpassword" name="rpassword" class="form-control" placeholder="*********" required=""/>
    2929            </p>
     30            <p>
     31                <label for="role" class="sr-only">
     32                    Одбери улога
     33                </label><select class="form-control" id="role" name="role" required>
     34                    <option value="client">CLIENT</option>
     35                    <option value="warehouseman">WAREHOUSEMAN</option>
     36                    <option value="deliveryman">DELIVERYMAN</option>
     37                </select>
     38            </p>
    3039        </form>
    3140        <button class="btn btn-lg btn-primary btn-block" type="submit">Регистрирај се</button>
  • src/main/resources/templates/repairsForUser.html

    rab952ab r7d43957  
    66    <th>Датум на нарачка</th>
    77    <th>Нарачан дел</th>
    8     <th>Број на шасија</th>
     8    <th>Број на нарачка</th>
    99    <th>Име на сервис</th>
    1010  </tr>
     
    1414    <td th:text="${repair.getOrderdate}"></td>
    1515    <td th:text="${repair.getPartname()}"></td>
    16     <td th:text="${repair.getVin()}"></td>
     16    <td th:text="${repair.getOrderid()}"></td>
    1717    <td th:text="${repair.getRsname()}"></td>
    1818  </tr>
Note: See TracChangeset for help on using the changeset viewer.