Changeset d4b888e for src


Ignore:
Timestamp:
01/06/23 15:26:00 (23 months ago)
Author:
andrejtodorovski <82031894+andrejtodorovski@…>
Branches:
main
Children:
9dcbf44
Parents:
f484b14
Message:

Added full functionality for adding review

Location:
src/main
Files:
4 added
11 edited

Legend:

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

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

    rf484b14 rd4b888e  
    103103    public void chooseRepairShop(@RequestParam Integer rs,HttpSession session, HttpServletResponse response){
    104104        Order o = (Order) session.getAttribute("order");
    105         CarSample cs = (CarSample)session.getAttribute("car");
     105        CarSample cs = carSampleRepository.findById((Integer)session.getAttribute("carVin")).get();
    106106        ServiceBook sb = serviceBookRepository.findByCarSample(cs);
    107107        RepairShop repairShop = repairShopService.getById(rs);
    108108        repairRepository.save(new Repair(o,repairShop,sb));
     109//        o.setOrder_status("finished");
     110//        orderService.save(o);
    109111        session.removeAttribute("order");
    110112        try {
     
    123125        Deliveryman dm = deliverymanList.get(deliverer);
    124126        deliveryRepository.save(new Delivery("in progress",address,dm,o));
     127//        o.setOrder_status("finished");
     128//        orderService.save(o);
    125129        session.removeAttribute("order");
    126130        try {
  • src/main/java/com/example/autopartz/controller/UserController.java

    rf484b14 rd4b888e  
    11package com.example.autopartz.controller;
    22
    3 import com.example.autopartz.model.CarSample;
    4 import com.example.autopartz.model.Client;
    5 import com.example.autopartz.model.ServiceBook;
     3import com.example.autopartz.model.*;
     4import com.example.autopartz.model.views.RepairsForUser;
    65import com.example.autopartz.repository.*;
    76import com.example.autopartz.service.CarService;
     7import com.example.autopartz.service.RepairService;
    88import com.example.autopartz.service.UserService;
    99import org.springframework.stereotype.Controller;
    1010import org.springframework.ui.Model;
    11 import org.springframework.web.bind.annotation.GetMapping;
    12 import org.springframework.web.bind.annotation.PostMapping;
    13 import org.springframework.web.bind.annotation.RequestMapping;
    14 import org.springframework.web.bind.annotation.RequestParam;
     11import org.springframework.web.bind.annotation.*;
    1512
    1613import javax.servlet.http.HttpServletRequest;
    1714import javax.servlet.http.HttpServletResponse;
    1815import java.io.IOException;
     16import java.util.ArrayList;
     17import java.util.List;
    1918
    2019@Controller
     
    2827    private final CarSampleRepository carSampleRepository;
    2928    private final ServiceBookRepository serviceBookRepository;
    30     public UserController(OrdersForUserRepository ordersForUserRepository, RepairsForUserRepository repairsForUserRepository, ReviewsForUserRepository reviewsForUserRepository, UserService userService, CarService carService, CarSampleRepository carSampleRepository, ServiceBookRepository serviceBookRepository) {
     29    private final RepairService repairService;
     30    private final ReviewRepository reviewRepository;
     31    private final RepairRepository repairRepository;
     32    private final OrderRepository orderRepository;
     33
     34    public UserController(OrdersForUserRepository ordersForUserRepository, RepairsForUserRepository repairsForUserRepository, ReviewsForUserRepository reviewsForUserRepository, UserService userService, CarService carService, CarSampleRepository carSampleRepository, ServiceBookRepository serviceBookRepository, RepairService repairService, ReviewRepository reviewRepository,
     35                          RepairRepository repairRepository,
     36                          OrderRepository orderRepository) {
    3137        this.ordersForUserRepository = ordersForUserRepository;
    3238        this.repairsForUserRepository = repairsForUserRepository;
     
    3642        this.carSampleRepository = carSampleRepository;
    3743        this.serviceBookRepository = serviceBookRepository;
     44        this.repairService = repairService;
     45        this.reviewRepository = reviewRepository;
     46        this.repairRepository = repairRepository;
     47        this.orderRepository = orderRepository;
    3848    }
    3949    @GetMapping("orders")
     
    4858        Integer id = userService.findByUsername(request.getRemoteUser()).getId();
    4959        model.addAttribute("userRepairs",repairsForUserRepository.findAllByUserid(id));
     60        List<Boolean> hasReview = new ArrayList<>(100);
     61        for (int i = 0; i < orderRepository.findAll().size(); i++) {
     62            hasReview.add(false);
     63        }
     64        for(RepairsForUser rfu: repairsForUserRepository.findAll()){
     65            if(reviewRepository.findByRepair(repairService.findById(rfu.getRepairid()))==null){
     66                hasReview.set(rfu.getRepairid(),false);
     67            }
     68            else {
     69                hasReview.set(rfu.getRepairid(),true);
     70            }
     71        }
     72        model.addAttribute("hasReview", hasReview);
    5073        model.addAttribute("bodyContent","repairsForUser");
    5174        return "master-template";
     
    78101        }
    79102    }
     103    @GetMapping("/repairs/addReview/{id}")
     104    public String addReviewFor(@PathVariable Integer id, Model model){
     105        model.addAttribute("bodyContent","addReviewPage");
     106        model.addAttribute("repair", repairService.findById(id));
     107        return "master-template";
     108    }
     109    @PostMapping("/repairs/addReview/{id}")
     110    public void addReview(@PathVariable Integer id, @RequestParam Integer rating, @RequestParam(required = false) String comment,
     111                          Model model, HttpServletResponse response, HttpServletRequest request){
     112        Client client = (Client) userService.findByUsername(request.getRemoteUser());
     113        Repair repair = repairService.findById(id);
     114        reviewRepository.save(new Review(rating,
     115                comment==null ? "" : comment, repair,client));
     116        try {
     117            response.sendRedirect("/repairs");
     118        } catch (IOException e) {
     119            throw new RuntimeException(e);
     120        }
     121    }
    80122}
  • src/main/java/com/example/autopartz/model/Repair.java

    rf484b14 rd4b888e  
    1818    @Id
    1919    @GeneratedValue(strategy = GenerationType.IDENTITY)
    20     Integer ID_repair;
     20    @Column(name = "ID_repair")
     21    Integer id;
    2122    @OneToOne
    2223    @JoinColumn(name = "id_order")
     
    3435        this.serviceBook = serviceBook;
    3536    }
    36 
    3737    @Override
    3838    public boolean equals(Object o) {
     
    4040        if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
    4141        Repair repair = (Repair) o;
    42         return ID_repair != null && Objects.equals(ID_repair, repair.ID_repair);
     42        return id != null && Objects.equals(id, repair.id);
    4343    }
    4444
  • src/main/java/com/example/autopartz/model/Review.java

    rf484b14 rd4b888e  
    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
     
    2017public class Review {
    2118    @Id
     19    @GeneratedValue(strategy = GenerationType.IDENTITY)
    2220    Integer ID_review;
    2321    Integer review_rating;
     
    2927    @JoinColumn(name = "id_user")
    3028    Client client;
     29
     30    public Review(Integer review_rating, String review_comment, Repair repair, Client client) {
     31        this.review_rating = review_rating;
     32        this.review_comment = review_comment;
     33        this.repair = repair;
     34        this.client = client;
     35    }
    3136
    3237    @Override
  • src/main/java/com/example/autopartz/model/views/RepairsForUser.java

    rf484b14 rd4b888e  
    2323@Immutable
    2424@Table(name = "`repairs_for_user`")
    25 @IdClass(OrdersForUserId.class)
    2625public class RepairsForUser implements Serializable {
    2726    @Serial
    2827    private static final long serialVersionUID = 1L;
    2928    @Id
     29    Integer repairid;
    3030    Integer userid;
    31     @Id
    3231    LocalDateTime orderdate;
    33     @Id
    34     String partname;
    3532    Integer orderid;
    3633    String rsname;
  • src/main/java/com/example/autopartz/repository/RepairRepository.java

    rf484b14 rd4b888e  
    44import org.springframework.data.jpa.repository.JpaRepository;
    55
     6import java.util.List;
     7
    68public interface RepairRepository extends JpaRepository<Repair,Integer> {
     9    List<Repair> findAllById(Integer id);
    710}
  • src/main/java/com/example/autopartz/repository/RepairsForUserRepository.java

    rf484b14 rd4b888e  
    99
    1010@Repository
    11 public interface RepairsForUserRepository extends JpaRepository<RepairsForUser, OrdersForUserId> {
     11public interface RepairsForUserRepository extends JpaRepository<RepairsForUser, Integer> {
    1212    List<RepairsForUser> findAllByUserid(Integer userid);
    1313}
  • src/main/java/com/example/autopartz/service/OrderService.java

    rf484b14 rd4b888e  
    1010    Order create(Client user);
    1111    Order findById(Integer id);
     12    void save(Order o);
    1213}
    1314
  • src/main/java/com/example/autopartz/service/impl/OrderServiceImpl.java

    rf484b14 rd4b888e  
    3030        return orderRepository.findById(id).get();
    3131    }
     32
     33    @Override
     34    public void save(Order o) {
     35        orderRepository.save(o);
     36    }
    3237}
  • src/main/resources/templates/repairsForUser.html

    rf484b14 rd4b888e  
    44  <thead>
    55  <tr>
     6    <th>Број на поправка</th>
    67    <th>Датум на нарачка</th>
    7     <th>Нарачан дел</th>
    88    <th>Број на нарачка</th>
    99    <th>Име на сервис</th>
     10    <th>Додај критика</th>
    1011  </tr>
    1112  </thead>
    1213  <tbody>
    1314  <tr th:each="repair : ${userRepairs}">
     15    <td th:text="${repair.getRepairid()}"></td>
    1416    <td th:text="${repair.getOrderdate}"></td>
    15     <td th:text="${repair.getPartname()}"></td>
    1617    <td th:text="${repair.getOrderid()}"></td>
    1718    <td th:text="${repair.getRsname()}"></td>
     19    <td>
     20      <div th:if="${!hasReview.get(repair.getRepairid())}">
     21      <form th:action="@{'/repairs/addReview/{id}' (id=${repair.getRepairid()}) }">
     22      <button type="submit">Додај критика</button>
     23      </form>
     24      </div>
     25      <div th:if="${hasReview.get(repair.getRepairid())}">
     26        Веќе има критика
     27      </div>
     28    </td>
    1829  </tr>
    1930  </tbody>
Note: See TracChangeset for help on using the changeset viewer.